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" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 39 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 43 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 44 | /// \brief Determine whether the use of this declaration is valid, and |
| 45 | /// emit any corresponding diagnostics. |
| 46 | /// |
| 47 | /// This routine diagnoses various problems with referencing |
| 48 | /// declarations that can occur when using a declaration. For example, |
| 49 | /// it might warn if a deprecated or unavailable declaration is being |
| 50 | /// used, or produce an error (and return true) if a C++0x deleted |
| 51 | /// function is being used. |
| 52 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 53 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 54 | /// decls. |
| 55 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 56 | /// \returns true if there was an error (this declaration cannot be |
| 57 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 58 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 59 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 60 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 61 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 62 | // If there were any diagnostics suppressed by template argument deduction, |
| 63 | // emit them now. |
| 64 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 65 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 66 | if (Pos != SuppressedDiagnostics.end()) { |
| 67 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
| 68 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 69 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 70 | |
| 71 | // 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] | 72 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 73 | // entry from the table, because we want to avoid ever emitting these |
| 74 | // diagnostics again. |
| 75 | Suppressed.clear(); |
| 76 | } |
| 77 | } |
| 78 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 79 | // 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] | 80 | if (ParsingInitForAutoVars.count(D)) { |
| 81 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 82 | << D->getDeclName(); |
| 83 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 86 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 87 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | if (FD->isDeleted()) { |
| 89 | Diag(Loc, diag::err_deleted_function_use); |
| 90 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 91 | return true; |
| 92 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 93 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 95 | // See if this declaration is unavailable or deprecated. |
| 96 | std::string Message; |
| 97 | switch (D->getAvailability(&Message)) { |
| 98 | case AR_Available: |
| 99 | case AR_NotYetIntroduced: |
| 100 | break; |
| 101 | |
| 102 | case AR_Deprecated: |
| 103 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 104 | break; |
| 105 | |
| 106 | case AR_Unavailable: |
| 107 | if (Message.empty()) { |
| 108 | if (!UnknownObjCClass) |
| 109 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 110 | else |
| 111 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 112 | << D->getDeclName(); |
| 113 | } |
| 114 | else |
| 115 | Diag(Loc, diag::err_unavailable_message) |
| 116 | << D->getDeclName() << Message; |
| 117 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 118 | break; |
| 119 | } |
| 120 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 121 | // Warn if this is used but marked unused. |
| 122 | if (D->hasAttr<UnusedAttr>()) |
| 123 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 124 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 125 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 128 | /// \brief Retrieve the message suffix that should be added to a |
| 129 | /// diagnostic complaining about the given function being deleted or |
| 130 | /// unavailable. |
| 131 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 132 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 133 | // detected here so that we could improve diagnostics to say, e.g., |
| 134 | // "base class 'A' had a deleted copy constructor". |
| 135 | if (FD->isDeleted()) |
| 136 | return std::string(); |
| 137 | |
| 138 | std::string Message; |
| 139 | if (FD->getAvailability(&Message)) |
| 140 | return ": " + Message; |
| 141 | |
| 142 | return std::string(); |
| 143 | } |
| 144 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 145 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 147 | /// attribute. It warns if call does not have the sentinel argument. |
| 148 | /// |
| 149 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 151 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 153 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 154 | |
| 155 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 156 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | int sentinelPos = attr->getSentinel(); |
| 158 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 160 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 161 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 163 | bool warnNotEnoughArgs = false; |
| 164 | int isMethod = 0; |
| 165 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 166 | // skip over named parameters. |
| 167 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 168 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 169 | if (nullPos) |
| 170 | --nullPos; |
| 171 | else |
| 172 | ++i; |
| 173 | } |
| 174 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 175 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 176 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 177 | // skip over named parameters. |
| 178 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 179 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 180 | if (nullPos) |
| 181 | --nullPos; |
| 182 | else |
| 183 | ++i; |
| 184 | } |
| 185 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 186 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 187 | // block or function pointer call. |
| 188 | QualType Ty = V->getType(); |
| 189 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 191 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 192 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 193 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 194 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 195 | unsigned k; |
| 196 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 197 | if (nullPos) |
| 198 | --nullPos; |
| 199 | else |
| 200 | ++i; |
| 201 | } |
| 202 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 203 | } |
| 204 | if (Ty->isBlockPointerType()) |
| 205 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 206 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 207 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 208 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 212 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 213 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | int sentinel = i; |
| 217 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 218 | --sentinelPos; |
| 219 | ++i; |
| 220 | } |
| 221 | if (sentinelPos > 0) { |
| 222 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 223 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | while (i < NumArgs-1) { |
| 227 | ++i; |
| 228 | ++sentinel; |
| 229 | } |
| 230 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 231 | if (!sentinelExpr) return; |
| 232 | if (sentinelExpr->isTypeDependent()) return; |
| 233 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 234 | |
| 235 | // nullptr_t is always treated as null. |
| 236 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 237 | |
Fariborz Jahanian | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 238 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 239 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 240 | Expr::NPC_ValueDependentIsNull)) |
| 241 | return; |
| 242 | |
| 243 | // Unfortunately, __null has type 'int'. |
| 244 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 245 | |
| 246 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 247 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 250 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 251 | Expr *Ex = (Expr *)E; |
| 252 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 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); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +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(); |
| 353 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 354 | CheckArrayAccess(E); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 355 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 356 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 357 | E, 0, VK_RValue)); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 358 | } |
| 359 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 360 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 361 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 362 | if (Res.isInvalid()) |
| 363 | return ExprError(); |
| 364 | Res = DefaultLvalueConversion(Res.take()); |
| 365 | if (Res.isInvalid()) |
| 366 | return ExprError(); |
| 367 | return move(Res); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 371 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 373 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 374 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 375 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 376 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 377 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 378 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 379 | if (Res.isInvalid()) |
| 380 | return Owned(E); |
| 381 | E = Res.take(); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 382 | |
| 383 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 384 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 385 | |
| 386 | // Try to perform integral promotions if the object has a theoretically |
| 387 | // promotable type. |
| 388 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 389 | // C99 6.3.1.1p2: |
| 390 | // |
| 391 | // The following may be used in an expression wherever an int or |
| 392 | // unsigned int may be used: |
| 393 | // - an object or expression with an integer type whose integer |
| 394 | // conversion rank is less than or equal to the rank of int |
| 395 | // and unsigned int. |
| 396 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 397 | // |
| 398 | // If an int can represent all values of the original type, the |
| 399 | // value is converted to an int; otherwise, it is converted to an |
| 400 | // unsigned int. These are called the integer promotions. All |
| 401 | // other types are unchanged by the integer promotions. |
| 402 | |
| 403 | QualType PTy = Context.isPromotableBitField(E); |
| 404 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 405 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 406 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 407 | } |
| 408 | if (Ty->isPromotableIntegerType()) { |
| 409 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 410 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 411 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 412 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 413 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 414 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 417 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | /// 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] | 419 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 420 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 421 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 422 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 424 | ExprResult Res = UsualUnaryConversions(E); |
| 425 | if (Res.isInvalid()) |
| 426 | return Owned(E); |
| 427 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 429 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 430 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 431 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 432 | |
| 433 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 436 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 437 | /// 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] | 438 | /// interfaces passed by value. |
| 439 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 440 | FunctionDecl *FDecl) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 441 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
| 442 | if (ExprRes.isInvalid()) |
| 443 | return ExprError(); |
| 444 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 446 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 447 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 448 | // etc. |
| 449 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 450 | return Owned(E); |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 451 | |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 452 | // 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] | 453 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 454 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 455 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 456 | << E->getType() << CT)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 457 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 459 | if (!E->getType()->isPODType()) { |
| 460 | // C++0x [expr.call]p7: |
| 461 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 462 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 463 | // or a non-trivial destructor, with no corresponding parameter, |
| 464 | // is conditionally-supported with implementation-defined semantics. |
| 465 | bool TrivialEnough = false; |
| 466 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 467 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 468 | if (Record->hasTrivialCopyConstructor() && |
| 469 | Record->hasTrivialMoveConstructor() && |
| 470 | Record->hasTrivialDestructor()) |
| 471 | TrivialEnough = true; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (TrivialEnough) { |
| 476 | // Nothing to diagnose. This is okay. |
| 477 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 478 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 479 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 480 | << CT)) { |
| 481 | // Turn this into a trap. |
| 482 | CXXScopeSpec SS; |
| 483 | UnqualifiedId Name; |
| 484 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 485 | E->getLocStart()); |
| 486 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 487 | if (TrapFn.isInvalid()) |
| 488 | return ExprError(); |
| 489 | |
| 490 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 491 | MultiExprArg(), E->getLocEnd()); |
| 492 | if (Call.isInvalid()) |
| 493 | return ExprError(); |
| 494 | |
| 495 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 496 | Call.get(), E); |
| 497 | if (Comma.isInvalid()) |
| 498 | return ExprError(); |
| 499 | |
| 500 | E = Comma.get(); |
| 501 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 502 | } |
| 503 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 504 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 507 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 508 | /// 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] | 509 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 510 | /// responsible for emitting appropriate error diagnostics. |
| 511 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 512 | /// GCC. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 513 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 514 | bool isCompAssign) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 515 | if (!isCompAssign) { |
| 516 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 517 | if (lhsExpr.isInvalid()) |
| 518 | return QualType(); |
| 519 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 520 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 521 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 522 | if (rhsExpr.isInvalid()) |
| 523 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 524 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 526 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 527 | QualType lhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 528 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | QualType rhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 530 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 531 | |
| 532 | // If both types are identical, no conversion is needed. |
| 533 | if (lhs == rhs) |
| 534 | return lhs; |
| 535 | |
| 536 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 537 | // The caller can deal with this (e.g. pointer + int). |
| 538 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 539 | return lhs; |
| 540 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 541 | // Apply unary and bitfield promotions to the LHS's type. |
| 542 | QualType lhs_unpromoted = lhs; |
| 543 | if (lhs->isPromotableIntegerType()) |
| 544 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 545 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 546 | if (!LHSBitfieldPromoteTy.isNull()) |
| 547 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 548 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 549 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 550 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 551 | // If both types are identical, no conversion is needed. |
| 552 | if (lhs == rhs) |
| 553 | return lhs; |
| 554 | |
| 555 | // At this point, we have two different arithmetic types. |
| 556 | |
| 557 | // Handle complex types first (C99 6.3.1.8p1). |
| 558 | bool LHSComplexFloat = lhs->isComplexType(); |
| 559 | bool RHSComplexFloat = rhs->isComplexType(); |
| 560 | if (LHSComplexFloat || RHSComplexFloat) { |
| 561 | // if we have an integer operand, the result is the complex type. |
| 562 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 563 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 564 | if (rhs->isIntegerType()) { |
| 565 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 566 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 567 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 568 | } else { |
| 569 | assert(rhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 570 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 571 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 572 | return lhs; |
| 573 | } |
| 574 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 575 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 576 | if (!isCompAssign) { |
| 577 | // int -> float -> _Complex float |
| 578 | if (lhs->isIntegerType()) { |
| 579 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 580 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 581 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 582 | } else { |
| 583 | assert(lhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 584 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 587 | return rhs; |
| 588 | } |
| 589 | |
| 590 | // This handles complex/complex, complex/float, or float/complex. |
| 591 | // When both operands are complex, the shorter operand is converted to the |
| 592 | // type of the longer, and that is the type of the result. This corresponds |
| 593 | // to what is done when combining two real floating-point operands. |
| 594 | // The fun begins when size promotion occur across type domains. |
| 595 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 596 | // floating-point type, the less precise type is converted, within it's |
| 597 | // real or complex domain, to the precision of the other type. For example, |
| 598 | // when combining a "long double" with a "double _Complex", the |
| 599 | // "double _Complex" is promoted to "long double _Complex". |
| 600 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 601 | |
| 602 | // If both are complex, just cast to the more precise type. |
| 603 | if (LHSComplexFloat && RHSComplexFloat) { |
| 604 | if (order > 0) { |
| 605 | // _Complex float -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 606 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 607 | return lhs; |
| 608 | |
| 609 | } else if (order < 0) { |
| 610 | // _Complex float -> _Complex double |
| 611 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 612 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 613 | return rhs; |
| 614 | } |
| 615 | return lhs; |
| 616 | } |
| 617 | |
| 618 | // If just the LHS is complex, the RHS needs to be converted, |
| 619 | // and the LHS might need to be promoted. |
| 620 | if (LHSComplexFloat) { |
| 621 | if (order > 0) { // LHS is wider |
| 622 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 623 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 624 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 625 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 626 | return lhs; |
| 627 | } |
| 628 | |
| 629 | // RHS is at least as wide. Find its corresponding complex type. |
| 630 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 631 | |
| 632 | // double -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 633 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 634 | |
| 635 | // _Complex float -> _Complex double |
| 636 | if (!isCompAssign && order < 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 637 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 638 | |
| 639 | return result; |
| 640 | } |
| 641 | |
| 642 | // Just the RHS is complex, so the LHS needs to be converted |
| 643 | // and the RHS might need to be promoted. |
| 644 | assert(RHSComplexFloat); |
| 645 | |
| 646 | if (order < 0) { // RHS is wider |
| 647 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 648 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 649 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 650 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 651 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 652 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 653 | return rhs; |
| 654 | } |
| 655 | |
| 656 | // LHS is at least as wide. Find its corresponding complex type. |
| 657 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 658 | |
| 659 | // double -> _Complex double |
| 660 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 661 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 662 | |
| 663 | // _Complex float -> _Complex double |
| 664 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 665 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 666 | |
| 667 | return result; |
| 668 | } |
| 669 | |
| 670 | // Now handle "real" floating types (i.e. float, double, long double). |
| 671 | bool LHSFloat = lhs->isRealFloatingType(); |
| 672 | bool RHSFloat = rhs->isRealFloatingType(); |
| 673 | if (LHSFloat || RHSFloat) { |
| 674 | // If we have two real floating types, convert the smaller operand |
| 675 | // to the bigger result. |
| 676 | if (LHSFloat && RHSFloat) { |
| 677 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 678 | if (order > 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 679 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 680 | return lhs; |
| 681 | } |
| 682 | |
| 683 | assert(order < 0 && "illegal float comparison"); |
| 684 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 685 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 686 | return rhs; |
| 687 | } |
| 688 | |
| 689 | // If we have an integer operand, the result is the real floating type. |
| 690 | if (LHSFloat) { |
| 691 | if (rhs->isIntegerType()) { |
| 692 | // Convert rhs to the lhs floating point type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 693 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 694 | return lhs; |
| 695 | } |
| 696 | |
| 697 | // Convert both sides to the appropriate complex float. |
| 698 | assert(rhs->isComplexIntegerType()); |
| 699 | QualType result = Context.getComplexType(lhs); |
| 700 | |
| 701 | // _Complex int -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 702 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 703 | |
| 704 | // float -> _Complex float |
| 705 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 706 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 707 | |
| 708 | return result; |
| 709 | } |
| 710 | |
| 711 | assert(RHSFloat); |
| 712 | if (lhs->isIntegerType()) { |
| 713 | // Convert lhs to the rhs floating point type. |
| 714 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 716 | return rhs; |
| 717 | } |
| 718 | |
| 719 | // Convert both sides to the appropriate complex float. |
| 720 | assert(lhs->isComplexIntegerType()); |
| 721 | QualType result = Context.getComplexType(rhs); |
| 722 | |
| 723 | // _Complex int -> _Complex float |
| 724 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 725 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 726 | |
| 727 | // float -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 728 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 729 | |
| 730 | return result; |
| 731 | } |
| 732 | |
| 733 | // Handle GCC complex int extension. |
| 734 | // FIXME: if the operands are (int, _Complex long), we currently |
| 735 | // don't promote the complex. Also, signedness? |
| 736 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 737 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 738 | if (lhsComplexInt && rhsComplexInt) { |
| 739 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 740 | rhsComplexInt->getElementType()); |
| 741 | assert(order && "inequal types with equal element ordering"); |
| 742 | if (order > 0) { |
| 743 | // _Complex int -> _Complex long |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 744 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 745 | return lhs; |
| 746 | } |
| 747 | |
| 748 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 749 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 750 | return rhs; |
| 751 | } else if (lhsComplexInt) { |
| 752 | // int -> _Complex int |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 753 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 754 | return lhs; |
| 755 | } else if (rhsComplexInt) { |
| 756 | // int -> _Complex int |
| 757 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 758 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 759 | return rhs; |
| 760 | } |
| 761 | |
| 762 | // Finally, we have two differing integer types. |
| 763 | // The rules for this case are in C99 6.3.1.8 |
| 764 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 765 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 766 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 767 | if (lhsSigned == rhsSigned) { |
| 768 | // Same signedness; use the higher-ranked type |
| 769 | if (compare >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 770 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 771 | return lhs; |
| 772 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 773 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 774 | return rhs; |
| 775 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 776 | // The unsigned type has greater than or equal rank to the |
| 777 | // signed type, so use the unsigned type |
| 778 | if (rhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 779 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 780 | return lhs; |
| 781 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 782 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 783 | return rhs; |
| 784 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 785 | // The two types are different widths; if we are here, that |
| 786 | // means the signed type is larger than the unsigned type, so |
| 787 | // use the signed type. |
| 788 | if (lhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 789 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 790 | return lhs; |
| 791 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 792 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 793 | return rhs; |
| 794 | } else { |
| 795 | // The signed type is higher-ranked than the unsigned type, |
| 796 | // but isn't actually any bigger (like unsigned int and long |
| 797 | // on most 32-bit systems). Use the unsigned type corresponding |
| 798 | // to the signed type. |
| 799 | QualType result = |
| 800 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 801 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 802 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 803 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 804 | return result; |
| 805 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 808 | //===----------------------------------------------------------------------===// |
| 809 | // Semantic Analysis for various Expression Types |
| 810 | //===----------------------------------------------------------------------===// |
| 811 | |
| 812 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 813 | ExprResult |
| 814 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 815 | SourceLocation DefaultLoc, |
| 816 | SourceLocation RParenLoc, |
| 817 | Expr *ControllingExpr, |
| 818 | MultiTypeArg types, |
| 819 | MultiExprArg exprs) { |
| 820 | unsigned NumAssocs = types.size(); |
| 821 | assert(NumAssocs == exprs.size()); |
| 822 | |
| 823 | ParsedType *ParsedTypes = types.release(); |
| 824 | Expr **Exprs = exprs.release(); |
| 825 | |
| 826 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 827 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 828 | if (ParsedTypes[i]) |
| 829 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 830 | else |
| 831 | Types[i] = 0; |
| 832 | } |
| 833 | |
| 834 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 835 | ControllingExpr, Types, Exprs, |
| 836 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 837 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 838 | return ER; |
| 839 | } |
| 840 | |
| 841 | ExprResult |
| 842 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 843 | SourceLocation DefaultLoc, |
| 844 | SourceLocation RParenLoc, |
| 845 | Expr *ControllingExpr, |
| 846 | TypeSourceInfo **Types, |
| 847 | Expr **Exprs, |
| 848 | unsigned NumAssocs) { |
| 849 | bool TypeErrorFound = false, |
| 850 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 851 | ContainsUnexpandedParameterPack |
| 852 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 853 | |
| 854 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 855 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 856 | ContainsUnexpandedParameterPack = true; |
| 857 | |
| 858 | if (Types[i]) { |
| 859 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 860 | ContainsUnexpandedParameterPack = true; |
| 861 | |
| 862 | if (Types[i]->getType()->isDependentType()) { |
| 863 | IsResultDependent = true; |
| 864 | } else { |
| 865 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 866 | // complete object type other than a variably modified type." |
| 867 | unsigned D = 0; |
| 868 | if (Types[i]->getType()->isIncompleteType()) |
| 869 | D = diag::err_assoc_type_incomplete; |
| 870 | else if (!Types[i]->getType()->isObjectType()) |
| 871 | D = diag::err_assoc_type_nonobject; |
| 872 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 873 | D = diag::err_assoc_type_variably_modified; |
| 874 | |
| 875 | if (D != 0) { |
| 876 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 877 | << Types[i]->getTypeLoc().getSourceRange() |
| 878 | << Types[i]->getType(); |
| 879 | TypeErrorFound = true; |
| 880 | } |
| 881 | |
| 882 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 883 | // selection shall specify compatible types." |
| 884 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 885 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 886 | Context.typesAreCompatible(Types[i]->getType(), |
| 887 | Types[j]->getType())) { |
| 888 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 889 | diag::err_assoc_compatible_types) |
| 890 | << Types[j]->getTypeLoc().getSourceRange() |
| 891 | << Types[j]->getType() |
| 892 | << Types[i]->getType(); |
| 893 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 894 | diag::note_compat_assoc) |
| 895 | << Types[i]->getTypeLoc().getSourceRange() |
| 896 | << Types[i]->getType(); |
| 897 | TypeErrorFound = true; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | if (TypeErrorFound) |
| 903 | return ExprError(); |
| 904 | |
| 905 | // If we determined that the generic selection is result-dependent, don't |
| 906 | // try to compute the result expression. |
| 907 | if (IsResultDependent) |
| 908 | return Owned(new (Context) GenericSelectionExpr( |
| 909 | Context, KeyLoc, ControllingExpr, |
| 910 | Types, Exprs, NumAssocs, DefaultLoc, |
| 911 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 912 | |
| 913 | llvm::SmallVector<unsigned, 1> CompatIndices; |
| 914 | unsigned DefaultIndex = -1U; |
| 915 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 916 | if (!Types[i]) |
| 917 | DefaultIndex = i; |
| 918 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 919 | Types[i]->getType())) |
| 920 | CompatIndices.push_back(i); |
| 921 | } |
| 922 | |
| 923 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 924 | // type compatible with at most one of the types named in its generic |
| 925 | // association list." |
| 926 | if (CompatIndices.size() > 1) { |
| 927 | // We strip parens here because the controlling expression is typically |
| 928 | // parenthesized in macro definitions. |
| 929 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 930 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 931 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 932 | << (unsigned) CompatIndices.size(); |
| 933 | for (llvm::SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
| 934 | E = CompatIndices.end(); I != E; ++I) { |
| 935 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 936 | diag::note_compat_assoc) |
| 937 | << Types[*I]->getTypeLoc().getSourceRange() |
| 938 | << Types[*I]->getType(); |
| 939 | } |
| 940 | return ExprError(); |
| 941 | } |
| 942 | |
| 943 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 944 | // its controlling expression shall have type compatible with exactly one of |
| 945 | // the types named in its generic association list." |
| 946 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 947 | // We strip parens here because the controlling expression is typically |
| 948 | // parenthesized in macro definitions. |
| 949 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 950 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 951 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 952 | return ExprError(); |
| 953 | } |
| 954 | |
| 955 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 956 | // type name that is compatible with the type of the controlling expression, |
| 957 | // then the result expression of the generic selection is the expression |
| 958 | // in that generic association. Otherwise, the result expression of the |
| 959 | // generic selection is the expression in the default generic association." |
| 960 | unsigned ResultIndex = |
| 961 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 962 | |
| 963 | return Owned(new (Context) GenericSelectionExpr( |
| 964 | Context, KeyLoc, ControllingExpr, |
| 965 | Types, Exprs, NumAssocs, DefaultLoc, |
| 966 | RParenLoc, ContainsUnexpandedParameterPack, |
| 967 | ResultIndex)); |
| 968 | } |
| 969 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 970 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 971 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 972 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 973 | /// multiple tokens. However, the common case is that StringToks points to one |
| 974 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 975 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 976 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 977 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 978 | assert(NumStringToks && "Must have at least one string!"); |
| 979 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 980 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 981 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 982 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | |
| 984 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 985 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 986 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 987 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 988 | QualType StrTy = Context.CharTy; |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 989 | if (Literal.AnyWide) |
| 990 | StrTy = Context.getWCharType(); |
| 991 | else if (Literal.Pascal) |
| 992 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 993 | |
| 994 | // 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] | 995 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 996 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 997 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 998 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 999 | // the nul terminator character as well as the string length for pascal |
| 1000 | // strings. |
| 1001 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1002 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1003 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1006 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 1007 | Literal.GetStringLength(), |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 1008 | Literal.AnyWide, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1009 | &StringTokLocs[0], |
| 1010 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1013 | enum CaptureResult { |
| 1014 | /// No capture is required. |
| 1015 | CR_NoCapture, |
| 1016 | |
| 1017 | /// A capture is required. |
| 1018 | CR_Capture, |
| 1019 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1020 | /// A by-ref capture is required. |
| 1021 | CR_CaptureByRef, |
| 1022 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1023 | /// An error occurred when trying to capture the given variable. |
| 1024 | CR_Error |
| 1025 | }; |
| 1026 | |
| 1027 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1028 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1029 | /// \param var - the variable referenced |
| 1030 | /// \param DC - the context which we couldn't capture through |
| 1031 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1032 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1033 | VarDecl *var, DeclContext *DC) { |
| 1034 | switch (S.ExprEvalContexts.back().Context) { |
| 1035 | case Sema::Unevaluated: |
| 1036 | // The argument will never be evaluated, so don't complain. |
| 1037 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1039 | case Sema::PotentiallyEvaluated: |
| 1040 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1041 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1042 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1043 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1044 | // FIXME: delay these! |
| 1045 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1046 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1048 | // Don't diagnose about capture if we're not actually in code right |
| 1049 | // now; in general, there are more appropriate places that will |
| 1050 | // diagnose this. |
| 1051 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1052 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1053 | // Certain madnesses can happen with parameter declarations, which |
| 1054 | // we want to ignore. |
| 1055 | if (isa<ParmVarDecl>(var)) { |
| 1056 | // - If the parameter still belongs to the translation unit, then |
| 1057 | // we're actually just using one parameter in the declaration of |
| 1058 | // the next. This is useful in e.g. VLAs. |
| 1059 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1060 | return CR_NoCapture; |
| 1061 | |
| 1062 | // - This particular madness can happen in ill-formed default |
| 1063 | // arguments; claim it's okay and let downstream code handle it. |
| 1064 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1065 | return CR_NoCapture; |
| 1066 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1067 | |
| 1068 | DeclarationName functionName; |
| 1069 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1070 | functionName = fn->getDeclName(); |
| 1071 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1072 | |
| 1073 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1074 | << var->getIdentifier() << functionName; |
| 1075 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1076 | << var->getIdentifier(); |
| 1077 | |
| 1078 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1081 | /// There is a well-formed capture at a particular scope level; |
| 1082 | /// propagate it through all the nested blocks. |
| 1083 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1084 | const BlockDecl::Capture &capture) { |
| 1085 | VarDecl *var = capture.getVariable(); |
| 1086 | |
| 1087 | // Update all the inner blocks with the capture information. |
| 1088 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1089 | i != e; ++i) { |
| 1090 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1091 | innerBlock->Captures.push_back( |
| 1092 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1093 | /*nested*/ true, capture.getCopyExpr())); |
| 1094 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1095 | } |
| 1096 | |
| 1097 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1098 | } |
| 1099 | |
| 1100 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1101 | /// given value in the current context requires a variable capture. |
| 1102 | /// |
| 1103 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1104 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1105 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1106 | ValueDecl *value) { |
| 1107 | // Only variables ever require capture. |
| 1108 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1109 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Fast path: variables from the current context never require capture. |
| 1112 | DeclContext *DC = S.CurContext; |
| 1113 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1114 | |
| 1115 | // Only variables with local storage require capture. |
| 1116 | // FIXME: What about 'const' variables in C++? |
| 1117 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1118 | |
| 1119 | // Otherwise, we need to capture. |
| 1120 | |
| 1121 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1122 | do { |
| 1123 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1124 | // scopes don't work. |
| 1125 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1126 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1127 | |
| 1128 | BlockScopeInfo *blockScope = |
| 1129 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1130 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1131 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1132 | // Check whether we've already captured it in this block. If so, |
| 1133 | // we're done. |
| 1134 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1135 | return propagateCapture(S, functionScopesIndex, |
| 1136 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1137 | |
| 1138 | functionScopesIndex--; |
| 1139 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1140 | } while (var->getDeclContext() != DC); |
| 1141 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1142 | // Okay, we descended all the way to the block that defines the variable. |
| 1143 | // Actually try to capture it. |
| 1144 | QualType type = var->getType(); |
| 1145 | |
| 1146 | // Prohibit variably-modified types. |
| 1147 | if (type->isVariablyModifiedType()) { |
| 1148 | S.Diag(loc, diag::err_ref_vm_type); |
| 1149 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1150 | return CR_Error; |
| 1151 | } |
| 1152 | |
| 1153 | // Prohibit arrays, even in __block variables, but not references to |
| 1154 | // them. |
| 1155 | if (type->isArrayType()) { |
| 1156 | S.Diag(loc, diag::err_ref_array_type); |
| 1157 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1158 | return CR_Error; |
| 1159 | } |
| 1160 | |
| 1161 | S.MarkDeclarationReferenced(loc, var); |
| 1162 | |
| 1163 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1164 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1165 | |
| 1166 | // Build a copy expression. |
| 1167 | Expr *copyExpr = 0; |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1168 | const RecordType *rtype; |
| 1169 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1170 | (rtype = type->getAs<RecordType>())) { |
| 1171 | |
| 1172 | // The capture logic needs the destructor, so make sure we mark it. |
| 1173 | // Usually this is unnecessary because most local variables have |
| 1174 | // their destructors marked at declaration time, but parameters are |
| 1175 | // an exception because it's technically only the call site that |
| 1176 | // actually requires the destructor. |
| 1177 | if (isa<ParmVarDecl>(var)) |
| 1178 | S.FinalizeVarWithDestructor(var, rtype); |
| 1179 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | // According to the blocks spec, the capture of a variable from |
| 1181 | // the stack requires a const copy constructor. This is not true |
| 1182 | // of the copy/move done to move a __block variable to the heap. |
| 1183 | type.addConst(); |
| 1184 | |
| 1185 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1186 | ExprResult result = |
| 1187 | S.PerformCopyInitialization( |
| 1188 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1189 | type, false), |
| 1190 | loc, S.Owned(declRef)); |
| 1191 | |
| 1192 | // Build a full-expression copy expression if initialization |
| 1193 | // succeeded and used a non-trivial constructor. Recover from |
| 1194 | // errors by pretending that the copy isn't necessary. |
| 1195 | if (!result.isInvalid() && |
| 1196 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1197 | result = S.MaybeCreateExprWithCleanups(result); |
| 1198 | copyExpr = result.take(); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | // We're currently at the declarer; go back to the closure. |
| 1203 | functionScopesIndex++; |
| 1204 | BlockScopeInfo *blockScope = |
| 1205 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1206 | |
| 1207 | // Build a valid capture in this scope. |
| 1208 | blockScope->Captures.push_back( |
| 1209 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1210 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1211 | |
| 1212 | // Propagate that to inner captures if necessary. |
| 1213 | return propagateCapture(S, functionScopesIndex, |
| 1214 | blockScope->Captures.back()); |
| 1215 | } |
| 1216 | |
| 1217 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1218 | const DeclarationNameInfo &NameInfo, |
| 1219 | bool byRef) { |
| 1220 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1221 | |
| 1222 | VarDecl *var = cast<VarDecl>(vd); |
| 1223 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1224 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1225 | |
| 1226 | QualType exprType = var->getType().getNonReferenceType(); |
| 1227 | |
| 1228 | BlockDeclRefExpr *BDRE; |
| 1229 | if (!byRef) { |
| 1230 | // The variable will be bound by copy; make it const within the |
| 1231 | // closure, but record that this was done in the expression. |
| 1232 | bool constAdded = !exprType.isConstQualified(); |
| 1233 | exprType.addConst(); |
| 1234 | |
| 1235 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1236 | NameInfo.getLoc(), false, |
| 1237 | constAdded); |
| 1238 | } else { |
| 1239 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1240 | NameInfo.getLoc(), true); |
| 1241 | } |
| 1242 | |
| 1243 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1244 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1245 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1246 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1247 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1248 | SourceLocation Loc, |
| 1249 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1250 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1251 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1254 | /// BuildDeclRefExpr - Build an expression that references a |
| 1255 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1256 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1257 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1258 | const DeclarationNameInfo &NameInfo, |
| 1259 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1260 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1262 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1263 | SS? SS->getWithLocInContext(Context) |
| 1264 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1265 | D, NameInfo, Ty, VK); |
| 1266 | |
| 1267 | // Just in case we're building an illegal pointer-to-member. |
| 1268 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1269 | E->setObjectKind(OK_BitField); |
| 1270 | |
| 1271 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1274 | static ExprResult |
| 1275 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1276 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1277 | DeclAccessPair FoundDecl, |
| 1278 | const DeclarationNameInfo &MemberNameInfo); |
| 1279 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1280 | ExprResult |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1281 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1282 | SourceLocation loc, |
| 1283 | IndirectFieldDecl *indirectField, |
| 1284 | Expr *baseObjectExpr, |
| 1285 | SourceLocation opLoc) { |
| 1286 | // First, build the expression that refers to the base object. |
| 1287 | |
| 1288 | bool baseObjectIsPointer = false; |
| 1289 | Qualifiers baseQuals; |
| 1290 | |
| 1291 | // Case 1: the base of the indirect field is not a field. |
| 1292 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1293 | CXXScopeSpec EmptySS; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1294 | if (baseVariable) { |
| 1295 | assert(baseVariable->getType()->isRecordType()); |
| 1296 | |
| 1297 | // In principle we could have a member access expression that |
| 1298 | // accesses an anonymous struct/union that's a static member of |
| 1299 | // the base object's class. However, under the current standard, |
| 1300 | // static data members cannot be anonymous structs or unions. |
| 1301 | // Supporting this is as easy as building a MemberExpr here. |
| 1302 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1303 | |
| 1304 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1305 | |
| 1306 | ExprResult result = |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1307 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1308 | if (result.isInvalid()) return ExprError(); |
| 1309 | |
| 1310 | baseObjectExpr = result.take(); |
| 1311 | baseObjectIsPointer = false; |
| 1312 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1313 | |
| 1314 | // Case 2: the base of the indirect field is a field and the user |
| 1315 | // wrote a member expression. |
| 1316 | } else if (baseObjectExpr) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1317 | // The caller provided the base object expression. Determine |
| 1318 | // whether its a pointer and whether it adds any qualifiers to the |
| 1319 | // anonymous struct/union fields we're looking into. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1320 | QualType objectType = baseObjectExpr->getType(); |
| 1321 | |
| 1322 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1323 | baseObjectIsPointer = true; |
| 1324 | objectType = ptr->getPointeeType(); |
| 1325 | } else { |
| 1326 | baseObjectIsPointer = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1327 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1328 | baseQuals = objectType.getQualifiers(); |
| 1329 | |
| 1330 | // Case 3: the base of the indirect field is a field and we should |
| 1331 | // build an implicit member access. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1332 | } else { |
| 1333 | // We've found a member of an anonymous struct/union that is |
| 1334 | // inside a non-anonymous struct/union, so in a well-formed |
| 1335 | // program our base object expression is "this". |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1336 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 1337 | if (!method) { |
| 1338 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1339 | << indirectField->getDeclName(); |
| 1340 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1343 | // Our base object expression is "this". |
| 1344 | baseObjectExpr = |
| 1345 | new (Context) CXXThisExpr(loc, method->getThisType(Context), |
| 1346 | /*isImplicit=*/ true); |
| 1347 | baseObjectIsPointer = true; |
| 1348 | baseQuals = Qualifiers::fromCVRMask(method->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Build the implicit member references to the field of the |
| 1352 | // anonymous struct/union. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1353 | Expr *result = baseObjectExpr; |
| 1354 | IndirectFieldDecl::chain_iterator |
| 1355 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1356 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1357 | // Build the first member access in the chain with full information. |
| 1358 | if (!baseVariable) { |
| 1359 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1360 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1361 | // FIXME: use the real found-decl info! |
| 1362 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1363 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1364 | // Make a nameInfo that properly uses the anonymous name. |
| 1365 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1366 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1367 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1368 | EmptySS, field, foundDecl, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1369 | memberNameInfo).take(); |
| 1370 | baseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1371 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1372 | // FIXME: check qualified member access |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1375 | // In all cases, we should now skip the first declaration in the chain. |
| 1376 | ++FI; |
| 1377 | |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1378 | while (FI != FEnd) { |
| 1379 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1380 | |
| 1381 | // FIXME: these are somewhat meaningless |
| 1382 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1383 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1384 | |
| 1385 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1386 | (FI == FEnd? SS : EmptySS), field, |
| 1387 | foundDecl, memberNameInfo) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1388 | .take(); |
| 1389 | } |
| 1390 | |
| 1391 | return Owned(result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1394 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1395 | /// possibly a list of template arguments. |
| 1396 | /// |
| 1397 | /// If this produces template arguments, it is permitted to call |
| 1398 | /// DecomposeTemplateName. |
| 1399 | /// |
| 1400 | /// This actually loses a lot of source location information for |
| 1401 | /// non-standard name kinds; we should consider preserving that in |
| 1402 | /// some way. |
| 1403 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1404 | const UnqualifiedId &Id, |
| 1405 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1406 | DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1407 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1408 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1409 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1410 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1411 | |
| 1412 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1413 | Id.TemplateId->getTemplateArgs(), |
| 1414 | Id.TemplateId->NumArgs); |
| 1415 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1416 | TemplateArgsPtr.release(); |
| 1417 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1418 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1419 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1420 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1421 | TemplateArgs = &Buffer; |
| 1422 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1423 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1424 | TemplateArgs = 0; |
| 1425 | } |
| 1426 | } |
| 1427 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1428 | /// Determines if the given class is provably not derived from all of |
| 1429 | /// the prospective base classes. |
| 1430 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1431 | CXXRecordDecl *Record, |
| 1432 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1433 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1434 | return false; |
| 1435 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1436 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1437 | if (!RD) return false; |
| 1438 | Record = cast<CXXRecordDecl>(RD); |
| 1439 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1440 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1441 | E = Record->bases_end(); I != E; ++I) { |
| 1442 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1443 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1444 | if (!BaseRT) return false; |
| 1445 | |
| 1446 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1447 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1448 | return false; |
| 1449 | } |
| 1450 | |
| 1451 | return true; |
| 1452 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1453 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1454 | enum IMAKind { |
| 1455 | /// The reference is definitely not an instance member access. |
| 1456 | IMA_Static, |
| 1457 | |
| 1458 | /// The reference may be an implicit instance member access. |
| 1459 | IMA_Mixed, |
| 1460 | |
| 1461 | /// The reference may be to an instance member, but it is invalid if |
| 1462 | /// so, because the context is not an instance method. |
| 1463 | IMA_Mixed_StaticContext, |
| 1464 | |
| 1465 | /// The reference may be to an instance member, but it is invalid if |
| 1466 | /// so, because the context is from an unrelated class. |
| 1467 | IMA_Mixed_Unrelated, |
| 1468 | |
| 1469 | /// The reference is definitely an implicit instance member access. |
| 1470 | IMA_Instance, |
| 1471 | |
| 1472 | /// The reference may be to an unresolved using declaration. |
| 1473 | IMA_Unresolved, |
| 1474 | |
| 1475 | /// The reference may be to an unresolved using declaration and the |
| 1476 | /// context is not an instance method. |
| 1477 | IMA_Unresolved_StaticContext, |
| 1478 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1479 | /// All possible referrents are instance members and the current |
| 1480 | /// context is not an instance method. |
| 1481 | IMA_Error_StaticContext, |
| 1482 | |
| 1483 | /// All possible referrents are instance members of an unrelated |
| 1484 | /// class. |
| 1485 | IMA_Error_Unrelated |
| 1486 | }; |
| 1487 | |
| 1488 | /// The given lookup names class member(s) and is not being used for |
| 1489 | /// an address-of-member expression. Classify the type of access |
| 1490 | /// according to whether it's possible that this reference names an |
| 1491 | /// instance member. This is best-effort; it is okay to |
| 1492 | /// conservatively answer "yes", in which case some errors will simply |
| 1493 | /// not be caught until template-instantiation. |
| 1494 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1495 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1496 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1497 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1498 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1499 | bool isStaticContext = |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1500 | (!isa<CXXMethodDecl>(DC) || |
| 1501 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1502 | |
| 1503 | if (R.isUnresolvableResult()) |
| 1504 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1505 | |
| 1506 | // Collect all the declaring classes of instance members we find. |
| 1507 | bool hasNonInstance = false; |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1508 | bool hasField = false; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1509 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1510 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1511 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1512 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1513 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1514 | if (dyn_cast<FieldDecl>(D)) |
| 1515 | hasField = true; |
| 1516 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1517 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1518 | Classes.insert(R->getCanonicalDecl()); |
| 1519 | } |
| 1520 | else |
| 1521 | hasNonInstance = true; |
| 1522 | } |
| 1523 | |
| 1524 | // If we didn't find any instance members, it can't be an implicit |
| 1525 | // member reference. |
| 1526 | if (Classes.empty()) |
| 1527 | return IMA_Static; |
| 1528 | |
| 1529 | // If the current context is not an instance method, it can't be |
| 1530 | // an implicit member reference. |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1531 | if (isStaticContext) { |
| 1532 | if (hasNonInstance) |
| 1533 | return IMA_Mixed_StaticContext; |
| 1534 | |
| 1535 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1536 | // C++0x [expr.prim.general]p10: |
| 1537 | // An id-expression that denotes a non-static data member or non-static |
| 1538 | // member function of a class can only be used: |
| 1539 | // (...) |
| 1540 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1541 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1542 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1543 | if (isUnevaluatedExpression) |
| 1544 | return IMA_Mixed_StaticContext; |
| 1545 | } |
| 1546 | |
| 1547 | return IMA_Error_StaticContext; |
| 1548 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1549 | |
Argyrios Kyrtzidis | 0d8dc46 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1550 | CXXRecordDecl * |
| 1551 | contextClass = cast<CXXMethodDecl>(DC)->getParent()->getCanonicalDecl(); |
| 1552 | |
| 1553 | // [class.mfct.non-static]p3: |
| 1554 | // ...is used in the body of a non-static member function of class X, |
| 1555 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 1556 | // non-static non-type member of some class C [...] |
| 1557 | // ...if C is not X or a base class of X, the class member access expression |
| 1558 | // is ill-formed. |
| 1559 | if (R.getNamingClass() && |
| 1560 | contextClass != R.getNamingClass()->getCanonicalDecl() && |
| 1561 | contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) |
| 1562 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1563 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1564 | // If we can prove that the current context is unrelated to all the |
| 1565 | // declaring classes, it can't be an implicit member reference (in |
| 1566 | // which case it's an error if any of those members are selected). |
Argyrios Kyrtzidis | 0d8dc46 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1567 | if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1568 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1569 | |
| 1570 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1571 | } |
| 1572 | |
| 1573 | /// Diagnose a reference to a field with no object available. |
| 1574 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1575 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1576 | NamedDecl *rep, |
| 1577 | const DeclarationNameInfo &nameInfo) { |
| 1578 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1579 | SourceRange Range(Loc); |
| 1580 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1581 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1582 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1583 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1584 | if (MD->isStatic()) { |
| 1585 | // "invalid use of member 'x' in static member function" |
| 1586 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1587 | << Range << nameInfo.getName(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1588 | return; |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1593 | << nameInfo.getName() << Range; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1594 | return; |
| 1595 | } |
| 1596 | |
| 1597 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1600 | /// Diagnose an empty lookup. |
| 1601 | /// |
| 1602 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1603 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1604 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1605 | DeclarationName Name = R.getLookupName(); |
| 1606 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1607 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1608 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1609 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1610 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1611 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1612 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1613 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1614 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1615 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1616 | // If the original lookup was an unqualified lookup, fake an |
| 1617 | // unqualified lookup. This is useful when (for example) the |
| 1618 | // original lookup would not have found something because it was a |
| 1619 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1620 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1621 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1622 | if (isa<CXXRecordDecl>(DC)) { |
| 1623 | LookupQualifiedName(R, DC); |
| 1624 | |
| 1625 | if (!R.empty()) { |
| 1626 | // Don't give errors about ambiguities in this lookup. |
| 1627 | R.suppressDiagnostics(); |
| 1628 | |
| 1629 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1630 | bool isInstance = CurMethod && |
| 1631 | CurMethod->isInstance() && |
| 1632 | DC == CurMethod->getParent(); |
| 1633 | |
| 1634 | // Give a code modification hint to insert 'this->'. |
| 1635 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1636 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1637 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1638 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1639 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1640 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1641 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1642 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1643 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1644 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1645 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1646 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1647 | R.getNameLoc(), DepThisType, false); |
| 1648 | TemplateArgumentListInfo TList; |
| 1649 | if (ULE->hasExplicitTemplateArgs()) |
| 1650 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1652 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1653 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1654 | CXXDependentScopeMemberExpr *DepExpr = |
| 1655 | CXXDependentScopeMemberExpr::Create( |
| 1656 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1657 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1658 | R.getLookupNameInfo(), &TList); |
| 1659 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1660 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1661 | // FIXME: we should be able to handle this case too. It is correct |
| 1662 | // to add this-> here. This is a workaround for PR7947. |
| 1663 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1664 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1665 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1666 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1667 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Do we really want to note all of these? |
| 1670 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1671 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1672 | |
| 1673 | // Tell the callee to try to recover. |
| 1674 | return false; |
| 1675 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1676 | |
| 1677 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } |
| 1680 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1681 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1682 | DeclarationName Corrected; |
Daniel Dunbar | dc32cdf | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1683 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1684 | if (!R.empty()) { |
| 1685 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1686 | if (SS.isEmpty()) |
| 1687 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1688 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1689 | R.getLookupName().getAsString()); |
| 1690 | else |
| 1691 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1692 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1693 | << SS.getRange() |
| 1694 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1695 | R.getLookupName().getAsString()); |
| 1696 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1697 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1698 | << ND->getDeclName(); |
| 1699 | |
| 1700 | // Tell the callee to try to recover. |
| 1701 | return false; |
| 1702 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1703 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1704 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1705 | // FIXME: If we ended up with a typo for a type name or |
| 1706 | // Objective-C class name, we're in trouble because the parser |
| 1707 | // is in the wrong place to recover. Suggest the typo |
| 1708 | // correction, but don't make it a fix-it since we're not going |
| 1709 | // to recover well anyway. |
| 1710 | if (SS.isEmpty()) |
| 1711 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1712 | else |
| 1713 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1714 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1715 | << SS.getRange(); |
| 1716 | |
| 1717 | // Don't try to recover; it won't work. |
| 1718 | return true; |
| 1719 | } |
| 1720 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1721 | // 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] | 1722 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1723 | if (SS.isEmpty()) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1724 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1725 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1726 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1727 | << Name << computeDeclContext(SS, false) << Corrected |
| 1728 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1729 | return true; |
| 1730 | } |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1731 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | // Emit a special diagnostic for failed member lookups. |
| 1735 | // FIXME: computing the declaration context might fail here (?) |
| 1736 | if (!SS.isEmpty()) { |
| 1737 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1738 | << Name << computeDeclContext(SS, false) |
| 1739 | << SS.getRange(); |
| 1740 | return true; |
| 1741 | } |
| 1742 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1743 | // Give up, we can't recover. |
| 1744 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1745 | return true; |
| 1746 | } |
| 1747 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1748 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1749 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1750 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1751 | if (!IDecl) |
| 1752 | return 0; |
| 1753 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1754 | if (!ClassImpDecl) |
| 1755 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1756 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1757 | if (!property) |
| 1758 | return 0; |
| 1759 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1760 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1761 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1762 | return 0; |
| 1763 | return property; |
| 1764 | } |
| 1765 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1766 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1767 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1768 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1769 | if (!IDecl) |
| 1770 | return false; |
| 1771 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1772 | if (!ClassImpDecl) |
| 1773 | return false; |
| 1774 | if (ObjCPropertyImplDecl *PIDecl |
| 1775 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1776 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1777 | PIDecl->getPropertyIvarDecl()) |
| 1778 | return false; |
| 1779 | |
| 1780 | return true; |
| 1781 | } |
| 1782 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1783 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1784 | IdentifierInfo *II, |
| 1785 | SourceLocation NameLoc) { |
| 1786 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1787 | bool LookForIvars; |
| 1788 | if (Lookup.empty()) |
| 1789 | LookForIvars = true; |
| 1790 | else if (CurMeth->isClassMethod()) |
| 1791 | LookForIvars = false; |
| 1792 | else |
| 1793 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1794 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1795 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1796 | if (!LookForIvars) |
| 1797 | return 0; |
| 1798 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1799 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1800 | if (!IDecl) |
| 1801 | return 0; |
| 1802 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1803 | if (!ClassImpDecl) |
| 1804 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1805 | bool DynamicImplSeen = false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1806 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1807 | if (!property) |
| 1808 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1809 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1810 | DynamicImplSeen = |
| 1811 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1812 | // property implementation has a designated ivar. No need to assume a new |
| 1813 | // one. |
| 1814 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1815 | return 0; |
| 1816 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1817 | if (!DynamicImplSeen) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1818 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1819 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1820 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1821 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1822 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1823 | (Expr *)0, true); |
| 1824 | ClassImpDecl->addDecl(Ivar); |
| 1825 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1826 | property->setPropertyIvarDecl(Ivar); |
| 1827 | return Ivar; |
| 1828 | } |
| 1829 | return 0; |
| 1830 | } |
| 1831 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1832 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1833 | CXXScopeSpec &SS, |
| 1834 | UnqualifiedId &Id, |
| 1835 | bool HasTrailingLParen, |
| 1836 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1837 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1838 | "cannot be direct & operand and have a trailing lparen"); |
| 1839 | |
| 1840 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1841 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1842 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1843 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1846 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1847 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1848 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1849 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1850 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1851 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1852 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1853 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | // C++ [temp.dep.expr]p3: |
| 1855 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1856 | // -- an identifier that was declared with a dependent type, |
| 1857 | // (note: handled after lookup) |
| 1858 | // -- a template-id that is dependent, |
| 1859 | // (note: handled in BuildTemplateIdExpr) |
| 1860 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1861 | // -- a nested-name-specifier that contains a class-name that |
| 1862 | // names a dependent type. |
| 1863 | // Determine whether this is a member of an unknown specialization; |
| 1864 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1865 | bool DependentID = false; |
| 1866 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1867 | Name.getCXXNameType()->isDependentType()) { |
| 1868 | DependentID = true; |
| 1869 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1870 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1871 | if (RequireCompleteDeclContext(SS, DC)) |
| 1872 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1873 | } else { |
| 1874 | DependentID = true; |
| 1875 | } |
| 1876 | } |
| 1877 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1878 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1879 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1880 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1881 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1882 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1883 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1884 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1885 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1886 | // Lookup the template name again to correctly establish the context in |
| 1887 | // which it was found. This is really unfortunate as we already did the |
| 1888 | // lookup to determine that it was a template name in the first place. If |
| 1889 | // this becomes a performance hit, we can work harder to preserve those |
| 1890 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1891 | bool MemberOfUnknownSpecialization; |
| 1892 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1893 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1894 | |
| 1895 | if (MemberOfUnknownSpecialization || |
| 1896 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1897 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1898 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1899 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1900 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1901 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1903 | // If the result might be in a dependent base class, this is a dependent |
| 1904 | // id-expression. |
| 1905 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1906 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1907 | TemplateArgs); |
| 1908 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1909 | // If this reference is in an Objective-C method, then we need to do |
| 1910 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1911 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1912 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1913 | if (E.isInvalid()) |
| 1914 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1916 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1917 | return Owned(Ex); |
| 1918 | |
| 1919 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1920 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1921 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1922 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1923 | if (const ObjCPropertyDecl *Property = |
| 1924 | canSynthesizeProvisionalIvar(II)) { |
| 1925 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1926 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1927 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1928 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1929 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1930 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1931 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1932 | // for further use, this must be set to false if in class method. |
| 1933 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1934 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1935 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1936 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1937 | if (R.isAmbiguous()) |
| 1938 | return ExprError(); |
| 1939 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1940 | // Determine whether this name might be a candidate for |
| 1941 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1942 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1943 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1944 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1945 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1946 | // in C90, extension in C99, forbidden in C++). |
| 1947 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1948 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1949 | if (D) R.addDecl(D); |
| 1950 | } |
| 1951 | |
| 1952 | // If this name wasn't predeclared and if this is not a function |
| 1953 | // call, diagnose the problem. |
| 1954 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1955 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1956 | return ExprError(); |
| 1957 | |
| 1958 | assert(!R.empty() && |
| 1959 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1960 | |
| 1961 | // If we found an Objective-C instance variable, let |
| 1962 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1963 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1964 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1965 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1966 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1967 | assert(E.isInvalid() || E.get()); |
| 1968 | return move(E); |
| 1969 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1970 | } |
| 1971 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1973 | // This is guaranteed from this point on. |
| 1974 | assert(!R.empty() || ADL); |
| 1975 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1976 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1977 | // C++ [class.mfct.non-static]p3: |
| 1978 | // When an id-expression that is not part of a class member access |
| 1979 | // syntax and not used to form a pointer to member is used in the |
| 1980 | // body of a non-static member function of class X, if name lookup |
| 1981 | // resolves the name in the id-expression to a non-static non-type |
| 1982 | // member of some class C, the id-expression is transformed into a |
| 1983 | // class member access expression using (*this) as the |
| 1984 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1985 | // |
| 1986 | // But we don't actually need to do this for '&' operands if R |
| 1987 | // resolved to a function or overloaded function set, because the |
| 1988 | // expression is ill-formed if it actually works out to be a |
| 1989 | // non-static member function: |
| 1990 | // |
| 1991 | // C++ [expr.ref]p4: |
| 1992 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1993 | // [t]he expression can be used only as the left-hand operand of a |
| 1994 | // member function call. |
| 1995 | // |
| 1996 | // There are other safeguards against such uses, but it's important |
| 1997 | // to get this right here so that we don't end up making a |
| 1998 | // spuriously dependent expression if we're inside a dependent |
| 1999 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2000 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2001 | bool MightBeImplicitMember; |
| 2002 | if (!isAddressOfOperand) |
| 2003 | MightBeImplicitMember = true; |
| 2004 | else if (!SS.isEmpty()) |
| 2005 | MightBeImplicitMember = false; |
| 2006 | else if (R.isOverloadedResult()) |
| 2007 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 2008 | else if (R.isUnresolvableResult()) |
| 2009 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2010 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2011 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 2012 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2013 | |
| 2014 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2015 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2018 | if (TemplateArgs) |
| 2019 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2020 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2021 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 2022 | } |
| 2023 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2024 | /// Builds an expression which might be an implicit member expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2025 | ExprResult |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2026 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2027 | LookupResult &R, |
| 2028 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2029 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 2030 | case IMA_Instance: |
| 2031 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 2032 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2033 | case IMA_Mixed: |
| 2034 | case IMA_Mixed_Unrelated: |
| 2035 | case IMA_Unresolved: |
| 2036 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 2037 | |
| 2038 | case IMA_Static: |
| 2039 | case IMA_Mixed_StaticContext: |
| 2040 | case IMA_Unresolved_StaticContext: |
| 2041 | if (TemplateArgs) |
| 2042 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 2043 | return BuildDeclarationNameExpr(SS, R, false); |
| 2044 | |
| 2045 | case IMA_Error_StaticContext: |
| 2046 | case IMA_Error_Unrelated: |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2047 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 2048 | R.getLookupNameInfo()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2049 | return ExprError(); |
| 2050 | } |
| 2051 | |
| 2052 | llvm_unreachable("unexpected instance member access kind"); |
| 2053 | return ExprError(); |
| 2054 | } |
| 2055 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2056 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 2057 | /// declaration name, generally during template instantiation. |
| 2058 | /// There's a large number of things which don't need to be done along |
| 2059 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2060 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2061 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2062 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2063 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2064 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2065 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2066 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2067 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2068 | return ExprError(); |
| 2069 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2070 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2071 | LookupQualifiedName(R, DC); |
| 2072 | |
| 2073 | if (R.isAmbiguous()) |
| 2074 | return ExprError(); |
| 2075 | |
| 2076 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2077 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 2078 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2079 | return ExprError(); |
| 2080 | } |
| 2081 | |
| 2082 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 2083 | } |
| 2084 | |
| 2085 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 2086 | /// detected that we're currently inside an ObjC method. Perform some |
| 2087 | /// additional lookup. |
| 2088 | /// |
| 2089 | /// Ideally, most of this would be done by lookup, but there's |
| 2090 | /// actually quite a lot of extra work involved. |
| 2091 | /// |
| 2092 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2093 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2094 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2095 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2096 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2097 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2098 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2099 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 2100 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 2101 | // found a decl, but that decl is outside the current instance method (i.e. |
| 2102 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 2103 | // this name, if the lookup sucedes, we replace it our current decl. |
| 2104 | |
| 2105 | // If we're in a class method, we don't normally want to look for |
| 2106 | // ivars. But if we don't find anything else, and there's an |
| 2107 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2108 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2109 | |
| 2110 | bool LookForIvars; |
| 2111 | if (Lookup.empty()) |
| 2112 | LookForIvars = true; |
| 2113 | else if (IsClassMethod) |
| 2114 | LookForIvars = false; |
| 2115 | else |
| 2116 | LookForIvars = (Lookup.isSingleResult() && |
| 2117 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2118 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2119 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2120 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2121 | ObjCInterfaceDecl *ClassDeclared; |
| 2122 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2123 | // Diagnose using an ivar in a class method. |
| 2124 | if (IsClassMethod) |
| 2125 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2126 | << IV->getDeclName()); |
| 2127 | |
| 2128 | // If we're referencing an invalid decl, just return this as a silent |
| 2129 | // error node. The error diagnostic was already emitted on the decl. |
| 2130 | if (IV->isInvalidDecl()) |
| 2131 | return ExprError(); |
| 2132 | |
| 2133 | // Check if referencing a field with __attribute__((deprecated)). |
| 2134 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2135 | return ExprError(); |
| 2136 | |
| 2137 | // Diagnose the use of an ivar outside of the declaring class. |
| 2138 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 2139 | ClassDeclared != IFace) |
| 2140 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2141 | |
| 2142 | // FIXME: This should use a new expr for a direct reference, don't |
| 2143 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2144 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2145 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2146 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2147 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2148 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2149 | SelfName, false, false); |
| 2150 | if (SelfExpr.isInvalid()) |
| 2151 | return ExprError(); |
| 2152 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2153 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2154 | if (SelfExpr.isInvalid()) |
| 2155 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2156 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2157 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2158 | Expr *base = SelfExpr.take(); |
| 2159 | base = base->IgnoreParenImpCasts(); |
| 2160 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 2161 | const NamedDecl *ND = DE->getDecl(); |
| 2162 | if (!isa<ImplicitParamDecl>(ND)) { |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2163 | // relax the rule such that it is allowed to have a shadow 'self' |
| 2164 | // where stand-alone ivar can be found in this 'self' object. |
| 2165 | // This is to match gcc's behavior. |
| 2166 | ObjCInterfaceDecl *selfIFace = 0; |
| 2167 | if (const ObjCObjectPointerType *OPT = |
| 2168 | base->getType()->getAsObjCInterfacePointerType()) |
| 2169 | selfIFace = OPT->getInterfaceDecl(); |
| 2170 | if (!selfIFace || |
| 2171 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2172 | Diag(Loc, diag::error_implicit_ivar_access) |
| 2173 | << IV->getDeclName(); |
| 2174 | Diag(ND->getLocation(), diag::note_declared_at); |
| 2175 | return ExprError(); |
| 2176 | } |
Fariborz Jahanian | eefa76e | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2177 | } |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2178 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2179 | return Owned(new (Context) |
| 2180 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2181 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2182 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2183 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2184 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2185 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2186 | ObjCInterfaceDecl *ClassDeclared; |
| 2187 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2188 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 2189 | IFace == ClassDeclared) |
| 2190 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2191 | } |
| 2192 | } |
| 2193 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2194 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2195 | // FIXME. Consolidate this with similar code in LookupName. |
| 2196 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 2197 | if (!(getLangOptions().CPlusPlus && |
| 2198 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2199 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2200 | S, Lookup.isForRedeclaration(), |
| 2201 | Lookup.getNameLoc()); |
| 2202 | if (D) Lookup.addDecl(D); |
| 2203 | } |
| 2204 | } |
| 2205 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2206 | // Sentinel value saying that we didn't do anything special. |
| 2207 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2208 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2209 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2210 | /// \brief Cast a base object to a member's actual type. |
| 2211 | /// |
| 2212 | /// Logically this happens in three phases: |
| 2213 | /// |
| 2214 | /// * First we cast from the base type to the naming class. |
| 2215 | /// The naming class is the class into which we were looking |
| 2216 | /// when we found the member; it's the qualifier type if a |
| 2217 | /// qualifier was provided, and otherwise it's the base type. |
| 2218 | /// |
| 2219 | /// * Next we cast from the naming class to the declaring class. |
| 2220 | /// If the member we found was brought into a class's scope by |
| 2221 | /// a using declaration, this is that class; otherwise it's |
| 2222 | /// the class declaring the member. |
| 2223 | /// |
| 2224 | /// * Finally we cast from the declaring class to the "true" |
| 2225 | /// declaring class of the member. This conversion does not |
| 2226 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2227 | ExprResult |
| 2228 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2229 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2230 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2231 | NamedDecl *Member) { |
| 2232 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2233 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2234 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2235 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2236 | QualType DestRecordType; |
| 2237 | QualType DestType; |
| 2238 | QualType FromRecordType; |
| 2239 | QualType FromType = From->getType(); |
| 2240 | bool PointerConversions = false; |
| 2241 | if (isa<FieldDecl>(Member)) { |
| 2242 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2244 | if (FromType->getAs<PointerType>()) { |
| 2245 | DestType = Context.getPointerType(DestRecordType); |
| 2246 | FromRecordType = FromType->getPointeeType(); |
| 2247 | PointerConversions = true; |
| 2248 | } else { |
| 2249 | DestType = DestRecordType; |
| 2250 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2251 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2252 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2253 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2254 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2255 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2256 | DestType = Method->getThisType(Context); |
| 2257 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2259 | if (FromType->getAs<PointerType>()) { |
| 2260 | FromRecordType = FromType->getPointeeType(); |
| 2261 | PointerConversions = true; |
| 2262 | } else { |
| 2263 | FromRecordType = FromType; |
| 2264 | DestType = DestRecordType; |
| 2265 | } |
| 2266 | } else { |
| 2267 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2268 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2269 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2270 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2271 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2272 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2273 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2274 | // If the unqualified types are the same, no conversion is necessary. |
| 2275 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2276 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2277 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2278 | SourceRange FromRange = From->getSourceRange(); |
| 2279 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2280 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2281 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2282 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2283 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2284 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2285 | // class name. |
| 2286 | // |
| 2287 | // If the member was a qualified name and the qualified referred to a |
| 2288 | // specific base subobject type, we'll cast to that intermediate type |
| 2289 | // first and then to the object in which the member is declared. That allows |
| 2290 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2291 | // |
| 2292 | // class Base { public: int x; }; |
| 2293 | // class Derived1 : public Base { }; |
| 2294 | // class Derived2 : public Base { }; |
| 2295 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2296 | // |
| 2297 | // void VeryDerived::f() { |
| 2298 | // x = 17; // error: ambiguous base subobjects |
| 2299 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2300 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2301 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2302 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2303 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2304 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2305 | |
| 2306 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2307 | |
| 2308 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2309 | // type of the object type, in which case we just ignore it. |
| 2310 | // Otherwise build the appropriate casts. |
| 2311 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2312 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2313 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2314 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2315 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2316 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2317 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2318 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2319 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2320 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2321 | |
| 2322 | FromType = QType; |
| 2323 | FromRecordType = QRecordType; |
| 2324 | |
| 2325 | // If the qualifier type was the same as the destination type, |
| 2326 | // we're done. |
| 2327 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2328 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2329 | } |
| 2330 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2331 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2332 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2333 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2334 | // If we actually found the member through a using declaration, cast |
| 2335 | // down to the using declaration's type. |
| 2336 | // |
| 2337 | // Pointer equality is fine here because only one declaration of a |
| 2338 | // class ever has member declarations. |
| 2339 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2340 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2341 | QualType URecordType = Context.getTypeDeclType( |
| 2342 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2343 | |
| 2344 | // We only need to do this if the naming-class to declaring-class |
| 2345 | // conversion is non-trivial. |
| 2346 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2347 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2348 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2349 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2350 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2351 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2352 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2353 | QualType UType = URecordType; |
| 2354 | if (PointerConversions) |
| 2355 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2356 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2357 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2358 | FromType = UType; |
| 2359 | FromRecordType = URecordType; |
| 2360 | } |
| 2361 | |
| 2362 | // We don't do access control for the conversion from the |
| 2363 | // declaring class to the true declaring class. |
| 2364 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2365 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2366 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2367 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2368 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2369 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2370 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2371 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2372 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2373 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2374 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2375 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2376 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2377 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2378 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2379 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2380 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2381 | const DeclarationNameInfo &MemberNameInfo, |
| 2382 | QualType Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2383 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2384 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2385 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2386 | Member, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2387 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2390 | static ExprResult |
| 2391 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2392 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2393 | DeclAccessPair FoundDecl, |
| 2394 | const DeclarationNameInfo &MemberNameInfo) { |
| 2395 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2396 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2397 | // that *x is always an l-value), except that if the base isn't |
| 2398 | // an ordinary object then we must have an rvalue. |
| 2399 | ExprValueKind VK = VK_LValue; |
| 2400 | ExprObjectKind OK = OK_Ordinary; |
| 2401 | if (!IsArrow) { |
| 2402 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2403 | VK = BaseExpr->getValueKind(); |
| 2404 | else |
| 2405 | VK = VK_RValue; |
| 2406 | } |
| 2407 | if (VK != VK_RValue && Field->isBitField()) |
| 2408 | OK = OK_BitField; |
| 2409 | |
| 2410 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2411 | QualType MemberType = Field->getType(); |
| 2412 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2413 | MemberType = Ref->getPointeeType(); |
| 2414 | VK = VK_LValue; |
| 2415 | } else { |
| 2416 | QualType BaseType = BaseExpr->getType(); |
| 2417 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2418 | |
| 2419 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2420 | |
| 2421 | // GC attributes are never picked up by members. |
| 2422 | BaseQuals.removeObjCGCAttr(); |
| 2423 | |
| 2424 | // CVR attributes from the base are picked up by members, |
| 2425 | // except that 'mutable' members don't pick up 'const'. |
| 2426 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2427 | |
| 2428 | Qualifiers MemberQuals |
| 2429 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2430 | |
| 2431 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2432 | assert(!MemberQuals.hasAddressSpace()); |
| 2433 | |
| 2434 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2435 | if (Combined != MemberQuals) |
| 2436 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2437 | } |
| 2438 | |
| 2439 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2440 | ExprResult Base = |
| 2441 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2442 | FoundDecl, Field); |
| 2443 | if (Base.isInvalid()) |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2444 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2445 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2446 | Field, FoundDecl, MemberNameInfo, |
| 2447 | MemberType, VK, OK)); |
| 2448 | } |
| 2449 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2450 | /// Builds an implicit member access expression. The current context |
| 2451 | /// is known to be an instance method, and the given unqualified lookup |
| 2452 | /// set is known to contain only instance members, at least one of which |
| 2453 | /// is from an appropriate type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2454 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2455 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2456 | LookupResult &R, |
| 2457 | const TemplateArgumentListInfo *TemplateArgs, |
| 2458 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2459 | assert(!R.empty() && !R.isAmbiguous()); |
| 2460 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2461 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2463 | // We may have found a field within an anonymous union or struct |
| 2464 | // (C++ [class.union]). |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2465 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2466 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2467 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2468 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2469 | // If this is known to be an instance access, go ahead and build an |
| 2470 | // implicit 'this' expression now. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2471 | // 'this' expression now. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2472 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 2473 | assert(method && "didn't correctly pre-flight capture of 'this'"); |
| 2474 | |
| 2475 | QualType thisType = method->getThisType(Context); |
| 2476 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2477 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2478 | SourceLocation Loc = R.getNameLoc(); |
| 2479 | if (SS.getRange().isValid()) |
| 2480 | Loc = SS.getRange().getBegin(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2481 | baseExpr = new (Context) CXXThisExpr(loc, thisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2484 | return BuildMemberReferenceExpr(baseExpr, thisType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2485 | /*OpLoc*/ SourceLocation(), |
| 2486 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2487 | SS, |
| 2488 | /*FirstQualifierInScope*/ 0, |
| 2489 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2490 | } |
| 2491 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2492 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2493 | const LookupResult &R, |
| 2494 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2495 | // Only when used directly as the postfix-expression of a call. |
| 2496 | if (!HasTrailingLParen) |
| 2497 | return false; |
| 2498 | |
| 2499 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2500 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2501 | return false; |
| 2502 | |
| 2503 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2504 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2505 | return false; |
| 2506 | |
| 2507 | // Turn off ADL when we find certain kinds of declarations during |
| 2508 | // normal lookup: |
| 2509 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2510 | NamedDecl *D = *I; |
| 2511 | |
| 2512 | // C++0x [basic.lookup.argdep]p3: |
| 2513 | // -- a declaration of a class member |
| 2514 | // Since using decls preserve this property, we check this on the |
| 2515 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2516 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2517 | return false; |
| 2518 | |
| 2519 | // C++0x [basic.lookup.argdep]p3: |
| 2520 | // -- a block-scope function declaration that is not a |
| 2521 | // using-declaration |
| 2522 | // NOTE: we also trigger this for function templates (in fact, we |
| 2523 | // don't check the decl type at all, since all other decl types |
| 2524 | // turn off ADL anyway). |
| 2525 | if (isa<UsingShadowDecl>(D)) |
| 2526 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2527 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2528 | return false; |
| 2529 | |
| 2530 | // C++0x [basic.lookup.argdep]p3: |
| 2531 | // -- a declaration that is neither a function or a function |
| 2532 | // template |
| 2533 | // And also for builtin functions. |
| 2534 | if (isa<FunctionDecl>(D)) { |
| 2535 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2536 | |
| 2537 | // But also builtin functions. |
| 2538 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2539 | return false; |
| 2540 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
| 2544 | return true; |
| 2545 | } |
| 2546 | |
| 2547 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2548 | /// Diagnoses obvious problems with the use of the given declaration |
| 2549 | /// as an expression. This is only actually called for lookups that |
| 2550 | /// were not overloaded, and it doesn't promise that the declaration |
| 2551 | /// will in fact be used. |
| 2552 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2553 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2554 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2555 | return true; |
| 2556 | } |
| 2557 | |
| 2558 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2559 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2560 | return true; |
| 2561 | } |
| 2562 | |
| 2563 | if (isa<NamespaceDecl>(D)) { |
| 2564 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2565 | return true; |
| 2566 | } |
| 2567 | |
| 2568 | return false; |
| 2569 | } |
| 2570 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2571 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2572 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2573 | LookupResult &R, |
| 2574 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2575 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2576 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2577 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2578 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2579 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2580 | |
| 2581 | // We only need to check the declaration if there's exactly one |
| 2582 | // result, because in the overloaded case the results can only be |
| 2583 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2584 | if (R.isSingleResult() && |
| 2585 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2586 | return ExprError(); |
| 2587 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2588 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2589 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2590 | // we've picked a target. |
| 2591 | R.suppressDiagnostics(); |
| 2592 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2593 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2594 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2595 | SS.getWithLocInContext(Context), |
| 2596 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2597 | NeedsADL, R.isOverloadedResult(), |
| 2598 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2599 | |
| 2600 | return Owned(ULE); |
| 2601 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2602 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2603 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2604 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2605 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2606 | const DeclarationNameInfo &NameInfo, |
| 2607 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2608 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2609 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2610 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2611 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2612 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2613 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2614 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2615 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2616 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2617 | // Specifically diagnose references to class templates that are missing |
| 2618 | // a template argument list. |
| 2619 | Diag(Loc, diag::err_template_decl_ref) |
| 2620 | << Template << SS.getRange(); |
| 2621 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2622 | return ExprError(); |
| 2623 | } |
| 2624 | |
| 2625 | // Make sure that we're referring to a value. |
| 2626 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2627 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2628 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2629 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2630 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2631 | return ExprError(); |
| 2632 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2634 | // Check whether this declaration can be used. Note that we suppress |
| 2635 | // this check when we're going to perform argument-dependent lookup |
| 2636 | // on this function name, because this might not be the function |
| 2637 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2638 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2639 | return ExprError(); |
| 2640 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2641 | // Only create DeclRefExpr's for valid Decl's. |
| 2642 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2643 | return ExprError(); |
| 2644 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2645 | // Handle members of anonymous structs and unions. If we got here, |
| 2646 | // and the reference is to a class member indirect field, then this |
| 2647 | // must be the subject of a pointer-to-member expression. |
| 2648 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2649 | if (!indirectField->isCXXClassMember()) |
| 2650 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2651 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2652 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2653 | // If the identifier reference is inside a block, and it refers to a value |
| 2654 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2655 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2656 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2657 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2658 | // We do not do this for things like enum constants, global variables, etc, |
| 2659 | // as they do not get snapshotted. |
| 2660 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2661 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2662 | case CR_Error: |
| 2663 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2664 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2665 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2666 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2667 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2668 | |
| 2669 | case CR_CaptureByRef: |
| 2670 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2671 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2672 | |
| 2673 | case CR_NoCapture: { |
| 2674 | // If this reference is not in a block or if the referenced |
| 2675 | // variable is within the block, create a normal DeclRefExpr. |
| 2676 | |
| 2677 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2678 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2679 | |
| 2680 | switch (D->getKind()) { |
| 2681 | // Ignore all the non-ValueDecl kinds. |
| 2682 | #define ABSTRACT_DECL(kind) |
| 2683 | #define VALUE(type, base) |
| 2684 | #define DECL(type, base) \ |
| 2685 | case Decl::type: |
| 2686 | #include "clang/AST/DeclNodes.inc" |
| 2687 | llvm_unreachable("invalid value decl kind"); |
| 2688 | return ExprError(); |
| 2689 | |
| 2690 | // These shouldn't make it here. |
| 2691 | case Decl::ObjCAtDefsField: |
| 2692 | case Decl::ObjCIvar: |
| 2693 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2694 | return ExprError(); |
| 2695 | |
| 2696 | // Enum constants are always r-values and never references. |
| 2697 | // Unresolved using declarations are dependent. |
| 2698 | case Decl::EnumConstant: |
| 2699 | case Decl::UnresolvedUsingValue: |
| 2700 | valueKind = VK_RValue; |
| 2701 | break; |
| 2702 | |
| 2703 | // Fields and indirect fields that got here must be for |
| 2704 | // pointer-to-member expressions; we just call them l-values for |
| 2705 | // internal consistency, because this subexpression doesn't really |
| 2706 | // exist in the high-level semantics. |
| 2707 | case Decl::Field: |
| 2708 | case Decl::IndirectField: |
| 2709 | assert(getLangOptions().CPlusPlus && |
| 2710 | "building reference to field in C?"); |
| 2711 | |
| 2712 | // These can't have reference type in well-formed programs, but |
| 2713 | // for internal consistency we do this anyway. |
| 2714 | type = type.getNonReferenceType(); |
| 2715 | valueKind = VK_LValue; |
| 2716 | break; |
| 2717 | |
| 2718 | // Non-type template parameters are either l-values or r-values |
| 2719 | // depending on the type. |
| 2720 | case Decl::NonTypeTemplateParm: { |
| 2721 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2722 | type = reftype->getPointeeType(); |
| 2723 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2724 | break; |
| 2725 | } |
| 2726 | |
| 2727 | // For non-references, we need to strip qualifiers just in case |
| 2728 | // the template parameter was declared as 'const int' or whatever. |
| 2729 | valueKind = VK_RValue; |
| 2730 | type = type.getUnqualifiedType(); |
| 2731 | break; |
| 2732 | } |
| 2733 | |
| 2734 | case Decl::Var: |
| 2735 | // In C, "extern void blah;" is valid and is an r-value. |
| 2736 | if (!getLangOptions().CPlusPlus && |
| 2737 | !type.hasQualifiers() && |
| 2738 | type->isVoidType()) { |
| 2739 | valueKind = VK_RValue; |
| 2740 | break; |
| 2741 | } |
| 2742 | // fallthrough |
| 2743 | |
| 2744 | case Decl::ImplicitParam: |
| 2745 | case Decl::ParmVar: |
| 2746 | // These are always l-values. |
| 2747 | valueKind = VK_LValue; |
| 2748 | type = type.getNonReferenceType(); |
| 2749 | break; |
| 2750 | |
| 2751 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2752 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2753 | |
| 2754 | // If we're referring to a function with an __unknown_anytype |
| 2755 | // result type, make the entire expression __unknown_anytype. |
| 2756 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2757 | type = Context.UnknownAnyTy; |
| 2758 | valueKind = VK_RValue; |
| 2759 | break; |
| 2760 | } |
| 2761 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2762 | // Functions are l-values in C++. |
| 2763 | if (getLangOptions().CPlusPlus) { |
| 2764 | valueKind = VK_LValue; |
| 2765 | break; |
| 2766 | } |
| 2767 | |
| 2768 | // C99 DR 316 says that, if a function type comes from a |
| 2769 | // function definition (without a prototype), that type is only |
| 2770 | // used for checking compatibility. Therefore, when referencing |
| 2771 | // the function, we pretend that we don't have the full function |
| 2772 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2773 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2774 | isa<FunctionProtoType>(fty)) |
| 2775 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2776 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2777 | |
| 2778 | // Functions are r-values in C. |
| 2779 | valueKind = VK_RValue; |
| 2780 | break; |
| 2781 | } |
| 2782 | |
| 2783 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2784 | // If we're referring to a method with an __unknown_anytype |
| 2785 | // result type, make the entire expression __unknown_anytype. |
| 2786 | // This should only be possible with a type written directly. |
| 2787 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2788 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2789 | type = Context.UnknownAnyTy; |
| 2790 | valueKind = VK_RValue; |
| 2791 | break; |
| 2792 | } |
| 2793 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2794 | // C++ methods are l-values if static, r-values if non-static. |
| 2795 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2796 | valueKind = VK_LValue; |
| 2797 | break; |
| 2798 | } |
| 2799 | // fallthrough |
| 2800 | |
| 2801 | case Decl::CXXConversion: |
| 2802 | case Decl::CXXDestructor: |
| 2803 | case Decl::CXXConstructor: |
| 2804 | valueKind = VK_RValue; |
| 2805 | break; |
| 2806 | } |
| 2807 | |
| 2808 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2809 | } |
| 2810 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2811 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2812 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2813 | llvm_unreachable("unknown capture result"); |
| 2814 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2817 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2818 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2819 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2820 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2821 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2822 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2823 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2824 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2825 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2826 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2827 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2828 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2829 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2830 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2831 | if (!currentDecl && getCurBlock()) |
| 2832 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2833 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2834 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2835 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2836 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2837 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2838 | QualType ResTy; |
| 2839 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2840 | ResTy = Context.DependentTy; |
| 2841 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2842 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2843 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2844 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2845 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2846 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2847 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2848 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2851 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2853 | bool Invalid = false; |
| 2854 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2855 | if (Invalid) |
| 2856 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2857 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2858 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2859 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2860 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2861 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2862 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2863 | QualType Ty; |
| 2864 | if (!getLangOptions().CPlusPlus) |
| 2865 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2866 | else if (Literal.isWide()) |
| 2867 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2868 | else if (Literal.isMultiChar()) |
| 2869 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2870 | else |
| 2871 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2872 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2873 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2874 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2875 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2878 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2879 | // 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] | 2880 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2881 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2882 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2883 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2884 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2885 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2886 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2889 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2890 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2891 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2893 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2894 | bool Invalid = false; |
| 2895 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2896 | if (Invalid) |
| 2897 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2898 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2899 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2900 | Tok.getLocation(), PP); |
| 2901 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2902 | return ExprError(); |
| 2903 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2904 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2905 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2906 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2907 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2908 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2909 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2910 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2911 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2912 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2913 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2914 | |
| 2915 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2916 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2917 | using llvm::APFloat; |
| 2918 | APFloat Val(Format); |
| 2919 | |
| 2920 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2921 | |
| 2922 | // Overflow is always an error, but underflow is only an error if |
| 2923 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2924 | if ((result & APFloat::opOverflow) || |
| 2925 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2926 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2927 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2928 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2929 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2930 | APFloat::getLargest(Format).toString(buffer); |
| 2931 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2932 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2933 | APFloat::getSmallest(Format).toString(buffer); |
| 2934 | } |
| 2935 | |
| 2936 | Diag(Tok.getLocation(), diagnostic) |
| 2937 | << Ty |
| 2938 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2939 | } |
| 2940 | |
| 2941 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2942 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2943 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2944 | if (Ty == Context.DoubleTy) { |
| 2945 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2946 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2947 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2948 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2949 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2950 | } |
| 2951 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2952 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2953 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2954 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2955 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2956 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2957 | // long long is a C99 feature. |
| 2958 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2959 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2960 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2961 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2962 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2963 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2964 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2966 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2967 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2968 | Ty = Context.UnsignedLongLongTy; |
| 2969 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2970 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2971 | } else { |
| 2972 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2973 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2975 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2976 | // be an unsigned int. |
| 2977 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2978 | |
| 2979 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2980 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2981 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2982 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2983 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2984 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2985 | // Does it fit in a unsigned int? |
| 2986 | if (ResultVal.isIntN(IntSize)) { |
| 2987 | // Does it fit in a signed int? |
| 2988 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2989 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2990 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2991 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2992 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2993 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2994 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2995 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2996 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2997 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2998 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2999 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3000 | // Does it fit in a unsigned long? |
| 3001 | if (ResultVal.isIntN(LongSize)) { |
| 3002 | // Does it fit in a signed long? |
| 3003 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3004 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3005 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3006 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3007 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3008 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3011 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3012 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3013 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3014 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3015 | // Does it fit in a unsigned long long? |
| 3016 | if (ResultVal.isIntN(LongLongSize)) { |
| 3017 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 3018 | // To be compatible with MSVC, hex integer literals ending with the |
| 3019 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 3020 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 3021 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3022 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3023 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3024 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3025 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3026 | } |
| 3027 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3028 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3029 | // If we still couldn't decide a type, we probably have something that |
| 3030 | // 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] | 3031 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3032 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3033 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3034 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3035 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3036 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3037 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3038 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3039 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3040 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3041 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3042 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 3043 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 3044 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3046 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3047 | |
| 3048 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3049 | } |
| 3050 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3051 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3052 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3053 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3054 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3055 | } |
| 3056 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3057 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 3058 | SourceLocation Loc, |
| 3059 | SourceRange ArgRange) { |
| 3060 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 3061 | // scalar or vector data type argument..." |
| 3062 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 3063 | // type (C99 6.2.5p18) or void. |
| 3064 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 3065 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 3066 | << T << ArgRange; |
| 3067 | return true; |
| 3068 | } |
| 3069 | |
| 3070 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 3071 | "Scalar types should always be complete"); |
| 3072 | return false; |
| 3073 | } |
| 3074 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3075 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 3076 | SourceLocation Loc, |
| 3077 | SourceRange ArgRange, |
| 3078 | UnaryExprOrTypeTrait TraitKind) { |
| 3079 | // C99 6.5.3.4p1: |
| 3080 | if (T->isFunctionType()) { |
| 3081 | // alignof(function) is allowed as an extension. |
| 3082 | if (TraitKind == UETT_SizeOf) |
| 3083 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 3084 | return false; |
| 3085 | } |
| 3086 | |
| 3087 | // Allow sizeof(void)/alignof(void) as an extension. |
| 3088 | if (T->isVoidType()) { |
| 3089 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 3090 | return false; |
| 3091 | } |
| 3092 | |
| 3093 | return true; |
| 3094 | } |
| 3095 | |
| 3096 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 3097 | SourceLocation Loc, |
| 3098 | SourceRange ArgRange, |
| 3099 | UnaryExprOrTypeTrait TraitKind) { |
| 3100 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 3101 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 3102 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 3103 | << T << (TraitKind == UETT_SizeOf) |
| 3104 | << ArgRange; |
| 3105 | return true; |
| 3106 | } |
| 3107 | |
| 3108 | return false; |
| 3109 | } |
| 3110 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3111 | /// \brief Check the constrains on expression operands to unary type expression |
| 3112 | /// and type traits. |
| 3113 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3114 | /// Completes any types necessary and validates the constraints on the operand |
| 3115 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 3116 | /// the expression as it completes the type for that expression through template |
| 3117 | /// instantiation, etc. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3118 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 3119 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3120 | QualType ExprTy = Op->getType(); |
| 3121 | |
| 3122 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3123 | // the result is the size of the referenced type." |
| 3124 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3125 | // result shall be the alignment of the referenced type." |
| 3126 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3127 | ExprTy = Ref->getPointeeType(); |
| 3128 | |
| 3129 | if (ExprKind == UETT_VecStep) |
| 3130 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3131 | Op->getSourceRange()); |
| 3132 | |
| 3133 | // Whitelist some types as extensions |
| 3134 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3135 | Op->getSourceRange(), ExprKind)) |
| 3136 | return false; |
| 3137 | |
| 3138 | if (RequireCompleteExprType(Op, |
| 3139 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 3140 | << ExprKind << Op->getSourceRange(), |
| 3141 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 3142 | return true; |
| 3143 | |
| 3144 | // Completeing the expression's type may have changed it. |
| 3145 | ExprTy = Op->getType(); |
| 3146 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3147 | ExprTy = Ref->getPointeeType(); |
| 3148 | |
| 3149 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 3150 | Op->getSourceRange(), ExprKind)) |
| 3151 | return true; |
| 3152 | |
| 3153 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
| 3156 | /// \brief Check the constraints on operands to unary expression and type |
| 3157 | /// traits. |
| 3158 | /// |
| 3159 | /// This will complete any types necessary, and validate the various constraints |
| 3160 | /// on those operands. |
| 3161 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3162 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3163 | /// C99 6.3.2.1p[2-4] all state: |
| 3164 | /// Except when it is the operand of the sizeof operator ... |
| 3165 | /// |
| 3166 | /// C++ [expr.sizeof]p4 |
| 3167 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3168 | /// standard conversions are not applied to the operand of sizeof. |
| 3169 | /// |
| 3170 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3171 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 3172 | SourceLocation OpLoc, |
| 3173 | SourceRange ExprRange, |
| 3174 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3175 | if (exprType->isDependentType()) |
| 3176 | return false; |
| 3177 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3178 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3179 | // the result is the size of the referenced type." |
| 3180 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3181 | // result shall be the alignment of the referenced type." |
| 3182 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 3183 | exprType = Ref->getPointeeType(); |
| 3184 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3185 | if (ExprKind == UETT_VecStep) |
| 3186 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3187 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3188 | // Whitelist some types as extensions |
| 3189 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 3190 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3191 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3193 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 3194 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3195 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3196 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3198 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 3199 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3200 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3202 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3203 | } |
| 3204 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3205 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3206 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3207 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3208 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3209 | if (isa<DeclRefExpr>(E)) |
| 3210 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3211 | |
| 3212 | // Cannot know anything else if the expression is dependent. |
| 3213 | if (E->isTypeDependent()) |
| 3214 | return false; |
| 3215 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3216 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3217 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3218 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3219 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3220 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3221 | |
| 3222 | // Alignment of a field access is always okay, so long as it isn't a |
| 3223 | // bit-field. |
| 3224 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3225 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3226 | return false; |
| 3227 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3228 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3229 | } |
| 3230 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3231 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3232 | E = E->IgnoreParens(); |
| 3233 | |
| 3234 | // Cannot know anything else if the expression is dependent. |
| 3235 | if (E->isTypeDependent()) |
| 3236 | return false; |
| 3237 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3238 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3241 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3242 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3243 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3244 | SourceLocation OpLoc, |
| 3245 | UnaryExprOrTypeTrait ExprKind, |
| 3246 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3247 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3248 | return ExprError(); |
| 3249 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3250 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3251 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3252 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3253 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3254 | return ExprError(); |
| 3255 | |
| 3256 | // 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] | 3257 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3258 | Context.getSizeType(), |
| 3259 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | /// \brief Build a sizeof or alignof expression given an expression |
| 3263 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3264 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3265 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3266 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3267 | // Verify that the operand is valid. |
| 3268 | bool isInvalid = false; |
| 3269 | if (E->isTypeDependent()) { |
| 3270 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3271 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3272 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3273 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3274 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3275 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3276 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3277 | isInvalid = true; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3278 | } else if (E->getType()->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3279 | ExprResult PE = CheckPlaceholderExpr(E); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3280 | if (PE.isInvalid()) return ExprError(); |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3281 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3282 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3283 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | if (isInvalid) |
| 3287 | return ExprError(); |
| 3288 | |
| 3289 | // 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] | 3290 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3291 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3292 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3295 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3296 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3297 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3298 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3299 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 3300 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 3301 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3302 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3303 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3304 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3305 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3306 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3307 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3308 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3311 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3312 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3313 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3316 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3317 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3318 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3319 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3321 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3322 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3323 | V = S.DefaultLvalueConversion(V.take()); |
| 3324 | if (V.isInvalid()) |
| 3325 | return QualType(); |
| 3326 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3327 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3328 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3329 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3330 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3332 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3333 | if (V.get()->getType()->isArithmeticType()) |
| 3334 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3336 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3337 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3338 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3339 | if (PR.get() != V.get()) { |
| 3340 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3341 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3344 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3345 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3346 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3347 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3351 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3352 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3353 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3354 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3355 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3356 | switch (Kind) { |
| 3357 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3358 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3359 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3360 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3361 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3362 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3365 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3366 | /// having l-value type. These are: |
| 3367 | /// - 'void', but not qualified void |
| 3368 | /// - function types |
| 3369 | /// |
| 3370 | /// The exact rule here is C99 6.3.2.1: |
| 3371 | /// An lvalue is an expression with an object type or an incomplete |
| 3372 | /// type other than void. |
| 3373 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3374 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3375 | T->isFunctionType()); |
| 3376 | } |
| 3377 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3378 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3379 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3380 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3381 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3382 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3383 | if (Result.isInvalid()) return ExprError(); |
| 3384 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3385 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3386 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3388 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3389 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3390 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3391 | Context.DependentTy, |
| 3392 | VK_LValue, OK_Ordinary, |
| 3393 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3397 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3398 | LHSExp->getType()->isEnumeralType() || |
| 3399 | RHSExp->getType()->isRecordType() || |
| 3400 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3401 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3404 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3408 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3409 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3410 | Expr *Idx, SourceLocation RLoc) { |
| 3411 | Expr *LHSExp = Base; |
| 3412 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3414 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3415 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3416 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3417 | if (Result.isInvalid()) |
| 3418 | return ExprError(); |
| 3419 | LHSExp = Result.take(); |
| 3420 | } |
| 3421 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3422 | if (Result.isInvalid()) |
| 3423 | return ExprError(); |
| 3424 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3425 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3426 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3427 | ExprValueKind VK = VK_LValue; |
| 3428 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3429 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3430 | // 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] | 3431 | // 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] | 3432 | // 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] | 3433 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3434 | Expr *BaseExpr, *IndexExpr; |
| 3435 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3436 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3437 | BaseExpr = LHSExp; |
| 3438 | IndexExpr = RHSExp; |
| 3439 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3440 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3441 | BaseExpr = LHSExp; |
| 3442 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3443 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3444 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3445 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3446 | BaseExpr = RHSExp; |
| 3447 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3448 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3450 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3451 | BaseExpr = LHSExp; |
| 3452 | IndexExpr = RHSExp; |
| 3453 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3454 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3455 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3456 | // Handle the uncommon case of "123[Ptr]". |
| 3457 | BaseExpr = RHSExp; |
| 3458 | IndexExpr = LHSExp; |
| 3459 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3460 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3461 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3462 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3463 | VK = LHSExp->getValueKind(); |
| 3464 | if (VK != VK_RValue) |
| 3465 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3466 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3467 | // FIXME: need to deal with const... |
| 3468 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3469 | } else if (LHSTy->isArrayType()) { |
| 3470 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3471 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3472 | // wasn't promoted because of the C90 rule that doesn't |
| 3473 | // allow promoting non-lvalue arrays. Warn, then |
| 3474 | // force the promotion here. |
| 3475 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3476 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3477 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3478 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3479 | LHSTy = LHSExp->getType(); |
| 3480 | |
| 3481 | BaseExpr = LHSExp; |
| 3482 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3483 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3484 | } else if (RHSTy->isArrayType()) { |
| 3485 | // Same as previous, except for 123[f().a] case |
| 3486 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3487 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3488 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3489 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3490 | RHSTy = RHSExp->getType(); |
| 3491 | |
| 3492 | BaseExpr = RHSExp; |
| 3493 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3494 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3495 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3496 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3497 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3498 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3499 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3500 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3501 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3502 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3503 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3504 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3505 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3506 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3507 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3508 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3509 | // 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] | 3510 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3511 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3512 | // incomplete types are not object types. |
| 3513 | if (ResultType->isFunctionType()) { |
| 3514 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3515 | << ResultType << BaseExpr->getSourceRange(); |
| 3516 | return ExprError(); |
| 3517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3518 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3519 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3520 | // GNU extension: subscripting on pointer to void |
| 3521 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3522 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3523 | |
| 3524 | // C forbids expressions of unqualified void type from being l-values. |
| 3525 | // See IsCForbiddenLValueType. |
| 3526 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3527 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3529 | PDiag(diag::err_subscript_incomplete_type) |
| 3530 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3531 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3532 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3533 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3534 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3535 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3536 | << ResultType << BaseExpr->getSourceRange(); |
| 3537 | return ExprError(); |
| 3538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3540 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3541 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3542 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3543 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3544 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3547 | /// Check an ext-vector component access expression. |
| 3548 | /// |
| 3549 | /// VK should be set in advance to the value kind of the base |
| 3550 | /// expression. |
| 3551 | static QualType |
| 3552 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3553 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3554 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3555 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3556 | // see FIXME there. |
| 3557 | // |
| 3558 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3559 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3560 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3561 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3562 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3563 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3564 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3565 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3566 | // special names that indicate a subset of exactly half the elements are |
| 3567 | // to be selected. |
| 3568 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3569 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3570 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3571 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 131f465 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 3572 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3573 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3574 | bool HasRepeated = false; |
| 3575 | bool HasIndex[16] = {}; |
| 3576 | |
| 3577 | int Idx; |
| 3578 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3579 | // Check that we've found one of the special components, or that the component |
| 3580 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3581 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3582 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3583 | HalvingSwizzle = true; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3584 | } else if (!HexSwizzle && |
| 3585 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3586 | do { |
| 3587 | if (HasIndex[Idx]) HasRepeated = true; |
| 3588 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3589 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3590 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3591 | } else { |
| 3592 | if (HexSwizzle) compStr++; |
| 3593 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3594 | if (HasIndex[Idx]) HasRepeated = true; |
| 3595 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3596 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3597 | } |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3598 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3599 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3600 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3601 | // We didn't get to the end of the string. This means the component names |
| 3602 | // didn't come from the same set *or* we encountered an illegal name. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3603 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3604 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3605 | return QualType(); |
| 3606 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3607 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3608 | // Ensure no component accessor exceeds the width of the vector type it |
| 3609 | // operates on. |
| 3610 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3611 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3612 | |
| 3613 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3614 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3615 | |
| 3616 | while (*compStr) { |
| 3617 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3618 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3619 | << baseType << SourceRange(CompLoc); |
| 3620 | return QualType(); |
| 3621 | } |
| 3622 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3623 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3624 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3625 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3626 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3627 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3628 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3629 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3630 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3631 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3632 | if (HexSwizzle) |
| 3633 | CompSize--; |
| 3634 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3635 | if (CompSize == 1) |
| 3636 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3637 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3638 | if (HasRepeated) VK = VK_RValue; |
| 3639 | |
| 3640 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3641 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3642 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3643 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3644 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3645 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3646 | } |
| 3647 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3650 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3651 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3652 | const Selector &Sel, |
| 3653 | ASTContext &Context) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3654 | if (Member) |
| 3655 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3656 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3657 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3658 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3660 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3661 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3662 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3663 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3664 | return D; |
| 3665 | } |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3669 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3670 | IdentifierInfo *Member, |
| 3671 | const Selector &Sel, |
| 3672 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3673 | // Check protocols on qualified interfaces. |
| 3674 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3675 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3676 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3677 | if (Member) |
| 3678 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3679 | GDecl = PD; |
| 3680 | break; |
| 3681 | } |
| 3682 | // Also must look for a getter or setter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3683 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3684 | GDecl = OMD; |
| 3685 | break; |
| 3686 | } |
| 3687 | } |
| 3688 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3689 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3690 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3691 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3692 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3693 | Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3694 | if (GDecl) |
| 3695 | return GDecl; |
| 3696 | } |
| 3697 | } |
| 3698 | return GDecl; |
| 3699 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3700 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3701 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3702 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3703 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3704 | const CXXScopeSpec &SS, |
| 3705 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3706 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3707 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3708 | // Even in dependent contexts, try to diagnose base expressions with |
| 3709 | // obviously wrong types, e.g.: |
| 3710 | // |
| 3711 | // T* t; |
| 3712 | // t.f; |
| 3713 | // |
| 3714 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3715 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3716 | // allows this, while still reporting an error if T is a struct pointer. |
| 3717 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3718 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3719 | if (PT && (!getLangOptions().ObjC1 || |
| 3720 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3721 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3722 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3723 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3724 | return ExprError(); |
| 3725 | } |
| 3726 | } |
| 3727 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3728 | assert(BaseType->isDependentType() || |
| 3729 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3730 | isDependentScopeSpecifier(SS)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3731 | |
| 3732 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3733 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3734 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3735 | IsArrow, OpLoc, |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3736 | SS.getWithLocInContext(Context), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3737 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3738 | NameInfo, TemplateArgs)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | /// We know that the given qualified member reference points only to |
| 3742 | /// declarations which do not belong to the static type of the base |
| 3743 | /// expression. Diagnose the problem. |
| 3744 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3745 | Expr *BaseExpr, |
| 3746 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3747 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3748 | NamedDecl *rep, |
| 3749 | const DeclarationNameInfo &nameInfo) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3750 | // If this is an implicit member access, use a different set of |
| 3751 | // diagnostics. |
| 3752 | if (!BaseExpr) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3753 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3754 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3755 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3756 | << SS.getRange() << rep << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | // Check whether the declarations we found through a nested-name |
| 3760 | // specifier in a member expression are actually members of the base |
| 3761 | // type. The restriction here is: |
| 3762 | // |
| 3763 | // C++ [expr.ref]p2: |
| 3764 | // ... In these cases, the id-expression shall name a |
| 3765 | // member of the class or of one of its base classes. |
| 3766 | // |
| 3767 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3768 | // an unrelated class, and for us to find an overload set including |
| 3769 | // decls from classes which are not superclasses, as long as the decl |
| 3770 | // we actually pick through overload resolution is from a superclass. |
| 3771 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3772 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3773 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3774 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3775 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3776 | if (!BaseRT) { |
| 3777 | // We can't check this yet because the base type is still |
| 3778 | // dependent. |
| 3779 | assert(BaseType->isDependentType()); |
| 3780 | return false; |
| 3781 | } |
| 3782 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3783 | |
| 3784 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3785 | // If this is an implicit member reference and we find a |
| 3786 | // non-instance member, it's not an error. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3787 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3788 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3789 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3790 | // Note that we use the DC of the decl, not the underlying decl. |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3791 | DeclContext *DC = (*I)->getDeclContext(); |
| 3792 | while (DC->isTransparentContext()) |
| 3793 | DC = DC->getParent(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | 9d4bb94 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3795 | if (!DC->isRecord()) |
| 3796 | continue; |
| 3797 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3798 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3799 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3800 | |
| 3801 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3802 | return false; |
| 3803 | } |
| 3804 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3805 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3806 | R.getRepresentativeDecl(), |
| 3807 | R.getLookupNameInfo()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3808 | return true; |
| 3809 | } |
| 3810 | |
| 3811 | static bool |
| 3812 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3813 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3814 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3815 | bool HasTemplateArgs) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3816 | RecordDecl *RDecl = RTy->getDecl(); |
| 3817 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3818 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3819 | << BaseRange)) |
| 3820 | return true; |
| 3821 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3822 | if (HasTemplateArgs) { |
| 3823 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3824 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3825 | |
| 3826 | bool MOUS; |
| 3827 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3828 | return false; |
| 3829 | } |
| 3830 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3831 | DeclContext *DC = RDecl; |
| 3832 | if (SS.isSet()) { |
| 3833 | // If the member name was a qualified-id, look into the |
| 3834 | // nested-name-specifier. |
| 3835 | DC = SemaRef.computeDeclContext(SS, false); |
| 3836 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3837 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3838 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3839 | << SS.getRange() << DC; |
| 3840 | return true; |
| 3841 | } |
| 3842 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3843 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3844 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3845 | if (!isa<TypeDecl>(DC)) { |
| 3846 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3847 | << DC << SS.getRange(); |
| 3848 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3852 | // The record definition is complete, now look up the member. |
| 3853 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3855 | if (!R.empty()) |
| 3856 | return false; |
| 3857 | |
| 3858 | // We didn't find anything with the given name, so try to correct |
| 3859 | // for typos. |
| 3860 | DeclarationName Name = R.getLookupName(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3861 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3862 | !R.empty() && |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3863 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3864 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3865 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3866 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3867 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3868 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3869 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3870 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3871 | return false; |
| 3872 | } else { |
| 3873 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3874 | R.setLookupName(Name); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3877 | return false; |
| 3878 | } |
| 3879 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3880 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3881 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3882 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3883 | CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3884 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3885 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3886 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3887 | if (BaseType->isDependentType() || |
| 3888 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3889 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3890 | IsArrow, OpLoc, |
| 3891 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3892 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3893 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3894 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3895 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3896 | // Implicit member accesses. |
| 3897 | if (!Base) { |
| 3898 | QualType RecordTy = BaseType; |
| 3899 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3900 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3901 | RecordTy->getAs<RecordType>(), |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3902 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3903 | return ExprError(); |
| 3904 | |
| 3905 | // Explicit member accesses. |
| 3906 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3907 | ExprResult BaseResult = Owned(Base); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3908 | ExprResult Result = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3909 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3910 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3911 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3912 | if (BaseResult.isInvalid()) |
| 3913 | return ExprError(); |
| 3914 | Base = BaseResult.take(); |
| 3915 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3916 | if (Result.isInvalid()) { |
| 3917 | Owned(Base); |
| 3918 | return ExprError(); |
| 3919 | } |
| 3920 | |
| 3921 | if (Result.get()) |
| 3922 | return move(Result); |
Sebastian Redl | f3e6337 | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3923 | |
| 3924 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3925 | BaseType = Base->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3928 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3929 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3930 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3933 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3934 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3935 | SourceLocation OpLoc, bool IsArrow, |
| 3936 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3937 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3938 | LookupResult &R, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3939 | const TemplateArgumentListInfo *TemplateArgs, |
| 3940 | bool SuppressQualifierCheck) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3941 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3942 | if (IsArrow) { |
| 3943 | assert(BaseType->isPointerType()); |
| 3944 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3945 | } |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3946 | R.setBaseObjectType(BaseType); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3947 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3948 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3949 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3950 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3951 | |
| 3952 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3953 | return ExprError(); |
| 3954 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3955 | if (R.empty()) { |
| 3956 | // Rederive where we looked up. |
| 3957 | DeclContext *DC = (SS.isSet() |
| 3958 | ? computeDeclContext(SS, false) |
| 3959 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3960 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3961 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3962 | << MemberName << DC |
| 3963 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3964 | return ExprError(); |
| 3965 | } |
| 3966 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3967 | // Diagnose lookups that find only declarations from a non-base |
| 3968 | // type. This is possible for either qualified lookups (which may |
| 3969 | // have been qualified with an unrelated type) or implicit member |
| 3970 | // expressions (which were found with unqualified lookup and thus |
| 3971 | // may have come from an enclosing scope). Note that it's okay for |
| 3972 | // lookup to find declarations from a non-base type as long as those |
| 3973 | // aren't the ones picked by overload resolution. |
| 3974 | if ((SS.isSet() || !BaseExpr || |
| 3975 | (isa<CXXThisExpr>(BaseExpr) && |
| 3976 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3977 | !SuppressQualifierCheck && |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3978 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3979 | return ExprError(); |
| 3980 | |
| 3981 | // Construct an unresolved result if we in fact got an unresolved |
| 3982 | // result. |
| 3983 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3984 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3985 | // pick a member. |
| 3986 | R.suppressDiagnostics(); |
| 3987 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3988 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3989 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3990 | BaseExpr, BaseExprType, |
| 3991 | IsArrow, OpLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3992 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3993 | MemberNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3994 | TemplateArgs, R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3995 | |
| 3996 | return Owned(MemExpr); |
| 3997 | } |
| 3998 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3999 | assert(R.isSingleResult()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4000 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4001 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 4002 | |
| 4003 | // FIXME: diagnose the presence of template arguments now. |
| 4004 | |
| 4005 | // If the decl being referenced had an error, return an error for this |
| 4006 | // sub-expr without emitting another error, in order to avoid cascading |
| 4007 | // error cases. |
| 4008 | if (MemberDecl->isInvalidDecl()) |
| 4009 | return ExprError(); |
| 4010 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4011 | // Handle the implicit-member-access case. |
| 4012 | if (!BaseExpr) { |
| 4013 | // If this is not an instance member, convert to a non-member access. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4014 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4015 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4016 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 4017 | SourceLocation Loc = R.getNameLoc(); |
| 4018 | if (SS.getRange().isValid()) |
| 4019 | Loc = SS.getRange().getBegin(); |
| 4020 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4023 | bool ShouldCheckUse = true; |
| 4024 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 4025 | // Don't diagnose the use of a virtual member function unless it's |
| 4026 | // explicitly qualified. |
| 4027 | if (MD->isVirtual() && !SS.isSet()) |
| 4028 | ShouldCheckUse = false; |
| 4029 | } |
| 4030 | |
| 4031 | // Check the use of this member. |
| 4032 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 4033 | Owned(BaseExpr); |
| 4034 | return ExprError(); |
| 4035 | } |
| 4036 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4037 | // Perform a property load on the base regardless of whether we |
| 4038 | // actually need it for the declaration. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4039 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 4040 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 4041 | if (Result.isInvalid()) |
| 4042 | return ExprError(); |
| 4043 | BaseExpr = Result.take(); |
| 4044 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4045 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4046 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 4047 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 4048 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4049 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4050 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 4051 | // We may have found a field within an anonymous union or struct |
| 4052 | // (C++ [class.union]). |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 4053 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4054 | BaseExpr, OpLoc); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4055 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4056 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 4057 | MarkDeclarationReferenced(MemberLoc, Var); |
| 4058 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4059 | Var, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4060 | Var->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4061 | VK_LValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4064 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4065 | ExprValueKind valueKind; |
| 4066 | QualType type; |
| 4067 | if (MemberFn->isInstance()) { |
| 4068 | valueKind = VK_RValue; |
| 4069 | type = Context.BoundMemberTy; |
| 4070 | } else { |
| 4071 | valueKind = VK_LValue; |
| 4072 | type = MemberFn->getType(); |
| 4073 | } |
| 4074 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4075 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4076 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4077 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4078 | type, valueKind, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4079 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4080 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 4083 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4084 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4085 | Enum, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4086 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | Owned(BaseExpr); |
| 4090 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4091 | // We found something that we didn't expect. Complain. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4092 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4093 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4094 | << MemberName << BaseType << int(IsArrow); |
| 4095 | else |
| 4096 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 4097 | << MemberName << BaseType << int(IsArrow); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4098 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4099 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 4100 | << MemberName; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4101 | R.suppressDiagnostics(); |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4102 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4105 | /// Given that normal member access failed on the given expression, |
| 4106 | /// and given that the expression's type involves builtin-id or |
| 4107 | /// builtin-Class, decide whether substituting in the redefinition |
| 4108 | /// types would be profitable. The redefinition type is whatever |
| 4109 | /// this translation unit tried to typedef to id/Class; we store |
| 4110 | /// it to the side and then re-use it in places like this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4111 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4112 | const ObjCObjectPointerType *opty |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4113 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4114 | if (!opty) return false; |
| 4115 | |
| 4116 | const ObjCObjectType *ty = opty->getObjectType(); |
| 4117 | |
| 4118 | QualType redef; |
| 4119 | if (ty->isObjCId()) { |
| 4120 | redef = S.Context.ObjCIdRedefinitionType; |
| 4121 | } else if (ty->isObjCClass()) { |
| 4122 | redef = S.Context.ObjCClassRedefinitionType; |
| 4123 | } else { |
| 4124 | return false; |
| 4125 | } |
| 4126 | |
| 4127 | // Do the substitution as long as the redefinition type isn't just a |
| 4128 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 4129 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 4130 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 4131 | return false; |
| 4132 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4133 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4134 | return true; |
| 4135 | } |
| 4136 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4137 | /// Look up the given member of the given non-type-dependent |
| 4138 | /// expression. This can return in one of two ways: |
| 4139 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 4140 | /// assume that lookup was performed and the results written into |
| 4141 | /// the provided structure. It will take over from there. |
| 4142 | /// * Otherwise, the returned expression will be produced in place of |
| 4143 | /// an ordinary member expression. |
| 4144 | /// |
| 4145 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 4146 | /// fixed for ObjC++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4147 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4148 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 4149 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4150 | CXXScopeSpec &SS, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4151 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4152 | assert(BaseExpr.get() && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 4154 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4155 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4156 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4157 | if (IsArrow) { |
| 4158 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4159 | if (BaseExpr.isInvalid()) |
| 4160 | return ExprError(); |
| 4161 | } |
| 4162 | |
| 4163 | QualType BaseType = BaseExpr.get()->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4164 | assert(!BaseType->isDependentType()); |
| 4165 | |
| 4166 | DeclarationName MemberName = R.getLookupName(); |
| 4167 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4168 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4169 | // For later type-checking purposes, turn arrow accesses into dot |
| 4170 | // accesses. The only access type we support that doesn't follow |
| 4171 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 4172 | // and those never use arrows, so this is unaffected. |
| 4173 | if (IsArrow) { |
| 4174 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4175 | BaseType = Ptr->getPointeeType(); |
| 4176 | else if (const ObjCObjectPointerType *Ptr |
| 4177 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 4178 | BaseType = Ptr->getPointeeType(); |
| 4179 | else if (BaseType->isRecordType()) { |
| 4180 | // Recover from arrow accesses to records, e.g.: |
| 4181 | // struct MyRecord foo; |
| 4182 | // foo->bar |
| 4183 | // This is actually well-formed in C++ if MyRecord has an |
| 4184 | // overloaded operator->, but that should have been dealt with |
| 4185 | // by now. |
| 4186 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4187 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4188 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4189 | IsArrow = false; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4190 | } else if (BaseType == Context.BoundMemberTy) { |
| 4191 | goto fail; |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4192 | } else { |
| 4193 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4194 | << BaseType << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4195 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4199 | // Handle field access to simple records. |
| 4200 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4201 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4202 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 4203 | return ExprError(); |
| 4204 | |
| 4205 | // Returning valid-but-null is how we indicate to the caller that |
| 4206 | // the lookup result was filled in. |
| 4207 | return Owned((Expr*) 0); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4208 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4209 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4210 | // Handle ivar access to Objective-C objects. |
| 4211 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4212 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4213 | |
| 4214 | // There are three cases for the base type: |
| 4215 | // - builtin id (qualified or unqualified) |
| 4216 | // - builtin Class (qualified or unqualified) |
| 4217 | // - an interface |
| 4218 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 4219 | if (!IDecl) { |
| 4220 | // There's an implicit 'isa' ivar on all objects. |
| 4221 | // But we only actually find it this way on objects of type 'id', |
| 4222 | // apparently. |
| 4223 | if (OTy->isObjCId() && Member->isStr("isa")) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4224 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4225 | Context.getObjCClassType())); |
| 4226 | |
| 4227 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4228 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4229 | ObjCImpDecl, HasTemplateArgs); |
| 4230 | goto fail; |
| 4231 | } |
| 4232 | |
| 4233 | ObjCInterfaceDecl *ClassDeclared; |
| 4234 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 4235 | |
| 4236 | if (!IV) { |
| 4237 | // Attempt to correct for typos in ivar names. |
| 4238 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 4239 | LookupMemberName); |
| 4240 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 4241 | IsArrow ? CTC_ObjCIvarLookup |
| 4242 | : CTC_ObjCPropertyLookup) && |
| 4243 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 4244 | Diag(R.getNameLoc(), |
| 4245 | diag::err_typecheck_member_reference_ivar_suggest) |
| 4246 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 4247 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 4248 | IV->getNameAsString()); |
| 4249 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 4250 | << IV->getDeclName(); |
| 4251 | } else { |
| 4252 | Res.clear(); |
| 4253 | Res.setLookupName(Member); |
| 4254 | |
| 4255 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 4256 | << IDecl->getDeclName() << MemberName |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4257 | << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4258 | return ExprError(); |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | // If the decl being referenced had an error, return an error for this |
| 4263 | // sub-expr without emitting another error, in order to avoid cascading |
| 4264 | // error cases. |
| 4265 | if (IV->isInvalidDecl()) |
| 4266 | return ExprError(); |
| 4267 | |
| 4268 | // Check whether we can reference this field. |
| 4269 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 4270 | return ExprError(); |
| 4271 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 4272 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 4273 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 4274 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 4275 | ClassOfMethodDecl = MD->getClassInterface(); |
| 4276 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 4277 | // Case of a c-function declared inside an objc implementation. |
| 4278 | // FIXME: For a c-style function nested inside an objc implementation |
| 4279 | // class, there is no implementation context available, so we pass |
| 4280 | // down the context as argument to this routine. Ideally, this context |
| 4281 | // need be passed down in the AST node and somehow calculated from the |
| 4282 | // AST for a function decl. |
| 4283 | if (ObjCImplementationDecl *IMPD = |
| 4284 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 4285 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 4286 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 4287 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 4288 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 4289 | } |
| 4290 | |
| 4291 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 4292 | if (ClassDeclared != IDecl || |
| 4293 | ClassOfMethodDecl != ClassDeclared) |
| 4294 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 4295 | << IV->getDeclName(); |
| 4296 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 4297 | // @protected |
| 4298 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 4299 | << IV->getDeclName(); |
| 4300 | } |
| 4301 | |
| 4302 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4303 | MemberLoc, BaseExpr.take(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4304 | IsArrow)); |
| 4305 | } |
| 4306 | |
| 4307 | // Objective-C property access. |
| 4308 | const ObjCObjectPointerType *OPT; |
| 4309 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 4310 | // This actually uses the base as an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4311 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4312 | if (BaseExpr.isInvalid()) |
| 4313 | return ExprError(); |
| 4314 | |
| 4315 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4316 | |
| 4317 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 4318 | |
| 4319 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 4320 | |
| 4321 | // id, with and without qualifiers. |
| 4322 | if (OT->isObjCId()) { |
| 4323 | // Check protocols on qualified interfaces. |
| 4324 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 4325 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 4326 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 4327 | // Check the use of this declaration |
| 4328 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 4329 | return ExprError(); |
| 4330 | |
| 4331 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 4332 | VK_LValue, |
| 4333 | OK_ObjCProperty, |
| 4334 | MemberLoc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4335 | BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4336 | } |
| 4337 | |
| 4338 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4339 | // Check the use of this method. |
| 4340 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4341 | return ExprError(); |
| 4342 | Selector SetterSel = |
| 4343 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4344 | PP.getSelectorTable(), Member); |
| 4345 | ObjCMethodDecl *SMD = 0; |
| 4346 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4347 | SetterSel, Context)) |
| 4348 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 4349 | QualType PType = OMD->getSendResultType(); |
| 4350 | |
| 4351 | ExprValueKind VK = VK_LValue; |
| 4352 | if (!getLangOptions().CPlusPlus && |
| 4353 | IsCForbiddenLValueType(Context, PType)) |
| 4354 | VK = VK_RValue; |
| 4355 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4356 | |
| 4357 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4358 | VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4359 | MemberLoc, BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
Fariborz Jahanian | 4eb7f69 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4362 | // Use of id.member can only be for a property reference. Do not |
| 4363 | // use the 'id' redefinition in this case. |
| 4364 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4365 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4366 | ObjCImpDecl, HasTemplateArgs); |
| 4367 | |
| 4368 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4369 | << MemberName << BaseType); |
| 4370 | } |
| 4371 | |
| 4372 | // 'Class', unqualified only. |
| 4373 | if (OT->isObjCClass()) { |
| 4374 | // Only works in a method declaration (??!). |
| 4375 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4376 | if (!MD) { |
| 4377 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4378 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4379 | ObjCImpDecl, HasTemplateArgs); |
| 4380 | |
| 4381 | goto fail; |
| 4382 | } |
| 4383 | |
| 4384 | // Also must look for a getter name which uses property syntax. |
| 4385 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4386 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4387 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4388 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4389 | // Check the use of this method. |
| 4390 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4391 | return ExprError(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4392 | } else |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4393 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4394 | // If we found a getter then this may be a valid dot-reference, we |
| 4395 | // will look for the matching setter, in case it is needed. |
| 4396 | Selector SetterSel = |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4397 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4398 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4399 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4400 | if (!Setter) { |
| 4401 | // If this reference is in an @implementation, also check for 'private' |
| 4402 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4403 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4404 | } |
| 4405 | // Look through local category implementations associated with the class. |
| 4406 | if (!Setter) |
| 4407 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4408 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4409 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4410 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4411 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4412 | if (Getter || Setter) { |
| 4413 | QualType PType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4414 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4415 | ExprValueKind VK = VK_LValue; |
| 4416 | if (Getter) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4417 | PType = Getter->getSendResultType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4418 | if (!getLangOptions().CPlusPlus && |
| 4419 | IsCForbiddenLValueType(Context, PType)) |
| 4420 | VK = VK_RValue; |
| 4421 | } else { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4422 | // Get the expression type from Setter's incoming parameter. |
| 4423 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4424 | } |
| 4425 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4426 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4427 | // FIXME: we must check that the setter has property type. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4428 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4429 | PType, VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4430 | MemberLoc, BaseExpr.take())); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4431 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4432 | |
| 4433 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4434 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4435 | ObjCImpDecl, HasTemplateArgs); |
| 4436 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4437 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4438 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4439 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4440 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4441 | // Normal property access. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4442 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4443 | SourceLocation(), QualType(), false); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4444 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4445 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4446 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4447 | if (BaseType->isExtVectorType()) { |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4448 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4449 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4450 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4451 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4452 | Member, MemberLoc); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4453 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4454 | return ExprError(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4455 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4456 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4457 | *Member, MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4458 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4459 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4460 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4461 | // not just a pointer to builtin-sel again. |
| 4462 | if (IsArrow && |
| 4463 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4464 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4465 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4466 | CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4467 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4468 | ObjCImpDecl, HasTemplateArgs); |
| 4469 | } |
| 4470 | |
| 4471 | // Failure cases. |
| 4472 | fail: |
| 4473 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4474 | // Recover from dot accesses to pointers, e.g.: |
| 4475 | // type *foo; |
| 4476 | // foo.bar |
| 4477 | // This is actually well-formed in two cases: |
| 4478 | // - 'type' is an Objective C type |
| 4479 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4480 | // the appropriate pointer type |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4481 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4482 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4483 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4484 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4485 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4486 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4487 | |
| 4488 | // Recurse as an -> access. |
| 4489 | IsArrow = true; |
| 4490 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4491 | ObjCImpDecl, HasTemplateArgs); |
| 4492 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4495 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4496 | // because they forgot parentheses to call that function. |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4497 | QualType ZeroArgCallTy; |
| 4498 | UnresolvedSet<4> Overloads; |
| 4499 | if (isExprCallable(*BaseExpr.get(), ZeroArgCallTy, Overloads)) { |
| 4500 | if (ZeroArgCallTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4501 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4502 | << (Overloads.size() > 1) << 0 << BaseExpr.get()->getSourceRange(); |
| 4503 | UnresolvedSet<2> PlausibleOverloads; |
| 4504 | for (OverloadExpr::decls_iterator It = Overloads.begin(), |
| 4505 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 4506 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); |
| 4507 | QualType OverloadResultTy = OverloadDecl->getResultType(); |
| 4508 | if ((!IsArrow && OverloadResultTy->isRecordType()) || |
| 4509 | (IsArrow && OverloadResultTy->isPointerType() && |
| 4510 | OverloadResultTy->getPointeeType()->isRecordType())) |
| 4511 | PlausibleOverloads.addDecl(It.getDecl()); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4512 | } |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4513 | NoteOverloads(PlausibleOverloads, BaseExpr.get()->getExprLoc()); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4514 | return ExprError(); |
| 4515 | } |
Matt Beaumont-Gay | c9366ba | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4516 | if ((!IsArrow && ZeroArgCallTy->isRecordType()) || |
| 4517 | (IsArrow && ZeroArgCallTy->isPointerType() && |
| 4518 | ZeroArgCallTy->getPointeeType()->isRecordType())) { |
| 4519 | // At this point, we know BaseExpr looks like it's potentially callable |
| 4520 | // with 0 arguments, and that it returns something of a reasonable type, |
| 4521 | // so we can emit a fixit and carry on pretending that BaseExpr was |
| 4522 | // actually a CallExpr. |
| 4523 | SourceLocation ParenInsertionLoc = |
| 4524 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4525 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
| 4526 | << (Overloads.size() > 1) << 1 << BaseExpr.get()->getSourceRange() |
| 4527 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4528 | // FIXME: Try this before emitting the fixit, and suppress diagnostics |
| 4529 | // while doing so. |
| 4530 | ExprResult NewBase = |
| 4531 | ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
| 4532 | MultiExprArg(*this, 0, 0), |
| 4533 | ParenInsertionLoc.getFileLocWithOffset(1)); |
| 4534 | if (NewBase.isInvalid()) |
| 4535 | return ExprError(); |
| 4536 | BaseExpr = NewBase; |
| 4537 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| 4538 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4539 | ObjCImpDecl, HasTemplateArgs); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4540 | } |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4543 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4544 | << BaseType << BaseExpr.get()->getSourceRange(); |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4546 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4547 | } |
| 4548 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4549 | /// The main callback when the parser finds something like |
| 4550 | /// expression . [nested-name-specifier] identifier |
| 4551 | /// expression -> [nested-name-specifier] identifier |
| 4552 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4553 | /// possibilities, including destructor and operator references. |
| 4554 | /// |
| 4555 | /// \param OpKind either tok::arrow or tok::period |
| 4556 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4557 | /// is used to diagnose mis-uses of special members that can |
| 4558 | /// only be called |
| 4559 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4560 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4561 | /// aren't properly put in the context chain |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4562 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4563 | SourceLocation OpLoc, |
| 4564 | tok::TokenKind OpKind, |
| 4565 | CXXScopeSpec &SS, |
| 4566 | UnqualifiedId &Id, |
| 4567 | Decl *ObjCImpDecl, |
| 4568 | bool HasTrailingLParen) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4569 | if (SS.isSet() && SS.isInvalid()) |
| 4570 | return ExprError(); |
| 4571 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4572 | // Warn about the explicit constructor calls Microsoft extension. |
| 4573 | if (getLangOptions().Microsoft && |
| 4574 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4575 | Diag(Id.getSourceRange().getBegin(), |
| 4576 | diag::ext_ms_explicit_constructor_call); |
| 4577 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4578 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4579 | |
| 4580 | // Decompose the name into its component parts. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4581 | DeclarationNameInfo NameInfo; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4582 | const TemplateArgumentListInfo *TemplateArgs; |
| 4583 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4584 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4585 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4586 | DeclarationName Name = NameInfo.getName(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4587 | bool IsArrow = (OpKind == tok::arrow); |
| 4588 | |
| 4589 | NamedDecl *FirstQualifierInScope |
| 4590 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4591 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4592 | |
| 4593 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4594 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4595 | if (Result.isInvalid()) return ExprError(); |
| 4596 | Base = Result.take(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4597 | |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4598 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4599 | isDependentScopeSpecifier(SS)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4600 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4601 | IsArrow, OpLoc, |
| 4602 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4603 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4604 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4605 | LookupResult R(*this, NameInfo, LookupMemberName); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4606 | ExprResult BaseResult = Owned(Base); |
| 4607 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4608 | SS, ObjCImpDecl, TemplateArgs != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4609 | if (BaseResult.isInvalid()) |
| 4610 | return ExprError(); |
| 4611 | Base = BaseResult.take(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4612 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4613 | if (Result.isInvalid()) { |
| 4614 | Owned(Base); |
| 4615 | return ExprError(); |
| 4616 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4617 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4618 | if (Result.get()) { |
| 4619 | // The only way a reference to a destructor can be used is to |
| 4620 | // immediately call it, which falls into this case. If the |
| 4621 | // next token is not a '(', produce a diagnostic and build the |
| 4622 | // call now. |
| 4623 | if (!HasTrailingLParen && |
| 4624 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4625 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4626 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4627 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4630 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4631 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4632 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4638 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4639 | FunctionDecl *FD, |
| 4640 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4641 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4642 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4643 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4644 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4646 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4647 | return ExprError(); |
| 4648 | } |
| 4649 | |
| 4650 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4651 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4653 | // Instantiate the expression. |
| 4654 | MultiLevelTemplateArgumentList ArgList |
| 4655 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4656 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4657 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4658 | = ArgList.getInnermost(); |
| 4659 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4660 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4661 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4662 | ExprResult Result; |
| 4663 | { |
| 4664 | // C++ [dcl.fct.default]p5: |
| 4665 | // The names in the [default argument] expression are bound, and |
| 4666 | // the semantic constraints are checked, at the point where the |
| 4667 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4668 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4669 | Result = SubstExpr(UninstExpr, ArgList); |
| 4670 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4671 | if (Result.isInvalid()) |
| 4672 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4673 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4674 | // Check the expression as an initializer for the parameter. |
| 4675 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4676 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4677 | InitializationKind Kind |
| 4678 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4679 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4680 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4682 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4683 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4684 | MultiExprArg(*this, &ResultE, 1)); |
| 4685 | if (Result.isInvalid()) |
| 4686 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4687 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4688 | // Build the default argument expression. |
| 4689 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4690 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4693 | // If the default expression creates temporaries, we need to |
| 4694 | // push them to the current stack of expression temporaries so they'll |
| 4695 | // be properly destroyed. |
| 4696 | // FIXME: We should really be rebuilding the default argument with new |
| 4697 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4698 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4699 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4700 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4701 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4702 | ExprTemporaries.push_back(Temporary); |
| 4703 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4704 | |
| 4705 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4706 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4707 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4708 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4709 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4712 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4713 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4714 | /// function prototype Proto. Call is the call expression itself, and |
| 4715 | /// Fn is the function expression. For a C++ member function, this |
| 4716 | /// routine does not attempt to convert the object argument. Returns |
| 4717 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4718 | bool |
| 4719 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4720 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4721 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4722 | Expr **Args, unsigned NumArgs, |
| 4723 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4724 | // Bail out early if calling a builtin with custom typechecking. |
| 4725 | // We don't need to do this in the |
| 4726 | if (FDecl) |
| 4727 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4728 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4729 | return false; |
| 4730 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4731 | // 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] | 4732 | // assignment, to the types of the corresponding parameter, ... |
| 4733 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4734 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4736 | // If too few arguments are available (and we don't have default |
| 4737 | // arguments for the remaining parameters), don't make the call. |
| 4738 | if (NumArgs < NumArgsInProto) { |
| 4739 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4740 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4741 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4742 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4743 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4744 | } |
| 4745 | |
| 4746 | // If too many are passed and not variadic, error on the extras and drop |
| 4747 | // them. |
| 4748 | if (NumArgs > NumArgsInProto) { |
| 4749 | if (!Proto->isVariadic()) { |
| 4750 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4751 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4752 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4753 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4754 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4755 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4756 | |
| 4757 | // Emit the location of the prototype. |
| 4758 | if (FDecl && !FDecl->getBuiltinID()) |
| 4759 | Diag(FDecl->getLocStart(), |
| 4760 | diag::note_typecheck_call_too_many_args) |
| 4761 | << FDecl; |
| 4762 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4763 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4764 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4765 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4766 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4767 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4768 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4769 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4770 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4771 | if (Fn->getType()->isBlockPointerType()) |
| 4772 | CallType = VariadicBlock; // Block |
| 4773 | else if (isa<MemberExpr>(Fn)) |
| 4774 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4775 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4776 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4777 | if (Invalid) |
| 4778 | return true; |
| 4779 | unsigned TotalNumArgs = AllArgs.size(); |
| 4780 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4781 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4782 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4783 | return false; |
| 4784 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4785 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4786 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4787 | FunctionDecl *FDecl, |
| 4788 | const FunctionProtoType *Proto, |
| 4789 | unsigned FirstProtoArg, |
| 4790 | Expr **Args, unsigned NumArgs, |
| 4791 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4792 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4793 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4794 | unsigned NumArgsToCheck = NumArgs; |
| 4795 | bool Invalid = false; |
| 4796 | if (NumArgs != NumArgsInProto) |
| 4797 | // Use default arguments for missing arguments |
| 4798 | NumArgsToCheck = NumArgsInProto; |
| 4799 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4800 | // 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] | 4801 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4802 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4804 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4805 | if (ArgIx < NumArgs) { |
| 4806 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4807 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4808 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4809 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4810 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4811 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4812 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4814 | // Pass the argument |
| 4815 | ParmVarDecl *Param = 0; |
| 4816 | if (FDecl && i < FDecl->getNumParams()) |
| 4817 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4818 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4819 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4820 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4821 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4822 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4823 | SourceLocation(), |
| 4824 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4825 | if (ArgE.isInvalid()) |
| 4826 | return true; |
| 4827 | |
| 4828 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4829 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4830 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4831 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4832 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4833 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4834 | if (ArgExpr.isInvalid()) |
| 4835 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4836 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4837 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4838 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4839 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4840 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4841 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4842 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4843 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4844 | |
| 4845 | // Assume that extern "C" functions with variadic arguments that |
| 4846 | // return __unknown_anytype aren't *really* variadic. |
| 4847 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4848 | FDecl && FDecl->isExternC()) { |
| 4849 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4850 | ExprResult arg; |
| 4851 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4852 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4853 | else |
| 4854 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4855 | Invalid |= arg.isInvalid(); |
| 4856 | AllArgs.push_back(arg.take()); |
| 4857 | } |
| 4858 | |
| 4859 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4860 | } else { |
| 4861 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4862 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4863 | Invalid |= Arg.isInvalid(); |
| 4864 | AllArgs.push_back(Arg.take()); |
| 4865 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4866 | } |
| 4867 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4868 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4871 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4872 | /// to have a function type. |
| 4873 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4874 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4875 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4876 | /// This provides the location of the left/right parens and a list of comma |
| 4877 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4878 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4879 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4880 | MultiExprArg args, SourceLocation RParenLoc, |
| 4881 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4882 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4883 | |
| 4884 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4885 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4886 | if (Result.isInvalid()) return ExprError(); |
| 4887 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4889 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4891 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4892 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4893 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4894 | if (NumArgs > 0) { |
| 4895 | // Pseudo-destructor calls should not have any arguments. |
| 4896 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4897 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4898 | SourceRange(Args[0]->getLocStart(), |
| 4899 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4900 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4901 | NumArgs = 0; |
| 4902 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4904 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4905 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4906 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4907 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4908 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4909 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4910 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4911 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4912 | bool Dependent = false; |
| 4913 | if (Fn->isTypeDependent()) |
| 4914 | Dependent = true; |
| 4915 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4916 | Dependent = true; |
| 4917 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4918 | if (Dependent) { |
| 4919 | if (ExecConfig) { |
| 4920 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4921 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4922 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4923 | } else { |
| 4924 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4925 | Context.DependentTy, VK_RValue, |
| 4926 | RParenLoc)); |
| 4927 | } |
| 4928 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4929 | |
| 4930 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4931 | if (Fn->getType()->isRecordType()) |
| 4932 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4933 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4934 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4935 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4936 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4937 | if (result.isInvalid()) return ExprError(); |
| 4938 | Fn = result.take(); |
| 4939 | } |
| 4940 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4941 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4942 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4943 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4944 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4945 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4946 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4947 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 4948 | if (Fn->getType() == Context.OverloadTy) { |
| 4949 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 4950 | |
| 4951 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 4952 | if (!find.IsAddressOfOperand) { |
| 4953 | OverloadExpr *ovl = find.Expression; |
| 4954 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 4955 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 4956 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 4957 | RParenLoc, ExecConfig); |
| 4958 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4959 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4960 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4961 | } |
| 4962 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4965 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4966 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4967 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4968 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4969 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4970 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4971 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4972 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4973 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4974 | if (isa<DeclRefExpr>(NakedFn)) |
| 4975 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4976 | else if (isa<MemberExpr>(NakedFn)) |
| 4977 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4978 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4979 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 4980 | ExecConfig); |
| 4981 | } |
| 4982 | |
| 4983 | ExprResult |
| 4984 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 4985 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 4986 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4987 | if (!ConfigDecl) |
| 4988 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4989 | << "cudaConfigureCall"); |
| 4990 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4991 | |
| 4992 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 4993 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 4994 | |
| 4995 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4996 | } |
| 4997 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4998 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4999 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5000 | /// unary-convert to an expression of function-pointer or |
| 5001 | /// block-pointer type. |
| 5002 | /// |
| 5003 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5004 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5005 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 5006 | SourceLocation LParenLoc, |
| 5007 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5008 | SourceLocation RParenLoc, |
| 5009 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5010 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 5011 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5012 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5013 | ExprResult Result = UsualUnaryConversions(Fn); |
| 5014 | if (Result.isInvalid()) |
| 5015 | return ExprError(); |
| 5016 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5017 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5018 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 5019 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5020 | CallExpr *TheCall; |
| 5021 | if (Config) { |
| 5022 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 5023 | cast<CallExpr>(Config), |
| 5024 | Args, NumArgs, |
| 5025 | Context.BoolTy, |
| 5026 | VK_RValue, |
| 5027 | RParenLoc); |
| 5028 | } else { |
| 5029 | TheCall = new (Context) CallExpr(Context, Fn, |
| 5030 | Args, NumArgs, |
| 5031 | Context.BoolTy, |
| 5032 | VK_RValue, |
| 5033 | RParenLoc); |
| 5034 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5035 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5036 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 5037 | |
| 5038 | // Bail out early if calling a builtin with custom typechecking. |
| 5039 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 5040 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 5041 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5042 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5043 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5044 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5045 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 5046 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5047 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5048 | if (FuncT == 0) |
| 5049 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5050 | << Fn->getType() << Fn->getSourceRange()); |
| 5051 | } else if (const BlockPointerType *BPT = |
| 5052 | Fn->getType()->getAs<BlockPointerType>()) { |
| 5053 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 5054 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5055 | // Handle calls to expressions of unknown-any type. |
| 5056 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 5057 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5058 | if (rewrite.isInvalid()) return ExprError(); |
| 5059 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 5060 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5061 | goto retry; |
| 5062 | } |
| 5063 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5064 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5065 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5066 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5067 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 5068 | if (getLangOptions().CUDA) { |
| 5069 | if (Config) { |
| 5070 | // CUDA: Kernel calls must be to global functions |
| 5071 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 5072 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 5073 | << FDecl->getName() << Fn->getSourceRange()); |
| 5074 | |
| 5075 | // CUDA: Kernel function must have 'void' return type |
| 5076 | if (!FuncT->getResultType()->isVoidType()) |
| 5077 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 5078 | << Fn->getType() << Fn->getSourceRange()); |
| 5079 | } |
| 5080 | } |
| 5081 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5082 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5083 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5084 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5085 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5086 | return ExprError(); |
| 5087 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5088 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5089 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5090 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5091 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5092 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5093 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5094 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5095 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5096 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5097 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5098 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5099 | if (FDecl) { |
| 5100 | // Check if we have too few/too many template arguments, based |
| 5101 | // on our knowledge of the function definition. |
| 5102 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 5103 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5104 | const FunctionProtoType *Proto |
| 5105 | = Def->getType()->getAs<FunctionProtoType>(); |
| 5106 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5107 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 5108 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5109 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5110 | |
| 5111 | // If the function we're calling isn't a function prototype, but we have |
| 5112 | // a function prototype from a prior declaratiom, use that prototype. |
| 5113 | if (!FDecl->hasPrototype()) |
| 5114 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5117 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5118 | for (unsigned i = 0; i != NumArgs; i++) { |
| 5119 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5120 | |
| 5121 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5122 | InitializedEntity Entity |
| 5123 | = InitializedEntity::InitializeParameter(Context, |
| 5124 | Proto->getArgType(i)); |
| 5125 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 5126 | SourceLocation(), |
| 5127 | Owned(Arg)); |
| 5128 | if (ArgE.isInvalid()) |
| 5129 | return true; |
| 5130 | |
| 5131 | Arg = ArgE.takeAs<Expr>(); |
| 5132 | |
| 5133 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5134 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 5135 | |
| 5136 | if (ArgE.isInvalid()) |
| 5137 | return true; |
| 5138 | |
| 5139 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 5142 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 5143 | Arg->getType(), |
| 5144 | PDiag(diag::err_call_incomplete_argument) |
| 5145 | << Arg->getSourceRange())) |
| 5146 | return ExprError(); |
| 5147 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5148 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5149 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5150 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5151 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5152 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 5153 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5154 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 5155 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5156 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 5157 | // Check for sentinels |
| 5158 | if (NDecl) |
| 5159 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5161 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5162 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5163 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5164 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5165 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5166 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 5167 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5168 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5169 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5170 | return ExprError(); |
| 5171 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5172 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5173 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5176 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5177 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5178 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5179 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 5180 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5181 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5182 | |
| 5183 | TypeSourceInfo *TInfo; |
| 5184 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 5185 | if (!TInfo) |
| 5186 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 5187 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5188 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5191 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5192 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5193 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5194 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 5195 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5196 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 5197 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 5198 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 5199 | << SourceRange(LParenLoc, |
| 5200 | literalExpr->getSourceRange().getEnd()))) |
| 5201 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5202 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5203 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 5204 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 5205 | } else if (!literalType->isDependentType() && |
| 5206 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5207 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5208 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5209 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5210 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5211 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5212 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5213 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5214 | InitializationKind Kind |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5215 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5216 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5217 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5218 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5219 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5220 | &literalType); |
| 5221 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5222 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5223 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5224 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5225 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5226 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5227 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5228 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5229 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5230 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5231 | // In C, compound literals are l-values for some reason. |
| 5232 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5233 | |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5234 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5235 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5238 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5239 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5240 | SourceLocation RBraceLoc) { |
| 5241 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5242 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5243 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5244 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5245 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5246 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5247 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5248 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5249 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5250 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5251 | } |
| 5252 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5253 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5254 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5255 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5256 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5257 | // Also, callers should have filtered out the invalid cases with |
| 5258 | // pointers. Everything else should be possible. |
| 5259 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5260 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5261 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5262 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5263 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5264 | switch (SrcTy->getScalarTypeKind()) { |
| 5265 | case Type::STK_MemberPointer: |
| 5266 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5267 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5268 | case Type::STK_Pointer: |
| 5269 | switch (DestTy->getScalarTypeKind()) { |
| 5270 | case Type::STK_Pointer: |
| 5271 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5272 | CK_AnyPointerToObjCPointerCast : |
| 5273 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5274 | case Type::STK_Bool: |
| 5275 | return CK_PointerToBoolean; |
| 5276 | case Type::STK_Integral: |
| 5277 | return CK_PointerToIntegral; |
| 5278 | case Type::STK_Floating: |
| 5279 | case Type::STK_FloatingComplex: |
| 5280 | case Type::STK_IntegralComplex: |
| 5281 | case Type::STK_MemberPointer: |
| 5282 | llvm_unreachable("illegal cast from pointer"); |
| 5283 | } |
| 5284 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5285 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5286 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5287 | case Type::STK_Integral: |
| 5288 | switch (DestTy->getScalarTypeKind()) { |
| 5289 | case Type::STK_Pointer: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5290 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5291 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5292 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5293 | case Type::STK_Bool: |
| 5294 | return CK_IntegralToBoolean; |
| 5295 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5296 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5297 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5298 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5299 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5300 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5301 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5302 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5303 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5304 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5305 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5306 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5307 | case Type::STK_MemberPointer: |
| 5308 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5309 | } |
| 5310 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5311 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5312 | case Type::STK_Floating: |
| 5313 | switch (DestTy->getScalarTypeKind()) { |
| 5314 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5315 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5316 | case Type::STK_Bool: |
| 5317 | return CK_FloatingToBoolean; |
| 5318 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5319 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5320 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5321 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5322 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5323 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5324 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5325 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5326 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5327 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5328 | case Type::STK_Pointer: |
| 5329 | llvm_unreachable("valid float->pointer cast?"); |
| 5330 | case Type::STK_MemberPointer: |
| 5331 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5332 | } |
| 5333 | break; |
| 5334 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5335 | case Type::STK_FloatingComplex: |
| 5336 | switch (DestTy->getScalarTypeKind()) { |
| 5337 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5338 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5339 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5340 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5341 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5342 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5343 | if (S.Context.hasSameType(ET, DestTy)) |
| 5344 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5345 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5346 | return CK_FloatingCast; |
| 5347 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5348 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5349 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5350 | case Type::STK_Integral: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5351 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5352 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5353 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5354 | case Type::STK_Pointer: |
| 5355 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5356 | case Type::STK_MemberPointer: |
| 5357 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5358 | } |
| 5359 | break; |
| 5360 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5361 | case Type::STK_IntegralComplex: |
| 5362 | switch (DestTy->getScalarTypeKind()) { |
| 5363 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5364 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5365 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5366 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5367 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5368 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5369 | if (S.Context.hasSameType(ET, DestTy)) |
| 5370 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5371 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5372 | return CK_IntegralCast; |
| 5373 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5374 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5375 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5376 | case Type::STK_Floating: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5377 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5378 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5379 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5380 | case Type::STK_Pointer: |
| 5381 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5382 | case Type::STK_MemberPointer: |
| 5383 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5384 | } |
| 5385 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5386 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5387 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5388 | llvm_unreachable("Unhandled scalar cast"); |
| 5389 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5392 | /// CheckCastTypes - Check type constraints for casting between types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5393 | ExprResult Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 5394 | Expr *castExpr, CastKind& Kind, ExprValueKind &VK, |
| 5395 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5396 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5397 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5398 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5399 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5400 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 5401 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5402 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5403 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5404 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5405 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5406 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5407 | // We only support r-value casts in C. |
| 5408 | VK = VK_RValue; |
| 5409 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5410 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5411 | // type needs to be scalar. |
| 5412 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5413 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5414 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5415 | if (castExprRes.isInvalid()) |
| 5416 | return ExprError(); |
| 5417 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5418 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5419 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5420 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5421 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5422 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5423 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5424 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5425 | if (castExprRes.isInvalid()) |
| 5426 | return ExprError(); |
| 5427 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5428 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5429 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5430 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5431 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5432 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5433 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5434 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5435 | (castType->isStructureType() || castType->isUnionType())) { |
| 5436 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5437 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5438 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5439 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5440 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5441 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5442 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5443 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5444 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5445 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5446 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5447 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5448 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5449 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5450 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5451 | castExpr->getType()) && |
| 5452 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5453 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5454 | << castExpr->getSourceRange(); |
| 5455 | break; |
| 5456 | } |
| 5457 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5458 | if (Field == FieldEnd) { |
| 5459 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5460 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5461 | return ExprError(); |
| 5462 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5463 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5464 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5465 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5466 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5467 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5468 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5469 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5470 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5471 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5472 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5473 | // The type we're casting to is known to be a scalar or vector. |
| 5474 | |
| 5475 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5476 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5477 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5478 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5479 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5480 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5481 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5482 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5483 | |
| 5484 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5485 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5486 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5487 | if (castType->isVectorType()) { |
| 5488 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5489 | VectorType::AltiVecVector && |
| 5490 | (castExpr->getType()->isIntegerType() || |
| 5491 | castExpr->getType()->isFloatingType())) { |
| 5492 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5493 | return Owned(castExpr); |
| 5494 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5495 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5496 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5497 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5498 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5499 | if (castExpr->getType()->isVectorType()) { |
| 5500 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5501 | return ExprError(); |
| 5502 | else |
| 5503 | return Owned(castExpr); |
| 5504 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5505 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5506 | // The source and target types are both scalars, i.e. |
| 5507 | // - arithmetic types (fundamental, enum, and complex) |
| 5508 | // - all kinds of pointers |
| 5509 | // Note that member pointers were filtered out with C++, above. |
| 5510 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5511 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5512 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5513 | return ExprError(); |
| 5514 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5515 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5516 | // If either type is a pointer, the other type has to be either an |
| 5517 | // integer or a pointer. |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5518 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5519 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5520 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5521 | castExprType->isArithmeticType()) { |
| 5522 | Diag(castExpr->getLocStart(), |
| 5523 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5524 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5525 | return ExprError(); |
| 5526 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5527 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5528 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5529 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5530 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5531 | return ExprError(); |
| 5532 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5533 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5534 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5535 | castExprRes = Owned(castExpr); |
| 5536 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5537 | if (castExprRes.isInvalid()) |
| 5538 | return ExprError(); |
| 5539 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5540 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5541 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5542 | CheckCastAlign(castExpr, castType, TyR); |
| 5543 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5544 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5545 | } |
| 5546 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5547 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5548 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5549 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5550 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5551 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5552 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5553 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5554 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5555 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5556 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5557 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5558 | } else |
| 5559 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5560 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5561 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5562 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5563 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5564 | return false; |
| 5565 | } |
| 5566 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5567 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5568 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5569 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5570 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5571 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5572 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5573 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5574 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5575 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5576 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5577 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5578 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5579 | return ExprError(); |
| 5580 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5581 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5582 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5583 | } |
| 5584 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5585 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5586 | // conversion will take place first from scalar to elt type, and then |
| 5587 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5588 | if (SrcTy->isPointerType()) |
| 5589 | return Diag(R.getBegin(), |
| 5590 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5591 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5592 | |
| 5593 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | ExprResult CastExprRes = Owned(CastExpr); |
| 5595 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5596 | if (CastExprRes.isInvalid()) |
| 5597 | return ExprError(); |
| 5598 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5599 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5600 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5601 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5604 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5605 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5606 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5607 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5608 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5609 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5610 | TypeSourceInfo *castTInfo; |
| 5611 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5612 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5613 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5615 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5616 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5617 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5618 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5619 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5620 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5621 | } |
| 5622 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5623 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5624 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5625 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5626 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5627 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5628 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5629 | ExprResult CastResult = |
| 5630 | CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
| 5631 | Kind, VK, BasePath); |
| 5632 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5633 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5634 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5635 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5636 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5637 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5638 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5639 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5642 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5643 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5644 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5645 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5646 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5647 | if (!E) |
| 5648 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5649 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5650 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5651 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5652 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5653 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5654 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5655 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5656 | if (Result.isInvalid()) return ExprError(); |
| 5657 | |
| 5658 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5661 | ExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5662 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5663 | SourceLocation RParenLoc, Expr *Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5664 | TypeSourceInfo *TInfo) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5665 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5666 | QualType Ty = TInfo->getType(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5667 | bool isVectorLiteral = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5668 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5669 | // Check for an altivec or OpenCL literal, |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5670 | // i.e. all the elements are integer constants. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5671 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5672 | if (PE->getNumExprs() == 0) { |
| 5673 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5674 | return ExprError(); |
| 5675 | } |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5676 | if (PE->getNumExprs() == 1) { |
| 5677 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5678 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5679 | } |
| 5680 | else |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5681 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5682 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5683 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5684 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5685 | // then handle it as such. |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5686 | if (isVectorLiteral) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5687 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5688 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5689 | // initializers must be one or must match the size of the vector. |
| 5690 | // If a single value is specified in the initializer then it will be |
| 5691 | // replicated to all the components of the vector |
| 5692 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5693 | VectorType::AltiVecVector) { |
| 5694 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5695 | // The number of initializers must be one or must match the size of the |
| 5696 | // vector. If a single value is specified in the initializer then it will |
| 5697 | // be replicated to all the components of the vector |
| 5698 | if (PE->getNumExprs() == 1) { |
| 5699 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5700 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5701 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5702 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5703 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5704 | } |
| 5705 | else if (PE->getNumExprs() < numElems) { |
| 5706 | Diag(PE->getExprLoc(), |
| 5707 | diag::err_incorrect_number_of_vector_initializers); |
| 5708 | return ExprError(); |
| 5709 | } |
| 5710 | else |
| 5711 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5712 | initExprs.push_back(PE->getExpr(i)); |
| 5713 | } |
| 5714 | else |
| 5715 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5716 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5717 | |
| 5718 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5719 | // braces instead of the original commas. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5720 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5721 | &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5722 | initExprs.size(), RParenLoc); |
| 5723 | E->setType(Ty); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5724 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5725 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5726 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5727 | // sequence of BinOp comma operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5728 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5729 | if (Result.isInvalid()) return ExprError(); |
| 5730 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5731 | } |
| 5732 | } |
| 5733 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5734 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5735 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5736 | MultiExprArg Val, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5737 | ParsedType TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5738 | unsigned nexprs = Val.size(); |
| 5739 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5740 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5741 | Expr *expr; |
| 5742 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5743 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5744 | else |
| 5745 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5746 | return Owned(expr); |
| 5747 | } |
| 5748 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5749 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5750 | /// constant and the other is not a pointer. |
| 5751 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5752 | SourceLocation QuestionLoc) { |
| 5753 | Expr *NullExpr = LHS; |
| 5754 | Expr *NonPointerExpr = RHS; |
| 5755 | Expr::NullPointerConstantKind NullKind = |
| 5756 | NullExpr->isNullPointerConstant(Context, |
| 5757 | Expr::NPC_ValueDependentIsNotNull); |
| 5758 | |
| 5759 | if (NullKind == Expr::NPCK_NotNull) { |
| 5760 | NullExpr = RHS; |
| 5761 | NonPointerExpr = LHS; |
| 5762 | NullKind = |
| 5763 | NullExpr->isNullPointerConstant(Context, |
| 5764 | Expr::NPC_ValueDependentIsNotNull); |
| 5765 | } |
| 5766 | |
| 5767 | if (NullKind == Expr::NPCK_NotNull) |
| 5768 | return false; |
| 5769 | |
| 5770 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5771 | // In this case, check to make sure that we got here from a "NULL" |
| 5772 | // string in the source code. |
| 5773 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5774 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5775 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5776 | return false; |
| 5777 | } |
| 5778 | |
| 5779 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5780 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5781 | << NonPointerExpr->getType() << DiagType |
| 5782 | << NonPointerExpr->getSourceRange(); |
| 5783 | return true; |
| 5784 | } |
| 5785 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5786 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5787 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5788 | /// C99 6.5.15 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5789 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5790 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5791 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5792 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5793 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5794 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5795 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5796 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5797 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5798 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5799 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5800 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5801 | // C++ is sufficiently different to merit its own checker. |
| 5802 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5803 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5804 | |
| 5805 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5806 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5807 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5808 | Cond = UsualUnaryConversions(Cond.take()); |
| 5809 | if (Cond.isInvalid()) |
| 5810 | return QualType(); |
| 5811 | LHS = UsualUnaryConversions(LHS.take()); |
| 5812 | if (LHS.isInvalid()) |
| 5813 | return QualType(); |
| 5814 | RHS = UsualUnaryConversions(RHS.take()); |
| 5815 | if (RHS.isInvalid()) |
| 5816 | return QualType(); |
| 5817 | |
| 5818 | QualType CondTy = Cond.get()->getType(); |
| 5819 | QualType LHSTy = LHS.get()->getType(); |
| 5820 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5822 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5823 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5824 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5825 | // Throw an error if its not either. |
| 5826 | if (getLangOptions().OpenCL) { |
| 5827 | if (!CondTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5828 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5829 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5830 | << CondTy; |
| 5831 | return QualType(); |
| 5832 | } |
| 5833 | } |
| 5834 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5835 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5836 | << CondTy; |
| 5837 | return QualType(); |
| 5838 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5839 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5840 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5841 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5842 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5843 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5844 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5845 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5846 | // attempt to implicity convert them to the vector type to act like the |
| 5847 | // built in select. |
| 5848 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5849 | // Both operands should be of scalar type. |
| 5850 | if (!LHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5851 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5852 | << CondTy; |
| 5853 | return QualType(); |
| 5854 | } |
| 5855 | if (!RHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5856 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5857 | << CondTy; |
| 5858 | return QualType(); |
| 5859 | } |
| 5860 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5861 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5862 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5863 | } |
| 5864 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5865 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5866 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5867 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5868 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5869 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5870 | return QualType(); |
| 5871 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5872 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5873 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5874 | // If both operands are the same structure or union type, the result is that |
| 5875 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5876 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5877 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5878 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5879 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5880 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5881 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5882 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5883 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5884 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5885 | // 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] | 5886 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5887 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5888 | if (!LHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5889 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5890 | << RHS.get()->getSourceRange(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5891 | if (!RHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5892 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5893 | << LHS.get()->getSourceRange(); |
| 5894 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 5895 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5896 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5897 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5898 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5899 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5900 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5901 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5902 | // promote the null to a pointer. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5903 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5904 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5905 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5906 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5907 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 5908 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5909 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5910 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5911 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5912 | // All objective-c pointer type analysis is done here. |
| 5913 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5914 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5915 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5916 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5917 | if (!compositeType.isNull()) |
| 5918 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5919 | |
| 5920 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5921 | // Handle block pointer types. |
| 5922 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5923 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5924 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5925 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5926 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 5927 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5928 | return destType; |
| 5929 | } |
| 5930 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5931 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5932 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5933 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5934 | // We have 2 block pointer types. |
| 5935 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5936 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5937 | return LHSTy; |
| 5938 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5939 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5940 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5941 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5942 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5943 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5944 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5945 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5946 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5947 | // In this situation, we assume void* type. No especially good |
| 5948 | // reason, but this is what gcc does, and we do have to pick |
| 5949 | // to get a consistent AST. |
| 5950 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5951 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5952 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5953 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5954 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5955 | // The block pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5956 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 5957 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5958 | return LHSTy; |
| 5959 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5960 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5961 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5962 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5963 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5964 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5965 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5966 | |
| 5967 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5968 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5969 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5970 | QualType destPointee |
| 5971 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5972 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5973 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5974 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5975 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5976 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5977 | return destType; |
| 5978 | } |
| 5979 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5980 | QualType destPointee |
| 5981 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5982 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5983 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5984 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5985 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5986 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5987 | return destType; |
| 5988 | } |
| 5989 | |
| 5990 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5991 | // Two identical pointer types are always compatible. |
| 5992 | return LHSTy; |
| 5993 | } |
| 5994 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5995 | rhptee.getUnqualifiedType())) { |
| 5996 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5997 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5998 | // In this situation, we assume void* type. No especially good |
| 5999 | // reason, but this is what gcc does, and we do have to pick |
| 6000 | // to get a consistent AST. |
| 6001 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6002 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6003 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6004 | return incompatTy; |
| 6005 | } |
| 6006 | // The pointer types are compatible. |
| 6007 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 6008 | // differently qualified versions of compatible types, the result type is |
| 6009 | // a pointer to an appropriately qualified version of the *composite* |
| 6010 | // type. |
| 6011 | // FIXME: Need to calculate the composite type. |
| 6012 | // FIXME: Need to add qualifiers |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6013 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6014 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6015 | return LHSTy; |
| 6016 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6017 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6018 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 6019 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6020 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 6021 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6022 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6023 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6024 | return RHSTy; |
| 6025 | } |
| 6026 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 6027 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6028 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6029 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6030 | return LHSTy; |
| 6031 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 6032 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6033 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 6034 | // constant and the other is not a pointer type. In this case, the user most |
| 6035 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6036 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6037 | return QualType(); |
| 6038 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 6039 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6040 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6041 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6042 | return QualType(); |
| 6043 | } |
| 6044 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6045 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 6046 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6047 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6048 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6049 | QualType LHSTy = LHS.get()->getType(); |
| 6050 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6051 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6052 | // Handle things like Class and struct objc_class*. Here we case the result |
| 6053 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 6054 | // redefinition type if an attempt is made to access its fields. |
| 6055 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6056 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6057 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6058 | return LHSTy; |
| 6059 | } |
| 6060 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6061 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6062 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6063 | return RHSTy; |
| 6064 | } |
| 6065 | // And the same for struct objc_object* / id |
| 6066 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6067 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6068 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6069 | return LHSTy; |
| 6070 | } |
| 6071 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6072 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6073 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6074 | return RHSTy; |
| 6075 | } |
| 6076 | // And the same for struct objc_selector* / SEL |
| 6077 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6078 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6079 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6080 | return LHSTy; |
| 6081 | } |
| 6082 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6083 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6084 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6085 | return RHSTy; |
| 6086 | } |
| 6087 | // Check constraints for Objective-C object pointers types. |
| 6088 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6089 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6090 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6091 | // Two identical object pointer types are always compatible. |
| 6092 | return LHSTy; |
| 6093 | } |
| 6094 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 6095 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 6096 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6097 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6098 | // If both operands are interfaces and either operand can be |
| 6099 | // assigned to the other, use that type as the composite |
| 6100 | // type. This allows |
| 6101 | // xxx ? (A*) a : (B*) b |
| 6102 | // where B is a subclass of A. |
| 6103 | // |
| 6104 | // Additionally, as for assignment, if either type is 'id' |
| 6105 | // allow silent coercion. Finally, if the types are |
| 6106 | // incompatible then make sure to use 'id' as the composite |
| 6107 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6108 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6109 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 6110 | // It could return the composite type. |
| 6111 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 6112 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 6113 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 6114 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 6115 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 6116 | RHSTy->isObjCQualifiedIdType()) && |
| 6117 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 6118 | // Need to handle "id<xx>" explicitly. |
| 6119 | // GCC allows qualified id and any Objective-C type to devolve to |
| 6120 | // id. Currently localizing to here until clear this should be |
| 6121 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 6122 | compositeType = Context.getObjCIdType(); |
| 6123 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 6124 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6125 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6126 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 6127 | ; |
| 6128 | else { |
| 6129 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 6130 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6131 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6132 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6133 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6134 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6135 | return incompatTy; |
| 6136 | } |
| 6137 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6138 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 6139 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6140 | return compositeType; |
| 6141 | } |
| 6142 | // Check Objective-C object pointer types and 'void *' |
| 6143 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 6144 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6145 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6146 | QualType destPointee |
| 6147 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 6148 | QualType destType = Context.getPointerType(destPointee); |
| 6149 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6150 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6151 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6152 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6153 | return destType; |
| 6154 | } |
| 6155 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 6156 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6157 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 6158 | QualType destPointee |
| 6159 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 6160 | QualType destType = Context.getPointerType(destPointee); |
| 6161 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6162 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6163 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6164 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6165 | return destType; |
| 6166 | } |
| 6167 | return QualType(); |
| 6168 | } |
| 6169 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame^] | 6170 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6171 | /// ParenRange in parentheses. |
| 6172 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6173 | const PartialDiagnostic &PD, |
| 6174 | const PartialDiagnostic &FirstNote, |
| 6175 | SourceRange FirstParenRange, |
| 6176 | const PartialDiagnostic &SecondNote, |
| 6177 | SourceRange SecondParenRange) { |
| 6178 | Self.Diag(Loc, PD); |
| 6179 | |
| 6180 | if (!FirstNote.getDiagID()) |
| 6181 | return; |
| 6182 | |
| 6183 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 6184 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6185 | // We can't display the parentheses, so just return. |
| 6186 | return; |
| 6187 | } |
| 6188 | |
| 6189 | Self.Diag(Loc, FirstNote) |
| 6190 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
| 6191 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6192 | |
| 6193 | if (!SecondNote.getDiagID()) |
| 6194 | return; |
| 6195 | |
| 6196 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6197 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6198 | // We can't display the parentheses, so just dig the |
| 6199 | // warning/error and return. |
| 6200 | Self.Diag(Loc, SecondNote); |
| 6201 | return; |
| 6202 | } |
| 6203 | |
| 6204 | Self.Diag(Loc, SecondNote) |
| 6205 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6206 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6207 | } |
| 6208 | |
| 6209 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 6210 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 6211 | } |
| 6212 | |
| 6213 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 6214 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 6215 | } |
| 6216 | |
| 6217 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 6218 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 6219 | /// the conditional operator has higher precedence, for example: |
| 6220 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 6221 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 6222 | SourceLocation OpLoc, |
| 6223 | Expr *cond, |
| 6224 | Expr *lhs, |
| 6225 | Expr *rhs) { |
| 6226 | while (ImplicitCastExpr *C = dyn_cast<ImplicitCastExpr>(cond)) |
| 6227 | cond = C->getSubExpr(); |
| 6228 | |
| 6229 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(cond)) { |
| 6230 | if (!IsArithmeticOp(OP->getOpcode())) |
| 6231 | return; |
| 6232 | |
| 6233 | // Drill down on the RHS of the condition. |
| 6234 | Expr *CondRHS = OP->getRHS(); |
| 6235 | while (ImplicitCastExpr *C = dyn_cast<ImplicitCastExpr>(CondRHS)) |
| 6236 | CondRHS = C->getSubExpr(); |
| 6237 | while (ParenExpr *P = dyn_cast<ParenExpr>(CondRHS)) |
| 6238 | CondRHS = P->getSubExpr(); |
| 6239 | |
| 6240 | bool CondRHSLooksBoolean = false; |
| 6241 | if (CondRHS->getType()->isBooleanType()) |
| 6242 | CondRHSLooksBoolean = true; |
| 6243 | else if (BinaryOperator *CondRHSOP = dyn_cast<BinaryOperator>(CondRHS)) |
| 6244 | CondRHSLooksBoolean = IsLogicOp(CondRHSOP->getOpcode()); |
| 6245 | else if (UnaryOperator *CondRHSOP = dyn_cast<UnaryOperator>(CondRHS)) |
| 6246 | CondRHSLooksBoolean = CondRHSOP->getOpcode() == UO_LNot; |
| 6247 | |
| 6248 | if (CondRHSLooksBoolean) { |
| 6249 | // The condition is an arithmetic binary expression, with a right- |
| 6250 | // hand side that looks boolean, so warn. |
| 6251 | |
| 6252 | PartialDiagnostic Warn = Self.PDiag(diag::warn_precedence_conditional) |
| 6253 | << OP->getSourceRange() |
| 6254 | << BinaryOperator::getOpcodeStr(OP->getOpcode()); |
| 6255 | |
| 6256 | PartialDiagnostic FirstNote = |
| 6257 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 6258 | << BinaryOperator::getOpcodeStr(OP->getOpcode()); |
| 6259 | |
| 6260 | SourceRange FirstParenRange(OP->getLHS()->getLocStart(), |
| 6261 | OP->getRHS()->getLocEnd()); |
| 6262 | |
| 6263 | PartialDiagnostic SecondNote = |
| 6264 | Self.PDiag(diag::note_precedence_conditional_first); |
| 6265 | SourceRange SecondParenRange(OP->getRHS()->getLocStart(), |
| 6266 | rhs->getLocEnd()); |
| 6267 | |
| 6268 | SuggestParentheses(Self, OpLoc, Warn, FirstNote, FirstParenRange, |
| 6269 | SecondNote, SecondParenRange); |
| 6270 | } |
| 6271 | } |
| 6272 | } |
| 6273 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6274 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6275 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6276 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6277 | SourceLocation ColonLoc, |
| 6278 | Expr *CondExpr, Expr *LHSExpr, |
| 6279 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 6280 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 6281 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6282 | OpaqueValueExpr *opaqueValue = 0; |
| 6283 | Expr *commonExpr = 0; |
| 6284 | if (LHSExpr == 0) { |
| 6285 | commonExpr = CondExpr; |
| 6286 | |
| 6287 | // We usually want to apply unary conversions *before* saving, except |
| 6288 | // in the special case of a C++ l-value conditional. |
| 6289 | if (!(getLangOptions().CPlusPlus |
| 6290 | && !commonExpr->isTypeDependent() |
| 6291 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 6292 | && commonExpr->isGLValue() |
| 6293 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 6294 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 6295 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6296 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 6297 | if (commonRes.isInvalid()) |
| 6298 | return ExprError(); |
| 6299 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
| 6302 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 6303 | commonExpr->getType(), |
| 6304 | commonExpr->getValueKind(), |
| 6305 | commonExpr->getObjectKind()); |
| 6306 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 6307 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6308 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6309 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6310 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6311 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 6312 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6313 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6314 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 6315 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6316 | return ExprError(); |
| 6317 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame^] | 6318 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 6319 | RHS.get()); |
| 6320 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6321 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6322 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 6323 | LHS.take(), ColonLoc, |
| 6324 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6325 | |
| 6326 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6327 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 6328 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6329 | } |
| 6330 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6331 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6332 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6333 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6334 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6335 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6336 | static Sema::AssignConvertType |
| 6337 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6338 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6339 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6340 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6341 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6342 | const Type *lhptee, *rhptee; |
| 6343 | Qualifiers lhq, rhq; |
| 6344 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6345 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6346 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6347 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6348 | |
| 6349 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6350 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6351 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6352 | Qualifiers lq; |
| 6353 | |
| 6354 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6355 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6356 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6357 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6358 | |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6359 | // It's okay to add or remove GC qualifiers when converting to |
| 6360 | // and from void*. |
| 6361 | else if (lhq.withoutObjCGCAttr().compatiblyIncludes(rhq.withoutObjCGCAttr()) |
| 6362 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6363 | ; // keep old |
| 6364 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6365 | // For GCC compatibility, other qualifier mismatches are treated |
| 6366 | // as still compatible in C. |
| 6367 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6368 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6369 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6370 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6371 | // 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] | 6372 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6373 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6374 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6375 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6376 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6377 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6378 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6379 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6380 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6381 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6382 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6383 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6384 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6385 | |
| 6386 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6387 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6388 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6389 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6390 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6391 | // 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] | 6392 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6393 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6394 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6395 | // Check if the pointee types are compatible ignoring the sign. |
| 6396 | // We explicitly check for char so that we catch "char" vs |
| 6397 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6398 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6399 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6400 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6401 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6402 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6403 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6404 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6405 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6406 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6407 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6408 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6409 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6410 | // takes priority over sign incompatibility because the sign |
| 6411 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6412 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6413 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6414 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6415 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6416 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6417 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6418 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6419 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6420 | // the eventual target type is the same and the pointers have the same |
| 6421 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6422 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6423 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6424 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6425 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6426 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6427 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6428 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6429 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6430 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6431 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6432 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6433 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6434 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6435 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6436 | } |
| 6437 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6438 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6439 | /// block pointer types are compatible or whether a block and normal pointer |
| 6440 | /// are compatible. It is more restrict than comparing two function pointer |
| 6441 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6442 | static Sema::AssignConvertType |
| 6443 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6444 | QualType rhsType) { |
| 6445 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6446 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6447 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6448 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6449 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6450 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6451 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6452 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6453 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6454 | // In C++, the types have to match exactly. |
| 6455 | if (S.getLangOptions().CPlusPlus) |
| 6456 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6457 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6458 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6459 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6460 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6461 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6462 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6463 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6464 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6465 | return Sema::IncompatibleBlockPointer; |
| 6466 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6467 | return ConvTy; |
| 6468 | } |
| 6469 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6470 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6471 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6472 | static Sema::AssignConvertType |
| 6473 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6474 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6475 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6476 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6477 | if (lhsType->isObjCBuiltinType()) { |
| 6478 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6479 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6480 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6481 | return Sema::IncompatiblePointer; |
| 6482 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6483 | } |
| 6484 | if (rhsType->isObjCBuiltinType()) { |
| 6485 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6486 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6487 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6488 | return Sema::IncompatiblePointer; |
| 6489 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6490 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6491 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6492 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6493 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6494 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6495 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6496 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6497 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6498 | |
| 6499 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6500 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6501 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6502 | return Sema::IncompatibleObjCQualifiedId; |
| 6503 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6506 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6507 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6508 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6509 | // Fake up an opaque expression. We don't actually care about what |
| 6510 | // cast operations are required, so if CheckAssignmentConstraints |
| 6511 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6512 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6513 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6514 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6515 | CastKind K = CK_Invalid; |
| 6516 | |
| 6517 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6518 | } |
| 6519 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6520 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6521 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6522 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6523 | /// |
| 6524 | /// int a, *pint; |
| 6525 | /// short *pshort; |
| 6526 | /// struct foo *pfoo; |
| 6527 | /// |
| 6528 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6529 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6530 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6531 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6532 | /// |
| 6533 | /// 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] | 6534 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6535 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6536 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6537 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6538 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6539 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6540 | QualType rhsType = rhs.get()->getType(); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6541 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6542 | // Get canonical types. We're not formatting these types, just comparing |
| 6543 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6544 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6545 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6546 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6547 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6548 | if (lhsType == rhsType) { |
| 6549 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6550 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6553 | // If the left-hand side is a reference type, then we are in a |
| 6554 | // (rare!) case where we've allowed the use of references in C, |
| 6555 | // e.g., as a parameter type in a built-in function. In this case, |
| 6556 | // just make sure that the type referenced is compatible with the |
| 6557 | // right-hand side type. The caller is responsible for adjusting |
| 6558 | // lhsType so that the resulting expression does not have reference |
| 6559 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6560 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6561 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6562 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6563 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6564 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6565 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6566 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6567 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6568 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6569 | // to the same ExtVector type. |
| 6570 | if (lhsType->isExtVectorType()) { |
| 6571 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6572 | return Incompatible; |
| 6573 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6574 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6575 | // element type. |
| 6576 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6577 | if (elType != rhsType) { |
| 6578 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6579 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6580 | } |
| 6581 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6582 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6583 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6585 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6586 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6587 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6588 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6589 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6590 | // vector type and vice versa |
| 6591 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6592 | Kind = CK_BitCast; |
| 6593 | return Compatible; |
| 6594 | } |
| 6595 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6596 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6597 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6598 | // no bits are changed but the result type is different. |
| 6599 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6600 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6601 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6602 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6603 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6604 | } |
| 6605 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6606 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6607 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6608 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6609 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6610 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6611 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6612 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6613 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6614 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6615 | // Conversions to normal pointers. |
| 6616 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6617 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6618 | if (isa<PointerType>(rhsType)) { |
| 6619 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6620 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6621 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6622 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6623 | // int -> T* |
| 6624 | if (rhsType->isIntegerType()) { |
| 6625 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6626 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6627 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6628 | |
| 6629 | // C pointers are not compatible with ObjC object pointers, |
| 6630 | // with two exceptions: |
| 6631 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6632 | // - conversions to void* |
| 6633 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6634 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6635 | return Compatible; |
| 6636 | } |
| 6637 | |
| 6638 | // - conversions from 'Class' to the redefinition type |
| 6639 | if (rhsType->isObjCClassType() && |
| 6640 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6641 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6642 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6643 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6644 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6645 | Kind = CK_BitCast; |
| 6646 | return IncompatiblePointer; |
| 6647 | } |
| 6648 | |
| 6649 | // U^ -> void* |
| 6650 | if (rhsType->getAs<BlockPointerType>()) { |
| 6651 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6652 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6653 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6654 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6655 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6656 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6657 | return Incompatible; |
| 6658 | } |
| 6659 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6660 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6661 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6662 | // U^ -> T^ |
| 6663 | if (rhsType->isBlockPointerType()) { |
| 6664 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6665 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6666 | } |
| 6667 | |
| 6668 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6669 | if (rhsType->isIntegerType()) { |
| 6670 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6671 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6672 | } |
| 6673 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6674 | // id -> T^ |
| 6675 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6676 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6677 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6678 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6679 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6680 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6681 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6682 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6683 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6684 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6685 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6686 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6687 | return Incompatible; |
| 6688 | } |
| 6689 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6690 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6691 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6692 | // A* -> B* |
| 6693 | if (rhsType->isObjCObjectPointerType()) { |
| 6694 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6695 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6699 | if (rhsType->isIntegerType()) { |
| 6700 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6701 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6702 | } |
| 6703 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6704 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6705 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6706 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6707 | // - conversions from 'void*' |
| 6708 | if (rhsType->isVoidPointerType()) { |
| 6709 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6710 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | // - conversions to 'Class' from its redefinition type |
| 6714 | if (lhsType->isObjCClassType() && |
| 6715 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6716 | Kind = CK_BitCast; |
| 6717 | return Compatible; |
| 6718 | } |
| 6719 | |
| 6720 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6721 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6722 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6723 | |
| 6724 | // T^ -> A* |
| 6725 | if (rhsType->isBlockPointerType()) { |
| 6726 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6727 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6728 | } |
| 6729 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6730 | return Incompatible; |
| 6731 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6732 | |
| 6733 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6734 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6735 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6736 | if (lhsType == Context.BoolTy) { |
| 6737 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6738 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6739 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6740 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6741 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6742 | if (lhsType->isIntegerType()) { |
| 6743 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6744 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6745 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6746 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6747 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6748 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6749 | |
| 6750 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6751 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6752 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6753 | if (lhsType == Context.BoolTy) { |
| 6754 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6755 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6756 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6757 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6758 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6759 | if (lhsType->isIntegerType()) { |
| 6760 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6761 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6764 | return Incompatible; |
| 6765 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6766 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6767 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6768 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6769 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6770 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6771 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6772 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6773 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6775 | return Incompatible; |
| 6776 | } |
| 6777 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6778 | /// \brief Constructs a transparent union from an expression that is |
| 6779 | /// used to initialize the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6780 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6781 | QualType UnionType, FieldDecl *Field) { |
| 6782 | // Build an initializer list that designates the appropriate member |
| 6783 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6784 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6785 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6786 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6787 | SourceLocation()); |
| 6788 | Initializer->setType(UnionType); |
| 6789 | Initializer->setInitializedFieldInUnion(Field); |
| 6790 | |
| 6791 | // Build a compound literal constructing a value of the transparent |
| 6792 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6793 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6794 | EResult = S.Owned( |
| 6795 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6796 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6800 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6801 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6802 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6803 | // 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] | 6804 | // transparent_union GCC extension. |
| 6805 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6806 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6807 | return Incompatible; |
| 6808 | |
| 6809 | // The field to initialize within the transparent union. |
| 6810 | RecordDecl *UD = UT->getDecl(); |
| 6811 | FieldDecl *InitField = 0; |
| 6812 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6813 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6814 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6815 | it != itend; ++it) { |
| 6816 | if (it->getType()->isPointerType()) { |
| 6817 | // If the transparent union contains a pointer type, we allow: |
| 6818 | // 1) void pointer |
| 6819 | // 2) null pointer constant |
| 6820 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6821 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6822 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6823 | InitField = *it; |
| 6824 | break; |
| 6825 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6826 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6827 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6828 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6829 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6830 | InitField = *it; |
| 6831 | break; |
| 6832 | } |
| 6833 | } |
| 6834 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6835 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6836 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6837 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6838 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6839 | InitField = *it; |
| 6840 | break; |
| 6841 | } |
| 6842 | } |
| 6843 | |
| 6844 | if (!InitField) |
| 6845 | return Incompatible; |
| 6846 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6847 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6848 | return Compatible; |
| 6849 | } |
| 6850 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6851 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6852 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6853 | if (getLangOptions().CPlusPlus) { |
| 6854 | if (!lhsType->isRecordType()) { |
| 6855 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6856 | // expression is implicitly converted (C++ 4) to the |
| 6857 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6858 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6859 | lhsType.getUnqualifiedType(), |
| 6860 | AA_Assigning); |
| 6861 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6862 | return Incompatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6863 | rExpr = move(Res); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6864 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6865 | } |
| 6866 | |
| 6867 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6868 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6869 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6870 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6871 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6872 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6873 | if ((lhsType->isPointerType() || |
| 6874 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6875 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6876 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6877 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6878 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6879 | return Compatible; |
| 6880 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6881 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6882 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6883 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6884 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6885 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6886 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6887 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6888 | if (!lhsType->isReferenceType()) { |
| 6889 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 6890 | if (rExpr.isInvalid()) |
| 6891 | return Incompatible; |
| 6892 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6893 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6894 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6895 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6896 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6897 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6898 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6899 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6900 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6901 | // so that we can use references in built-in functions even in C. |
| 6902 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6903 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6904 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 6905 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6906 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6907 | } |
| 6908 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6909 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6910 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6911 | << lex.get()->getType() << rex.get()->getType() |
| 6912 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6913 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6914 | } |
| 6915 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6916 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6917 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6918 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6919 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6920 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6921 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6922 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6923 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6924 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6925 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6926 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6927 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6928 | // Handle the case of a vector & extvector type of the same size and element |
| 6929 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6930 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6931 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6932 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6933 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6934 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6935 | if (lhsType->isExtVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6936 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6937 | return lhsType; |
| 6938 | } |
| 6939 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6940 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6941 | return rhsType; |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6942 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 6943 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6944 | // vectors, the total size only needs to be the same. This is a |
| 6945 | // bitcast; no bits are changed but the result type is different. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6946 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6947 | return lhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6948 | } |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6949 | } |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6950 | } |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6951 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6952 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6953 | // Handle the case of equivalent AltiVec and GCC vector types |
| 6954 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 6955 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6956 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6957 | return rhsType; |
| 6958 | } |
| 6959 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6960 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6961 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6962 | bool swapped = false; |
| 6963 | if (rhsType->isExtVectorType()) { |
| 6964 | swapped = true; |
| 6965 | std::swap(rex, lex); |
| 6966 | std::swap(rhsType, lhsType); |
| 6967 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6968 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6969 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6970 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6971 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 6972 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6973 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 6974 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6975 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6976 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6977 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6978 | if (swapped) std::swap(rex, lex); |
| 6979 | return lhsType; |
| 6980 | } |
| 6981 | } |
| 6982 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 6983 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6984 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 6985 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6986 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6987 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6988 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6989 | if (swapped) std::swap(rex, lex); |
| 6990 | return lhsType; |
| 6991 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6992 | } |
| 6993 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6994 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6995 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6996 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6997 | << lex.get()->getType() << rex.get()->getType() |
| 6998 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6999 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7000 | } |
| 7001 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7002 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7003 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 7004 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7005 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7006 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7007 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7008 | if (lex.isInvalid() || rex.isInvalid()) |
| 7009 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7010 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7011 | if (!lex.get()->getType()->isArithmeticType() || |
| 7012 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7013 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7014 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7015 | // Check for division by zero. |
| 7016 | if (isDiv && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7017 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7018 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 7019 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7020 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7021 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7024 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7025 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7026 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7027 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7028 | rex.get()->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 7029 | return CheckVectorOperands(Loc, lex, rex); |
| 7030 | return InvalidOperands(Loc, lex, rex); |
| 7031 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7032 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7033 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7034 | if (lex.isInvalid() || rex.isInvalid()) |
| 7035 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7036 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7037 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7038 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7039 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7040 | // Check for remainder by zero. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7041 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7042 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 7043 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7044 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7045 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7046 | } |
| 7047 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7048 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7049 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 7050 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7051 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7052 | if (CompLHSTy) *CompLHSTy = compType; |
| 7053 | return compType; |
| 7054 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7055 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7056 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7057 | if (lex.isInvalid() || rex.isInvalid()) |
| 7058 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7059 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7060 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7061 | if (lex.get()->getType()->isArithmeticType() && |
| 7062 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7063 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7064 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7065 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7066 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7067 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7068 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7069 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7070 | std::swap(PExp, IExp); |
| 7071 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7072 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7073 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7074 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7075 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7076 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7077 | // Check for arithmetic on pointers to incomplete types. |
| 7078 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7079 | if (getLangOptions().CPlusPlus) { |
| 7080 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7081 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7082 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7083 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7084 | |
| 7085 | // GNU extension: arithmetic on pointer to void |
| 7086 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7087 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7088 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7089 | if (getLangOptions().CPlusPlus) { |
| 7090 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7091 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7092 | return QualType(); |
| 7093 | } |
| 7094 | |
| 7095 | // GNU extension: arithmetic on pointer to function |
| 7096 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7097 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7098 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7099 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7100 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7101 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7102 | PExp->getType()->isObjCObjectPointerType()) && |
| 7103 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7104 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 7105 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7106 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7107 | return QualType(); |
| 7108 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7109 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7110 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7111 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 7112 | << PointeeTy << PExp->getSourceRange(); |
| 7113 | return QualType(); |
| 7114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7115 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7116 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7117 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7118 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7119 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7120 | if (LHSTy->isPromotableIntegerType()) |
| 7121 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7122 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7123 | *CompLHSTy = LHSTy; |
| 7124 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7125 | return PExp->getType(); |
| 7126 | } |
| 7127 | } |
| 7128 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7129 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7132 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7133 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7134 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7135 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7136 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7137 | if (CompLHSTy) *CompLHSTy = compType; |
| 7138 | return compType; |
| 7139 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7140 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7141 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7142 | if (lex.isInvalid() || rex.isInvalid()) |
| 7143 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7144 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7145 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7146 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7147 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7148 | if (lex.get()->getType()->isArithmeticType() && |
| 7149 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7150 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7151 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7152 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7153 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7154 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7155 | if (lex.get()->getType()->isAnyPointerType()) { |
| 7156 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7157 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7158 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7159 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7160 | bool ComplainAboutVoid = false; |
| 7161 | Expr *ComplainAboutFunc = 0; |
| 7162 | if (lpointee->isVoidType()) { |
| 7163 | if (getLangOptions().CPlusPlus) { |
| 7164 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7165 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7166 | return QualType(); |
| 7167 | } |
| 7168 | |
| 7169 | // GNU C extension: arithmetic on pointer to void |
| 7170 | ComplainAboutVoid = true; |
| 7171 | } else if (lpointee->isFunctionType()) { |
| 7172 | if (getLangOptions().CPlusPlus) { |
| 7173 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7174 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7175 | return QualType(); |
| 7176 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7177 | |
| 7178 | // GNU C extension: arithmetic on pointer to function |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7179 | ComplainAboutFunc = lex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7180 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7181 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7182 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7183 | << lex.get()->getSourceRange() |
| 7184 | << lex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7185 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7186 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7187 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7188 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7189 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7190 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7191 | return QualType(); |
| 7192 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7193 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7194 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7195 | if (rex.get()->getType()->isIntegerType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7196 | if (ComplainAboutVoid) |
| 7197 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7198 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7199 | if (ComplainAboutFunc) |
| 7200 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7201 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7202 | << ComplainAboutFunc->getSourceRange(); |
| 7203 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7204 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 7205 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7206 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7207 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7208 | // Handle pointer-pointer subtractions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7209 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 7210 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7211 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7212 | // RHS must be a completely-type object type. |
| 7213 | // Handle the GNU void* extension. |
| 7214 | if (rpointee->isVoidType()) { |
| 7215 | if (getLangOptions().CPlusPlus) { |
| 7216 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7217 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7218 | return QualType(); |
| 7219 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7220 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7221 | ComplainAboutVoid = true; |
| 7222 | } else if (rpointee->isFunctionType()) { |
| 7223 | if (getLangOptions().CPlusPlus) { |
| 7224 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7225 | << rex.get()->getType() << rex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7226 | return QualType(); |
| 7227 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7228 | |
| 7229 | // GNU extension: arithmetic on pointer to function |
| 7230 | if (!ComplainAboutFunc) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7231 | ComplainAboutFunc = rex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7232 | } else if (!rpointee->isDependentType() && |
| 7233 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7234 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7235 | << rex.get()->getSourceRange() |
| 7236 | << rex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7237 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7238 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7239 | if (getLangOptions().CPlusPlus) { |
| 7240 | // Pointee types must be the same: C++ [expr.add] |
| 7241 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 7242 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7243 | << lex.get()->getType() << rex.get()->getType() |
| 7244 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7245 | return QualType(); |
| 7246 | } |
| 7247 | } else { |
| 7248 | // Pointee types must be compatible C99 6.5.6p3 |
| 7249 | if (!Context.typesAreCompatible( |
| 7250 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 7251 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 7252 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7253 | << lex.get()->getType() << rex.get()->getType() |
| 7254 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7255 | return QualType(); |
| 7256 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7257 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7258 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7259 | if (ComplainAboutVoid) |
| 7260 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7261 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7262 | if (ComplainAboutFunc) |
| 7263 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7264 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7265 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7266 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7267 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7268 | return Context.getPointerDiffType(); |
| 7269 | } |
| 7270 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7271 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7272 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7273 | } |
| 7274 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7275 | static bool isScopedEnumerationType(QualType T) { |
| 7276 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 7277 | return ET->getDecl()->isScoped(); |
| 7278 | return false; |
| 7279 | } |
| 7280 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7281 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7282 | SourceLocation Loc, unsigned Opc, |
| 7283 | QualType LHSTy) { |
| 7284 | llvm::APSInt Right; |
| 7285 | // Check right/shifter operand |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7286 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7287 | return; |
| 7288 | |
| 7289 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7290 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 7291 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7292 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7293 | return; |
| 7294 | } |
| 7295 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7296 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7297 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7298 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 7299 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7300 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7301 | return; |
| 7302 | } |
| 7303 | if (Opc != BO_Shl) |
| 7304 | return; |
| 7305 | |
| 7306 | // When left shifting an ICE which is signed, we can check for overflow which |
| 7307 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 7308 | // integers have defined behavior modulo one more than the maximum value |
| 7309 | // representable in the result type, so never warn for those. |
| 7310 | llvm::APSInt Left; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7311 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7312 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 7313 | return; |
| 7314 | llvm::APInt ResultBits = |
| 7315 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 7316 | if (LeftBits.uge(ResultBits)) |
| 7317 | return; |
| 7318 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 7319 | Result = Result.shl(Right); |
| 7320 | |
| 7321 | // If we are only missing a sign bit, this is less likely to result in actual |
| 7322 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 7323 | // expected value. Thus we place this behind a different warning that can be |
| 7324 | // turned off separately if needed. |
| 7325 | if (LeftBits == ResultBits - 1) { |
| 7326 | S.Diag(Loc, diag::warn_shift_result_overrides_sign_bit) |
| 7327 | << Result.toString(10) << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7328 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7329 | return; |
| 7330 | } |
| 7331 | |
| 7332 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
| 7333 | << Result.toString(10) << Result.getMinSignedBits() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7334 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7335 | } |
| 7336 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7337 | // C99 6.5.7 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7338 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7339 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7340 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7341 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7342 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7343 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7344 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7345 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7346 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7347 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7348 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7349 | return InvalidOperands(Loc, lex, rex); |
| 7350 | } |
| 7351 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7352 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7353 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7354 | return CheckVectorOperands(Loc, lex, rex); |
| 7355 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7356 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7357 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7358 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7359 | // For the LHS, do usual unary conversions, but then reset them away |
| 7360 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7361 | ExprResult old_lex = lex; |
| 7362 | lex = UsualUnaryConversions(lex.take()); |
| 7363 | if (lex.isInvalid()) |
| 7364 | return QualType(); |
| 7365 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7366 | if (isCompAssign) lex = old_lex; |
| 7367 | |
| 7368 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7369 | rex = UsualUnaryConversions(rex.take()); |
| 7370 | if (rex.isInvalid()) |
| 7371 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7372 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7373 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7374 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7375 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7376 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7377 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7378 | } |
| 7379 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7380 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7381 | if (DeclContext *DC = D->getDeclContext()) { |
| 7382 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7383 | return true; |
| 7384 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7385 | return FD->isFunctionTemplateSpecialization(); |
| 7386 | } |
| 7387 | return false; |
| 7388 | } |
| 7389 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7390 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7391 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7392 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7393 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7394 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7395 | // Handle vector comparisons separately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7396 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7397 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7398 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7399 | QualType lType = lex.get()->getType(); |
| 7400 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7401 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7402 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7403 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7404 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7405 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7406 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7407 | |
| 7408 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7409 | // Two different enums will raise a warning when compared. |
| 7410 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7411 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7412 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7413 | RHSEnumType->getDecl()->getIdentifier() && |
| 7414 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7415 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7416 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7417 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | } |
| 7421 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7422 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7423 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7424 | !lex.get()->getLocStart().isMacroID() && |
| 7425 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7426 | // For non-floating point types, check for self-comparisons of the form |
| 7427 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7428 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7429 | // |
| 7430 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7431 | // expansion. Also don't warn about comparisons which are only self |
| 7432 | // comparisons within a template specialization. The warnings should catch |
| 7433 | // obvious cases in the definition of the template anyways. The idea is to |
| 7434 | // warn when the typed comparison operator will always evaluate to the same |
| 7435 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7436 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7437 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7438 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7439 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7440 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7441 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7442 | << (Opc == BO_EQ |
| 7443 | || Opc == BO_LE |
| 7444 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7445 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7446 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7447 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7448 | // what is it always going to eval to? |
| 7449 | char always_evals_to; |
| 7450 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7451 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7452 | always_evals_to = 0; // false |
| 7453 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7454 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7455 | always_evals_to = 1; // true |
| 7456 | break; |
| 7457 | default: |
| 7458 | // best we can say is 'a constant' |
| 7459 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7460 | break; |
| 7461 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7462 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7463 | << 1 // array |
| 7464 | << always_evals_to); |
| 7465 | } |
| 7466 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7467 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7468 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7469 | if (isa<CastExpr>(LHSStripped)) |
| 7470 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7471 | if (isa<CastExpr>(RHSStripped)) |
| 7472 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7473 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7474 | // Warn about comparisons against a string constant (unless the other |
| 7475 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7476 | Expr *literalString = 0; |
| 7477 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7478 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7479 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7480 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7481 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7482 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7483 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7484 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7485 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7486 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7487 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7488 | literalStringStripped = RHSStripped; |
| 7489 | } |
| 7490 | |
| 7491 | if (literalString) { |
| 7492 | std::string resultComparison; |
| 7493 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7494 | case BO_LT: resultComparison = ") < 0"; break; |
| 7495 | case BO_GT: resultComparison = ") > 0"; break; |
| 7496 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7497 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7498 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7499 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7500 | default: assert(false && "Invalid comparison operator"); |
| 7501 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7502 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7503 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7504 | PDiag(diag::warn_stringcompare) |
| 7505 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7506 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7507 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7508 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7509 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7510 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7511 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7512 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7513 | if (lex.isInvalid() || rex.isInvalid()) |
| 7514 | return QualType(); |
| 7515 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7516 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7517 | lex = UsualUnaryConversions(lex.take()); |
| 7518 | if (lex.isInvalid()) |
| 7519 | return QualType(); |
| 7520 | |
| 7521 | rex = UsualUnaryConversions(rex.take()); |
| 7522 | if (rex.isInvalid()) |
| 7523 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7524 | } |
| 7525 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7526 | lType = lex.get()->getType(); |
| 7527 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7528 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7529 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7530 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7531 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7532 | if (isRelational) { |
| 7533 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7534 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7535 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7536 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7537 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7538 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7539 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7540 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7541 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7542 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7543 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7544 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7545 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7546 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7547 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7548 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7549 | // All of the following pointer-related warnings are GCC extensions, except |
| 7550 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7551 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7552 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7553 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7554 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7555 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7556 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7557 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7558 | if (LCanPointeeTy == RCanPointeeTy) |
| 7559 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7560 | if (!isRelational && |
| 7561 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7562 | // Valid unless comparison between non-null pointer and function pointer |
| 7563 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7564 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7565 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7566 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7567 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7568 | Diag(Loc, |
| 7569 | isSFINAEContext()? |
| 7570 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7571 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7572 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7573 | |
| 7574 | if (isSFINAEContext()) |
| 7575 | return QualType(); |
| 7576 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7577 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7578 | return ResultTy; |
| 7579 | } |
| 7580 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7581 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7582 | // C++ [expr.rel]p2: |
| 7583 | // [...] Pointer conversions (4.10) and qualification |
| 7584 | // conversions (4.4) are performed on pointer operands (or on |
| 7585 | // a pointer operand and a null pointer constant) to bring |
| 7586 | // them to their composite pointer type. [...] |
| 7587 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7588 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7589 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7590 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7591 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7592 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7593 | if (T.isNull()) { |
| 7594 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7595 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7596 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7597 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7598 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7599 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7600 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7601 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7602 | } |
| 7603 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7604 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7605 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7606 | return ResultTy; |
| 7607 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7608 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7609 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7610 | RCanPointeeTy.getUnqualifiedType())) { |
| 7611 | // Valid unless a relational comparison of function pointers |
| 7612 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7613 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7614 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7615 | } |
| 7616 | } else if (!isRelational && |
| 7617 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7618 | // Valid unless comparison between non-null pointer and function pointer |
| 7619 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7620 | && !LHSIsNull && !RHSIsNull) { |
| 7621 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7622 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7623 | } |
| 7624 | } else { |
| 7625 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7626 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7627 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7628 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7629 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7630 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7631 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7632 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7633 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7634 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7635 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7637 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7638 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7639 | // Comparison of nullptr_t with itself. |
| 7640 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7641 | return ResultTy; |
| 7642 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7643 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7644 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7645 | if (RHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7646 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7647 | (!isRelational && lType->isMemberPointerType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7648 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7649 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7650 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7651 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7652 | return ResultTy; |
| 7653 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7654 | if (LHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7655 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7656 | (!isRelational && rType->isMemberPointerType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7657 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7658 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7659 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7660 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7661 | return ResultTy; |
| 7662 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7663 | |
| 7664 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7665 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7666 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7667 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7668 | // In addition, pointers to members can be compared, or a pointer to |
| 7669 | // member and a null pointer constant. Pointer to member conversions |
| 7670 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7671 | // them to a common type. If one operand is a null pointer constant, |
| 7672 | // the common type is the type of the other operand. Otherwise, the |
| 7673 | // common type is a pointer to member type similar (4.4) to the type |
| 7674 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7675 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7676 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7677 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7678 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7679 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7680 | if (T.isNull()) { |
| 7681 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7682 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7683 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7684 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7685 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7686 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7687 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7688 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7689 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7690 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7691 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7692 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7693 | return ResultTy; |
| 7694 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7695 | |
| 7696 | // Handle scoped enumeration types specifically, since they don't promote |
| 7697 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7698 | if (lex.get()->getType()->isEnumeralType() && |
| 7699 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7700 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7701 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7702 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7703 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7704 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7705 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7706 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7707 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7708 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7709 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7710 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7711 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7712 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7713 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7714 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7715 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7716 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7717 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7718 | if (!isRelational |
| 7719 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7720 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7721 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7722 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7723 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7724 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7725 | ->getPointeeType()->isVoidType()))) |
| 7726 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7727 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7728 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7729 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7730 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7731 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7732 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7733 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7734 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7735 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7736 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7737 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7738 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7739 | if (LPT || RPT) { |
| 7740 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7741 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7742 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7743 | if (!LPtrToVoid && !RPtrToVoid && |
| 7744 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7745 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7746 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7747 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7748 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7749 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7750 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7751 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7752 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7753 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7754 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7755 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7756 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7757 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7758 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7759 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7760 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7761 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7762 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7763 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7764 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7765 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7766 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7767 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7768 | bool isError = false; |
| 7769 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7770 | (RHSIsNull && rType->isIntegerType())) { |
| 7771 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7772 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7773 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7774 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7775 | else if (getLangOptions().CPlusPlus) { |
| 7776 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7777 | isError = true; |
| 7778 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7779 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7780 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7781 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7782 | Diag(Loc, DiagID) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7783 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7784 | if (isError) |
| 7785 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7786 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7787 | |
| 7788 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7789 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7790 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7791 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7792 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7793 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7794 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7795 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7796 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7797 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7798 | if (!isRelational && RHSIsNull |
| 7799 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7800 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7801 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7802 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7803 | if (!isRelational && LHSIsNull |
| 7804 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7805 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7806 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7807 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7808 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7809 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7810 | } |
| 7811 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7812 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7813 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7814 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7815 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7816 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7817 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7818 | bool isRelational) { |
| 7819 | // Check to make sure we're operating on vectors of the same type and width, |
| 7820 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7821 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7822 | if (vType.isNull()) |
| 7823 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7824 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7825 | QualType lType = lex.get()->getType(); |
| 7826 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7827 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7828 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7829 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7830 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7831 | return Context.getLogicalOperationType(); |
| 7832 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7833 | // For non-floating point types, check for self-comparisons of the form |
| 7834 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7835 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7836 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7837 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7838 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7839 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7840 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7841 | PDiag(diag::warn_comparison_always) |
| 7842 | << 0 // self- |
| 7843 | << 2 // "a constant" |
| 7844 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7845 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7846 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7847 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7848 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7849 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7850 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7851 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7852 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7853 | // Return the type for the comparison, which is the same as vector type for |
| 7854 | // integer vectors, or an integer type of identical size and number of |
| 7855 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7856 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7857 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7858 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7859 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7860 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7861 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7862 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7863 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7864 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7865 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7866 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7867 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7868 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7869 | } |
| 7870 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7871 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7872 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7873 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7874 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7875 | rex.get()->getType()->hasIntegerRepresentation()) |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7876 | return CheckVectorOperands(Loc, lex, rex); |
| 7877 | |
| 7878 | return InvalidOperands(Loc, lex, rex); |
| 7879 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7880 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7881 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 7882 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 7883 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 7884 | return QualType(); |
| 7885 | lex = lexResult.take(); |
| 7886 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7887 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7888 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7889 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7890 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7891 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7892 | } |
| 7893 | |
| 7894 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7895 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7896 | |
| 7897 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7898 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7899 | // is a constant. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7900 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 7901 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7902 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7903 | !Loc.isMacroID()) { |
| 7904 | // If the RHS can be constant folded, and if it constant folds to something |
| 7905 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7906 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7907 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7908 | Expr::EvalResult Result; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7909 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 7910 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 7911 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 7912 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 7913 | << rex.get()->getSourceRange() |
| 7914 | << (Opc == BO_LAnd ? "&&" : "||") |
| 7915 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7916 | } |
| 7917 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7918 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7919 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7920 | lex = UsualUnaryConversions(lex.take()); |
| 7921 | if (lex.isInvalid()) |
| 7922 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7923 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7924 | rex = UsualUnaryConversions(rex.take()); |
| 7925 | if (rex.isInvalid()) |
| 7926 | return QualType(); |
| 7927 | |
| 7928 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7929 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7930 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7931 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7932 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7933 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7934 | // The following is safe because we only use this method for |
| 7935 | // non-overloadable operands. |
| 7936 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7937 | // C++ [expr.log.and]p1 |
| 7938 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7939 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7940 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 7941 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7942 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7943 | lex = move(lexRes); |
| 7944 | |
| 7945 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 7946 | if (rexRes.isInvalid()) |
| 7947 | return InvalidOperands(Loc, lex, rex); |
| 7948 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7949 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7950 | // C++ [expr.log.and]p2 |
| 7951 | // C++ [expr.log.or]p2 |
| 7952 | // The result is a bool. |
| 7953 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7954 | } |
| 7955 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7956 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7957 | /// is a read-only property; return true if so. A readonly property expression |
| 7958 | /// depends on various declarations and thus must be treated specially. |
| 7959 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7960 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7961 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7962 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7963 | if (PropExpr->isImplicitProperty()) return false; |
| 7964 | |
| 7965 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7966 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 7967 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7968 | PropExpr->getBase()->getType(); |
| 7969 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7970 | if (const ObjCObjectPointerType *OPT = |
| 7971 | BaseType->getAsObjCInterfacePointerType()) |
| 7972 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7973 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7974 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7975 | } |
| 7976 | return false; |
| 7977 | } |
| 7978 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7979 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 7980 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7981 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 7982 | if (PropExpr->isImplicitProperty()) return false; |
| 7983 | |
| 7984 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7985 | QualType T = PDecl->getType(); |
| 7986 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 7987 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7988 | CanQualType CT = S.Context.getCanonicalType(T); |
| 7989 | return CT.isConstQualified(); |
| 7990 | } |
| 7991 | return false; |
| 7992 | } |
| 7993 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7994 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 7995 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 7996 | return false; |
| 7997 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 7998 | NamedDecl *Member = ME->getMemberDecl(); |
| 7999 | if (isa<FieldDecl>(Member)) { |
| 8000 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8001 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 8002 | return false; |
| 8003 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 8004 | } |
| 8005 | return false; |
| 8006 | } |
| 8007 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8008 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 8009 | /// emit an error and return true. If so, return false. |
| 8010 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8011 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8012 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8013 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8014 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 8015 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8016 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 8017 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8018 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 8019 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8020 | if (IsLV == Expr::MLV_Valid) |
| 8021 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8022 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8023 | unsigned Diag = 0; |
| 8024 | bool NeedType = false; |
| 8025 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8026 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8027 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8028 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 8029 | NeedType = true; |
| 8030 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8031 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8032 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 8033 | NeedType = true; |
| 8034 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 8035 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8036 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 8037 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8038 | case Expr::MLV_Valid: |
| 8039 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8040 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8041 | case Expr::MLV_MemberFunction: |
| 8042 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8043 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 8044 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8045 | case Expr::MLV_IncompleteType: |
| 8046 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 8047 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8048 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 8049 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8050 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8051 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 8052 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 8053 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8054 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 8055 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 8056 | case Expr::MLV_ReadonlyProperty: |
| 8057 | Diag = diag::error_readonly_property_assignment; |
| 8058 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 8059 | case Expr::MLV_NoSetterProperty: |
| 8060 | Diag = diag::error_nosetter_property_assignment; |
| 8061 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8062 | case Expr::MLV_InvalidMessageExpression: |
| 8063 | Diag = diag::error_readonly_message_assignment; |
| 8064 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 8065 | case Expr::MLV_SubObjCPropertySetting: |
| 8066 | Diag = diag::error_no_subobject_property_setting; |
| 8067 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8068 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 8069 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8070 | SourceRange Assign; |
| 8071 | if (Loc != OrigLoc) |
| 8072 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8073 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8074 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8075 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8076 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8077 | return true; |
| 8078 | } |
| 8079 | |
| 8080 | |
| 8081 | |
| 8082 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8083 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8084 | SourceLocation Loc, |
| 8085 | QualType CompoundType) { |
| 8086 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 8087 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8088 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8089 | |
| 8090 | QualType LHSType = LHS->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8091 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8092 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8093 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8094 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8095 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8096 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 8097 | ExprResult LHSResult = Owned(LHS); |
| 8098 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 8099 | if (LHSResult.isInvalid()) |
| 8100 | return QualType(); |
| 8101 | LHS = LHSResult.take(); |
| 8102 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8103 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8104 | if (RHS.isInvalid()) |
| 8105 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8106 | // Special case of NSObject attributes on c-style pointer types. |
| 8107 | if (ConvTy == IncompatiblePointer && |
| 8108 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8109 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8110 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8111 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8112 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8113 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8114 | if (ConvTy == Compatible && |
| 8115 | getLangOptions().ObjCNonFragileABI && |
| 8116 | LHSType->isObjCObjectType()) |
| 8117 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 8118 | << LHSType; |
| 8119 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8120 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 8121 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 8122 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8123 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8124 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 8125 | RHSCheck = ICE->getSubExpr(); |
| 8126 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8127 | if ((UO->getOpcode() == UO_Plus || |
| 8128 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8129 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8130 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8131 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 8132 | // And there is a space or other character before the subexpr of the |
| 8133 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 8134 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 8135 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8136 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8137 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8138 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8139 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8140 | } |
| 8141 | } else { |
| 8142 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 8143 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8144 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8145 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8146 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8147 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8148 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8149 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 8150 | CheckForNullPointerDereference(*this, LHS); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8151 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8152 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8154 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 8155 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8156 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8157 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 8158 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8159 | // 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] | 8160 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 8161 | return (getLangOptions().CPlusPlus |
| 8162 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8163 | } |
| 8164 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8165 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8166 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8167 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8168 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 8169 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8170 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 8171 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8172 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 8173 | return QualType(); |
| 8174 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8175 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 8176 | // operands, but not unary promotions. |
| 8177 | // 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] | 8178 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8179 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 8180 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8181 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 8182 | if (LHS.isInvalid()) |
| 8183 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8184 | |
| 8185 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8186 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 8187 | if (RHS.isInvalid()) |
| 8188 | return QualType(); |
| 8189 | if (!RHS.get()->getType()->isVoidType()) |
| 8190 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8191 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8192 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8193 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8194 | } |
| 8195 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 8196 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 8197 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8198 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 8199 | ExprValueKind &VK, |
| 8200 | SourceLocation OpLoc, |
| 8201 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8202 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8203 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8204 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8205 | QualType ResType = Op->getType(); |
| 8206 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8207 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8208 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8209 | // Decrement of bool is not allowed. |
| 8210 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8211 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8212 | return QualType(); |
| 8213 | } |
| 8214 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8215 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8216 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8217 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 8218 | } else if (ResType->isAnyPointerType()) { |
| 8219 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8220 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8221 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8222 | if (PointeeTy->isVoidType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8223 | if (S.getLangOptions().CPlusPlus) { |
| 8224 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8225 | << Op->getSourceRange(); |
| 8226 | return QualType(); |
| 8227 | } |
| 8228 | |
| 8229 | // Pointer to void is a GNU extension in C. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8230 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8231 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8232 | if (S.getLangOptions().CPlusPlus) { |
| 8233 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8234 | << Op->getType() << Op->getSourceRange(); |
| 8235 | return QualType(); |
| 8236 | } |
| 8237 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8238 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8239 | << ResType << Op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8240 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 8241 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8242 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 8243 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 8244 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8245 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8246 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 8247 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8248 | << PointeeTy << Op->getSourceRange(); |
| 8249 | return QualType(); |
| 8250 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 8251 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8252 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8253 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8254 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8255 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8256 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8257 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8258 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 8259 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 8260 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 8261 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8262 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8263 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 8264 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8265 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8266 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8267 | // 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] | 8268 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8269 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8270 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8271 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 8272 | // (in C or with postfix), the increment is the unqualified type of the |
| 8273 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8274 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 8275 | VK = VK_LValue; |
| 8276 | return ResType; |
| 8277 | } else { |
| 8278 | VK = VK_RValue; |
| 8279 | return ResType.getUnqualifiedType(); |
| 8280 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8281 | } |
| 8282 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8283 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8284 | assert(E->getValueKind() == VK_LValue && |
| 8285 | E->getObjectKind() == OK_ObjCProperty); |
| 8286 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 8287 | |
| 8288 | ExprValueKind VK = VK_RValue; |
| 8289 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8290 | if (const ObjCMethodDecl *GetterMethod = |
| 8291 | PRE->getImplicitPropertyGetter()) { |
| 8292 | QualType Result = GetterMethod->getResultType(); |
| 8293 | VK = Expr::getValueKindForType(Result); |
| 8294 | } |
| 8295 | else { |
| 8296 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 8297 | << PRE->getBase()->getType(); |
| 8298 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8299 | } |
| 8300 | |
| 8301 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 8302 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 8303 | |
| 8304 | ExprResult Result = MaybeBindToTemporary(E); |
| 8305 | if (!Result.isInvalid()) |
| 8306 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8307 | |
| 8308 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8309 | } |
| 8310 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8311 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 8312 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 8313 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8314 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8315 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8316 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8317 | // If using property-dot syntax notation for assignment, and there is a |
| 8318 | // setter, RHS expression is being passed to the setter argument. So, |
| 8319 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8320 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8321 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8322 | LHSTy = (*P)->getType(); |
| 8323 | |
| 8324 | // Otherwise, if the getter returns an l-value, just call that. |
| 8325 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8326 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8327 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8328 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8329 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8330 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8331 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8332 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8333 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8334 | } |
| 8335 | |
| 8336 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8337 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8338 | InitializedEntity::InitializeParameter(Context, LHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8339 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8340 | if (!ArgE.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8341 | RHS = ArgE; |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8342 | } |
| 8343 | } |
| 8344 | |
| 8345 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8346 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8347 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8348 | /// where the declaration is needed for type checking. We only need to |
| 8349 | /// handle cases when the expression references a function designator |
| 8350 | /// or is an lvalue. Here are some examples: |
| 8351 | /// - &(x) => x |
| 8352 | /// - &*****f => f for f a function designator. |
| 8353 | /// - &s.xx => s |
| 8354 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8355 | /// - *(x + 1) -> x, if x is an array |
| 8356 | /// - &"123"[2] -> 0 |
| 8357 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8358 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8359 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8360 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8361 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8362 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8363 | // If this is an arrow operator, the address is an offset from |
| 8364 | // the base's value, so the object the base refers to is |
| 8365 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8366 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8367 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8368 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8369 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8370 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8371 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8372 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8373 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8374 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8375 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8376 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8377 | } |
| 8378 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8379 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8380 | case Stmt::UnaryOperatorClass: { |
| 8381 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8382 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8383 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8384 | case UO_Real: |
| 8385 | case UO_Imag: |
| 8386 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8387 | return getPrimaryDecl(UO->getSubExpr()); |
| 8388 | default: |
| 8389 | return 0; |
| 8390 | } |
| 8391 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8392 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8393 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8394 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8395 | // If the result of an implicit cast is an l-value, we care about |
| 8396 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8397 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8398 | default: |
| 8399 | return 0; |
| 8400 | } |
| 8401 | } |
| 8402 | |
| 8403 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8404 | /// 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] | 8405 | /// 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] | 8406 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8407 | /// 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] | 8408 | /// 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] | 8409 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8410 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8411 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8412 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8413 | return S.Context.DependentTy; |
| 8414 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8415 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8416 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8417 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8418 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 8419 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8420 | << OrigOp->getSourceRange(); |
| 8421 | return QualType(); |
| 8422 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8423 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8424 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8425 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8426 | // Make sure to ignore parentheses in subsequent checks |
| 8427 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8428 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8429 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8430 | // Implement C99-only parts of addressof rules. |
| 8431 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8432 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8433 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8434 | // (assuming the deref expression is valid). |
| 8435 | return uOp->getSubExpr()->getType(); |
| 8436 | } |
| 8437 | // Technically, there should be a check for array subscript |
| 8438 | // expressions here, but the result of one is always an lvalue anyway. |
| 8439 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8440 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8441 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8442 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8443 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8444 | bool sfinae = S.isSFINAEContext(); |
| 8445 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8446 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8447 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8448 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8449 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8450 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8451 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8452 | } else if (lval == Expr::LV_MemberFunction) { |
| 8453 | // If it's an instance method, make a member pointer. |
| 8454 | // The expression must have exactly the form &A::foo. |
| 8455 | |
| 8456 | // If the underlying expression isn't a decl ref, give up. |
| 8457 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8458 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8459 | << OrigOp->getSourceRange(); |
| 8460 | return QualType(); |
| 8461 | } |
| 8462 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8463 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8464 | |
| 8465 | // The id-expression was parenthesized. |
| 8466 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8467 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8468 | << OrigOp->getSourceRange(); |
| 8469 | |
| 8470 | // The method was named without a qualifier. |
| 8471 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8472 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8473 | << op->getSourceRange(); |
| 8474 | } |
| 8475 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8476 | return S.Context.getMemberPointerType(op->getType(), |
| 8477 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8478 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8479 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8480 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8481 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8482 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8483 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8484 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8485 | return QualType(); |
| 8486 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8487 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8488 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8489 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8490 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8491 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8492 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8493 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8494 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8495 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8496 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8497 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8498 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8499 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8500 | << "property expression" << op->getSourceRange(); |
| 8501 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8502 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8503 | // 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] | 8504 | // with the register storage-class specifier. |
| 8505 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8506 | // in C++ it is not error to take address of a register |
| 8507 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8508 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8509 | !S.getLangOptions().CPlusPlus) { |
| 8510 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8511 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8512 | return QualType(); |
| 8513 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8514 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8515 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8516 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8517 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8518 | // Could be a pointer to member, though, if there is an explicit |
| 8519 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8520 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8521 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8522 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8523 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8524 | S.Diag(OpLoc, |
| 8525 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8526 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8527 | return QualType(); |
| 8528 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8529 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8530 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8531 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8532 | return S.Context.getMemberPointerType(op->getType(), |
| 8533 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8534 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8535 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8536 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8537 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8538 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8539 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8540 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8541 | // Taking the address of a void variable is technically illegal, but we |
| 8542 | // allow it in cases which are otherwise valid. |
| 8543 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8544 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8545 | } |
| 8546 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8547 | // 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] | 8548 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8549 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8550 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8551 | } |
| 8552 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8553 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8554 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8555 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8556 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8557 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8558 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8559 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8560 | if (ConvResult.isInvalid()) |
| 8561 | return QualType(); |
| 8562 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8563 | QualType OpTy = Op->getType(); |
| 8564 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8565 | |
| 8566 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8567 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8568 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8569 | Op->getSourceRange()); |
| 8570 | } |
| 8571 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8572 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8573 | // is an incomplete type or void. It would be possible to warn about |
| 8574 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8575 | // warning is unlikely to catch any mistakes. |
| 8576 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8577 | Result = PT->getPointeeType(); |
| 8578 | else if (const ObjCObjectPointerType *OPT = |
| 8579 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8580 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8581 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8582 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8583 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8584 | if (PR.take() != Op) |
| 8585 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8586 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8587 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8588 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8589 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8590 | << OpTy << Op->getSourceRange(); |
| 8591 | return QualType(); |
| 8592 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8593 | |
| 8594 | // Dereferences are usually l-values... |
| 8595 | VK = VK_LValue; |
| 8596 | |
| 8597 | // ...except that certain expressions are never l-values in C. |
| 8598 | if (!S.getLangOptions().CPlusPlus && |
| 8599 | IsCForbiddenLValueType(S.Context, Result)) |
| 8600 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8601 | |
| 8602 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8603 | } |
| 8604 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8605 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8606 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8607 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8608 | switch (Kind) { |
| 8609 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8610 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8611 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8612 | case tok::star: Opc = BO_Mul; break; |
| 8613 | case tok::slash: Opc = BO_Div; break; |
| 8614 | case tok::percent: Opc = BO_Rem; break; |
| 8615 | case tok::plus: Opc = BO_Add; break; |
| 8616 | case tok::minus: Opc = BO_Sub; break; |
| 8617 | case tok::lessless: Opc = BO_Shl; break; |
| 8618 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8619 | case tok::lessequal: Opc = BO_LE; break; |
| 8620 | case tok::less: Opc = BO_LT; break; |
| 8621 | case tok::greaterequal: Opc = BO_GE; break; |
| 8622 | case tok::greater: Opc = BO_GT; break; |
| 8623 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8624 | case tok::equalequal: Opc = BO_EQ; break; |
| 8625 | case tok::amp: Opc = BO_And; break; |
| 8626 | case tok::caret: Opc = BO_Xor; break; |
| 8627 | case tok::pipe: Opc = BO_Or; break; |
| 8628 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8629 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8630 | case tok::equal: Opc = BO_Assign; break; |
| 8631 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8632 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8633 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8634 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8635 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8636 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8637 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8638 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8639 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8640 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8641 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8642 | } |
| 8643 | return Opc; |
| 8644 | } |
| 8645 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8646 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8647 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8648 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8649 | switch (Kind) { |
| 8650 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8651 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8652 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8653 | case tok::amp: Opc = UO_AddrOf; break; |
| 8654 | case tok::star: Opc = UO_Deref; break; |
| 8655 | case tok::plus: Opc = UO_Plus; break; |
| 8656 | case tok::minus: Opc = UO_Minus; break; |
| 8657 | case tok::tilde: Opc = UO_Not; break; |
| 8658 | case tok::exclaim: Opc = UO_LNot; break; |
| 8659 | case tok::kw___real: Opc = UO_Real; break; |
| 8660 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8661 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8662 | } |
| 8663 | return Opc; |
| 8664 | } |
| 8665 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8666 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8667 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8668 | /// suppressed in the event of macro expansions. |
| 8669 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8670 | SourceLocation OpLoc) { |
| 8671 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8672 | return; |
| 8673 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8674 | return; |
| 8675 | lhs = lhs->IgnoreParenImpCasts(); |
| 8676 | rhs = rhs->IgnoreParenImpCasts(); |
| 8677 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8678 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8679 | if (!LeftDeclRef || !RightDeclRef || |
| 8680 | LeftDeclRef->getLocation().isMacroID() || |
| 8681 | RightDeclRef->getLocation().isMacroID()) |
| 8682 | return; |
| 8683 | const ValueDecl *LeftDecl = |
| 8684 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8685 | const ValueDecl *RightDecl = |
| 8686 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8687 | if (LeftDecl != RightDecl) |
| 8688 | return; |
| 8689 | if (LeftDecl->getType().isVolatileQualified()) |
| 8690 | return; |
| 8691 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8692 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8693 | return; |
| 8694 | |
| 8695 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8696 | << LeftDeclRef->getType() |
| 8697 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8698 | } |
| 8699 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8700 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8701 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8702 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8703 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8704 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8705 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8706 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8707 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8708 | // The following two variables are used for compound assignment operators |
| 8709 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8710 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8711 | ExprValueKind VK = VK_RValue; |
| 8712 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8713 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8714 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8715 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8716 | // But since an assignment can trigger target based overload, exclude it in |
| 8717 | // our blind search. i.e: |
| 8718 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8719 | // f<int> == 0; // resolve f<int> blindly |
| 8720 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8721 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8722 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8723 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8724 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8725 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8726 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8727 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8728 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8729 | } |
| 8730 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8731 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8732 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8733 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8734 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8735 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8736 | VK = lhs.get()->getValueKind(); |
| 8737 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8738 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8739 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8740 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8741 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8742 | case BO_PtrMemD: |
| 8743 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8744 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8745 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8746 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8747 | case BO_Mul: |
| 8748 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8749 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8750 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8751 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8752 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8753 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 8754 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8755 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8756 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 8757 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8758 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8759 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 8760 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8761 | case BO_Shl: |
| 8762 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8763 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8764 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8765 | case BO_LE: |
| 8766 | case BO_LT: |
| 8767 | case BO_GE: |
| 8768 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8769 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8770 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8771 | case BO_EQ: |
| 8772 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8773 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8774 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8775 | case BO_And: |
| 8776 | case BO_Xor: |
| 8777 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8778 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 8779 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8780 | case BO_LAnd: |
| 8781 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8782 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8783 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8784 | case BO_MulAssign: |
| 8785 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8786 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8787 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8788 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8789 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8790 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8791 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8792 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8793 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 8794 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8795 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8796 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8797 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8798 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8799 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8800 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8801 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8802 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8803 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8804 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8805 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8806 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8807 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8808 | case BO_ShlAssign: |
| 8809 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8810 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8811 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8812 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8813 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8814 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8815 | case BO_AndAssign: |
| 8816 | case BO_XorAssign: |
| 8817 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8818 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8819 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8820 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8821 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8822 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8823 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8824 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8825 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 8826 | VK = rhs.get()->getValueKind(); |
| 8827 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8828 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8829 | break; |
| 8830 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8831 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8832 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8833 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8834 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 8835 | ResultTy, VK, OK, OpLoc)); |
| 8836 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8837 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8838 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8839 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8840 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 8841 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8842 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8843 | } |
| 8844 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8845 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8846 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8847 | /// comparison operators have higher precedence. The most typical example of |
| 8848 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8849 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8850 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8851 | typedef BinaryOperator BinOp; |
| 8852 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8853 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8854 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8855 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8856 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8857 | rhsopc = BO->getOpcode(); |
| 8858 | |
| 8859 | // Subs are not binary operators. |
| 8860 | if (lhsopc == -1 && rhsopc == -1) |
| 8861 | return; |
| 8862 | |
| 8863 | // Bitwise operations are sometimes used as eager logical ops. |
| 8864 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8865 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8866 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8867 | return; |
| 8868 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8869 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8870 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8871 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8872 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8873 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8874 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8875 | << BinOp::getOpcodeStr(lhsopc), |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8876 | lhs->getSourceRange(), |
| 8877 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 8878 | << BinOp::getOpcodeStr(Opc), |
| 8879 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8880 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8881 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8882 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8883 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8884 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8885 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8886 | << BinOp::getOpcodeStr(rhsopc), |
| 8887 | rhs->getSourceRange(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8888 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8889 | << BinOp::getOpcodeStr(Opc), |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8890 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8891 | } |
| 8892 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8893 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8894 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8895 | /// in parentheses. |
| 8896 | static void |
| 8897 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8898 | BinaryOperator *Bop) { |
| 8899 | assert(Bop->getOpcode() == BO_LAnd); |
| 8900 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8901 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8902 | << Bop->getSourceRange() << OpLoc, |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8903 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8904 | Bop->getSourceRange(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8905 | Self.PDiag(0), SourceRange()); |
| 8906 | } |
| 8907 | |
| 8908 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8909 | /// 'true'. |
| 8910 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8911 | bool Res; |
| 8912 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8913 | } |
| 8914 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8915 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8916 | /// 'false'. |
| 8917 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8918 | bool Res; |
| 8919 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8920 | } |
| 8921 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8922 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8923 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8924 | Expr *OrLHS, Expr *OrRHS) { |
| 8925 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8926 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8927 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 8928 | if (EvaluatesAsFalse(S, OrRHS)) |
| 8929 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8930 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8931 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8932 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8933 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8934 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8935 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8936 | // "a || b && 1", but warn now. |
| 8937 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8938 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8939 | } |
| 8940 | } |
| 8941 | } |
| 8942 | } |
| 8943 | |
| 8944 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8945 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8946 | Expr *OrLHS, Expr *OrRHS) { |
| 8947 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8948 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8949 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 8950 | if (EvaluatesAsFalse(S, OrLHS)) |
| 8951 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8952 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8953 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8954 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8955 | } |
| 8956 | } |
| 8957 | } |
| 8958 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8959 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8960 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8961 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8962 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8963 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8964 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8965 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 8966 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8967 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8968 | // 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] | 8969 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8970 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 8971 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8972 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8973 | } |
| 8974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8975 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8976 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8977 | tok::TokenKind Kind, |
| 8978 | Expr *lhs, Expr *rhs) { |
| 8979 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8980 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8981 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8982 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8983 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8984 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8985 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8986 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8987 | } |
| 8988 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8989 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8990 | BinaryOperatorKind Opc, |
| 8991 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8992 | if (getLangOptions().CPlusPlus) { |
| 8993 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8994 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8995 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8996 | UseBuiltinOperator = false; |
| 8997 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8998 | UseBuiltinOperator = true; |
| 8999 | } else { |
| 9000 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 9001 | !rhs->getType()->isOverloadableType(); |
| 9002 | } |
| 9003 | |
| 9004 | if (!UseBuiltinOperator) { |
| 9005 | // Find all of the overloaded operators visible from this |
| 9006 | // point. We perform both an operator-name lookup from the local |
| 9007 | // scope and an argument-dependent lookup based on the types of |
| 9008 | // the arguments. |
| 9009 | UnresolvedSet<16> Functions; |
| 9010 | OverloadedOperatorKind OverOp |
| 9011 | = BinaryOperator::getOverloadedOperator(Opc); |
| 9012 | if (S && OverOp != OO_None) |
| 9013 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 9014 | Functions); |
| 9015 | |
| 9016 | // Build the (potentially-overloaded, potentially-dependent) |
| 9017 | // binary operation. |
| 9018 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 9019 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9020 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9021 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9022 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9023 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9024 | } |
| 9025 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9026 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 9027 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9028 | Expr *InputExpr) { |
| 9029 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9030 | ExprValueKind VK = VK_RValue; |
| 9031 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9032 | QualType resultType; |
| 9033 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9034 | case UO_PreInc: |
| 9035 | case UO_PreDec: |
| 9036 | case UO_PostInc: |
| 9037 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9038 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9039 | Opc == UO_PreInc || |
| 9040 | Opc == UO_PostInc, |
| 9041 | Opc == UO_PreInc || |
| 9042 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9043 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9044 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9045 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9046 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9047 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9048 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9049 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9050 | Input = move(resolved); |
| 9051 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9052 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9053 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9054 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9055 | case UO_Plus: |
| 9056 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9057 | Input = UsualUnaryConversions(Input.take()); |
| 9058 | if (Input.isInvalid()) return ExprError(); |
| 9059 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9060 | if (resultType->isDependentType()) |
| 9061 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 9062 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 9063 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9064 | break; |
| 9065 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 9066 | resultType->isEnumeralType()) |
| 9067 | break; |
| 9068 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9069 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9070 | resultType->isPointerType()) |
| 9071 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9072 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9073 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9074 | if (Input.isInvalid()) return ExprError(); |
| 9075 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9076 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9077 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9078 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9079 | << resultType << Input.get()->getSourceRange()); |
| 9080 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9081 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9082 | Input = UsualUnaryConversions(Input.take()); |
| 9083 | if (Input.isInvalid()) return ExprError(); |
| 9084 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9085 | if (resultType->isDependentType()) |
| 9086 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 9087 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 9088 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 9089 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 9090 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9091 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9092 | else if (resultType->hasIntegerRepresentation()) |
| 9093 | break; |
| 9094 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9095 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9096 | if (Input.isInvalid()) return ExprError(); |
| 9097 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9098 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9099 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9100 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9101 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9102 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9103 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9104 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9105 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9106 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9107 | if (Input.isInvalid()) return ExprError(); |
| 9108 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9109 | if (resultType->isDependentType()) |
| 9110 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9111 | if (resultType->isScalarType()) { |
| 9112 | // C99 6.5.3.3p1: ok, fallthrough; |
| 9113 | if (Context.getLangOptions().CPlusPlus) { |
| 9114 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 9115 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9116 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 9117 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9118 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9119 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9120 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9121 | if (Input.isInvalid()) return ExprError(); |
| 9122 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9123 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9124 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9125 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9126 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 9127 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9128 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9129 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 9130 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9131 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9132 | case UO_Real: |
| 9133 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9134 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9135 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9136 | if (Input.isInvalid()) return ExprError(); |
| 9137 | if (Input.get()->getValueKind() != VK_RValue && |
| 9138 | Input.get()->getObjectKind() == OK_Ordinary) |
| 9139 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 9140 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9141 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9142 | resultType = Input.get()->getType(); |
| 9143 | VK = Input.get()->getValueKind(); |
| 9144 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9145 | break; |
| 9146 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9147 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9148 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9149 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9150 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9151 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9152 | } |
| 9153 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9154 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9155 | UnaryOperatorKind Opc, |
| 9156 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 9157 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 9158 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9159 | // Find all of the overloaded operators visible from this |
| 9160 | // point. We perform both an operator-name lookup from the local |
| 9161 | // scope and an argument-dependent lookup based on the types of |
| 9162 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9163 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9164 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9165 | if (S && OverOp != OO_None) |
| 9166 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9167 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9168 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9169 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9170 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9171 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9172 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9173 | } |
| 9174 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9175 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9176 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9177 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9178 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9179 | } |
| 9180 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9181 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9182 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9183 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9184 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9185 | // 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] | 9186 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9187 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 9188 | } |
| 9189 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9190 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9191 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9192 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9193 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9194 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9195 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9196 | bool isFileScope |
| 9197 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9198 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9199 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9200 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9201 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9202 | // example, it is not possible to goto into a stmt expression apparently. |
| 9203 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9204 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9205 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9206 | // as the type of the stmtexpr. |
| 9207 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9208 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9209 | if (!Compound->body_empty()) { |
| 9210 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9211 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9212 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9213 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9214 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9215 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9216 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9217 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9218 | // Do function/array conversion on the last expression, but not |
| 9219 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9220 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9221 | if (LastExpr.isInvalid()) |
| 9222 | return ExprError(); |
| 9223 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9224 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9225 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
| 9226 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9227 | InitializedEntity::InitializeResult(LPLoc, |
| 9228 | Ty, |
| 9229 | false), |
| 9230 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9231 | LastExpr); |
| 9232 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9233 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9234 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9235 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9236 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9237 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9238 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9239 | StmtExprMayBindToTemp = true; |
| 9240 | } |
| 9241 | } |
| 9242 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9243 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9244 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9245 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9246 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9247 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9248 | if (StmtExprMayBindToTemp) |
| 9249 | return MaybeBindToTemporary(ResStmtExpr); |
| 9250 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9251 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9252 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9253 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9254 | TypeSourceInfo *TInfo, |
| 9255 | OffsetOfComponent *CompPtr, |
| 9256 | unsigned NumComponents, |
| 9257 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9258 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9259 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9260 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9261 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9262 | // We must have at least one component that refers to the type, and the first |
| 9263 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9264 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9265 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9266 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9267 | << ArgTy << TypeRange); |
| 9268 | |
| 9269 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9270 | // with an incomplete type would be ill-formed. |
| 9271 | if (!Dependent |
| 9272 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 9273 | PDiag(diag::err_offsetof_incomplete_type) |
| 9274 | << TypeRange)) |
| 9275 | return ExprError(); |
| 9276 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9277 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9278 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9279 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9280 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9281 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9282 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9283 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9284 | |
| 9285 | bool DidWarnAboutNonPOD = false; |
| 9286 | QualType CurrentType = ArgTy; |
| 9287 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9288 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9289 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9290 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9291 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9292 | if (OC.isBrackets) { |
| 9293 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9294 | if (!CurrentType->isDependentType()) { |
| 9295 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9296 | if(!AT) |
| 9297 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9298 | << CurrentType); |
| 9299 | CurrentType = AT->getElementType(); |
| 9300 | } else |
| 9301 | CurrentType = Context.DependentTy; |
| 9302 | |
| 9303 | // The expression must be an integral expression. |
| 9304 | // FIXME: An integral constant expression? |
| 9305 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9306 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9307 | !Idx->getType()->isIntegerType()) |
| 9308 | return ExprError(Diag(Idx->getLocStart(), |
| 9309 | diag::err_typecheck_subscript_not_integer) |
| 9310 | << Idx->getSourceRange()); |
| 9311 | |
| 9312 | // Record this array index. |
| 9313 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9314 | Exprs.push_back(Idx); |
| 9315 | continue; |
| 9316 | } |
| 9317 | |
| 9318 | // Offset of a field. |
| 9319 | if (CurrentType->isDependentType()) { |
| 9320 | // We have the offset of a field, but we can't look into the dependent |
| 9321 | // type. Just record the identifier of the field. |
| 9322 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9323 | CurrentType = Context.DependentTy; |
| 9324 | continue; |
| 9325 | } |
| 9326 | |
| 9327 | // We need to have a complete type to look into. |
| 9328 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9329 | diag::err_offsetof_incomplete_type)) |
| 9330 | return ExprError(); |
| 9331 | |
| 9332 | // Look for the designated field. |
| 9333 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9334 | if (!RC) |
| 9335 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9336 | << CurrentType); |
| 9337 | RecordDecl *RD = RC->getDecl(); |
| 9338 | |
| 9339 | // C++ [lib.support.types]p5: |
| 9340 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9341 | // International Standard. type shall be a POD structure or a POD union |
| 9342 | // (clause 9). |
| 9343 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9344 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9345 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9346 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9347 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9348 | << CurrentType)) |
| 9349 | DidWarnAboutNonPOD = true; |
| 9350 | } |
| 9351 | |
| 9352 | // Look for the field. |
| 9353 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9354 | LookupQualifiedName(R, RD); |
| 9355 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9356 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9357 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9358 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9359 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9360 | } |
| 9361 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9362 | if (!MemberDecl) |
| 9363 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9364 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9365 | OC.LocEnd)); |
| 9366 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9367 | // C99 7.17p3: |
| 9368 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9369 | // |
| 9370 | // We diagnose this as an error. |
| 9371 | if (MemberDecl->getBitWidth()) { |
| 9372 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9373 | << MemberDecl->getDeclName() |
| 9374 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9375 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9376 | return ExprError(); |
| 9377 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9378 | |
| 9379 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9380 | if (IndirectMemberDecl) |
| 9381 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9382 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9383 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9384 | // the base class indirections. |
| 9385 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9386 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9387 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9388 | CXXBasePath &Path = Paths.front(); |
| 9389 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9390 | B != BEnd; ++B) |
| 9391 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9392 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9393 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9394 | if (IndirectMemberDecl) { |
| 9395 | for (IndirectFieldDecl::chain_iterator FI = |
| 9396 | IndirectMemberDecl->chain_begin(), |
| 9397 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9398 | assert(isa<FieldDecl>(*FI)); |
| 9399 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9400 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9401 | } |
| 9402 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9403 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9404 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9405 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9406 | } |
| 9407 | |
| 9408 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9409 | TInfo, Comps.data(), Comps.size(), |
| 9410 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9411 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9412 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9413 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9414 | SourceLocation BuiltinLoc, |
| 9415 | SourceLocation TypeLoc, |
| 9416 | ParsedType argty, |
| 9417 | OffsetOfComponent *CompPtr, |
| 9418 | unsigned NumComponents, |
| 9419 | SourceLocation RPLoc) { |
| 9420 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9421 | TypeSourceInfo *ArgTInfo; |
| 9422 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9423 | if (ArgTy.isNull()) |
| 9424 | return ExprError(); |
| 9425 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9426 | if (!ArgTInfo) |
| 9427 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9428 | |
| 9429 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9430 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9431 | } |
| 9432 | |
| 9433 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9434 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9435 | Expr *CondExpr, |
| 9436 | Expr *LHSExpr, Expr *RHSExpr, |
| 9437 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9438 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9439 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9440 | ExprValueKind VK = VK_RValue; |
| 9441 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9442 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9443 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9444 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9445 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9446 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9447 | } else { |
| 9448 | // The conditional expression is required to be a constant expression. |
| 9449 | llvm::APSInt condEval(32); |
| 9450 | SourceLocation ExpLoc; |
| 9451 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9452 | return ExprError(Diag(ExpLoc, |
| 9453 | diag::err_typecheck_choose_expr_requires_constant) |
| 9454 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9455 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9456 | // 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] | 9457 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9458 | |
| 9459 | resType = ActiveExpr->getType(); |
| 9460 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9461 | VK = ActiveExpr->getValueKind(); |
| 9462 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9463 | } |
| 9464 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9465 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9466 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9467 | resType->isDependentType(), |
| 9468 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9469 | } |
| 9470 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9471 | //===----------------------------------------------------------------------===// |
| 9472 | // Clang Extensions. |
| 9473 | //===----------------------------------------------------------------------===// |
| 9474 | |
| 9475 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9476 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9477 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9478 | PushBlockScope(BlockScope, Block); |
| 9479 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9480 | if (BlockScope) |
| 9481 | PushDeclContext(BlockScope, Block); |
| 9482 | else |
| 9483 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9484 | } |
| 9485 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9486 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9487 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9488 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9489 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9490 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9491 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9492 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9493 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9494 | // GetTypeForDeclarator always produces a function type for a block |
| 9495 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9496 | // unless the function was written with a typedef. |
| 9497 | assert(T->isFunctionType() && |
| 9498 | "GetTypeForDeclarator made a non-function block signature"); |
| 9499 | |
| 9500 | // Look for an explicit signature in that function type. |
| 9501 | FunctionProtoTypeLoc ExplicitSignature; |
| 9502 | |
| 9503 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9504 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9505 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9506 | |
| 9507 | // Check whether that explicit signature was synthesized by |
| 9508 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9509 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9510 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9511 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9512 | // This would be much cheaper if we stored TypeLocs instead of |
| 9513 | // TypeSourceInfos. |
| 9514 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9515 | unsigned Size = Result.getFullDataSize(); |
| 9516 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9517 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9518 | |
| 9519 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9520 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9521 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9522 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9523 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9524 | CurBlock->FunctionType = T; |
| 9525 | |
| 9526 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9527 | QualType RetTy = Fn->getResultType(); |
| 9528 | bool isVariadic = |
| 9529 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9530 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9531 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9532 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9533 | // Don't allow returning a objc interface by value. |
| 9534 | if (RetTy->isObjCObjectType()) { |
| 9535 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9536 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9537 | return; |
| 9538 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9539 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9540 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9541 | // return type. TODO: what should we do with declarators like: |
| 9542 | // ^ * { ... } |
| 9543 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9544 | if (RetTy != Context.DependentTy) |
| 9545 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9546 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9547 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9548 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9549 | if (ExplicitSignature) { |
| 9550 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9551 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9552 | if (Param->getIdentifier() == 0 && |
| 9553 | !Param->isImplicit() && |
| 9554 | !Param->isInvalidDecl() && |
| 9555 | !getLangOptions().CPlusPlus) |
| 9556 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9557 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9558 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9559 | |
| 9560 | // Fake up parameter variables if we have a typedef, like |
| 9561 | // ^ fntype { ... } |
| 9562 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9563 | for (FunctionProtoType::arg_type_iterator |
| 9564 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9565 | ParmVarDecl *Param = |
| 9566 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9567 | ParamInfo.getSourceRange().getBegin(), |
| 9568 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9569 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9570 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9571 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9572 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9573 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9574 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9575 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9576 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9577 | CurBlock->TheDecl->param_end(), |
| 9578 | /*CheckParameterNames=*/false); |
| 9579 | } |
| 9580 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9581 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9582 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9583 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9584 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9585 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9586 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9587 | // FIXME: remove the attribute. |
| 9588 | } |
| 9589 | |
| 9590 | // Put the parameter variables in scope. We can bail out immediately |
| 9591 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9592 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9593 | return; |
| 9594 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9595 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9596 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9597 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9598 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9599 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9600 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9601 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9602 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9603 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9604 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9605 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9606 | } |
| 9607 | |
| 9608 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9609 | /// is invoked to pop the information about the block from the action impl. |
| 9610 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9611 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9612 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9613 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
| 9616 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9617 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9618 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9619 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9620 | // If blocks are disabled, emit an error. |
| 9621 | if (!LangOpts.Blocks) |
| 9622 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9623 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9624 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9625 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9626 | PopDeclContext(); |
| 9627 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9628 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9629 | if (!BSI->ReturnType.isNull()) |
| 9630 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9631 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9632 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9633 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9634 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9635 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9636 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9637 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9638 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9639 | // If the user wrote a function type in some form, try to use that. |
| 9640 | if (!BSI->FunctionType.isNull()) { |
| 9641 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9642 | |
| 9643 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9644 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9645 | |
| 9646 | // Turn protoless block types into nullary block types. |
| 9647 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9648 | FunctionProtoType::ExtProtoInfo EPI; |
| 9649 | EPI.ExtInfo = Ext; |
| 9650 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9651 | |
| 9652 | // Otherwise, if we don't need to change anything about the function type, |
| 9653 | // preserve its sugar structure. |
| 9654 | } else if (FTy->getResultType() == RetTy && |
| 9655 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9656 | BlockTy = BSI->FunctionType; |
| 9657 | |
| 9658 | // Otherwise, make the minimal modifications to the function type. |
| 9659 | } else { |
| 9660 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9661 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9662 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9663 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9664 | BlockTy = Context.getFunctionType(RetTy, |
| 9665 | FPT->arg_type_begin(), |
| 9666 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9667 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9668 | } |
| 9669 | |
| 9670 | // If we don't have a function type, just build one from nothing. |
| 9671 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9672 | FunctionProtoType::ExtProtoInfo EPI; |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 9673 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, false, 0, CC_Default); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9674 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9675 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9676 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9677 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9678 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9679 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9680 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9681 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9682 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9683 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9684 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9685 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9686 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9687 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9688 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9689 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9690 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9691 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9692 | } |
| 9693 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9694 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9695 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9696 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9697 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9698 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9699 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9700 | } |
| 9701 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9702 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9703 | Expr *E, TypeSourceInfo *TInfo, |
| 9704 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9705 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9706 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9707 | // Get the va_list type |
| 9708 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9709 | if (VaListType->isArrayType()) { |
| 9710 | // Deal with implicit array decay; for example, on x86-64, |
| 9711 | // va_list is an array, but it's supposed to decay to |
| 9712 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9713 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9714 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9715 | ExprResult Result = UsualUnaryConversions(E); |
| 9716 | if (Result.isInvalid()) |
| 9717 | return ExprError(); |
| 9718 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9719 | } else { |
| 9720 | // Otherwise, the va_list argument must be an l-value because |
| 9721 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9722 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9723 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9724 | return ExprError(); |
| 9725 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9726 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9727 | if (!E->isTypeDependent() && |
| 9728 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9729 | return ExprError(Diag(E->getLocStart(), |
| 9730 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9731 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9732 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9733 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9734 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9735 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9736 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9737 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9738 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9739 | } |
| 9740 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9741 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9742 | // The type of __null will be int or long, depending on the size of |
| 9743 | // pointers on the target. |
| 9744 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9745 | unsigned pw = Context.Target.getPointerWidth(0); |
| 9746 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9747 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9748 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9749 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9750 | else if (pw == Context.Target.getLongLongWidth()) |
| 9751 | Ty = Context.LongLongTy; |
| 9752 | else { |
| 9753 | assert(!"I don't know size of pointer!"); |
| 9754 | Ty = Context.IntTy; |
| 9755 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9756 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9757 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9760 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9761 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9762 | if (!SemaRef.getLangOptions().ObjC1) |
| 9763 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9764 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9765 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9766 | if (!PT) |
| 9767 | return; |
| 9768 | |
| 9769 | // Check if the destination is of type 'id'. |
| 9770 | if (!PT->isObjCIdType()) { |
| 9771 | // Check if the destination is the 'NSString' interface. |
| 9772 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9773 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9774 | return; |
| 9775 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9776 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9777 | // Strip off any parens and casts. |
| 9778 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 9779 | if (!SL || SL->isWide()) |
| 9780 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9781 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9782 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9783 | } |
| 9784 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9785 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9786 | SourceLocation Loc, |
| 9787 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9788 | Expr *SrcExpr, AssignmentAction Action, |
| 9789 | bool *Complained) { |
| 9790 | if (Complained) |
| 9791 | *Complained = false; |
| 9792 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9793 | // Decode the result (notice that AST's are still created for extensions). |
| 9794 | bool isInvalid = false; |
| 9795 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9796 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9797 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9798 | switch (ConvTy) { |
| 9799 | default: assert(0 && "Unknown conversion type"); |
| 9800 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9801 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9802 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9803 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9804 | case IntToPointer: |
| 9805 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9806 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9807 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9808 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9809 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 9810 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9811 | case IncompatiblePointerSign: |
| 9812 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9813 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9814 | case FunctionVoidPointer: |
| 9815 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9816 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9817 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9818 | // Perform array-to-pointer decay if necessary. |
| 9819 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9820 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9821 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9822 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9823 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9824 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9825 | break; |
| 9826 | } |
| 9827 | |
| 9828 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9829 | // fallthrough |
| 9830 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9831 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9832 | // If the qualifiers lost were because we were applying the |
| 9833 | // (deprecated) C++ conversion from a string literal to a char* |
| 9834 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9835 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9836 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9837 | // bit of refactoring (so that the second argument is an |
| 9838 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9839 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9840 | // C++ semantics. |
| 9841 | if (getLangOptions().CPlusPlus && |
| 9842 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9843 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9844 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9845 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9846 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9847 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9848 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9849 | case IntToBlockPointer: |
| 9850 | DiagKind = diag::err_int_to_block_pointer; |
| 9851 | break; |
| 9852 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9853 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9854 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9855 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9856 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9857 | // it can give a more specific diagnostic. |
| 9858 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9859 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9860 | case IncompatibleVectors: |
| 9861 | DiagKind = diag::warn_incompatible_vectors; |
| 9862 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9863 | case Incompatible: |
| 9864 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9865 | isInvalid = true; |
| 9866 | break; |
| 9867 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9868 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9869 | QualType FirstType, SecondType; |
| 9870 | switch (Action) { |
| 9871 | case AA_Assigning: |
| 9872 | case AA_Initializing: |
| 9873 | // The destination type comes first. |
| 9874 | FirstType = DstType; |
| 9875 | SecondType = SrcType; |
| 9876 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9877 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9878 | case AA_Returning: |
| 9879 | case AA_Passing: |
| 9880 | case AA_Converting: |
| 9881 | case AA_Sending: |
| 9882 | case AA_Casting: |
| 9883 | // The source type comes first. |
| 9884 | FirstType = SrcType; |
| 9885 | SecondType = DstType; |
| 9886 | break; |
| 9887 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9888 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9889 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9890 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9891 | if (Complained) |
| 9892 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9893 | return isInvalid; |
| 9894 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9895 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9896 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9897 | llvm::APSInt ICEResult; |
| 9898 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9899 | if (Result) |
| 9900 | *Result = ICEResult; |
| 9901 | return false; |
| 9902 | } |
| 9903 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9904 | Expr::EvalResult EvalResult; |
| 9905 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9906 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9907 | EvalResult.HasSideEffects) { |
| 9908 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9909 | |
| 9910 | if (EvalResult.Diag) { |
| 9911 | // We only show the note if it's not the usual "invalid subexpression" |
| 9912 | // or if it's actually in a subexpression. |
| 9913 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9914 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9915 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9916 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9917 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9918 | return true; |
| 9919 | } |
| 9920 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9921 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9922 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9923 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9924 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9925 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9926 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9927 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9928 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9929 | if (Result) |
| 9930 | *Result = EvalResult.Val.getInt(); |
| 9931 | return false; |
| 9932 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9933 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9934 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9935 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9936 | ExprEvalContexts.push_back( |
| 9937 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9938 | } |
| 9939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9940 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9941 | Sema::PopExpressionEvaluationContext() { |
| 9942 | // Pop the current expression evaluation context off the stack. |
| 9943 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9944 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9945 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9946 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9947 | if (Rec.PotentiallyReferenced) { |
| 9948 | // Mark any remaining declarations in the current position of the stack |
| 9949 | // as "referenced". If they were not meant to be referenced, semantic |
| 9950 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9951 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9952 | I = Rec.PotentiallyReferenced->begin(), |
| 9953 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9954 | I != IEnd; ++I) |
| 9955 | MarkDeclarationReferenced(I->first, I->second); |
| 9956 | } |
| 9957 | |
| 9958 | if (Rec.PotentiallyDiagnosed) { |
| 9959 | // Emit any pending diagnostics. |
| 9960 | for (PotentiallyEmittedDiagnostics::iterator |
| 9961 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9962 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9963 | I != IEnd; ++I) |
| 9964 | Diag(I->first, I->second); |
| 9965 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9966 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9967 | |
| 9968 | // When are coming out of an unevaluated context, clear out any |
| 9969 | // temporaries that we may have created as part of the evaluation of |
| 9970 | // the expression in that context: they aren't relevant because they |
| 9971 | // will never be constructed. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9972 | if (Rec.Context == Unevaluated && |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9973 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 9974 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9975 | ExprTemporaries.end()); |
| 9976 | |
| 9977 | // Destroy the popped expression evaluation record. |
| 9978 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9979 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9980 | |
| 9981 | /// \brief Note that the given declaration was referenced in the source code. |
| 9982 | /// |
| 9983 | /// This routine should be invoke whenever a given declaration is referenced |
| 9984 | /// in the source code, and where that reference occurred. If this declaration |
| 9985 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9986 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9987 | /// |
| 9988 | /// \param Loc the location where the declaration was referenced. |
| 9989 | /// |
| 9990 | /// \param D the declaration that has been referenced by the source code. |
| 9991 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9992 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9993 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9994 | D->setReferenced(); |
| 9995 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9996 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9997 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9998 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 9999 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 10000 | // template or not. The reason for this is that unevaluated expressions |
| 10001 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 10002 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10003 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10004 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 10005 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10006 | return; |
| 10007 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10008 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10009 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 10010 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10011 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10012 | // Do not mark anything as "used" within a dependent context; wait for |
| 10013 | // an instantiation. |
| 10014 | if (CurContext->isDependentContext()) |
| 10015 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10016 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10017 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10018 | case Unevaluated: |
| 10019 | // We are in an expression that is not potentially evaluated; do nothing. |
| 10020 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10021 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10022 | case PotentiallyEvaluated: |
| 10023 | // We are in a potentially-evaluated expression, so this declaration is |
| 10024 | // "used"; handle this below. |
| 10025 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10026 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10027 | case PotentiallyPotentiallyEvaluated: |
| 10028 | // We are in an expression that may be potentially evaluated; queue this |
| 10029 | // declaration reference until we know whether the expression is |
| 10030 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10031 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10032 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10033 | |
| 10034 | case PotentiallyEvaluatedIfUsed: |
| 10035 | // Referenced declarations will only be used if the construct in the |
| 10036 | // containing expression is used. |
| 10037 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10038 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10039 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10040 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 10041 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 10042 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 10043 | if (Constructor->isTrivial()) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 10044 | return; |
| 10045 | if (!Constructor->isUsed(false)) |
| 10046 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Sean Hunt | 509f048 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 10047 | } else if (Constructor->isDefaulted() && |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10048 | Constructor->isCopyConstructor()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10049 | if (!Constructor->isUsed(false)) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10050 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10051 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10052 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10053 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10054 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10055 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10056 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10057 | if (Destructor->isVirtual()) |
| 10058 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10059 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10060 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10061 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10062 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 10063 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10064 | } else if (MethodDecl->isVirtual()) |
| 10065 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10066 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 10067 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10068 | // Recursive functions should be marked when used from another function. |
| 10069 | if (CurContext == Function) return; |
| 10070 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10071 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 10072 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 10073 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10074 | bool AlreadyInstantiated = false; |
| 10075 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10076 | = Function->getTemplateSpecializationInfo()) { |
| 10077 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10078 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10079 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10080 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10081 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10082 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10083 | = Function->getMemberSpecializationInfo()) { |
| 10084 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 10085 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10086 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10087 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10088 | AlreadyInstantiated = true; |
| 10089 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10090 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10091 | if (!AlreadyInstantiated) { |
| 10092 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 10093 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 10094 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 10095 | Loc)); |
| 10096 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10097 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10098 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10099 | } else { |
| 10100 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10101 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 10102 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 10103 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10104 | MarkDeclarationReferenced(Loc, *i); |
| 10105 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10106 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10107 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10108 | // Keep track of used but undefined functions. |
| 10109 | if (!Function->isPure() && !Function->hasBody() && |
| 10110 | Function->getLinkage() != ExternalLinkage) { |
| 10111 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 10112 | if (old.isInvalid()) old = Loc; |
| 10113 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 10114 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10115 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10116 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10118 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10119 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10120 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10121 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10122 | Var->getInstantiatedFromStaticDataMember()) { |
| 10123 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10124 | assert(MSInfo && "Missing member specialization information?"); |
| 10125 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 10126 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 10127 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 10128 | // This is a modification of an existing AST node. Notify listeners. |
| 10129 | if (ASTMutationListener *L = getASTMutationListener()) |
| 10130 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10131 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10132 | } |
| 10133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10134 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10135 | // Keep track of used but undefined variables. We make a hole in |
| 10136 | // the warning for static const data members with in-line |
| 10137 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10138 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10139 | && Var->getLinkage() != ExternalLinkage |
| 10140 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10141 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 10142 | if (old.isInvalid()) old = Loc; |
| 10143 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10144 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10145 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10146 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10147 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10148 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10149 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10150 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10151 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10152 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10153 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10154 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10155 | Sema &S; |
| 10156 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10157 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10158 | public: |
| 10159 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10160 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10161 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10162 | |
| 10163 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10164 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10165 | }; |
| 10166 | } |
| 10167 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10168 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10169 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10170 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 10171 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 10172 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10173 | |
| 10174 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10175 | } |
| 10176 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10177 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10178 | if (ClassTemplateSpecializationDecl *Spec |
| 10179 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10180 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10181 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10182 | } |
| 10183 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10184 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10185 | } |
| 10186 | |
| 10187 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10188 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10189 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10190 | } |
| 10191 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10192 | namespace { |
| 10193 | /// \brief Helper class that marks all of the declarations referenced by |
| 10194 | /// potentially-evaluated subexpressions as "referenced". |
| 10195 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10196 | Sema &S; |
| 10197 | |
| 10198 | public: |
| 10199 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10200 | |
| 10201 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 10202 | |
| 10203 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 10204 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10205 | } |
| 10206 | |
| 10207 | void VisitMemberExpr(MemberExpr *E) { |
| 10208 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10209 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10210 | } |
| 10211 | |
| 10212 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 10213 | if (E->getConstructor()) |
| 10214 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 10215 | if (E->getOperatorNew()) |
| 10216 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 10217 | if (E->getOperatorDelete()) |
| 10218 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10219 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10220 | } |
| 10221 | |
| 10222 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10223 | if (E->getOperatorDelete()) |
| 10224 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10225 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10226 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10227 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 10228 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 10229 | S.LookupDestructor(Record)); |
| 10230 | } |
| 10231 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10232 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10233 | } |
| 10234 | |
| 10235 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 10236 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10237 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10238 | } |
| 10239 | |
| 10240 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 10241 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10242 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10243 | |
| 10244 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10245 | Visit(E->getExpr()); |
| 10246 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10247 | }; |
| 10248 | } |
| 10249 | |
| 10250 | /// \brief Mark any declarations that appear within this expression or any |
| 10251 | /// potentially-evaluated subexpressions as "referenced". |
| 10252 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 10253 | EvaluatedExprMarker(*this).Visit(E); |
| 10254 | } |
| 10255 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10256 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10257 | /// of the program being compiled. |
| 10258 | /// |
| 10259 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10260 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10261 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10262 | /// expressions, code used only during overload resolution, etc., are not |
| 10263 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10264 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10265 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10266 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10267 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10268 | /// This routine should be used for all diagnostics that describe the run-time |
| 10269 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10270 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10271 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 10272 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10273 | const PartialDiagnostic &PD) { |
| 10274 | switch (ExprEvalContexts.back().Context ) { |
| 10275 | case Unevaluated: |
| 10276 | // The argument will never be evaluated, so don't complain. |
| 10277 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10278 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10279 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10280 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10281 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 10282 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 10283 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 10284 | } |
| 10285 | else |
| 10286 | Diag(Loc, PD); |
| 10287 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10288 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10289 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10290 | case PotentiallyPotentiallyEvaluated: |
| 10291 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10292 | break; |
| 10293 | } |
| 10294 | |
| 10295 | return false; |
| 10296 | } |
| 10297 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10298 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10299 | CallExpr *CE, FunctionDecl *FD) { |
| 10300 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10301 | return false; |
| 10302 | |
| 10303 | PartialDiagnostic Note = |
| 10304 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10305 | << FD->getDeclName() : PDiag(); |
| 10306 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10307 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10308 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10309 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10310 | PDiag(diag::err_call_function_incomplete_return) |
| 10311 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10312 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10313 | << CE->getSourceRange(), |
| 10314 | std::make_pair(NoteLoc, Note))) |
| 10315 | return true; |
| 10316 | |
| 10317 | return false; |
| 10318 | } |
| 10319 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10320 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10321 | // will prevent this condition from triggering, which is what we want. |
| 10322 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10323 | SourceLocation Loc; |
| 10324 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10325 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10326 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10327 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10328 | if (isa<BinaryOperator>(E)) { |
| 10329 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10330 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10331 | return; |
| 10332 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10333 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10334 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10335 | // Greylist some idioms by putting them into a warning subcategory. |
| 10336 | if (ObjCMessageExpr *ME |
| 10337 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10338 | Selector Sel = ME->getSelector(); |
| 10339 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10340 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10341 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10342 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10343 | |
| 10344 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10345 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10346 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10347 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10348 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10349 | Loc = Op->getOperatorLoc(); |
| 10350 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10351 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10352 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10353 | return; |
| 10354 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10355 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10356 | Loc = Op->getOperatorLoc(); |
| 10357 | } else { |
| 10358 | // Not an assignment. |
| 10359 | return; |
| 10360 | } |
| 10361 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10362 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10363 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10364 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10365 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 10366 | Diag(Loc, diag::note_condition_assign_silence) |
| 10367 | << FixItHint::CreateInsertion(Open, "(") |
| 10368 | << FixItHint::CreateInsertion(Close, ")"); |
| 10369 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10370 | if (IsOrAssign) |
| 10371 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10372 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10373 | else |
| 10374 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10375 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10376 | } |
| 10377 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10378 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10379 | /// that the user intended an assignment used as condition. |
| 10380 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10381 | // Don't warn if the parens came from a macro. |
| 10382 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10383 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10384 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10385 | // Don't warn for dependent expressions. |
| 10386 | if (parenE->isTypeDependent()) |
| 10387 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10388 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10389 | Expr *E = parenE->IgnoreParens(); |
| 10390 | |
| 10391 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10392 | if (opE->getOpcode() == BO_EQ && |
| 10393 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10394 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10395 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10396 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10397 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10398 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10399 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10400 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10401 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10402 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10403 | } |
| 10404 | } |
| 10405 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10406 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10407 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10408 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10409 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10410 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10411 | ExprResult result = CheckPlaceholderExpr(E); |
| 10412 | if (result.isInvalid()) return ExprError(); |
| 10413 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10414 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10415 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10416 | if (getLangOptions().CPlusPlus) |
| 10417 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10418 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10419 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10420 | if (ERes.isInvalid()) |
| 10421 | return ExprError(); |
| 10422 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10423 | |
| 10424 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10425 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10426 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10427 | << T << E->getSourceRange(); |
| 10428 | return ExprError(); |
| 10429 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10430 | } |
| 10431 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10432 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10433 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10434 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10435 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10436 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10437 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10438 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10439 | |
| 10440 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10441 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10442 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10443 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10444 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10445 | /// to have an appropriate type. |
| 10446 | struct RebuildUnknownAnyFunction |
| 10447 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10448 | |
| 10449 | Sema &S; |
| 10450 | |
| 10451 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10452 | |
| 10453 | ExprResult VisitStmt(Stmt *S) { |
| 10454 | llvm_unreachable("unexpected statement!"); |
| 10455 | return ExprError(); |
| 10456 | } |
| 10457 | |
| 10458 | ExprResult VisitExpr(Expr *expr) { |
| 10459 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10460 | << expr->getSourceRange(); |
| 10461 | return ExprError(); |
| 10462 | } |
| 10463 | |
| 10464 | /// Rebuild an expression which simply semantically wraps another |
| 10465 | /// expression which it shares the type and value kind of. |
| 10466 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10467 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10468 | if (subResult.isInvalid()) return ExprError(); |
| 10469 | |
| 10470 | Expr *subExpr = subResult.take(); |
| 10471 | expr->setSubExpr(subExpr); |
| 10472 | expr->setType(subExpr->getType()); |
| 10473 | expr->setValueKind(subExpr->getValueKind()); |
| 10474 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10475 | return expr; |
| 10476 | } |
| 10477 | |
| 10478 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10479 | return rebuildSugarExpr(paren); |
| 10480 | } |
| 10481 | |
| 10482 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10483 | return rebuildSugarExpr(op); |
| 10484 | } |
| 10485 | |
| 10486 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10487 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10488 | if (subResult.isInvalid()) return ExprError(); |
| 10489 | |
| 10490 | Expr *subExpr = subResult.take(); |
| 10491 | op->setSubExpr(subExpr); |
| 10492 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10493 | assert(op->getValueKind() == VK_RValue); |
| 10494 | assert(op->getObjectKind() == OK_Ordinary); |
| 10495 | return op; |
| 10496 | } |
| 10497 | |
| 10498 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10499 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10500 | |
| 10501 | expr->setType(decl->getType()); |
| 10502 | |
| 10503 | assert(expr->getValueKind() == VK_RValue); |
| 10504 | if (S.getLangOptions().CPlusPlus && |
| 10505 | !(isa<CXXMethodDecl>(decl) && |
| 10506 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10507 | expr->setValueKind(VK_LValue); |
| 10508 | |
| 10509 | return expr; |
| 10510 | } |
| 10511 | |
| 10512 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10513 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10514 | } |
| 10515 | |
| 10516 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10517 | return resolveDecl(ref, ref->getDecl()); |
| 10518 | } |
| 10519 | }; |
| 10520 | } |
| 10521 | |
| 10522 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10523 | /// to have a function type. |
| 10524 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10525 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10526 | if (result.isInvalid()) return ExprError(); |
| 10527 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10528 | } |
| 10529 | |
| 10530 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10531 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10532 | /// into one which resolves the type directly on the referring |
| 10533 | /// expression. Strict preservation of the original source |
| 10534 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10535 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10536 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10537 | |
| 10538 | Sema &S; |
| 10539 | |
| 10540 | /// The current destination type. |
| 10541 | QualType DestType; |
| 10542 | |
| 10543 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10544 | : S(S), DestType(castType) {} |
| 10545 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10546 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10547 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10548 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10549 | } |
| 10550 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10551 | ExprResult VisitExpr(Expr *expr) { |
| 10552 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10553 | << expr->getSourceRange(); |
| 10554 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10555 | } |
| 10556 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10557 | ExprResult VisitCallExpr(CallExpr *call); |
| 10558 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10559 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10560 | /// Rebuild an expression which simply semantically wraps another |
| 10561 | /// expression which it shares the type and value kind of. |
| 10562 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10563 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10564 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10565 | Expr *subExpr = subResult.take(); |
| 10566 | expr->setSubExpr(subExpr); |
| 10567 | expr->setType(subExpr->getType()); |
| 10568 | expr->setValueKind(subExpr->getValueKind()); |
| 10569 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10570 | return expr; |
| 10571 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10572 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10573 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10574 | return rebuildSugarExpr(paren); |
| 10575 | } |
| 10576 | |
| 10577 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10578 | return rebuildSugarExpr(op); |
| 10579 | } |
| 10580 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10581 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10582 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10583 | if (!ptr) { |
| 10584 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10585 | << op->getSourceRange(); |
| 10586 | return ExprError(); |
| 10587 | } |
| 10588 | assert(op->getValueKind() == VK_RValue); |
| 10589 | assert(op->getObjectKind() == OK_Ordinary); |
| 10590 | op->setType(DestType); |
| 10591 | |
| 10592 | // Build the sub-expression as if it were an object of the pointee type. |
| 10593 | DestType = ptr->getPointeeType(); |
| 10594 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10595 | if (subResult.isInvalid()) return ExprError(); |
| 10596 | op->setSubExpr(subResult.take()); |
| 10597 | return op; |
| 10598 | } |
| 10599 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10600 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10601 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10602 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10603 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10604 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10605 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10606 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10607 | |
| 10608 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10609 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10610 | } |
| 10611 | }; |
| 10612 | } |
| 10613 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10614 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10615 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10616 | Expr *callee = call->getCallee(); |
| 10617 | |
| 10618 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10619 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10620 | FK_FunctionPointer, |
| 10621 | FK_BlockPointer |
| 10622 | }; |
| 10623 | |
| 10624 | FnKind kind; |
| 10625 | QualType type = callee->getType(); |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10626 | if (type == S.Context.BoundMemberTy) { |
| 10627 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10628 | kind = FK_MemberFunction; |
| 10629 | type = Expr::findBoundMemberType(callee); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10630 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10631 | type = ptr->getPointeeType(); |
| 10632 | kind = FK_FunctionPointer; |
| 10633 | } else { |
| 10634 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 10635 | kind = FK_BlockPointer; |
| 10636 | } |
| 10637 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 10638 | |
| 10639 | // Verify that this is a legal result type of a function. |
| 10640 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10641 | unsigned diagID = diag::err_func_returning_array_function; |
| 10642 | if (kind == FK_BlockPointer) |
| 10643 | diagID = diag::err_block_returning_array_function; |
| 10644 | |
| 10645 | S.Diag(call->getExprLoc(), diagID) |
| 10646 | << DestType->isFunctionType() << DestType; |
| 10647 | return ExprError(); |
| 10648 | } |
| 10649 | |
| 10650 | // Otherwise, go ahead and set DestType as the call's result. |
| 10651 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 10652 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 10653 | assert(call->getObjectKind() == OK_Ordinary); |
| 10654 | |
| 10655 | // Rebuild the function type, replacing the result type with DestType. |
| 10656 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 10657 | DestType = S.Context.getFunctionType(DestType, |
| 10658 | proto->arg_type_begin(), |
| 10659 | proto->getNumArgs(), |
| 10660 | proto->getExtProtoInfo()); |
| 10661 | else |
| 10662 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 10663 | fnType->getExtInfo()); |
| 10664 | |
| 10665 | // Rebuild the appropriate pointer-to-function type. |
| 10666 | switch (kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10667 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10668 | // Nothing to do. |
| 10669 | break; |
| 10670 | |
| 10671 | case FK_FunctionPointer: |
| 10672 | DestType = S.Context.getPointerType(DestType); |
| 10673 | break; |
| 10674 | |
| 10675 | case FK_BlockPointer: |
| 10676 | DestType = S.Context.getBlockPointerType(DestType); |
| 10677 | break; |
| 10678 | } |
| 10679 | |
| 10680 | // Finally, we can recurse. |
| 10681 | ExprResult calleeResult = Visit(callee); |
| 10682 | if (!calleeResult.isUsable()) return ExprError(); |
| 10683 | call->setCallee(calleeResult.take()); |
| 10684 | |
| 10685 | // Bind a temporary if necessary. |
| 10686 | return S.MaybeBindToTemporary(call); |
| 10687 | } |
| 10688 | |
| 10689 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10690 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 10691 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10692 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10693 | // Verify that this is a legal result type of a call. |
| 10694 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10695 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 10696 | << DestType->isFunctionType() << DestType; |
| 10697 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10698 | } |
| 10699 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10700 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 10701 | method->setResultType(DestType); |
| 10702 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10703 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10704 | msg->setType(DestType.getNonReferenceType()); |
| 10705 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10706 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10707 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
| 10710 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10711 | // The only case we should ever see here is a function-to-pointer decay. |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10712 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10713 | assert(ice->getValueKind() == VK_RValue); |
| 10714 | assert(ice->getObjectKind() == OK_Ordinary); |
| 10715 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10716 | ice->setType(DestType); |
| 10717 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10718 | // Rebuild the sub-expression as the pointee (function) type. |
| 10719 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 10720 | |
| 10721 | ExprResult result = Visit(ice->getSubExpr()); |
| 10722 | if (!result.isUsable()) return ExprError(); |
| 10723 | |
| 10724 | ice->setSubExpr(result.take()); |
| 10725 | return S.Owned(ice); |
| 10726 | } |
| 10727 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10728 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10729 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10730 | QualType type = DestType; |
| 10731 | |
| 10732 | // We know how to make this work for certain kinds of decls: |
| 10733 | |
| 10734 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10735 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10736 | // This is true because FunctionDecls must always have function |
| 10737 | // type, so we can't be resolving the entire thing at once. |
| 10738 | assert(type->isFunctionType()); |
| 10739 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10740 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 10741 | if (method->isInstance()) { |
| 10742 | valueKind = VK_RValue; |
| 10743 | type = S.Context.BoundMemberTy; |
| 10744 | } |
| 10745 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10746 | // Function references aren't l-values in C. |
| 10747 | if (!S.getLangOptions().CPlusPlus) |
| 10748 | valueKind = VK_RValue; |
| 10749 | |
| 10750 | // - variables |
| 10751 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10752 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 10753 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10754 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10755 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10756 | << decl << expr->getSourceRange(); |
| 10757 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10758 | } |
| 10759 | |
| 10760 | // - nothing else |
| 10761 | } else { |
| 10762 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10763 | << decl << expr->getSourceRange(); |
| 10764 | return ExprError(); |
| 10765 | } |
| 10766 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10767 | decl->setType(DestType); |
| 10768 | expr->setType(type); |
| 10769 | expr->setValueKind(valueKind); |
| 10770 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10771 | } |
| 10772 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10773 | /// Check a cast of an unknown-any type. We intentionally only |
| 10774 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10775 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 10776 | Expr *castExpr, CastKind &castKind, |
| 10777 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10778 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10779 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 10780 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10781 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10782 | castExpr = result.take(); |
| 10783 | VK = castExpr->getValueKind(); |
| 10784 | castKind = CK_NoOp; |
| 10785 | |
| 10786 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10787 | } |
| 10788 | |
| 10789 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 10790 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10791 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10792 | while (true) { |
| 10793 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10794 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10795 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10796 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10797 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10798 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10799 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10800 | } |
| 10801 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10802 | SourceLocation loc; |
| 10803 | NamedDecl *d; |
| 10804 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 10805 | loc = ref->getLocation(); |
| 10806 | d = ref->getDecl(); |
| 10807 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 10808 | loc = mem->getMemberLoc(); |
| 10809 | d = mem->getMemberDecl(); |
| 10810 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 10811 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10812 | loc = msg->getSelectorLoc(); |
| 10813 | d = msg->getMethodDecl(); |
| 10814 | assert(d && "unknown method returning __unknown_any?"); |
| 10815 | } else { |
| 10816 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10817 | << e->getSourceRange(); |
| 10818 | return ExprError(); |
| 10819 | } |
| 10820 | |
| 10821 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10822 | |
| 10823 | // Never recoverable. |
| 10824 | return ExprError(); |
| 10825 | } |
| 10826 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10827 | /// Check for operands with placeholder types and complain if found. |
| 10828 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10829 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10830 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10831 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10832 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10833 | // Overloaded expressions. |
| 10834 | if (type == Context.OverloadTy) |
| 10835 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10836 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10837 | QualType(), |
| 10838 | diag::err_ovl_unresolvable); |
| 10839 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10840 | // Bound member functions. |
| 10841 | if (type == Context.BoundMemberTy) { |
| 10842 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10843 | << E->getSourceRange(); |
| 10844 | return ExprError(); |
| 10845 | } |
| 10846 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10847 | // Expressions of unknown type. |
| 10848 | if (type == Context.UnknownAnyTy) |
| 10849 | return diagnoseUnknownAnyExpr(*this, E); |
| 10850 | |
| 10851 | assert(!type->isPlaceholderType()); |
| 10852 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10853 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10854 | |
| 10855 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 10856 | if (expr->isTypeDependent()) |
| 10857 | return true; |
| 10858 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 10859 | return expr->getType()->isIntegralOrEnumerationType(); |
| 10860 | return false; |
| 10861 | } |