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" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/LiteralSupport.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 33 | #include "clang/Sema/DeclSpec.h" |
| 34 | #include "clang/Sema/Designator.h" |
| 35 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 38 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 43 | /// \brief Determine whether the use of this declaration is valid, and |
| 44 | /// emit any corresponding diagnostics. |
| 45 | /// |
| 46 | /// This routine diagnoses various problems with referencing |
| 47 | /// declarations that can occur when using a declaration. For example, |
| 48 | /// it might warn if a deprecated or unavailable declaration is being |
| 49 | /// used, or produce an error (and return true) if a C++0x deleted |
| 50 | /// function is being used. |
| 51 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 52 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 53 | /// decls. |
| 54 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 55 | /// \returns true if there was an error (this declaration cannot be |
| 56 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 57 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 58 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 59 | bool UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 60 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 61 | // If there were any diagnostics suppressed by template argument deduction, |
| 62 | // emit them now. |
| 63 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 64 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 65 | if (Pos != SuppressedDiagnostics.end()) { |
| 66 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
| 67 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 68 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 69 | |
| 70 | // 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] | 71 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 72 | // entry from the table, because we want to avoid ever emitting these |
| 73 | // diagnostics again. |
| 74 | Suppressed.clear(); |
| 75 | } |
| 76 | } |
| 77 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 78 | // 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] | 79 | if (ParsingInitForAutoVars.count(D)) { |
| 80 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 81 | << D->getDeclName(); |
| 82 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 86 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | if (FD->isDeleted()) { |
| 88 | Diag(Loc, diag::err_deleted_function_use); |
| 89 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 90 | return true; |
| 91 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 92 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 94 | // See if this declaration is unavailable or deprecated. |
| 95 | std::string Message; |
| 96 | switch (D->getAvailability(&Message)) { |
| 97 | case AR_Available: |
| 98 | case AR_NotYetIntroduced: |
| 99 | break; |
| 100 | |
| 101 | case AR_Deprecated: |
| 102 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 103 | break; |
| 104 | |
| 105 | case AR_Unavailable: |
| 106 | if (Message.empty()) { |
| 107 | if (!UnknownObjCClass) |
| 108 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 109 | else |
| 110 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 111 | << D->getDeclName(); |
| 112 | } |
| 113 | else |
| 114 | Diag(Loc, diag::err_unavailable_message) |
| 115 | << D->getDeclName() << Message; |
| 116 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 117 | break; |
| 118 | } |
| 119 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 120 | // Warn if this is used but marked unused. |
| 121 | if (D->hasAttr<UnusedAttr>()) |
| 122 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 123 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 124 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 127 | /// \brief Retrieve the message suffix that should be added to a |
| 128 | /// diagnostic complaining about the given function being deleted or |
| 129 | /// unavailable. |
| 130 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 131 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 132 | // detected here so that we could improve diagnostics to say, e.g., |
| 133 | // "base class 'A' had a deleted copy constructor". |
| 134 | if (FD->isDeleted()) |
| 135 | return std::string(); |
| 136 | |
| 137 | std::string Message; |
| 138 | if (FD->getAvailability(&Message)) |
| 139 | return ": " + Message; |
| 140 | |
| 141 | return std::string(); |
| 142 | } |
| 143 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 144 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 146 | /// attribute. It warns if call does not have the sentinel argument. |
| 147 | /// |
| 148 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 150 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 152 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 153 | |
| 154 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 155 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 156 | int sentinelPos = attr->getSentinel(); |
| 157 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 159 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 160 | // 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] | 161 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 162 | bool warnNotEnoughArgs = false; |
| 163 | int isMethod = 0; |
| 164 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 165 | // skip over named parameters. |
| 166 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 167 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 168 | if (nullPos) |
| 169 | --nullPos; |
| 170 | else |
| 171 | ++i; |
| 172 | } |
| 173 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 174 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 175 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 176 | // skip over named parameters. |
| 177 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 178 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 179 | if (nullPos) |
| 180 | --nullPos; |
| 181 | else |
| 182 | ++i; |
| 183 | } |
| 184 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 185 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 186 | // block or function pointer call. |
| 187 | QualType Ty = V->getType(); |
| 188 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 190 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 191 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 192 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 193 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 194 | unsigned k; |
| 195 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 196 | if (nullPos) |
| 197 | --nullPos; |
| 198 | else |
| 199 | ++i; |
| 200 | } |
| 201 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 202 | } |
| 203 | if (Ty->isBlockPointerType()) |
| 204 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 205 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 206 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 207 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 208 | return; |
| 209 | |
| 210 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 211 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 212 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 213 | return; |
| 214 | } |
| 215 | int sentinel = i; |
| 216 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 217 | --sentinelPos; |
| 218 | ++i; |
| 219 | } |
| 220 | if (sentinelPos > 0) { |
| 221 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 222 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | while (i < NumArgs-1) { |
| 226 | ++i; |
| 227 | ++sentinel; |
| 228 | } |
| 229 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 230 | if (!sentinelExpr) return; |
| 231 | if (sentinelExpr->isTypeDependent()) return; |
| 232 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | 343e6ff | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 233 | |
| 234 | // nullptr_t is always treated as null. |
| 235 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 236 | |
Fariborz Jahanian | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 237 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 238 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 239 | Expr::NPC_ValueDependentIsNull)) |
| 240 | return; |
| 241 | |
| 242 | // Unfortunately, __null has type 'int'. |
| 243 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 244 | |
| 245 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 246 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 249 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 250 | Expr *Ex = (Expr *)E; |
| 251 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 252 | } |
| 253 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
| 255 | // Standard Promotions and Conversions |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 258 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 259 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 260 | QualType Ty = E->getType(); |
| 261 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 262 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 263 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 264 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 265 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 266 | else if (Ty->isArrayType()) { |
| 267 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 268 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 269 | // type 'array of type' is converted to an expression that has type 'pointer |
| 270 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 271 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 272 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 273 | // |
| 274 | // C++ 4.2p1: |
| 275 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 276 | // T" can be converted to an rvalue of type "pointer to T". |
| 277 | // |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 278 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 279 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 280 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 281 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 282 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 283 | } |
| 284 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 285 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 286 | // C++ [conv.lval]p1: |
| 287 | // A glvalue of a non-function, non-array type T can be |
| 288 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 289 | if (!E->isGLValue()) return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 290 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 291 | QualType T = E->getType(); |
| 292 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 293 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 294 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 295 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 296 | ExprResult Res = ConvertPropertyForRValue(E); |
| 297 | if (Res.isInvalid()) |
| 298 | return Owned(E); |
| 299 | E = Res.take(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 300 | if (!E->isGLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 301 | return Owned(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 302 | } |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 303 | |
| 304 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 305 | // expressions of certain types in C++. |
| 306 | if (getLangOptions().CPlusPlus && |
| 307 | (E->getType() == Context.OverloadTy || |
| 308 | T->isDependentType() || |
| 309 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 310 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 311 | |
| 312 | // The C standard is actually really unclear on this point, and |
| 313 | // DR106 tells us what the result should be but not why. It's |
| 314 | // generally best to say that void types just doesn't undergo |
| 315 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 316 | // 'void' type are never l-values, but qualified void can be. |
| 317 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 318 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 319 | |
| 320 | // C++ [conv.lval]p1: |
| 321 | // [...] If T is a non-class type, the type of the prvalue is the |
| 322 | // cv-unqualified version of T. Otherwise, the type of the |
| 323 | // rvalue is T. |
| 324 | // |
| 325 | // C99 6.3.2.1p2: |
| 326 | // If the lvalue has qualified type, the value has the unqualified |
| 327 | // version of the type of the lvalue; otherwise, the value has the |
| 328 | // type of the lvalue. |
| 329 | if (T.hasQualifiers()) |
| 330 | T = T.getUnqualifiedType(); |
| 331 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 332 | CheckArrayAccess(E); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 333 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 334 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 335 | E, 0, VK_RValue)); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 336 | } |
| 337 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 338 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 339 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 340 | if (Res.isInvalid()) |
| 341 | return ExprError(); |
| 342 | Res = DefaultLvalueConversion(Res.take()); |
| 343 | if (Res.isInvalid()) |
| 344 | return ExprError(); |
| 345 | return move(Res); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 349 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 351 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 352 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 353 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 354 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 355 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 356 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 357 | if (Res.isInvalid()) |
| 358 | return Owned(E); |
| 359 | E = Res.take(); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 360 | |
| 361 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 362 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 363 | |
| 364 | // Try to perform integral promotions if the object has a theoretically |
| 365 | // promotable type. |
| 366 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 367 | // C99 6.3.1.1p2: |
| 368 | // |
| 369 | // The following may be used in an expression wherever an int or |
| 370 | // unsigned int may be used: |
| 371 | // - an object or expression with an integer type whose integer |
| 372 | // conversion rank is less than or equal to the rank of int |
| 373 | // and unsigned int. |
| 374 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 375 | // |
| 376 | // If an int can represent all values of the original type, the |
| 377 | // value is converted to an int; otherwise, it is converted to an |
| 378 | // unsigned int. These are called the integer promotions. All |
| 379 | // other types are unchanged by the integer promotions. |
| 380 | |
| 381 | QualType PTy = Context.isPromotableBitField(E); |
| 382 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 383 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 384 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 385 | } |
| 386 | if (Ty->isPromotableIntegerType()) { |
| 387 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 388 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 389 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 390 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 391 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 392 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 395 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | /// 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] | 397 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 398 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 399 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 400 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 402 | ExprResult Res = UsualUnaryConversions(E); |
| 403 | if (Res.isInvalid()) |
| 404 | return Owned(E); |
| 405 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 407 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 408 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 409 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 410 | |
| 411 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 414 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 415 | /// 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] | 416 | /// interfaces passed by value. |
| 417 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 418 | FunctionDecl *FDecl) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 419 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
| 420 | if (ExprRes.isInvalid()) |
| 421 | return ExprError(); |
| 422 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 424 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 425 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 426 | // etc. |
| 427 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 428 | return Owned(E); |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 429 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 430 | if (E->getType()->isObjCObjectType() && |
| 431 | DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 432 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 433 | << E->getType() << CT)) |
| 434 | return ExprError(); |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 435 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 436 | if (!E->getType()->isPODType() && |
| 437 | DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 438 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 439 | << E->getType() << CT)) |
| 440 | return ExprError(); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 441 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 442 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 445 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 446 | /// 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] | 447 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 448 | /// responsible for emitting appropriate error diagnostics. |
| 449 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 450 | /// GCC. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 451 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 452 | bool isCompAssign) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | if (!isCompAssign) { |
| 454 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 455 | if (lhsExpr.isInvalid()) |
| 456 | return QualType(); |
| 457 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 458 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 459 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 460 | if (rhsExpr.isInvalid()) |
| 461 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 462 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 464 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 465 | QualType lhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 466 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | QualType rhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 468 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 469 | |
| 470 | // If both types are identical, no conversion is needed. |
| 471 | if (lhs == rhs) |
| 472 | return lhs; |
| 473 | |
| 474 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 475 | // The caller can deal with this (e.g. pointer + int). |
| 476 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 477 | return lhs; |
| 478 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 479 | // Apply unary and bitfield promotions to the LHS's type. |
| 480 | QualType lhs_unpromoted = lhs; |
| 481 | if (lhs->isPromotableIntegerType()) |
| 482 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 483 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 484 | if (!LHSBitfieldPromoteTy.isNull()) |
| 485 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 486 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 487 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 488 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 489 | // If both types are identical, no conversion is needed. |
| 490 | if (lhs == rhs) |
| 491 | return lhs; |
| 492 | |
| 493 | // At this point, we have two different arithmetic types. |
| 494 | |
| 495 | // Handle complex types first (C99 6.3.1.8p1). |
| 496 | bool LHSComplexFloat = lhs->isComplexType(); |
| 497 | bool RHSComplexFloat = rhs->isComplexType(); |
| 498 | if (LHSComplexFloat || RHSComplexFloat) { |
| 499 | // if we have an integer operand, the result is the complex type. |
| 500 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 501 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 502 | if (rhs->isIntegerType()) { |
| 503 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 504 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 505 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 506 | } else { |
| 507 | assert(rhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 508 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 509 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 510 | return lhs; |
| 511 | } |
| 512 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 513 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 514 | if (!isCompAssign) { |
| 515 | // int -> float -> _Complex float |
| 516 | if (lhs->isIntegerType()) { |
| 517 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 518 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 519 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 520 | } else { |
| 521 | assert(lhs->isComplexIntegerType()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 522 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 525 | return rhs; |
| 526 | } |
| 527 | |
| 528 | // This handles complex/complex, complex/float, or float/complex. |
| 529 | // When both operands are complex, the shorter operand is converted to the |
| 530 | // type of the longer, and that is the type of the result. This corresponds |
| 531 | // to what is done when combining two real floating-point operands. |
| 532 | // The fun begins when size promotion occur across type domains. |
| 533 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 534 | // floating-point type, the less precise type is converted, within it's |
| 535 | // real or complex domain, to the precision of the other type. For example, |
| 536 | // when combining a "long double" with a "double _Complex", the |
| 537 | // "double _Complex" is promoted to "long double _Complex". |
| 538 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 539 | |
| 540 | // If both are complex, just cast to the more precise type. |
| 541 | if (LHSComplexFloat && RHSComplexFloat) { |
| 542 | if (order > 0) { |
| 543 | // _Complex float -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 544 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 545 | return lhs; |
| 546 | |
| 547 | } else if (order < 0) { |
| 548 | // _Complex float -> _Complex double |
| 549 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 550 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 551 | return rhs; |
| 552 | } |
| 553 | return lhs; |
| 554 | } |
| 555 | |
| 556 | // If just the LHS is complex, the RHS needs to be converted, |
| 557 | // and the LHS might need to be promoted. |
| 558 | if (LHSComplexFloat) { |
| 559 | if (order > 0) { // LHS is wider |
| 560 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 561 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 562 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 563 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 564 | return lhs; |
| 565 | } |
| 566 | |
| 567 | // RHS is at least as wide. Find its corresponding complex type. |
| 568 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 569 | |
| 570 | // double -> _Complex double |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 571 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 572 | |
| 573 | // _Complex float -> _Complex double |
| 574 | if (!isCompAssign && order < 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 575 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 576 | |
| 577 | return result; |
| 578 | } |
| 579 | |
| 580 | // Just the RHS is complex, so the LHS needs to be converted |
| 581 | // and the RHS might need to be promoted. |
| 582 | assert(RHSComplexFloat); |
| 583 | |
| 584 | if (order < 0) { // RHS is wider |
| 585 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 586 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 587 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 588 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 589 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 590 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 591 | return rhs; |
| 592 | } |
| 593 | |
| 594 | // LHS is at least as wide. Find its corresponding complex type. |
| 595 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 596 | |
| 597 | // double -> _Complex double |
| 598 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 599 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 600 | |
| 601 | // _Complex float -> _Complex double |
| 602 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 603 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 604 | |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | // Now handle "real" floating types (i.e. float, double, long double). |
| 609 | bool LHSFloat = lhs->isRealFloatingType(); |
| 610 | bool RHSFloat = rhs->isRealFloatingType(); |
| 611 | if (LHSFloat || RHSFloat) { |
| 612 | // If we have two real floating types, convert the smaller operand |
| 613 | // to the bigger result. |
| 614 | if (LHSFloat && RHSFloat) { |
| 615 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 616 | if (order > 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 617 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 618 | return lhs; |
| 619 | } |
| 620 | |
| 621 | assert(order < 0 && "illegal float comparison"); |
| 622 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 623 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 624 | return rhs; |
| 625 | } |
| 626 | |
| 627 | // If we have an integer operand, the result is the real floating type. |
| 628 | if (LHSFloat) { |
| 629 | if (rhs->isIntegerType()) { |
| 630 | // Convert rhs to the lhs floating point type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 631 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 632 | return lhs; |
| 633 | } |
| 634 | |
| 635 | // Convert both sides to the appropriate complex float. |
| 636 | assert(rhs->isComplexIntegerType()); |
| 637 | QualType result = Context.getComplexType(lhs); |
| 638 | |
| 639 | // _Complex int -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 640 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 641 | |
| 642 | // float -> _Complex float |
| 643 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 644 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 645 | |
| 646 | return result; |
| 647 | } |
| 648 | |
| 649 | assert(RHSFloat); |
| 650 | if (lhs->isIntegerType()) { |
| 651 | // Convert lhs to the rhs floating point type. |
| 652 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 653 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 654 | return rhs; |
| 655 | } |
| 656 | |
| 657 | // Convert both sides to the appropriate complex float. |
| 658 | assert(lhs->isComplexIntegerType()); |
| 659 | QualType result = Context.getComplexType(rhs); |
| 660 | |
| 661 | // _Complex int -> _Complex float |
| 662 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 663 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 664 | |
| 665 | // float -> _Complex float |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 666 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 667 | |
| 668 | return result; |
| 669 | } |
| 670 | |
| 671 | // Handle GCC complex int extension. |
| 672 | // FIXME: if the operands are (int, _Complex long), we currently |
| 673 | // don't promote the complex. Also, signedness? |
| 674 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 675 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 676 | if (lhsComplexInt && rhsComplexInt) { |
| 677 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 678 | rhsComplexInt->getElementType()); |
| 679 | assert(order && "inequal types with equal element ordering"); |
| 680 | if (order > 0) { |
| 681 | // _Complex int -> _Complex long |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 682 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 683 | return lhs; |
| 684 | } |
| 685 | |
| 686 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 687 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 688 | return rhs; |
| 689 | } else if (lhsComplexInt) { |
| 690 | // int -> _Complex int |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 691 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 692 | return lhs; |
| 693 | } else if (rhsComplexInt) { |
| 694 | // int -> _Complex int |
| 695 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 696 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 697 | return rhs; |
| 698 | } |
| 699 | |
| 700 | // Finally, we have two differing integer types. |
| 701 | // The rules for this case are in C99 6.3.1.8 |
| 702 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 703 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 704 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 705 | if (lhsSigned == rhsSigned) { |
| 706 | // Same signedness; use the higher-ranked type |
| 707 | if (compare >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 709 | return lhs; |
| 710 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 711 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 712 | return rhs; |
| 713 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 714 | // The unsigned type has greater than or equal rank to the |
| 715 | // signed type, so use the unsigned type |
| 716 | if (rhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 717 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 718 | return lhs; |
| 719 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 720 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 721 | return rhs; |
| 722 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 723 | // The two types are different widths; if we are here, that |
| 724 | // means the signed type is larger than the unsigned type, so |
| 725 | // use the signed type. |
| 726 | if (lhsSigned) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 727 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 728 | return lhs; |
| 729 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 730 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 731 | return rhs; |
| 732 | } else { |
| 733 | // The signed type is higher-ranked than the unsigned type, |
| 734 | // but isn't actually any bigger (like unsigned int and long |
| 735 | // on most 32-bit systems). Use the unsigned type corresponding |
| 736 | // to the signed type. |
| 737 | QualType result = |
| 738 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 739 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 740 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 741 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 742 | return result; |
| 743 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------===// |
| 747 | // Semantic Analysis for various Expression Types |
| 748 | //===----------------------------------------------------------------------===// |
| 749 | |
| 750 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 751 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 752 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 753 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 754 | /// multiple tokens. However, the common case is that StringToks points to one |
| 755 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 756 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 757 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 758 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 759 | assert(NumStringToks && "Must have at least one string!"); |
| 760 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 761 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 762 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 763 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | |
| 765 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 766 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 767 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 768 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 769 | QualType StrTy = Context.CharTy; |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 770 | if (Literal.AnyWide) |
| 771 | StrTy = Context.getWCharType(); |
| 772 | else if (Literal.Pascal) |
| 773 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 774 | |
| 775 | // 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] | 776 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 777 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 778 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 779 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 780 | // the nul terminator character as well as the string length for pascal |
| 781 | // strings. |
| 782 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 783 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 784 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 786 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 787 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 788 | Literal.GetStringLength(), |
Anders Carlsson | 3e2193c | 2011-04-14 00:40:03 +0000 | [diff] [blame^] | 789 | Literal.AnyWide, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 790 | &StringTokLocs[0], |
| 791 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 792 | } |
| 793 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 794 | enum CaptureResult { |
| 795 | /// No capture is required. |
| 796 | CR_NoCapture, |
| 797 | |
| 798 | /// A capture is required. |
| 799 | CR_Capture, |
| 800 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 801 | /// A by-ref capture is required. |
| 802 | CR_CaptureByRef, |
| 803 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 804 | /// An error occurred when trying to capture the given variable. |
| 805 | CR_Error |
| 806 | }; |
| 807 | |
| 808 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 809 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 810 | /// \param var - the variable referenced |
| 811 | /// \param DC - the context which we couldn't capture through |
| 812 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 813 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 814 | VarDecl *var, DeclContext *DC) { |
| 815 | switch (S.ExprEvalContexts.back().Context) { |
| 816 | case Sema::Unevaluated: |
| 817 | // The argument will never be evaluated, so don't complain. |
| 818 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 820 | case Sema::PotentiallyEvaluated: |
| 821 | case Sema::PotentiallyEvaluatedIfUsed: |
| 822 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 823 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 824 | case Sema::PotentiallyPotentiallyEvaluated: |
| 825 | // FIXME: delay these! |
| 826 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 828 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 829 | // Don't diagnose about capture if we're not actually in code right |
| 830 | // now; in general, there are more appropriate places that will |
| 831 | // diagnose this. |
| 832 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 833 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 834 | // Certain madnesses can happen with parameter declarations, which |
| 835 | // we want to ignore. |
| 836 | if (isa<ParmVarDecl>(var)) { |
| 837 | // - If the parameter still belongs to the translation unit, then |
| 838 | // we're actually just using one parameter in the declaration of |
| 839 | // the next. This is useful in e.g. VLAs. |
| 840 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 841 | return CR_NoCapture; |
| 842 | |
| 843 | // - This particular madness can happen in ill-formed default |
| 844 | // arguments; claim it's okay and let downstream code handle it. |
| 845 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 846 | return CR_NoCapture; |
| 847 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 848 | |
| 849 | DeclarationName functionName; |
| 850 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 851 | functionName = fn->getDeclName(); |
| 852 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 853 | |
| 854 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 855 | << var->getIdentifier() << functionName; |
| 856 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 857 | << var->getIdentifier(); |
| 858 | |
| 859 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | } |
| 861 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 862 | /// There is a well-formed capture at a particular scope level; |
| 863 | /// propagate it through all the nested blocks. |
| 864 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 865 | const BlockDecl::Capture &capture) { |
| 866 | VarDecl *var = capture.getVariable(); |
| 867 | |
| 868 | // Update all the inner blocks with the capture information. |
| 869 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 870 | i != e; ++i) { |
| 871 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 872 | innerBlock->Captures.push_back( |
| 873 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 874 | /*nested*/ true, capture.getCopyExpr())); |
| 875 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 876 | } |
| 877 | |
| 878 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 879 | } |
| 880 | |
| 881 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 882 | /// given value in the current context requires a variable capture. |
| 883 | /// |
| 884 | /// This also keeps the captures set in the BlockScopeInfo records |
| 885 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 886 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 887 | ValueDecl *value) { |
| 888 | // Only variables ever require capture. |
| 889 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 890 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 891 | |
| 892 | // Fast path: variables from the current context never require capture. |
| 893 | DeclContext *DC = S.CurContext; |
| 894 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 895 | |
| 896 | // Only variables with local storage require capture. |
| 897 | // FIXME: What about 'const' variables in C++? |
| 898 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 899 | |
| 900 | // Otherwise, we need to capture. |
| 901 | |
| 902 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 903 | do { |
| 904 | // Only blocks (and eventually C++0x closures) can capture; other |
| 905 | // scopes don't work. |
| 906 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 907 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 908 | |
| 909 | BlockScopeInfo *blockScope = |
| 910 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 911 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 912 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 913 | // Check whether we've already captured it in this block. If so, |
| 914 | // we're done. |
| 915 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 916 | return propagateCapture(S, functionScopesIndex, |
| 917 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 918 | |
| 919 | functionScopesIndex--; |
| 920 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 921 | } while (var->getDeclContext() != DC); |
| 922 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 923 | // Okay, we descended all the way to the block that defines the variable. |
| 924 | // Actually try to capture it. |
| 925 | QualType type = var->getType(); |
| 926 | |
| 927 | // Prohibit variably-modified types. |
| 928 | if (type->isVariablyModifiedType()) { |
| 929 | S.Diag(loc, diag::err_ref_vm_type); |
| 930 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 931 | return CR_Error; |
| 932 | } |
| 933 | |
| 934 | // Prohibit arrays, even in __block variables, but not references to |
| 935 | // them. |
| 936 | if (type->isArrayType()) { |
| 937 | S.Diag(loc, diag::err_ref_array_type); |
| 938 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 939 | return CR_Error; |
| 940 | } |
| 941 | |
| 942 | S.MarkDeclarationReferenced(loc, var); |
| 943 | |
| 944 | // The BlocksAttr indicates the variable is bound by-reference. |
| 945 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 946 | |
| 947 | // Build a copy expression. |
| 948 | Expr *copyExpr = 0; |
| 949 | if (!byRef && S.getLangOptions().CPlusPlus && |
| 950 | !type->isDependentType() && type->isStructureOrClassType()) { |
| 951 | // According to the blocks spec, the capture of a variable from |
| 952 | // the stack requires a const copy constructor. This is not true |
| 953 | // of the copy/move done to move a __block variable to the heap. |
| 954 | type.addConst(); |
| 955 | |
| 956 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 957 | ExprResult result = |
| 958 | S.PerformCopyInitialization( |
| 959 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 960 | type, false), |
| 961 | loc, S.Owned(declRef)); |
| 962 | |
| 963 | // Build a full-expression copy expression if initialization |
| 964 | // succeeded and used a non-trivial constructor. Recover from |
| 965 | // errors by pretending that the copy isn't necessary. |
| 966 | if (!result.isInvalid() && |
| 967 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 968 | result = S.MaybeCreateExprWithCleanups(result); |
| 969 | copyExpr = result.take(); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // We're currently at the declarer; go back to the closure. |
| 974 | functionScopesIndex++; |
| 975 | BlockScopeInfo *blockScope = |
| 976 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 977 | |
| 978 | // Build a valid capture in this scope. |
| 979 | blockScope->Captures.push_back( |
| 980 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 981 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 982 | |
| 983 | // Propagate that to inner captures if necessary. |
| 984 | return propagateCapture(S, functionScopesIndex, |
| 985 | blockScope->Captures.back()); |
| 986 | } |
| 987 | |
| 988 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 989 | const DeclarationNameInfo &NameInfo, |
| 990 | bool byRef) { |
| 991 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 992 | |
| 993 | VarDecl *var = cast<VarDecl>(vd); |
| 994 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 995 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 996 | |
| 997 | QualType exprType = var->getType().getNonReferenceType(); |
| 998 | |
| 999 | BlockDeclRefExpr *BDRE; |
| 1000 | if (!byRef) { |
| 1001 | // The variable will be bound by copy; make it const within the |
| 1002 | // closure, but record that this was done in the expression. |
| 1003 | bool constAdded = !exprType.isConstQualified(); |
| 1004 | exprType.addConst(); |
| 1005 | |
| 1006 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1007 | NameInfo.getLoc(), false, |
| 1008 | constAdded); |
| 1009 | } else { |
| 1010 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1011 | NameInfo.getLoc(), true); |
| 1012 | } |
| 1013 | |
| 1014 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1015 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1016 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1017 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1018 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1019 | SourceLocation Loc, |
| 1020 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1021 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1022 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1025 | /// BuildDeclRefExpr - Build an expression that references a |
| 1026 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1027 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1028 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1029 | const DeclarationNameInfo &NameInfo, |
| 1030 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1031 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1033 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1034 | SS? SS->getWithLocInContext(Context) |
| 1035 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1036 | D, NameInfo, Ty, VK); |
| 1037 | |
| 1038 | // Just in case we're building an illegal pointer-to-member. |
| 1039 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1040 | E->setObjectKind(OK_BitField); |
| 1041 | |
| 1042 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1045 | static ExprResult |
| 1046 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1047 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1048 | DeclAccessPair FoundDecl, |
| 1049 | const DeclarationNameInfo &MemberNameInfo); |
| 1050 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1051 | ExprResult |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1052 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1053 | SourceLocation loc, |
| 1054 | IndirectFieldDecl *indirectField, |
| 1055 | Expr *baseObjectExpr, |
| 1056 | SourceLocation opLoc) { |
| 1057 | // First, build the expression that refers to the base object. |
| 1058 | |
| 1059 | bool baseObjectIsPointer = false; |
| 1060 | Qualifiers baseQuals; |
| 1061 | |
| 1062 | // Case 1: the base of the indirect field is not a field. |
| 1063 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1064 | CXXScopeSpec EmptySS; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1065 | if (baseVariable) { |
| 1066 | assert(baseVariable->getType()->isRecordType()); |
| 1067 | |
| 1068 | // In principle we could have a member access expression that |
| 1069 | // accesses an anonymous struct/union that's a static member of |
| 1070 | // the base object's class. However, under the current standard, |
| 1071 | // static data members cannot be anonymous structs or unions. |
| 1072 | // Supporting this is as easy as building a MemberExpr here. |
| 1073 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1074 | |
| 1075 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1076 | |
| 1077 | ExprResult result = |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1078 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1079 | if (result.isInvalid()) return ExprError(); |
| 1080 | |
| 1081 | baseObjectExpr = result.take(); |
| 1082 | baseObjectIsPointer = false; |
| 1083 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1084 | |
| 1085 | // Case 2: the base of the indirect field is a field and the user |
| 1086 | // wrote a member expression. |
| 1087 | } else if (baseObjectExpr) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1088 | // The caller provided the base object expression. Determine |
| 1089 | // whether its a pointer and whether it adds any qualifiers to the |
| 1090 | // anonymous struct/union fields we're looking into. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1091 | QualType objectType = baseObjectExpr->getType(); |
| 1092 | |
| 1093 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1094 | baseObjectIsPointer = true; |
| 1095 | objectType = ptr->getPointeeType(); |
| 1096 | } else { |
| 1097 | baseObjectIsPointer = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1098 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1099 | baseQuals = objectType.getQualifiers(); |
| 1100 | |
| 1101 | // Case 3: the base of the indirect field is a field and we should |
| 1102 | // build an implicit member access. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1103 | } else { |
| 1104 | // We've found a member of an anonymous struct/union that is |
| 1105 | // inside a non-anonymous struct/union, so in a well-formed |
| 1106 | // program our base object expression is "this". |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1107 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 1108 | if (!method) { |
| 1109 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1110 | << indirectField->getDeclName(); |
| 1111 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1114 | // Our base object expression is "this". |
| 1115 | baseObjectExpr = |
| 1116 | new (Context) CXXThisExpr(loc, method->getThisType(Context), |
| 1117 | /*isImplicit=*/ true); |
| 1118 | baseObjectIsPointer = true; |
| 1119 | baseQuals = Qualifiers::fromCVRMask(method->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Build the implicit member references to the field of the |
| 1123 | // anonymous struct/union. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1124 | Expr *result = baseObjectExpr; |
| 1125 | IndirectFieldDecl::chain_iterator |
| 1126 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1127 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1128 | // Build the first member access in the chain with full information. |
| 1129 | if (!baseVariable) { |
| 1130 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1131 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1132 | // FIXME: use the real found-decl info! |
| 1133 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1134 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1135 | // Make a nameInfo that properly uses the anonymous name. |
| 1136 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1137 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1138 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1139 | EmptySS, field, foundDecl, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1140 | memberNameInfo).take(); |
| 1141 | baseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1142 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1143 | // FIXME: check qualified member access |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1146 | // In all cases, we should now skip the first declaration in the chain. |
| 1147 | ++FI; |
| 1148 | |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1149 | while (FI != FEnd) { |
| 1150 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1151 | |
| 1152 | // FIXME: these are somewhat meaningless |
| 1153 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1154 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1155 | |
| 1156 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1157 | (FI == FEnd? SS : EmptySS), field, |
| 1158 | foundDecl, memberNameInfo) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1159 | .take(); |
| 1160 | } |
| 1161 | |
| 1162 | return Owned(result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1165 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1166 | /// possibly a list of template arguments. |
| 1167 | /// |
| 1168 | /// If this produces template arguments, it is permitted to call |
| 1169 | /// DecomposeTemplateName. |
| 1170 | /// |
| 1171 | /// This actually loses a lot of source location information for |
| 1172 | /// non-standard name kinds; we should consider preserving that in |
| 1173 | /// some way. |
| 1174 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1175 | const UnqualifiedId &Id, |
| 1176 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1177 | DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1178 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1179 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1180 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1181 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1182 | |
| 1183 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1184 | Id.TemplateId->getTemplateArgs(), |
| 1185 | Id.TemplateId->NumArgs); |
| 1186 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1187 | TemplateArgsPtr.release(); |
| 1188 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1189 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1190 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1191 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1192 | TemplateArgs = &Buffer; |
| 1193 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1194 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1195 | TemplateArgs = 0; |
| 1196 | } |
| 1197 | } |
| 1198 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1199 | /// Determines if the given class is provably not derived from all of |
| 1200 | /// the prospective base classes. |
| 1201 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1202 | CXXRecordDecl *Record, |
| 1203 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1204 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1205 | return false; |
| 1206 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1207 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1208 | if (!RD) return false; |
| 1209 | Record = cast<CXXRecordDecl>(RD); |
| 1210 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1211 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1212 | E = Record->bases_end(); I != E; ++I) { |
| 1213 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1214 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1215 | if (!BaseRT) return false; |
| 1216 | |
| 1217 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1218 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
| 1222 | return true; |
| 1223 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1224 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1225 | enum IMAKind { |
| 1226 | /// The reference is definitely not an instance member access. |
| 1227 | IMA_Static, |
| 1228 | |
| 1229 | /// The reference may be an implicit instance member access. |
| 1230 | IMA_Mixed, |
| 1231 | |
| 1232 | /// The reference may be to an instance member, but it is invalid if |
| 1233 | /// so, because the context is not an instance method. |
| 1234 | IMA_Mixed_StaticContext, |
| 1235 | |
| 1236 | /// The reference may be to an instance member, but it is invalid if |
| 1237 | /// so, because the context is from an unrelated class. |
| 1238 | IMA_Mixed_Unrelated, |
| 1239 | |
| 1240 | /// The reference is definitely an implicit instance member access. |
| 1241 | IMA_Instance, |
| 1242 | |
| 1243 | /// The reference may be to an unresolved using declaration. |
| 1244 | IMA_Unresolved, |
| 1245 | |
| 1246 | /// The reference may be to an unresolved using declaration and the |
| 1247 | /// context is not an instance method. |
| 1248 | IMA_Unresolved_StaticContext, |
| 1249 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1250 | /// All possible referrents are instance members and the current |
| 1251 | /// context is not an instance method. |
| 1252 | IMA_Error_StaticContext, |
| 1253 | |
| 1254 | /// All possible referrents are instance members of an unrelated |
| 1255 | /// class. |
| 1256 | IMA_Error_Unrelated |
| 1257 | }; |
| 1258 | |
| 1259 | /// The given lookup names class member(s) and is not being used for |
| 1260 | /// an address-of-member expression. Classify the type of access |
| 1261 | /// according to whether it's possible that this reference names an |
| 1262 | /// instance member. This is best-effort; it is okay to |
| 1263 | /// conservatively answer "yes", in which case some errors will simply |
| 1264 | /// not be caught until template-instantiation. |
| 1265 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1266 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1267 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1268 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1269 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1270 | bool isStaticContext = |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1271 | (!isa<CXXMethodDecl>(DC) || |
| 1272 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1273 | |
| 1274 | if (R.isUnresolvableResult()) |
| 1275 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1276 | |
| 1277 | // Collect all the declaring classes of instance members we find. |
| 1278 | bool hasNonInstance = false; |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1279 | bool hasField = false; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1280 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1281 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1282 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1283 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1284 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1285 | if (dyn_cast<FieldDecl>(D)) |
| 1286 | hasField = true; |
| 1287 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1288 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1289 | Classes.insert(R->getCanonicalDecl()); |
| 1290 | } |
| 1291 | else |
| 1292 | hasNonInstance = true; |
| 1293 | } |
| 1294 | |
| 1295 | // If we didn't find any instance members, it can't be an implicit |
| 1296 | // member reference. |
| 1297 | if (Classes.empty()) |
| 1298 | return IMA_Static; |
| 1299 | |
| 1300 | // If the current context is not an instance method, it can't be |
| 1301 | // an implicit member reference. |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1302 | if (isStaticContext) { |
| 1303 | if (hasNonInstance) |
| 1304 | return IMA_Mixed_StaticContext; |
| 1305 | |
| 1306 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1307 | // C++0x [expr.prim.general]p10: |
| 1308 | // An id-expression that denotes a non-static data member or non-static |
| 1309 | // member function of a class can only be used: |
| 1310 | // (...) |
| 1311 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1312 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1313 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1314 | if (isUnevaluatedExpression) |
| 1315 | return IMA_Mixed_StaticContext; |
| 1316 | } |
| 1317 | |
| 1318 | return IMA_Error_StaticContext; |
| 1319 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1320 | |
| 1321 | // If we can prove that the current context is unrelated to all the |
| 1322 | // declaring classes, it can't be an implicit member reference (in |
| 1323 | // which case it's an error if any of those members are selected). |
| 1324 | if (IsProvablyNotDerivedFrom(SemaRef, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1325 | cast<CXXMethodDecl>(DC)->getParent(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1326 | Classes)) |
| 1327 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1328 | |
| 1329 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1330 | } |
| 1331 | |
| 1332 | /// Diagnose a reference to a field with no object available. |
| 1333 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1334 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1335 | NamedDecl *rep, |
| 1336 | const DeclarationNameInfo &nameInfo) { |
| 1337 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1338 | SourceRange Range(Loc); |
| 1339 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1340 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1341 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1342 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1343 | if (MD->isStatic()) { |
| 1344 | // "invalid use of member 'x' in static member function" |
| 1345 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1346 | << Range << nameInfo.getName(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1352 | << nameInfo.getName() << Range; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1353 | return; |
| 1354 | } |
| 1355 | |
| 1356 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1359 | /// Diagnose an empty lookup. |
| 1360 | /// |
| 1361 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1362 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1363 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1364 | DeclarationName Name = R.getLookupName(); |
| 1365 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1366 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1367 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1368 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1369 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1370 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1371 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1372 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1373 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1374 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1375 | // If the original lookup was an unqualified lookup, fake an |
| 1376 | // unqualified lookup. This is useful when (for example) the |
| 1377 | // original lookup would not have found something because it was a |
| 1378 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1379 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1380 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1381 | if (isa<CXXRecordDecl>(DC)) { |
| 1382 | LookupQualifiedName(R, DC); |
| 1383 | |
| 1384 | if (!R.empty()) { |
| 1385 | // Don't give errors about ambiguities in this lookup. |
| 1386 | R.suppressDiagnostics(); |
| 1387 | |
| 1388 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1389 | bool isInstance = CurMethod && |
| 1390 | CurMethod->isInstance() && |
| 1391 | DC == CurMethod->getParent(); |
| 1392 | |
| 1393 | // Give a code modification hint to insert 'this->'. |
| 1394 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1395 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1396 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1397 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1398 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1399 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1400 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1401 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1402 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1403 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1404 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1405 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1406 | R.getNameLoc(), DepThisType, false); |
| 1407 | TemplateArgumentListInfo TList; |
| 1408 | if (ULE->hasExplicitTemplateArgs()) |
| 1409 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1411 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1412 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1413 | CXXDependentScopeMemberExpr *DepExpr = |
| 1414 | CXXDependentScopeMemberExpr::Create( |
| 1415 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1416 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1417 | R.getLookupNameInfo(), &TList); |
| 1418 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1419 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1420 | // FIXME: we should be able to handle this case too. It is correct |
| 1421 | // to add this-> here. This is a workaround for PR7947. |
| 1422 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1423 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1424 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1425 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1426 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1427 | |
| 1428 | // Do we really want to note all of these? |
| 1429 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1430 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1431 | |
| 1432 | // Tell the callee to try to recover. |
| 1433 | return false; |
| 1434 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1435 | |
| 1436 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1437 | } |
| 1438 | } |
| 1439 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1440 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1441 | DeclarationName Corrected; |
Daniel Dunbar | dc32cdf | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1442 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1443 | if (!R.empty()) { |
| 1444 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1445 | if (SS.isEmpty()) |
| 1446 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1447 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1448 | R.getLookupName().getAsString()); |
| 1449 | else |
| 1450 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1451 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1452 | << SS.getRange() |
| 1453 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1454 | R.getLookupName().getAsString()); |
| 1455 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1456 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1457 | << ND->getDeclName(); |
| 1458 | |
| 1459 | // Tell the callee to try to recover. |
| 1460 | return false; |
| 1461 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1463 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1464 | // FIXME: If we ended up with a typo for a type name or |
| 1465 | // Objective-C class name, we're in trouble because the parser |
| 1466 | // is in the wrong place to recover. Suggest the typo |
| 1467 | // correction, but don't make it a fix-it since we're not going |
| 1468 | // to recover well anyway. |
| 1469 | if (SS.isEmpty()) |
| 1470 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1471 | else |
| 1472 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1473 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1474 | << SS.getRange(); |
| 1475 | |
| 1476 | // Don't try to recover; it won't work. |
| 1477 | return true; |
| 1478 | } |
| 1479 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1480 | // 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] | 1481 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1482 | if (SS.isEmpty()) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1483 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1484 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1485 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1486 | << Name << computeDeclContext(SS, false) << Corrected |
| 1487 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1488 | return true; |
| 1489 | } |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1490 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | // Emit a special diagnostic for failed member lookups. |
| 1494 | // FIXME: computing the declaration context might fail here (?) |
| 1495 | if (!SS.isEmpty()) { |
| 1496 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1497 | << Name << computeDeclContext(SS, false) |
| 1498 | << SS.getRange(); |
| 1499 | return true; |
| 1500 | } |
| 1501 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1502 | // Give up, we can't recover. |
| 1503 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1504 | return true; |
| 1505 | } |
| 1506 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1507 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1508 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1509 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1510 | if (!IDecl) |
| 1511 | return 0; |
| 1512 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1513 | if (!ClassImpDecl) |
| 1514 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1515 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1516 | if (!property) |
| 1517 | return 0; |
| 1518 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1519 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1520 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1521 | return 0; |
| 1522 | return property; |
| 1523 | } |
| 1524 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1525 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1526 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1527 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1528 | if (!IDecl) |
| 1529 | return false; |
| 1530 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1531 | if (!ClassImpDecl) |
| 1532 | return false; |
| 1533 | if (ObjCPropertyImplDecl *PIDecl |
| 1534 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1535 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1536 | PIDecl->getPropertyIvarDecl()) |
| 1537 | return false; |
| 1538 | |
| 1539 | return true; |
| 1540 | } |
| 1541 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1542 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1543 | LookupResult &Lookup, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1544 | IdentifierInfo *II, |
| 1545 | SourceLocation NameLoc) { |
| 1546 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1547 | bool LookForIvars; |
| 1548 | if (Lookup.empty()) |
| 1549 | LookForIvars = true; |
| 1550 | else if (CurMeth->isClassMethod()) |
| 1551 | LookForIvars = false; |
| 1552 | else |
| 1553 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1554 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1555 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1556 | if (!LookForIvars) |
| 1557 | return 0; |
| 1558 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1559 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1560 | if (!IDecl) |
| 1561 | return 0; |
| 1562 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1563 | if (!ClassImpDecl) |
| 1564 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1565 | bool DynamicImplSeen = false; |
| 1566 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1567 | if (!property) |
| 1568 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1569 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1570 | DynamicImplSeen = |
| 1571 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1572 | // property implementation has a designated ivar. No need to assume a new |
| 1573 | // one. |
| 1574 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1575 | return 0; |
| 1576 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1577 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1578 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1579 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1580 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1581 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1582 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1583 | (Expr *)0, true); |
| 1584 | ClassImpDecl->addDecl(Ivar); |
| 1585 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1586 | property->setPropertyIvarDecl(Ivar); |
| 1587 | return Ivar; |
| 1588 | } |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1592 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1593 | CXXScopeSpec &SS, |
| 1594 | UnqualifiedId &Id, |
| 1595 | bool HasTrailingLParen, |
| 1596 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1597 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1598 | "cannot be direct & operand and have a trailing lparen"); |
| 1599 | |
| 1600 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1601 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1602 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1603 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1604 | |
| 1605 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1606 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1607 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1608 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1609 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1610 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1611 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1612 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1613 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1614 | // C++ [temp.dep.expr]p3: |
| 1615 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1616 | // -- an identifier that was declared with a dependent type, |
| 1617 | // (note: handled after lookup) |
| 1618 | // -- a template-id that is dependent, |
| 1619 | // (note: handled in BuildTemplateIdExpr) |
| 1620 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1621 | // -- a nested-name-specifier that contains a class-name that |
| 1622 | // names a dependent type. |
| 1623 | // Determine whether this is a member of an unknown specialization; |
| 1624 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1625 | bool DependentID = false; |
| 1626 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1627 | Name.getCXXNameType()->isDependentType()) { |
| 1628 | DependentID = true; |
| 1629 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1630 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1631 | if (RequireCompleteDeclContext(SS, DC)) |
| 1632 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1633 | } else { |
| 1634 | DependentID = true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1638 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1639 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1640 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1641 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1642 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1643 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1644 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1645 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1646 | // Lookup the template name again to correctly establish the context in |
| 1647 | // which it was found. This is really unfortunate as we already did the |
| 1648 | // lookup to determine that it was a template name in the first place. If |
| 1649 | // this becomes a performance hit, we can work harder to preserve those |
| 1650 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1651 | bool MemberOfUnknownSpecialization; |
| 1652 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1653 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1654 | |
| 1655 | if (MemberOfUnknownSpecialization || |
| 1656 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1657 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1658 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1659 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1660 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1661 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1663 | // If the result might be in a dependent base class, this is a dependent |
| 1664 | // id-expression. |
| 1665 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1666 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1667 | TemplateArgs); |
| 1668 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1669 | // If this reference is in an Objective-C method, then we need to do |
| 1670 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1671 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1672 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1673 | if (E.isInvalid()) |
| 1674 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1676 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1677 | return Owned(Ex); |
| 1678 | |
| 1679 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1680 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1681 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1682 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) { |
| 1683 | if (const ObjCPropertyDecl *Property = |
| 1684 | canSynthesizeProvisionalIvar(II)) { |
| 1685 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1686 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1687 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1688 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1689 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1690 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1691 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1692 | // for further use, this must be set to false if in class method. |
| 1693 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1694 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1695 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1696 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1697 | if (R.isAmbiguous()) |
| 1698 | return ExprError(); |
| 1699 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1700 | // Determine whether this name might be a candidate for |
| 1701 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1702 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1703 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1704 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1706 | // in C90, extension in C99, forbidden in C++). |
| 1707 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1708 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1709 | if (D) R.addDecl(D); |
| 1710 | } |
| 1711 | |
| 1712 | // If this name wasn't predeclared and if this is not a function |
| 1713 | // call, diagnose the problem. |
| 1714 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1715 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1716 | return ExprError(); |
| 1717 | |
| 1718 | assert(!R.empty() && |
| 1719 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1720 | |
| 1721 | // If we found an Objective-C instance variable, let |
| 1722 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1723 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1724 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1725 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1726 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1727 | assert(E.isInvalid() || E.get()); |
| 1728 | return move(E); |
| 1729 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1732 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1733 | // This is guaranteed from this point on. |
| 1734 | assert(!R.empty() || ADL); |
| 1735 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1736 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1737 | // C++ [class.mfct.non-static]p3: |
| 1738 | // When an id-expression that is not part of a class member access |
| 1739 | // syntax and not used to form a pointer to member is used in the |
| 1740 | // body of a non-static member function of class X, if name lookup |
| 1741 | // resolves the name in the id-expression to a non-static non-type |
| 1742 | // member of some class C, the id-expression is transformed into a |
| 1743 | // class member access expression using (*this) as the |
| 1744 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1745 | // |
| 1746 | // But we don't actually need to do this for '&' operands if R |
| 1747 | // resolved to a function or overloaded function set, because the |
| 1748 | // expression is ill-formed if it actually works out to be a |
| 1749 | // non-static member function: |
| 1750 | // |
| 1751 | // C++ [expr.ref]p4: |
| 1752 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1753 | // [t]he expression can be used only as the left-hand operand of a |
| 1754 | // member function call. |
| 1755 | // |
| 1756 | // There are other safeguards against such uses, but it's important |
| 1757 | // to get this right here so that we don't end up making a |
| 1758 | // spuriously dependent expression if we're inside a dependent |
| 1759 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1760 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1761 | bool MightBeImplicitMember; |
| 1762 | if (!isAddressOfOperand) |
| 1763 | MightBeImplicitMember = true; |
| 1764 | else if (!SS.isEmpty()) |
| 1765 | MightBeImplicitMember = false; |
| 1766 | else if (R.isOverloadedResult()) |
| 1767 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1768 | else if (R.isUnresolvableResult()) |
| 1769 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1770 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1771 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1772 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1773 | |
| 1774 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1775 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1776 | } |
| 1777 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1778 | if (TemplateArgs) |
| 1779 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1780 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1781 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1782 | } |
| 1783 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1784 | /// Builds an expression which might be an implicit member expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1785 | ExprResult |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1786 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1787 | LookupResult &R, |
| 1788 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1789 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1790 | case IMA_Instance: |
| 1791 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1792 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1793 | case IMA_Mixed: |
| 1794 | case IMA_Mixed_Unrelated: |
| 1795 | case IMA_Unresolved: |
| 1796 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1797 | |
| 1798 | case IMA_Static: |
| 1799 | case IMA_Mixed_StaticContext: |
| 1800 | case IMA_Unresolved_StaticContext: |
| 1801 | if (TemplateArgs) |
| 1802 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1803 | return BuildDeclarationNameExpr(SS, R, false); |
| 1804 | |
| 1805 | case IMA_Error_StaticContext: |
| 1806 | case IMA_Error_Unrelated: |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1807 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 1808 | R.getLookupNameInfo()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1809 | return ExprError(); |
| 1810 | } |
| 1811 | |
| 1812 | llvm_unreachable("unexpected instance member access kind"); |
| 1813 | return ExprError(); |
| 1814 | } |
| 1815 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1816 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1817 | /// declaration name, generally during template instantiation. |
| 1818 | /// There's a large number of things which don't need to be done along |
| 1819 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1820 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1821 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1822 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1823 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1824 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1825 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1826 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1827 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1828 | return ExprError(); |
| 1829 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1830 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1831 | LookupQualifiedName(R, DC); |
| 1832 | |
| 1833 | if (R.isAmbiguous()) |
| 1834 | return ExprError(); |
| 1835 | |
| 1836 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1837 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1838 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1839 | return ExprError(); |
| 1840 | } |
| 1841 | |
| 1842 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1843 | } |
| 1844 | |
| 1845 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1846 | /// detected that we're currently inside an ObjC method. Perform some |
| 1847 | /// additional lookup. |
| 1848 | /// |
| 1849 | /// Ideally, most of this would be done by lookup, but there's |
| 1850 | /// actually quite a lot of extra work involved. |
| 1851 | /// |
| 1852 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1853 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1855 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1856 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1857 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1858 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1859 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1860 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1861 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1862 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1863 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1864 | |
| 1865 | // If we're in a class method, we don't normally want to look for |
| 1866 | // ivars. But if we don't find anything else, and there's an |
| 1867 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1868 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1869 | |
| 1870 | bool LookForIvars; |
| 1871 | if (Lookup.empty()) |
| 1872 | LookForIvars = true; |
| 1873 | else if (IsClassMethod) |
| 1874 | LookForIvars = false; |
| 1875 | else |
| 1876 | LookForIvars = (Lookup.isSingleResult() && |
| 1877 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1878 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1879 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1880 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1881 | ObjCInterfaceDecl *ClassDeclared; |
| 1882 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1883 | // Diagnose using an ivar in a class method. |
| 1884 | if (IsClassMethod) |
| 1885 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1886 | << IV->getDeclName()); |
| 1887 | |
| 1888 | // If we're referencing an invalid decl, just return this as a silent |
| 1889 | // error node. The error diagnostic was already emitted on the decl. |
| 1890 | if (IV->isInvalidDecl()) |
| 1891 | return ExprError(); |
| 1892 | |
| 1893 | // Check if referencing a field with __attribute__((deprecated)). |
| 1894 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1895 | return ExprError(); |
| 1896 | |
| 1897 | // Diagnose the use of an ivar outside of the declaring class. |
| 1898 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1899 | ClassDeclared != IFace) |
| 1900 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1901 | |
| 1902 | // FIXME: This should use a new expr for a direct reference, don't |
| 1903 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1904 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1905 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1906 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1907 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1908 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1909 | SelfName, false, false); |
| 1910 | if (SelfExpr.isInvalid()) |
| 1911 | return ExprError(); |
| 1912 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1913 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1914 | if (SelfExpr.isInvalid()) |
| 1915 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1916 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1917 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | b8f17ab | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 1918 | Expr *base = SelfExpr.take(); |
| 1919 | base = base->IgnoreParenImpCasts(); |
| 1920 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 1921 | const NamedDecl *ND = DE->getDecl(); |
| 1922 | if (!isa<ImplicitParamDecl>(ND)) { |
| 1923 | Diag(Loc, diag::error_implicit_ivar_access) |
| 1924 | << IV->getDeclName(); |
| 1925 | Diag(ND->getLocation(), diag::note_declared_at); |
| 1926 | return ExprError(); |
| 1927 | } |
| 1928 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1929 | return Owned(new (Context) |
| 1930 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1931 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1932 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1933 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1934 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1935 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1936 | ObjCInterfaceDecl *ClassDeclared; |
| 1937 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1938 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1939 | IFace == ClassDeclared) |
| 1940 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1941 | } |
| 1942 | } |
| 1943 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1944 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1945 | // FIXME. Consolidate this with similar code in LookupName. |
| 1946 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1947 | if (!(getLangOptions().CPlusPlus && |
| 1948 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1949 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1950 | S, Lookup.isForRedeclaration(), |
| 1951 | Lookup.getNameLoc()); |
| 1952 | if (D) Lookup.addDecl(D); |
| 1953 | } |
| 1954 | } |
| 1955 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1956 | // Sentinel value saying that we didn't do anything special. |
| 1957 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1958 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1959 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1960 | /// \brief Cast a base object to a member's actual type. |
| 1961 | /// |
| 1962 | /// Logically this happens in three phases: |
| 1963 | /// |
| 1964 | /// * First we cast from the base type to the naming class. |
| 1965 | /// The naming class is the class into which we were looking |
| 1966 | /// when we found the member; it's the qualifier type if a |
| 1967 | /// qualifier was provided, and otherwise it's the base type. |
| 1968 | /// |
| 1969 | /// * Next we cast from the naming class to the declaring class. |
| 1970 | /// If the member we found was brought into a class's scope by |
| 1971 | /// a using declaration, this is that class; otherwise it's |
| 1972 | /// the class declaring the member. |
| 1973 | /// |
| 1974 | /// * Finally we cast from the declaring class to the "true" |
| 1975 | /// declaring class of the member. This conversion does not |
| 1976 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1977 | ExprResult |
| 1978 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1979 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1980 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1981 | NamedDecl *Member) { |
| 1982 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1983 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1984 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1986 | QualType DestRecordType; |
| 1987 | QualType DestType; |
| 1988 | QualType FromRecordType; |
| 1989 | QualType FromType = From->getType(); |
| 1990 | bool PointerConversions = false; |
| 1991 | if (isa<FieldDecl>(Member)) { |
| 1992 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1994 | if (FromType->getAs<PointerType>()) { |
| 1995 | DestType = Context.getPointerType(DestRecordType); |
| 1996 | FromRecordType = FromType->getPointeeType(); |
| 1997 | PointerConversions = true; |
| 1998 | } else { |
| 1999 | DestType = DestRecordType; |
| 2000 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2001 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2002 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2003 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2004 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2006 | DestType = Method->getThisType(Context); |
| 2007 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2008 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2009 | if (FromType->getAs<PointerType>()) { |
| 2010 | FromRecordType = FromType->getPointeeType(); |
| 2011 | PointerConversions = true; |
| 2012 | } else { |
| 2013 | FromRecordType = FromType; |
| 2014 | DestType = DestRecordType; |
| 2015 | } |
| 2016 | } else { |
| 2017 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2018 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2019 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2021 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2022 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2023 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2024 | // If the unqualified types are the same, no conversion is necessary. |
| 2025 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2026 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2027 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2028 | SourceRange FromRange = From->getSourceRange(); |
| 2029 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2030 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2031 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2033 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2034 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2035 | // class name. |
| 2036 | // |
| 2037 | // If the member was a qualified name and the qualified referred to a |
| 2038 | // specific base subobject type, we'll cast to that intermediate type |
| 2039 | // first and then to the object in which the member is declared. That allows |
| 2040 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2041 | // |
| 2042 | // class Base { public: int x; }; |
| 2043 | // class Derived1 : public Base { }; |
| 2044 | // class Derived2 : public Base { }; |
| 2045 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2046 | // |
| 2047 | // void VeryDerived::f() { |
| 2048 | // x = 17; // error: ambiguous base subobjects |
| 2049 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2050 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2051 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2052 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2053 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2054 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2055 | |
| 2056 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2057 | |
| 2058 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2059 | // type of the object type, in which case we just ignore it. |
| 2060 | // Otherwise build the appropriate casts. |
| 2061 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2062 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2063 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2064 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2065 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2067 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2068 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2069 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2070 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2071 | |
| 2072 | FromType = QType; |
| 2073 | FromRecordType = QRecordType; |
| 2074 | |
| 2075 | // If the qualifier type was the same as the destination type, |
| 2076 | // we're done. |
| 2077 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2078 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2079 | } |
| 2080 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2081 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2082 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2083 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2084 | // If we actually found the member through a using declaration, cast |
| 2085 | // down to the using declaration's type. |
| 2086 | // |
| 2087 | // Pointer equality is fine here because only one declaration of a |
| 2088 | // class ever has member declarations. |
| 2089 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2090 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2091 | QualType URecordType = Context.getTypeDeclType( |
| 2092 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2093 | |
| 2094 | // We only need to do this if the naming-class to declaring-class |
| 2095 | // conversion is non-trivial. |
| 2096 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2097 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2098 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2099 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2100 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2101 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2102 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2103 | QualType UType = URecordType; |
| 2104 | if (PointerConversions) |
| 2105 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2106 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2107 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2108 | FromType = UType; |
| 2109 | FromRecordType = URecordType; |
| 2110 | } |
| 2111 | |
| 2112 | // We don't do access control for the conversion from the |
| 2113 | // declaring class to the true declaring class. |
| 2114 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2115 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2116 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2117 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2118 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2119 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2120 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2121 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2122 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2123 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2124 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2125 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2127 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2129 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2130 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2131 | const DeclarationNameInfo &MemberNameInfo, |
| 2132 | QualType Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2133 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2134 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2135 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2136 | Member, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2137 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2140 | static ExprResult |
| 2141 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2142 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2143 | DeclAccessPair FoundDecl, |
| 2144 | const DeclarationNameInfo &MemberNameInfo) { |
| 2145 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2146 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2147 | // that *x is always an l-value), except that if the base isn't |
| 2148 | // an ordinary object then we must have an rvalue. |
| 2149 | ExprValueKind VK = VK_LValue; |
| 2150 | ExprObjectKind OK = OK_Ordinary; |
| 2151 | if (!IsArrow) { |
| 2152 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2153 | VK = BaseExpr->getValueKind(); |
| 2154 | else |
| 2155 | VK = VK_RValue; |
| 2156 | } |
| 2157 | if (VK != VK_RValue && Field->isBitField()) |
| 2158 | OK = OK_BitField; |
| 2159 | |
| 2160 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2161 | QualType MemberType = Field->getType(); |
| 2162 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2163 | MemberType = Ref->getPointeeType(); |
| 2164 | VK = VK_LValue; |
| 2165 | } else { |
| 2166 | QualType BaseType = BaseExpr->getType(); |
| 2167 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2168 | |
| 2169 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2170 | |
| 2171 | // GC attributes are never picked up by members. |
| 2172 | BaseQuals.removeObjCGCAttr(); |
| 2173 | |
| 2174 | // CVR attributes from the base are picked up by members, |
| 2175 | // except that 'mutable' members don't pick up 'const'. |
| 2176 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2177 | |
| 2178 | Qualifiers MemberQuals |
| 2179 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2180 | |
| 2181 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2182 | assert(!MemberQuals.hasAddressSpace()); |
| 2183 | |
| 2184 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2185 | if (Combined != MemberQuals) |
| 2186 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2187 | } |
| 2188 | |
| 2189 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2190 | ExprResult Base = |
| 2191 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2192 | FoundDecl, Field); |
| 2193 | if (Base.isInvalid()) |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2194 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2195 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2196 | Field, FoundDecl, MemberNameInfo, |
| 2197 | MemberType, VK, OK)); |
| 2198 | } |
| 2199 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2200 | /// Builds an implicit member access expression. The current context |
| 2201 | /// is known to be an instance method, and the given unqualified lookup |
| 2202 | /// set is known to contain only instance members, at least one of which |
| 2203 | /// is from an appropriate type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2204 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2205 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2206 | LookupResult &R, |
| 2207 | const TemplateArgumentListInfo *TemplateArgs, |
| 2208 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2209 | assert(!R.empty() && !R.isAmbiguous()); |
| 2210 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2211 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2212 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2213 | // We may have found a field within an anonymous union or struct |
| 2214 | // (C++ [class.union]). |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2215 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2216 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2217 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2218 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2219 | // If this is known to be an instance access, go ahead and build an |
| 2220 | // implicit 'this' expression now. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2221 | // 'this' expression now. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2222 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 2223 | assert(method && "didn't correctly pre-flight capture of 'this'"); |
| 2224 | |
| 2225 | QualType thisType = method->getThisType(Context); |
| 2226 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2227 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2228 | SourceLocation Loc = R.getNameLoc(); |
| 2229 | if (SS.getRange().isValid()) |
| 2230 | Loc = SS.getRange().getBegin(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2231 | baseExpr = new (Context) CXXThisExpr(loc, thisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2234 | return BuildMemberReferenceExpr(baseExpr, thisType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2235 | /*OpLoc*/ SourceLocation(), |
| 2236 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2237 | SS, |
| 2238 | /*FirstQualifierInScope*/ 0, |
| 2239 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2242 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2243 | const LookupResult &R, |
| 2244 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2245 | // Only when used directly as the postfix-expression of a call. |
| 2246 | if (!HasTrailingLParen) |
| 2247 | return false; |
| 2248 | |
| 2249 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2250 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2251 | return false; |
| 2252 | |
| 2253 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2254 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2255 | return false; |
| 2256 | |
| 2257 | // Turn off ADL when we find certain kinds of declarations during |
| 2258 | // normal lookup: |
| 2259 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2260 | NamedDecl *D = *I; |
| 2261 | |
| 2262 | // C++0x [basic.lookup.argdep]p3: |
| 2263 | // -- a declaration of a class member |
| 2264 | // Since using decls preserve this property, we check this on the |
| 2265 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2266 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2267 | return false; |
| 2268 | |
| 2269 | // C++0x [basic.lookup.argdep]p3: |
| 2270 | // -- a block-scope function declaration that is not a |
| 2271 | // using-declaration |
| 2272 | // NOTE: we also trigger this for function templates (in fact, we |
| 2273 | // don't check the decl type at all, since all other decl types |
| 2274 | // turn off ADL anyway). |
| 2275 | if (isa<UsingShadowDecl>(D)) |
| 2276 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2277 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2278 | return false; |
| 2279 | |
| 2280 | // C++0x [basic.lookup.argdep]p3: |
| 2281 | // -- a declaration that is neither a function or a function |
| 2282 | // template |
| 2283 | // And also for builtin functions. |
| 2284 | if (isa<FunctionDecl>(D)) { |
| 2285 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2286 | |
| 2287 | // But also builtin functions. |
| 2288 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2289 | return false; |
| 2290 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2291 | return false; |
| 2292 | } |
| 2293 | |
| 2294 | return true; |
| 2295 | } |
| 2296 | |
| 2297 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2298 | /// Diagnoses obvious problems with the use of the given declaration |
| 2299 | /// as an expression. This is only actually called for lookups that |
| 2300 | /// were not overloaded, and it doesn't promise that the declaration |
| 2301 | /// will in fact be used. |
| 2302 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 2303 | if (isa<TypedefDecl>(D)) { |
| 2304 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2305 | return true; |
| 2306 | } |
| 2307 | |
| 2308 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2309 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2310 | return true; |
| 2311 | } |
| 2312 | |
| 2313 | if (isa<NamespaceDecl>(D)) { |
| 2314 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2315 | return true; |
| 2316 | } |
| 2317 | |
| 2318 | return false; |
| 2319 | } |
| 2320 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2321 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2322 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2323 | LookupResult &R, |
| 2324 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2325 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2326 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2327 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2328 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2329 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2330 | |
| 2331 | // We only need to check the declaration if there's exactly one |
| 2332 | // result, because in the overloaded case the results can only be |
| 2333 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2334 | if (R.isSingleResult() && |
| 2335 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2336 | return ExprError(); |
| 2337 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2338 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2339 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2340 | // we've picked a target. |
| 2341 | R.suppressDiagnostics(); |
| 2342 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2343 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2344 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2345 | SS.getWithLocInContext(Context), |
| 2346 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2347 | NeedsADL, R.isOverloadedResult(), |
| 2348 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2349 | |
| 2350 | return Owned(ULE); |
| 2351 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2352 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2353 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2354 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2355 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2356 | const DeclarationNameInfo &NameInfo, |
| 2357 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2358 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2359 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2360 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2361 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2362 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2363 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2364 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2365 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2366 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2367 | // Specifically diagnose references to class templates that are missing |
| 2368 | // a template argument list. |
| 2369 | Diag(Loc, diag::err_template_decl_ref) |
| 2370 | << Template << SS.getRange(); |
| 2371 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2372 | return ExprError(); |
| 2373 | } |
| 2374 | |
| 2375 | // Make sure that we're referring to a value. |
| 2376 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2377 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2378 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2379 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2380 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2381 | return ExprError(); |
| 2382 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2384 | // Check whether this declaration can be used. Note that we suppress |
| 2385 | // this check when we're going to perform argument-dependent lookup |
| 2386 | // on this function name, because this might not be the function |
| 2387 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2388 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2389 | return ExprError(); |
| 2390 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2391 | // Only create DeclRefExpr's for valid Decl's. |
| 2392 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2393 | return ExprError(); |
| 2394 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2395 | // Handle members of anonymous structs and unions. If we got here, |
| 2396 | // and the reference is to a class member indirect field, then this |
| 2397 | // must be the subject of a pointer-to-member expression. |
| 2398 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2399 | if (!indirectField->isCXXClassMember()) |
| 2400 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2401 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2402 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2403 | // If the identifier reference is inside a block, and it refers to a value |
| 2404 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2405 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2406 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2407 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2408 | // We do not do this for things like enum constants, global variables, etc, |
| 2409 | // as they do not get snapshotted. |
| 2410 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2411 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2412 | case CR_Error: |
| 2413 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2414 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2415 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2416 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2417 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2418 | |
| 2419 | case CR_CaptureByRef: |
| 2420 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2421 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2422 | |
| 2423 | case CR_NoCapture: { |
| 2424 | // If this reference is not in a block or if the referenced |
| 2425 | // variable is within the block, create a normal DeclRefExpr. |
| 2426 | |
| 2427 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2428 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2429 | |
| 2430 | switch (D->getKind()) { |
| 2431 | // Ignore all the non-ValueDecl kinds. |
| 2432 | #define ABSTRACT_DECL(kind) |
| 2433 | #define VALUE(type, base) |
| 2434 | #define DECL(type, base) \ |
| 2435 | case Decl::type: |
| 2436 | #include "clang/AST/DeclNodes.inc" |
| 2437 | llvm_unreachable("invalid value decl kind"); |
| 2438 | return ExprError(); |
| 2439 | |
| 2440 | // These shouldn't make it here. |
| 2441 | case Decl::ObjCAtDefsField: |
| 2442 | case Decl::ObjCIvar: |
| 2443 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2444 | return ExprError(); |
| 2445 | |
| 2446 | // Enum constants are always r-values and never references. |
| 2447 | // Unresolved using declarations are dependent. |
| 2448 | case Decl::EnumConstant: |
| 2449 | case Decl::UnresolvedUsingValue: |
| 2450 | valueKind = VK_RValue; |
| 2451 | break; |
| 2452 | |
| 2453 | // Fields and indirect fields that got here must be for |
| 2454 | // pointer-to-member expressions; we just call them l-values for |
| 2455 | // internal consistency, because this subexpression doesn't really |
| 2456 | // exist in the high-level semantics. |
| 2457 | case Decl::Field: |
| 2458 | case Decl::IndirectField: |
| 2459 | assert(getLangOptions().CPlusPlus && |
| 2460 | "building reference to field in C?"); |
| 2461 | |
| 2462 | // These can't have reference type in well-formed programs, but |
| 2463 | // for internal consistency we do this anyway. |
| 2464 | type = type.getNonReferenceType(); |
| 2465 | valueKind = VK_LValue; |
| 2466 | break; |
| 2467 | |
| 2468 | // Non-type template parameters are either l-values or r-values |
| 2469 | // depending on the type. |
| 2470 | case Decl::NonTypeTemplateParm: { |
| 2471 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2472 | type = reftype->getPointeeType(); |
| 2473 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2474 | break; |
| 2475 | } |
| 2476 | |
| 2477 | // For non-references, we need to strip qualifiers just in case |
| 2478 | // the template parameter was declared as 'const int' or whatever. |
| 2479 | valueKind = VK_RValue; |
| 2480 | type = type.getUnqualifiedType(); |
| 2481 | break; |
| 2482 | } |
| 2483 | |
| 2484 | case Decl::Var: |
| 2485 | // In C, "extern void blah;" is valid and is an r-value. |
| 2486 | if (!getLangOptions().CPlusPlus && |
| 2487 | !type.hasQualifiers() && |
| 2488 | type->isVoidType()) { |
| 2489 | valueKind = VK_RValue; |
| 2490 | break; |
| 2491 | } |
| 2492 | // fallthrough |
| 2493 | |
| 2494 | case Decl::ImplicitParam: |
| 2495 | case Decl::ParmVar: |
| 2496 | // These are always l-values. |
| 2497 | valueKind = VK_LValue; |
| 2498 | type = type.getNonReferenceType(); |
| 2499 | break; |
| 2500 | |
| 2501 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2502 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2503 | |
| 2504 | // If we're referring to a function with an __unknown_anytype |
| 2505 | // result type, make the entire expression __unknown_anytype. |
| 2506 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2507 | type = Context.UnknownAnyTy; |
| 2508 | valueKind = VK_RValue; |
| 2509 | break; |
| 2510 | } |
| 2511 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2512 | // Functions are l-values in C++. |
| 2513 | if (getLangOptions().CPlusPlus) { |
| 2514 | valueKind = VK_LValue; |
| 2515 | break; |
| 2516 | } |
| 2517 | |
| 2518 | // C99 DR 316 says that, if a function type comes from a |
| 2519 | // function definition (without a prototype), that type is only |
| 2520 | // used for checking compatibility. Therefore, when referencing |
| 2521 | // the function, we pretend that we don't have the full function |
| 2522 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2523 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2524 | isa<FunctionProtoType>(fty)) |
| 2525 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2526 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2527 | |
| 2528 | // Functions are r-values in C. |
| 2529 | valueKind = VK_RValue; |
| 2530 | break; |
| 2531 | } |
| 2532 | |
| 2533 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2534 | // If we're referring to a method with an __unknown_anytype |
| 2535 | // result type, make the entire expression __unknown_anytype. |
| 2536 | // This should only be possible with a type written directly. |
| 2537 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2538 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2539 | type = Context.UnknownAnyTy; |
| 2540 | valueKind = VK_RValue; |
| 2541 | break; |
| 2542 | } |
| 2543 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2544 | // C++ methods are l-values if static, r-values if non-static. |
| 2545 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2546 | valueKind = VK_LValue; |
| 2547 | break; |
| 2548 | } |
| 2549 | // fallthrough |
| 2550 | |
| 2551 | case Decl::CXXConversion: |
| 2552 | case Decl::CXXDestructor: |
| 2553 | case Decl::CXXConstructor: |
| 2554 | valueKind = VK_RValue; |
| 2555 | break; |
| 2556 | } |
| 2557 | |
| 2558 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2559 | } |
| 2560 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2561 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2562 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2563 | llvm_unreachable("unknown capture result"); |
| 2564 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2567 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2568 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2569 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2570 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2571 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2572 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2573 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2574 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2575 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2577 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2578 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2579 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2580 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2581 | if (!currentDecl && getCurBlock()) |
| 2582 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2583 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2584 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2585 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2586 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2587 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2588 | QualType ResTy; |
| 2589 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2590 | ResTy = Context.DependentTy; |
| 2591 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2592 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2593 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2594 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2595 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2596 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2597 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2598 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2601 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2602 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2603 | bool Invalid = false; |
| 2604 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2605 | if (Invalid) |
| 2606 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2607 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2608 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2609 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2610 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2611 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2612 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2613 | QualType Ty; |
| 2614 | if (!getLangOptions().CPlusPlus) |
| 2615 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2616 | else if (Literal.isWide()) |
| 2617 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2618 | else if (Literal.isMultiChar()) |
| 2619 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2620 | else |
| 2621 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2622 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2623 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2624 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2625 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2626 | } |
| 2627 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2628 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2629 | // 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] | 2630 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2631 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2632 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2633 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2634 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2635 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2636 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2638 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2639 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2640 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2641 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2642 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2643 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2644 | bool Invalid = false; |
| 2645 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2646 | if (Invalid) |
| 2647 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2648 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2649 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2650 | Tok.getLocation(), PP); |
| 2651 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2652 | return ExprError(); |
| 2653 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2654 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2655 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2656 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2657 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2658 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2659 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2660 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2661 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2662 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2663 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2664 | |
| 2665 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2666 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2667 | using llvm::APFloat; |
| 2668 | APFloat Val(Format); |
| 2669 | |
| 2670 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2671 | |
| 2672 | // Overflow is always an error, but underflow is only an error if |
| 2673 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2674 | if ((result & APFloat::opOverflow) || |
| 2675 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2676 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2677 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2678 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2679 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2680 | APFloat::getLargest(Format).toString(buffer); |
| 2681 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2682 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2683 | APFloat::getSmallest(Format).toString(buffer); |
| 2684 | } |
| 2685 | |
| 2686 | Diag(Tok.getLocation(), diagnostic) |
| 2687 | << Ty |
| 2688 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2689 | } |
| 2690 | |
| 2691 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2692 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2693 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2694 | if (Ty == Context.DoubleTy) { |
| 2695 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2696 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2697 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2698 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2699 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2700 | } |
| 2701 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2702 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2703 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2704 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2705 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2706 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2707 | // long long is a C99 feature. |
| 2708 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2709 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2710 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2711 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2712 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2713 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2714 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2715 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2716 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2717 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2718 | Ty = Context.UnsignedLongLongTy; |
| 2719 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2720 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2721 | } else { |
| 2722 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2723 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2724 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2726 | // be an unsigned int. |
| 2727 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2728 | |
| 2729 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2730 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2731 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2732 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2733 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2734 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2735 | // Does it fit in a unsigned int? |
| 2736 | if (ResultVal.isIntN(IntSize)) { |
| 2737 | // Does it fit in a signed int? |
| 2738 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2739 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2740 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2741 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2742 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2743 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2744 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2745 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2746 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2747 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2748 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2749 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2750 | // Does it fit in a unsigned long? |
| 2751 | if (ResultVal.isIntN(LongSize)) { |
| 2752 | // Does it fit in a signed long? |
| 2753 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2754 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2755 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2756 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2757 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2758 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2762 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2763 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2764 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2765 | // Does it fit in a unsigned long long? |
| 2766 | if (ResultVal.isIntN(LongLongSize)) { |
| 2767 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2768 | // To be compatible with MSVC, hex integer literals ending with the |
| 2769 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2770 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2771 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2772 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2773 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2774 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2775 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2778 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2779 | // If we still couldn't decide a type, we probably have something that |
| 2780 | // 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] | 2781 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2782 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2783 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2784 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2785 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2786 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2787 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2788 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2790 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2791 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2792 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2793 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2794 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2796 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2797 | |
| 2798 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2801 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2802 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2803 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2804 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 2808 | /// See C99 6.3.2.1p[2-4] for more details. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2809 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2810 | SourceLocation OpLoc, |
| 2811 | SourceRange ExprRange, |
| 2812 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2813 | if (exprType->isDependentType()) |
| 2814 | return false; |
| 2815 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2816 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2817 | // the result is the size of the referenced type." |
| 2818 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2819 | // result shall be the alignment of the referenced type." |
| 2820 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2821 | exprType = Ref->getPointeeType(); |
| 2822 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2823 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2824 | // scalar or vector data type argument..." |
| 2825 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2826 | // type (C99 6.2.5p18) or void. |
| 2827 | if (ExprKind == UETT_VecStep) { |
| 2828 | if (!(exprType->isArithmeticType() || exprType->isVoidType() || |
| 2829 | exprType->isVectorType())) { |
| 2830 | Diag(OpLoc, diag::err_vecstep_non_scalar_vector_type) |
| 2831 | << exprType << ExprRange; |
| 2832 | return true; |
| 2833 | } |
| 2834 | } |
| 2835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2836 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2837 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2838 | // alignof(function) is allowed as an extension. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2839 | if (ExprKind == UETT_SizeOf) |
| 2840 | Diag(OpLoc, diag::ext_sizeof_function_type) |
| 2841 | << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2842 | return false; |
| 2843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2845 | // Allow sizeof(void)/alignof(void) as an extension. vec_step(void) is not |
| 2846 | // an extension, as void is a built-in scalar type (OpenCL 1.1 6.1.1). |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2847 | if (exprType->isVoidType()) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2848 | if (ExprKind != UETT_VecStep) |
| 2849 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 2850 | << ExprKind << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2851 | return false; |
| 2852 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2854 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2855 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2856 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2857 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2859 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2860 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2861 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2862 | << exprType << (ExprKind == UETT_SizeOf) |
| 2863 | << ExprRange; |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2864 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 2865 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2867 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2870 | static bool CheckAlignOfExpr(Sema &S, Expr *E, SourceLocation OpLoc, |
| 2871 | SourceRange ExprRange) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2872 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2873 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2874 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2875 | if (isa<DeclRefExpr>(E)) |
| 2876 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2877 | |
| 2878 | // Cannot know anything else if the expression is dependent. |
| 2879 | if (E->isTypeDependent()) |
| 2880 | return false; |
| 2881 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2882 | if (E->getBitField()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2883 | S. Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2884 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2885 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2886 | |
| 2887 | // Alignment of a field access is always okay, so long as it isn't a |
| 2888 | // bit-field. |
| 2889 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2890 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2891 | return false; |
| 2892 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2893 | return S.CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 2894 | UETT_AlignOf); |
| 2895 | } |
| 2896 | |
| 2897 | bool Sema::CheckVecStepExpr(Expr *E, SourceLocation OpLoc, |
| 2898 | SourceRange ExprRange) { |
| 2899 | E = E->IgnoreParens(); |
| 2900 | |
| 2901 | // Cannot know anything else if the expression is dependent. |
| 2902 | if (E->isTypeDependent()) |
| 2903 | return false; |
| 2904 | |
| 2905 | return CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 2906 | UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2909 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2910 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2911 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2912 | SourceLocation OpLoc, |
| 2913 | UnaryExprOrTypeTrait ExprKind, |
| 2914 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2915 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2916 | return ExprError(); |
| 2917 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2918 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2919 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2920 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2921 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2922 | return ExprError(); |
| 2923 | |
| 2924 | // 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] | 2925 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2926 | Context.getSizeType(), |
| 2927 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /// \brief Build a sizeof or alignof expression given an expression |
| 2931 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2932 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2933 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2934 | UnaryExprOrTypeTrait ExprKind, |
| 2935 | SourceRange R) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2936 | // Verify that the operand is valid. |
| 2937 | bool isInvalid = false; |
| 2938 | if (E->isTypeDependent()) { |
| 2939 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2940 | } else if (ExprKind == UETT_AlignOf) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2941 | isInvalid = CheckAlignOfExpr(*this, E, OpLoc, R); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2942 | } else if (ExprKind == UETT_VecStep) { |
| 2943 | isInvalid = CheckVecStepExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2944 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2945 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 2946 | isInvalid = true; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2947 | } else if (E->getType()->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2948 | ExprResult PE = CheckPlaceholderExpr(E); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2949 | if (PE.isInvalid()) return ExprError(); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2950 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind, R); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2951 | } else { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2952 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, R, |
| 2953 | UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | if (isInvalid) |
| 2957 | return ExprError(); |
| 2958 | |
| 2959 | // 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] | 2960 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, E, |
| 2961 | Context.getSizeType(), |
| 2962 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2963 | } |
| 2964 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2965 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2966 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2967 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2968 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2969 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2970 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2971 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2972 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2973 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2974 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2975 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2976 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2977 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2978 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2981 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2982 | ExprResult Result |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2983 | = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind, |
| 2984 | ArgEx->getSourceRange()); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2985 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2986 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2987 | } |
| 2988 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2989 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2990 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2991 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2992 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2993 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2994 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2995 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2996 | V = S.DefaultLvalueConversion(V.take()); |
| 2997 | if (V.isInvalid()) |
| 2998 | return QualType(); |
| 2999 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3000 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3001 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3002 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3003 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3004 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3005 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3006 | if (V.get()->getType()->isArithmeticType()) |
| 3007 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3009 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3010 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3011 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3012 | if (PR.get() != V.get()) { |
| 3013 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3014 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3017 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3018 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3019 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3020 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3024 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3025 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3026 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3027 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3028 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3029 | switch (Kind) { |
| 3030 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3031 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3032 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3033 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3034 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3035 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3038 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3039 | /// having l-value type. These are: |
| 3040 | /// - 'void', but not qualified void |
| 3041 | /// - function types |
| 3042 | /// |
| 3043 | /// The exact rule here is C99 6.3.2.1: |
| 3044 | /// An lvalue is an expression with an object type or an incomplete |
| 3045 | /// type other than void. |
| 3046 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3047 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3048 | T->isFunctionType()); |
| 3049 | } |
| 3050 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3051 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3052 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3053 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3054 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3055 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3056 | if (Result.isInvalid()) return ExprError(); |
| 3057 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3058 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3059 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3061 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3062 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3063 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3064 | Context.DependentTy, |
| 3065 | VK_LValue, OK_Ordinary, |
| 3066 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3069 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3070 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3071 | LHSExp->getType()->isEnumeralType() || |
| 3072 | RHSExp->getType()->isRecordType() || |
| 3073 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3074 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3077 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3081 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3082 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3083 | Expr *Idx, SourceLocation RLoc) { |
| 3084 | Expr *LHSExp = Base; |
| 3085 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3086 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3087 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3088 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3089 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3090 | if (Result.isInvalid()) |
| 3091 | return ExprError(); |
| 3092 | LHSExp = Result.take(); |
| 3093 | } |
| 3094 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3095 | if (Result.isInvalid()) |
| 3096 | return ExprError(); |
| 3097 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3098 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3099 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3100 | ExprValueKind VK = VK_LValue; |
| 3101 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3103 | // 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] | 3104 | // 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] | 3105 | // 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] | 3106 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3107 | Expr *BaseExpr, *IndexExpr; |
| 3108 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3109 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3110 | BaseExpr = LHSExp; |
| 3111 | IndexExpr = RHSExp; |
| 3112 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3113 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3114 | BaseExpr = LHSExp; |
| 3115 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3116 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3117 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3118 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3119 | BaseExpr = RHSExp; |
| 3120 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3121 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3123 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3124 | BaseExpr = LHSExp; |
| 3125 | IndexExpr = RHSExp; |
| 3126 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3127 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3128 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3129 | // Handle the uncommon case of "123[Ptr]". |
| 3130 | BaseExpr = RHSExp; |
| 3131 | IndexExpr = LHSExp; |
| 3132 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3133 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3134 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3135 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3136 | VK = LHSExp->getValueKind(); |
| 3137 | if (VK != VK_RValue) |
| 3138 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3140 | // FIXME: need to deal with const... |
| 3141 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3142 | } else if (LHSTy->isArrayType()) { |
| 3143 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3144 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3145 | // wasn't promoted because of the C90 rule that doesn't |
| 3146 | // allow promoting non-lvalue arrays. Warn, then |
| 3147 | // force the promotion here. |
| 3148 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3149 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3150 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3151 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3152 | LHSTy = LHSExp->getType(); |
| 3153 | |
| 3154 | BaseExpr = LHSExp; |
| 3155 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3156 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3157 | } else if (RHSTy->isArrayType()) { |
| 3158 | // Same as previous, except for 123[f().a] case |
| 3159 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3160 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3161 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3162 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3163 | RHSTy = RHSExp->getType(); |
| 3164 | |
| 3165 | BaseExpr = RHSExp; |
| 3166 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3167 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3168 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3169 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3170 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3171 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3172 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3173 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3174 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3175 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3176 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3177 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3178 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3179 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3180 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3181 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3182 | // 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] | 3183 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3184 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3185 | // incomplete types are not object types. |
| 3186 | if (ResultType->isFunctionType()) { |
| 3187 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3188 | << ResultType << BaseExpr->getSourceRange(); |
| 3189 | return ExprError(); |
| 3190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3192 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3193 | // GNU extension: subscripting on pointer to void |
| 3194 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3195 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3196 | |
| 3197 | // C forbids expressions of unqualified void type from being l-values. |
| 3198 | // See IsCForbiddenLValueType. |
| 3199 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3200 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3202 | PDiag(diag::err_subscript_incomplete_type) |
| 3203 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3204 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3206 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3207 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3208 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3209 | << ResultType << BaseExpr->getSourceRange(); |
| 3210 | return ExprError(); |
| 3211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3212 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3213 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3214 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3215 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3216 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3217 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3220 | /// Check an ext-vector component access expression. |
| 3221 | /// |
| 3222 | /// VK should be set in advance to the value kind of the base |
| 3223 | /// expression. |
| 3224 | static QualType |
| 3225 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3226 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3227 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3228 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3229 | // see FIXME there. |
| 3230 | // |
| 3231 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3232 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3233 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3234 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3235 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3236 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3237 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3238 | // 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] | 3239 | // special names that indicate a subset of exactly half the elements are |
| 3240 | // to be selected. |
| 3241 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3242 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3243 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3244 | // 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] | 3245 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3246 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3247 | bool HasRepeated = false; |
| 3248 | bool HasIndex[16] = {}; |
| 3249 | |
| 3250 | int Idx; |
| 3251 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3252 | // Check that we've found one of the special components, or that the component |
| 3253 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3254 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3255 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3256 | HalvingSwizzle = true; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3257 | } else if (!HexSwizzle && |
| 3258 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3259 | do { |
| 3260 | if (HasIndex[Idx]) HasRepeated = true; |
| 3261 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3262 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3263 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3264 | } else { |
| 3265 | if (HexSwizzle) compStr++; |
| 3266 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3267 | if (HasIndex[Idx]) HasRepeated = true; |
| 3268 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3269 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3270 | } |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3271 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3272 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3273 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3274 | // We didn't get to the end of the string. This means the component names |
| 3275 | // 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] | 3276 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3277 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3278 | return QualType(); |
| 3279 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3280 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3281 | // Ensure no component accessor exceeds the width of the vector type it |
| 3282 | // operates on. |
| 3283 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3284 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3285 | |
| 3286 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3287 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3288 | |
| 3289 | while (*compStr) { |
| 3290 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3291 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3292 | << baseType << SourceRange(CompLoc); |
| 3293 | return QualType(); |
| 3294 | } |
| 3295 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3296 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3297 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3298 | // 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] | 3299 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3300 | // 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] | 3301 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3302 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3303 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3304 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3305 | if (HexSwizzle) |
| 3306 | CompSize--; |
| 3307 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3308 | if (CompSize == 1) |
| 3309 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3310 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3311 | if (HasRepeated) VK = VK_RValue; |
| 3312 | |
| 3313 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3314 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3315 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3316 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3317 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3318 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3319 | } |
| 3320 | 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] | 3321 | } |
| 3322 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3323 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3324 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3325 | const Selector &Sel, |
| 3326 | ASTContext &Context) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3327 | if (Member) |
| 3328 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3329 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3330 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3331 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3332 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3333 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3334 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3335 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3336 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3337 | return D; |
| 3338 | } |
| 3339 | return 0; |
| 3340 | } |
| 3341 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3342 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3343 | IdentifierInfo *Member, |
| 3344 | const Selector &Sel, |
| 3345 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3346 | // Check protocols on qualified interfaces. |
| 3347 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3348 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3349 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3350 | if (Member) |
| 3351 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3352 | GDecl = PD; |
| 3353 | break; |
| 3354 | } |
| 3355 | // 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] | 3356 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3357 | GDecl = OMD; |
| 3358 | break; |
| 3359 | } |
| 3360 | } |
| 3361 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3362 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3363 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3364 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3365 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3366 | Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3367 | if (GDecl) |
| 3368 | return GDecl; |
| 3369 | } |
| 3370 | } |
| 3371 | return GDecl; |
| 3372 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3373 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3374 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3375 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3376 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3377 | const CXXScopeSpec &SS, |
| 3378 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3379 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3380 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3381 | // Even in dependent contexts, try to diagnose base expressions with |
| 3382 | // obviously wrong types, e.g.: |
| 3383 | // |
| 3384 | // T* t; |
| 3385 | // t.f; |
| 3386 | // |
| 3387 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3388 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3389 | // allows this, while still reporting an error if T is a struct pointer. |
| 3390 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3391 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3392 | if (PT && (!getLangOptions().ObjC1 || |
| 3393 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3394 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3395 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3396 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3397 | return ExprError(); |
| 3398 | } |
| 3399 | } |
| 3400 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3401 | assert(BaseType->isDependentType() || |
| 3402 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3403 | isDependentScopeSpecifier(SS)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3404 | |
| 3405 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3406 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3407 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3408 | IsArrow, OpLoc, |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3409 | SS.getWithLocInContext(Context), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3410 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3411 | NameInfo, TemplateArgs)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | /// We know that the given qualified member reference points only to |
| 3415 | /// declarations which do not belong to the static type of the base |
| 3416 | /// expression. Diagnose the problem. |
| 3417 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3418 | Expr *BaseExpr, |
| 3419 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3420 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3421 | NamedDecl *rep, |
| 3422 | const DeclarationNameInfo &nameInfo) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3423 | // If this is an implicit member access, use a different set of |
| 3424 | // diagnostics. |
| 3425 | if (!BaseExpr) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3426 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3427 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3428 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3429 | << SS.getRange() << rep << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3430 | } |
| 3431 | |
| 3432 | // Check whether the declarations we found through a nested-name |
| 3433 | // specifier in a member expression are actually members of the base |
| 3434 | // type. The restriction here is: |
| 3435 | // |
| 3436 | // C++ [expr.ref]p2: |
| 3437 | // ... In these cases, the id-expression shall name a |
| 3438 | // member of the class or of one of its base classes. |
| 3439 | // |
| 3440 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3441 | // an unrelated class, and for us to find an overload set including |
| 3442 | // decls from classes which are not superclasses, as long as the decl |
| 3443 | // we actually pick through overload resolution is from a superclass. |
| 3444 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3445 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3446 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3447 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3448 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3449 | if (!BaseRT) { |
| 3450 | // We can't check this yet because the base type is still |
| 3451 | // dependent. |
| 3452 | assert(BaseType->isDependentType()); |
| 3453 | return false; |
| 3454 | } |
| 3455 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3456 | |
| 3457 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3458 | // If this is an implicit member reference and we find a |
| 3459 | // non-instance member, it's not an error. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3460 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3461 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3462 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3463 | // 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] | 3464 | DeclContext *DC = (*I)->getDeclContext(); |
| 3465 | while (DC->isTransparentContext()) |
| 3466 | DC = DC->getParent(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | 9d4bb94 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3468 | if (!DC->isRecord()) |
| 3469 | continue; |
| 3470 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3471 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3472 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3473 | |
| 3474 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3475 | return false; |
| 3476 | } |
| 3477 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3478 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3479 | R.getRepresentativeDecl(), |
| 3480 | R.getLookupNameInfo()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3481 | return true; |
| 3482 | } |
| 3483 | |
| 3484 | static bool |
| 3485 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3486 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3487 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3488 | bool HasTemplateArgs) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3489 | RecordDecl *RDecl = RTy->getDecl(); |
| 3490 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3491 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3492 | << BaseRange)) |
| 3493 | return true; |
| 3494 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3495 | if (HasTemplateArgs) { |
| 3496 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3497 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3498 | |
| 3499 | bool MOUS; |
| 3500 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3501 | return false; |
| 3502 | } |
| 3503 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3504 | DeclContext *DC = RDecl; |
| 3505 | if (SS.isSet()) { |
| 3506 | // If the member name was a qualified-id, look into the |
| 3507 | // nested-name-specifier. |
| 3508 | DC = SemaRef.computeDeclContext(SS, false); |
| 3509 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3510 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3511 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3512 | << SS.getRange() << DC; |
| 3513 | return true; |
| 3514 | } |
| 3515 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3516 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3517 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3518 | if (!isa<TypeDecl>(DC)) { |
| 3519 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3520 | << DC << SS.getRange(); |
| 3521 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3522 | } |
| 3523 | } |
| 3524 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3525 | // The record definition is complete, now look up the member. |
| 3526 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3528 | if (!R.empty()) |
| 3529 | return false; |
| 3530 | |
| 3531 | // We didn't find anything with the given name, so try to correct |
| 3532 | // for typos. |
| 3533 | DeclarationName Name = R.getLookupName(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3534 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3535 | !R.empty() && |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3536 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3537 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3538 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3539 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3540 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3541 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3542 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3543 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3544 | return false; |
| 3545 | } else { |
| 3546 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3547 | R.setLookupName(Name); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3550 | return false; |
| 3551 | } |
| 3552 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3553 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3554 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3555 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3556 | CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3557 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3558 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3559 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3560 | if (BaseType->isDependentType() || |
| 3561 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3562 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3563 | IsArrow, OpLoc, |
| 3564 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3565 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3566 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3567 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3568 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3569 | // Implicit member accesses. |
| 3570 | if (!Base) { |
| 3571 | QualType RecordTy = BaseType; |
| 3572 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3573 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3574 | RecordTy->getAs<RecordType>(), |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3575 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3576 | return ExprError(); |
| 3577 | |
| 3578 | // Explicit member accesses. |
| 3579 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3580 | ExprResult BaseResult = Owned(Base); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3581 | ExprResult Result = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3582 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3583 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3584 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3585 | if (BaseResult.isInvalid()) |
| 3586 | return ExprError(); |
| 3587 | Base = BaseResult.take(); |
| 3588 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3589 | if (Result.isInvalid()) { |
| 3590 | Owned(Base); |
| 3591 | return ExprError(); |
| 3592 | } |
| 3593 | |
| 3594 | if (Result.get()) |
| 3595 | return move(Result); |
Sebastian Redl | f3e6337 | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3596 | |
| 3597 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3598 | BaseType = Base->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3601 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3602 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3603 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3606 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3607 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3608 | SourceLocation OpLoc, bool IsArrow, |
| 3609 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3610 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3611 | LookupResult &R, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3612 | const TemplateArgumentListInfo *TemplateArgs, |
| 3613 | bool SuppressQualifierCheck) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3614 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3615 | if (IsArrow) { |
| 3616 | assert(BaseType->isPointerType()); |
| 3617 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3618 | } |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3619 | R.setBaseObjectType(BaseType); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3620 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3621 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3622 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3623 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3624 | |
| 3625 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3626 | return ExprError(); |
| 3627 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3628 | if (R.empty()) { |
| 3629 | // Rederive where we looked up. |
| 3630 | DeclContext *DC = (SS.isSet() |
| 3631 | ? computeDeclContext(SS, false) |
| 3632 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3633 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3634 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3635 | << MemberName << DC |
| 3636 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3637 | return ExprError(); |
| 3638 | } |
| 3639 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3640 | // Diagnose lookups that find only declarations from a non-base |
| 3641 | // type. This is possible for either qualified lookups (which may |
| 3642 | // have been qualified with an unrelated type) or implicit member |
| 3643 | // expressions (which were found with unqualified lookup and thus |
| 3644 | // may have come from an enclosing scope). Note that it's okay for |
| 3645 | // lookup to find declarations from a non-base type as long as those |
| 3646 | // aren't the ones picked by overload resolution. |
| 3647 | if ((SS.isSet() || !BaseExpr || |
| 3648 | (isa<CXXThisExpr>(BaseExpr) && |
| 3649 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3650 | !SuppressQualifierCheck && |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3651 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3652 | return ExprError(); |
| 3653 | |
| 3654 | // Construct an unresolved result if we in fact got an unresolved |
| 3655 | // result. |
| 3656 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3657 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3658 | // pick a member. |
| 3659 | R.suppressDiagnostics(); |
| 3660 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3661 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3662 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3663 | BaseExpr, BaseExprType, |
| 3664 | IsArrow, OpLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3665 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3666 | MemberNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3667 | TemplateArgs, R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3668 | |
| 3669 | return Owned(MemExpr); |
| 3670 | } |
| 3671 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3672 | assert(R.isSingleResult()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3673 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3674 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 3675 | |
| 3676 | // FIXME: diagnose the presence of template arguments now. |
| 3677 | |
| 3678 | // If the decl being referenced had an error, return an error for this |
| 3679 | // sub-expr without emitting another error, in order to avoid cascading |
| 3680 | // error cases. |
| 3681 | if (MemberDecl->isInvalidDecl()) |
| 3682 | return ExprError(); |
| 3683 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3684 | // Handle the implicit-member-access case. |
| 3685 | if (!BaseExpr) { |
| 3686 | // 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] | 3687 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3688 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 3690 | SourceLocation Loc = R.getNameLoc(); |
| 3691 | if (SS.getRange().isValid()) |
| 3692 | Loc = SS.getRange().getBegin(); |
| 3693 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3694 | } |
| 3695 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3696 | bool ShouldCheckUse = true; |
| 3697 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 3698 | // Don't diagnose the use of a virtual member function unless it's |
| 3699 | // explicitly qualified. |
| 3700 | if (MD->isVirtual() && !SS.isSet()) |
| 3701 | ShouldCheckUse = false; |
| 3702 | } |
| 3703 | |
| 3704 | // Check the use of this member. |
| 3705 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 3706 | Owned(BaseExpr); |
| 3707 | return ExprError(); |
| 3708 | } |
| 3709 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3710 | // Perform a property load on the base regardless of whether we |
| 3711 | // actually need it for the declaration. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3712 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 3713 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 3714 | if (Result.isInvalid()) |
| 3715 | return ExprError(); |
| 3716 | BaseExpr = Result.take(); |
| 3717 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3718 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3719 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 3720 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 3721 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3722 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3723 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 3724 | // We may have found a field within an anonymous union or struct |
| 3725 | // (C++ [class.union]). |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3726 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3727 | BaseExpr, OpLoc); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3728 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3729 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 3730 | MarkDeclarationReferenced(MemberLoc, Var); |
| 3731 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3732 | Var, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3733 | Var->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3734 | VK_LValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3735 | } |
| 3736 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3737 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3738 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3739 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3740 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3741 | MemberFn->getType(), |
| 3742 | MemberFn->isInstance() ? VK_RValue : VK_LValue, |
| 3743 | OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3744 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3745 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3746 | |
| 3747 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 3748 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3749 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3750 | Enum, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3751 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | Owned(BaseExpr); |
| 3755 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3756 | // We found something that we didn't expect. Complain. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3757 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3758 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3759 | << MemberName << BaseType << int(IsArrow); |
| 3760 | else |
| 3761 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 3762 | << MemberName << BaseType << int(IsArrow); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3763 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3764 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 3765 | << MemberName; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3766 | R.suppressDiagnostics(); |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3767 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3770 | /// Given that normal member access failed on the given expression, |
| 3771 | /// and given that the expression's type involves builtin-id or |
| 3772 | /// builtin-Class, decide whether substituting in the redefinition |
| 3773 | /// types would be profitable. The redefinition type is whatever |
| 3774 | /// this translation unit tried to typedef to id/Class; we store |
| 3775 | /// 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] | 3776 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3777 | const ObjCObjectPointerType *opty |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3778 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3779 | if (!opty) return false; |
| 3780 | |
| 3781 | const ObjCObjectType *ty = opty->getObjectType(); |
| 3782 | |
| 3783 | QualType redef; |
| 3784 | if (ty->isObjCId()) { |
| 3785 | redef = S.Context.ObjCIdRedefinitionType; |
| 3786 | } else if (ty->isObjCClass()) { |
| 3787 | redef = S.Context.ObjCClassRedefinitionType; |
| 3788 | } else { |
| 3789 | return false; |
| 3790 | } |
| 3791 | |
| 3792 | // Do the substitution as long as the redefinition type isn't just a |
| 3793 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 3794 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 3795 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 3796 | return false; |
| 3797 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3798 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3799 | return true; |
| 3800 | } |
| 3801 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3802 | /// Look up the given member of the given non-type-dependent |
| 3803 | /// expression. This can return in one of two ways: |
| 3804 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 3805 | /// assume that lookup was performed and the results written into |
| 3806 | /// the provided structure. It will take over from there. |
| 3807 | /// * Otherwise, the returned expression will be produced in place of |
| 3808 | /// an ordinary member expression. |
| 3809 | /// |
| 3810 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 3811 | /// fixed for ObjC++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3812 | ExprResult |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3813 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3814 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3815 | CXXScopeSpec &SS, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3816 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3817 | assert(BaseExpr.get() && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3818 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 3819 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3820 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3821 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3822 | if (IsArrow) { |
| 3823 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 3824 | if (BaseExpr.isInvalid()) |
| 3825 | return ExprError(); |
| 3826 | } |
| 3827 | |
| 3828 | QualType BaseType = BaseExpr.get()->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3829 | assert(!BaseType->isDependentType()); |
| 3830 | |
| 3831 | DeclarationName MemberName = R.getLookupName(); |
| 3832 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3833 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3834 | // For later type-checking purposes, turn arrow accesses into dot |
| 3835 | // accesses. The only access type we support that doesn't follow |
| 3836 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 3837 | // and those never use arrows, so this is unaffected. |
| 3838 | if (IsArrow) { |
| 3839 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3840 | BaseType = Ptr->getPointeeType(); |
| 3841 | else if (const ObjCObjectPointerType *Ptr |
| 3842 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3843 | BaseType = Ptr->getPointeeType(); |
| 3844 | else if (BaseType->isRecordType()) { |
| 3845 | // Recover from arrow accesses to records, e.g.: |
| 3846 | // struct MyRecord foo; |
| 3847 | // foo->bar |
| 3848 | // This is actually well-formed in C++ if MyRecord has an |
| 3849 | // overloaded operator->, but that should have been dealt with |
| 3850 | // by now. |
| 3851 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3852 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3853 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 3854 | IsArrow = false; |
| 3855 | } else { |
| 3856 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3857 | << BaseType << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3858 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3862 | // Handle field access to simple records. |
| 3863 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3864 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3865 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 3866 | return ExprError(); |
| 3867 | |
| 3868 | // Returning valid-but-null is how we indicate to the caller that |
| 3869 | // the lookup result was filled in. |
| 3870 | return Owned((Expr*) 0); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3871 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3872 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3873 | // Handle ivar access to Objective-C objects. |
| 3874 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3875 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3876 | |
| 3877 | // There are three cases for the base type: |
| 3878 | // - builtin id (qualified or unqualified) |
| 3879 | // - builtin Class (qualified or unqualified) |
| 3880 | // - an interface |
| 3881 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 3882 | if (!IDecl) { |
| 3883 | // There's an implicit 'isa' ivar on all objects. |
| 3884 | // But we only actually find it this way on objects of type 'id', |
| 3885 | // apparently. |
| 3886 | if (OTy->isObjCId() && Member->isStr("isa")) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3887 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3888 | Context.getObjCClassType())); |
| 3889 | |
| 3890 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3891 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3892 | ObjCImpDecl, HasTemplateArgs); |
| 3893 | goto fail; |
| 3894 | } |
| 3895 | |
| 3896 | ObjCInterfaceDecl *ClassDeclared; |
| 3897 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 3898 | |
| 3899 | if (!IV) { |
| 3900 | // Attempt to correct for typos in ivar names. |
| 3901 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3902 | LookupMemberName); |
| 3903 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 3904 | IsArrow ? CTC_ObjCIvarLookup |
| 3905 | : CTC_ObjCPropertyLookup) && |
| 3906 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 3907 | Diag(R.getNameLoc(), |
| 3908 | diag::err_typecheck_member_reference_ivar_suggest) |
| 3909 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 3910 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3911 | IV->getNameAsString()); |
| 3912 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 3913 | << IV->getDeclName(); |
| 3914 | } else { |
| 3915 | Res.clear(); |
| 3916 | Res.setLookupName(Member); |
| 3917 | |
| 3918 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 3919 | << IDecl->getDeclName() << MemberName |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3920 | << BaseExpr.get()->getSourceRange(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3921 | return ExprError(); |
| 3922 | } |
| 3923 | } |
| 3924 | |
| 3925 | // If the decl being referenced had an error, return an error for this |
| 3926 | // sub-expr without emitting another error, in order to avoid cascading |
| 3927 | // error cases. |
| 3928 | if (IV->isInvalidDecl()) |
| 3929 | return ExprError(); |
| 3930 | |
| 3931 | // Check whether we can reference this field. |
| 3932 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 3933 | return ExprError(); |
| 3934 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 3935 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 3936 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 3937 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 3938 | ClassOfMethodDecl = MD->getClassInterface(); |
| 3939 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 3940 | // Case of a c-function declared inside an objc implementation. |
| 3941 | // FIXME: For a c-style function nested inside an objc implementation |
| 3942 | // class, there is no implementation context available, so we pass |
| 3943 | // down the context as argument to this routine. Ideally, this context |
| 3944 | // need be passed down in the AST node and somehow calculated from the |
| 3945 | // AST for a function decl. |
| 3946 | if (ObjCImplementationDecl *IMPD = |
| 3947 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 3948 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 3949 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 3950 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 3951 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 3952 | } |
| 3953 | |
| 3954 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 3955 | if (ClassDeclared != IDecl || |
| 3956 | ClassOfMethodDecl != ClassDeclared) |
| 3957 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 3958 | << IV->getDeclName(); |
| 3959 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 3960 | // @protected |
| 3961 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 3962 | << IV->getDeclName(); |
| 3963 | } |
| 3964 | |
| 3965 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3966 | MemberLoc, BaseExpr.take(), |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3967 | IsArrow)); |
| 3968 | } |
| 3969 | |
| 3970 | // Objective-C property access. |
| 3971 | const ObjCObjectPointerType *OPT; |
| 3972 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 3973 | // This actually uses the base as an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3974 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 3975 | if (BaseExpr.isInvalid()) |
| 3976 | return ExprError(); |
| 3977 | |
| 3978 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3979 | |
| 3980 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3981 | |
| 3982 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 3983 | |
| 3984 | // id, with and without qualifiers. |
| 3985 | if (OT->isObjCId()) { |
| 3986 | // Check protocols on qualified interfaces. |
| 3987 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 3988 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 3989 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 3990 | // Check the use of this declaration |
| 3991 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3992 | return ExprError(); |
| 3993 | |
| 3994 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 3995 | VK_LValue, |
| 3996 | OK_ObjCProperty, |
| 3997 | MemberLoc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3998 | BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4002 | // Check the use of this method. |
| 4003 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4004 | return ExprError(); |
| 4005 | Selector SetterSel = |
| 4006 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4007 | PP.getSelectorTable(), Member); |
| 4008 | ObjCMethodDecl *SMD = 0; |
| 4009 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4010 | SetterSel, Context)) |
| 4011 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 4012 | QualType PType = OMD->getSendResultType(); |
| 4013 | |
| 4014 | ExprValueKind VK = VK_LValue; |
| 4015 | if (!getLangOptions().CPlusPlus && |
| 4016 | IsCForbiddenLValueType(Context, PType)) |
| 4017 | VK = VK_RValue; |
| 4018 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4019 | |
| 4020 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4021 | VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | MemberLoc, BaseExpr.take())); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4023 | } |
| 4024 | } |
Fariborz Jahanian | 4eb7f69 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4025 | // Use of id.member can only be for a property reference. Do not |
| 4026 | // use the 'id' redefinition in this case. |
| 4027 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4028 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4029 | ObjCImpDecl, HasTemplateArgs); |
| 4030 | |
| 4031 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4032 | << MemberName << BaseType); |
| 4033 | } |
| 4034 | |
| 4035 | // 'Class', unqualified only. |
| 4036 | if (OT->isObjCClass()) { |
| 4037 | // Only works in a method declaration (??!). |
| 4038 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4039 | if (!MD) { |
| 4040 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4041 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4042 | ObjCImpDecl, HasTemplateArgs); |
| 4043 | |
| 4044 | goto fail; |
| 4045 | } |
| 4046 | |
| 4047 | // Also must look for a getter name which uses property syntax. |
| 4048 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4049 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4050 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4051 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4052 | // Check the use of this method. |
| 4053 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4054 | return ExprError(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4055 | } else |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4056 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4057 | // If we found a getter then this may be a valid dot-reference, we |
| 4058 | // will look for the matching setter, in case it is needed. |
| 4059 | Selector SetterSel = |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4060 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4061 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4062 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4063 | if (!Setter) { |
| 4064 | // If this reference is in an @implementation, also check for 'private' |
| 4065 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4066 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4067 | } |
| 4068 | // Look through local category implementations associated with the class. |
| 4069 | if (!Setter) |
| 4070 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4071 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4072 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4073 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4074 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4075 | if (Getter || Setter) { |
| 4076 | QualType PType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4077 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4078 | ExprValueKind VK = VK_LValue; |
| 4079 | if (Getter) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4080 | PType = Getter->getSendResultType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4081 | if (!getLangOptions().CPlusPlus && |
| 4082 | IsCForbiddenLValueType(Context, PType)) |
| 4083 | VK = VK_RValue; |
| 4084 | } else { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4085 | // Get the expression type from Setter's incoming parameter. |
| 4086 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4087 | } |
| 4088 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4089 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4090 | // FIXME: we must check that the setter has property type. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4091 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4092 | PType, VK, OK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4093 | MemberLoc, BaseExpr.take())); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4094 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4095 | |
| 4096 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4097 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4098 | ObjCImpDecl, HasTemplateArgs); |
| 4099 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4100 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4101 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4102 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4103 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4104 | // Normal property access. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4105 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4106 | SourceLocation(), QualType(), false); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4107 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4108 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4109 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4110 | if (BaseType->isExtVectorType()) { |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4111 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4112 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4113 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4114 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4115 | Member, MemberLoc); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4116 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4117 | return ExprError(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4118 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4119 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4120 | *Member, MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4121 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4122 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4123 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4124 | // not just a pointer to builtin-sel again. |
| 4125 | if (IsArrow && |
| 4126 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4127 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4128 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4129 | CK_BitCast); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4130 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4131 | ObjCImpDecl, HasTemplateArgs); |
| 4132 | } |
| 4133 | |
| 4134 | // Failure cases. |
| 4135 | fail: |
| 4136 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4137 | // Recover from dot accesses to pointers, e.g.: |
| 4138 | // type *foo; |
| 4139 | // foo.bar |
| 4140 | // This is actually well-formed in two cases: |
| 4141 | // - 'type' is an Objective C type |
| 4142 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4143 | // the appropriate pointer type |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4144 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4145 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4146 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4147 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4148 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4149 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4150 | |
| 4151 | // Recurse as an -> access. |
| 4152 | IsArrow = true; |
| 4153 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4154 | ObjCImpDecl, HasTemplateArgs); |
| 4155 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4158 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4159 | // because they forgot parentheses to call that function. |
| 4160 | bool TryCall = false; |
| 4161 | bool Overloaded = false; |
| 4162 | UnresolvedSet<8> AllOverloads; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(BaseExpr.get())) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4164 | AllOverloads.append(Overloads->decls_begin(), Overloads->decls_end()); |
| 4165 | TryCall = true; |
| 4166 | Overloaded = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4167 | } else if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(BaseExpr.get())) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4168 | if (FunctionDecl* Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 4169 | AllOverloads.addDecl(Fun); |
| 4170 | TryCall = true; |
| 4171 | } |
| 4172 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4173 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4174 | if (TryCall) { |
| 4175 | // Plunder the overload set for something that would make the member |
| 4176 | // expression valid. |
| 4177 | UnresolvedSet<4> ViableOverloads; |
| 4178 | bool HasViableZeroArgOverload = false; |
| 4179 | for (OverloadExpr::decls_iterator it = AllOverloads.begin(), |
| 4180 | DeclsEnd = AllOverloads.end(); it != DeclsEnd; ++it) { |
Matt Beaumont-Gay | fbe5994 | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4181 | // Our overload set may include TemplateDecls, which we'll ignore for the |
| 4182 | // purposes of determining whether we can issue a '()' fixit. |
| 4183 | if (const FunctionDecl *OverloadDecl = dyn_cast<FunctionDecl>(*it)) { |
| 4184 | QualType ResultTy = OverloadDecl->getResultType(); |
| 4185 | if ((!IsArrow && ResultTy->isRecordType()) || |
| 4186 | (IsArrow && ResultTy->isPointerType() && |
| 4187 | ResultTy->getPointeeType()->isRecordType())) { |
| 4188 | ViableOverloads.addDecl(*it); |
| 4189 | if (OverloadDecl->getMinRequiredArguments() == 0) { |
| 4190 | HasViableZeroArgOverload = true; |
| 4191 | } |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4192 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4196 | if (!HasViableZeroArgOverload || ViableOverloads.size() != 1) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4197 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | fbe5994 | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4198 | << (AllOverloads.size() > 1) << 0 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4199 | << BaseExpr.get()->getSourceRange(); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4200 | int ViableOverloadCount = ViableOverloads.size(); |
| 4201 | int I; |
| 4202 | for (I = 0; I < ViableOverloadCount; ++I) { |
| 4203 | // FIXME: Magic number for max shown overloads stolen from |
| 4204 | // OverloadCandidateSet::NoteCandidates. |
| 4205 | if (I >= 4 && Diags.getShowOverloads() == Diagnostic::Ovl_Best) { |
| 4206 | break; |
| 4207 | } |
| 4208 | Diag(ViableOverloads[I].getDecl()->getSourceRange().getBegin(), |
| 4209 | diag::note_member_ref_possible_intended_overload); |
| 4210 | } |
| 4211 | if (I != ViableOverloadCount) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4212 | Diag(BaseExpr.get()->getExprLoc(), diag::note_ovl_too_many_candidates) |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4213 | << int(ViableOverloadCount - I); |
| 4214 | } |
| 4215 | return ExprError(); |
| 4216 | } |
| 4217 | } else { |
| 4218 | // We don't have an expression that's convenient to get a Decl from, but we |
| 4219 | // can at least check if the type is "function of 0 arguments which returns |
| 4220 | // an acceptable type". |
| 4221 | const FunctionType *Fun = NULL; |
| 4222 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 4223 | if ((Fun = Ptr->getPointeeType()->getAs<FunctionType>())) { |
| 4224 | TryCall = true; |
| 4225 | } |
| 4226 | } else if ((Fun = BaseType->getAs<FunctionType>())) { |
| 4227 | TryCall = true; |
| 4228 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4229 | |
| 4230 | if (TryCall) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4231 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Fun)) { |
| 4232 | if (FPT->getNumArgs() == 0) { |
| 4233 | QualType ResultTy = Fun->getResultType(); |
| 4234 | TryCall = (!IsArrow && ResultTy->isRecordType()) || |
| 4235 | (IsArrow && ResultTy->isPointerType() && |
| 4236 | ResultTy->getPointeeType()->isRecordType()); |
| 4237 | } |
Matt Beaumont-Gay | 26ae5dd | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 4238 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4239 | } |
| 4240 | } |
| 4241 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4242 | if (TryCall) { |
| 4243 | // At this point, we know BaseExpr looks like it's potentially callable with |
| 4244 | // 0 arguments, and that it returns something of a reasonable type, so we |
| 4245 | // can emit a fixit and carry on pretending that BaseExpr was actually a |
| 4246 | // CallExpr. |
| 4247 | SourceLocation ParenInsertionLoc = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4248 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4249 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4250 | << int(Overloaded) << 1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4251 | << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4252 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4253 | ExprResult NewBase = ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4254 | MultiExprArg(*this, 0, 0), |
| 4255 | ParenInsertionLoc); |
| 4256 | if (NewBase.isInvalid()) |
| 4257 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4258 | BaseExpr = NewBase; |
| 4259 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4260 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4261 | ObjCImpDecl, HasTemplateArgs); |
| 4262 | } |
| 4263 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4264 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4265 | << BaseType << BaseExpr.get()->getSourceRange(); |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4266 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4267 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4270 | /// The main callback when the parser finds something like |
| 4271 | /// expression . [nested-name-specifier] identifier |
| 4272 | /// expression -> [nested-name-specifier] identifier |
| 4273 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4274 | /// possibilities, including destructor and operator references. |
| 4275 | /// |
| 4276 | /// \param OpKind either tok::arrow or tok::period |
| 4277 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4278 | /// is used to diagnose mis-uses of special members that can |
| 4279 | /// only be called |
| 4280 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4281 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4282 | /// aren't properly put in the context chain |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4283 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4284 | SourceLocation OpLoc, |
| 4285 | tok::TokenKind OpKind, |
| 4286 | CXXScopeSpec &SS, |
| 4287 | UnqualifiedId &Id, |
| 4288 | Decl *ObjCImpDecl, |
| 4289 | bool HasTrailingLParen) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4290 | if (SS.isSet() && SS.isInvalid()) |
| 4291 | return ExprError(); |
| 4292 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4293 | // Warn about the explicit constructor calls Microsoft extension. |
| 4294 | if (getLangOptions().Microsoft && |
| 4295 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4296 | Diag(Id.getSourceRange().getBegin(), |
| 4297 | diag::ext_ms_explicit_constructor_call); |
| 4298 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4299 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4300 | |
| 4301 | // Decompose the name into its component parts. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4302 | DeclarationNameInfo NameInfo; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4303 | const TemplateArgumentListInfo *TemplateArgs; |
| 4304 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4305 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4306 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4307 | DeclarationName Name = NameInfo.getName(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4308 | bool IsArrow = (OpKind == tok::arrow); |
| 4309 | |
| 4310 | NamedDecl *FirstQualifierInScope |
| 4311 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4312 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4313 | |
| 4314 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4315 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4316 | if (Result.isInvalid()) return ExprError(); |
| 4317 | Base = Result.take(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4318 | |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4319 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4320 | isDependentScopeSpecifier(SS)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4321 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4322 | IsArrow, OpLoc, |
| 4323 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4324 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4325 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4326 | LookupResult R(*this, NameInfo, LookupMemberName); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4327 | ExprResult BaseResult = Owned(Base); |
| 4328 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4329 | SS, ObjCImpDecl, TemplateArgs != 0); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4330 | if (BaseResult.isInvalid()) |
| 4331 | return ExprError(); |
| 4332 | Base = BaseResult.take(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4333 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4334 | if (Result.isInvalid()) { |
| 4335 | Owned(Base); |
| 4336 | return ExprError(); |
| 4337 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4338 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4339 | if (Result.get()) { |
| 4340 | // The only way a reference to a destructor can be used is to |
| 4341 | // immediately call it, which falls into this case. If the |
| 4342 | // next token is not a '(', produce a diagnostic and build the |
| 4343 | // call now. |
| 4344 | if (!HasTrailingLParen && |
| 4345 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4346 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4347 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4348 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4351 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4352 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4353 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4357 | } |
| 4358 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4359 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4360 | FunctionDecl *FD, |
| 4361 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4362 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4363 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4364 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4365 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4366 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4367 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4368 | return ExprError(); |
| 4369 | } |
| 4370 | |
| 4371 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4372 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4374 | // Instantiate the expression. |
| 4375 | MultiLevelTemplateArgumentList ArgList |
| 4376 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4377 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4378 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4379 | = ArgList.getInnermost(); |
| 4380 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4381 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4382 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4383 | ExprResult Result; |
| 4384 | { |
| 4385 | // C++ [dcl.fct.default]p5: |
| 4386 | // The names in the [default argument] expression are bound, and |
| 4387 | // the semantic constraints are checked, at the point where the |
| 4388 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4389 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4390 | Result = SubstExpr(UninstExpr, ArgList); |
| 4391 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4392 | if (Result.isInvalid()) |
| 4393 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4395 | // Check the expression as an initializer for the parameter. |
| 4396 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4397 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4398 | InitializationKind Kind |
| 4399 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4400 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4401 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4402 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4403 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4404 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4405 | MultiExprArg(*this, &ResultE, 1)); |
| 4406 | if (Result.isInvalid()) |
| 4407 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4409 | // Build the default argument expression. |
| 4410 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4411 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4414 | // If the default expression creates temporaries, we need to |
| 4415 | // push them to the current stack of expression temporaries so they'll |
| 4416 | // be properly destroyed. |
| 4417 | // FIXME: We should really be rebuilding the default argument with new |
| 4418 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4419 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4420 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4421 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4422 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4423 | ExprTemporaries.push_back(Temporary); |
| 4424 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4425 | |
| 4426 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4427 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4428 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4429 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4430 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4433 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4434 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4435 | /// function prototype Proto. Call is the call expression itself, and |
| 4436 | /// Fn is the function expression. For a C++ member function, this |
| 4437 | /// routine does not attempt to convert the object argument. Returns |
| 4438 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4439 | bool |
| 4440 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4441 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4442 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4443 | Expr **Args, unsigned NumArgs, |
| 4444 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4445 | // Bail out early if calling a builtin with custom typechecking. |
| 4446 | // We don't need to do this in the |
| 4447 | if (FDecl) |
| 4448 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4449 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4450 | return false; |
| 4451 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4452 | // 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] | 4453 | // assignment, to the types of the corresponding parameter, ... |
| 4454 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4455 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4456 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4457 | // If too few arguments are available (and we don't have default |
| 4458 | // arguments for the remaining parameters), don't make the call. |
| 4459 | if (NumArgs < NumArgsInProto) { |
| 4460 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4461 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4462 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4463 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4464 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | // If too many are passed and not variadic, error on the extras and drop |
| 4468 | // them. |
| 4469 | if (NumArgs > NumArgsInProto) { |
| 4470 | if (!Proto->isVariadic()) { |
| 4471 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4472 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4473 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4474 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4475 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4476 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4477 | |
| 4478 | // Emit the location of the prototype. |
| 4479 | if (FDecl && !FDecl->getBuiltinID()) |
| 4480 | Diag(FDecl->getLocStart(), |
| 4481 | diag::note_typecheck_call_too_many_args) |
| 4482 | << FDecl; |
| 4483 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4484 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4485 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4486 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4487 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4488 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4489 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4490 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4491 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4492 | if (Fn->getType()->isBlockPointerType()) |
| 4493 | CallType = VariadicBlock; // Block |
| 4494 | else if (isa<MemberExpr>(Fn)) |
| 4495 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4496 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4497 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4498 | if (Invalid) |
| 4499 | return true; |
| 4500 | unsigned TotalNumArgs = AllArgs.size(); |
| 4501 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4502 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4503 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4504 | return false; |
| 4505 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4506 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4507 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4508 | FunctionDecl *FDecl, |
| 4509 | const FunctionProtoType *Proto, |
| 4510 | unsigned FirstProtoArg, |
| 4511 | Expr **Args, unsigned NumArgs, |
| 4512 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4513 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4514 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4515 | unsigned NumArgsToCheck = NumArgs; |
| 4516 | bool Invalid = false; |
| 4517 | if (NumArgs != NumArgsInProto) |
| 4518 | // Use default arguments for missing arguments |
| 4519 | NumArgsToCheck = NumArgsInProto; |
| 4520 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4521 | // 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] | 4522 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4523 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4525 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4526 | if (ArgIx < NumArgs) { |
| 4527 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4528 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4529 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4530 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4531 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4532 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4533 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4534 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4535 | // Pass the argument |
| 4536 | ParmVarDecl *Param = 0; |
| 4537 | if (FDecl && i < FDecl->getNumParams()) |
| 4538 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4539 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4540 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4541 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4542 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4543 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4544 | SourceLocation(), |
| 4545 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4546 | if (ArgE.isInvalid()) |
| 4547 | return true; |
| 4548 | |
| 4549 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4550 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4551 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4552 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4553 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4554 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4555 | if (ArgExpr.isInvalid()) |
| 4556 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4557 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4558 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4559 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4560 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4561 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4562 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4563 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4564 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4565 | |
| 4566 | // Assume that extern "C" functions with variadic arguments that |
| 4567 | // return __unknown_anytype aren't *really* variadic. |
| 4568 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4569 | FDecl && FDecl->isExternC()) { |
| 4570 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4571 | ExprResult arg; |
| 4572 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4573 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4574 | else |
| 4575 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4576 | Invalid |= arg.isInvalid(); |
| 4577 | AllArgs.push_back(arg.take()); |
| 4578 | } |
| 4579 | |
| 4580 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4581 | } else { |
| 4582 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4583 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4584 | Invalid |= Arg.isInvalid(); |
| 4585 | AllArgs.push_back(Arg.take()); |
| 4586 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4587 | } |
| 4588 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4589 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4592 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4593 | /// to have a function type. |
| 4594 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4595 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4596 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4597 | /// This provides the location of the left/right parens and a list of comma |
| 4598 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4599 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4600 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4601 | MultiExprArg args, SourceLocation RParenLoc, |
| 4602 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4603 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4604 | |
| 4605 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4606 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4607 | if (Result.isInvalid()) return ExprError(); |
| 4608 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4609 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4610 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4611 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4612 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4613 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4614 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4615 | if (NumArgs > 0) { |
| 4616 | // Pseudo-destructor calls should not have any arguments. |
| 4617 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4618 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4619 | SourceRange(Args[0]->getLocStart(), |
| 4620 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4621 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4622 | NumArgs = 0; |
| 4623 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4625 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4626 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4627 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4629 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4630 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4631 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4632 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4633 | bool Dependent = false; |
| 4634 | if (Fn->isTypeDependent()) |
| 4635 | Dependent = true; |
| 4636 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4637 | Dependent = true; |
| 4638 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4639 | if (Dependent) { |
| 4640 | if (ExecConfig) { |
| 4641 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4642 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4643 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4644 | } else { |
| 4645 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4646 | Context.DependentTy, VK_RValue, |
| 4647 | RParenLoc)); |
| 4648 | } |
| 4649 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4650 | |
| 4651 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4652 | if (Fn->getType()->isRecordType()) |
| 4653 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4654 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4655 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4656 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4657 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4658 | if (result.isInvalid()) return ExprError(); |
| 4659 | Fn = result.take(); |
| 4660 | } |
| 4661 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4662 | Expr *NakedFn = Fn->IgnoreParens(); |
| 4663 | |
| 4664 | // Determine whether this is a call to an unresolved member function. |
| 4665 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4666 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 4667 | // an unresolved using declaration), it has to be an overloaded |
| 4668 | // function set, which means it must contain either multiple |
| 4669 | // declarations (all methods or method templates) or a single |
| 4670 | // method template. |
| 4671 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4672 | isa<FunctionTemplateDecl>( |
| 4673 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 4674 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4675 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4676 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4677 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4680 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4681 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4682 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4683 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4684 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4685 | RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4686 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4687 | |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4688 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4689 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4690 | if (BO->getOpcode() == BO_PtrMemD || |
| 4691 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 4692 | if (const FunctionProtoType *FPT |
| 4693 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4694 | QualType ResultTy = FPT->getCallResultType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4695 | ExprValueKind VK = Expr::getValueKindForType(FPT->getResultType()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4696 | |
Douglas Gregor | fdc13a0 | 2011-02-04 12:57:49 +0000 | [diff] [blame] | 4697 | // Check that the object type isn't more qualified than the |
| 4698 | // member function we're calling. |
| 4699 | Qualifiers FuncQuals = Qualifiers::fromCVRMask(FPT->getTypeQuals()); |
| 4700 | Qualifiers ObjectQuals |
| 4701 | = BO->getOpcode() == BO_PtrMemD |
| 4702 | ? BO->getLHS()->getType().getQualifiers() |
| 4703 | : BO->getLHS()->getType()->getAs<PointerType>() |
| 4704 | ->getPointeeType().getQualifiers(); |
| 4705 | |
| 4706 | Qualifiers Difference = ObjectQuals - FuncQuals; |
| 4707 | Difference.removeObjCGCAttr(); |
| 4708 | Difference.removeAddressSpace(); |
| 4709 | if (Difference) { |
| 4710 | std::string QualsString = Difference.getAsString(); |
| 4711 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 4712 | << BO->getType().getUnqualifiedType() |
| 4713 | << QualsString |
| 4714 | << (QualsString.find(' ') == std::string::npos? 1 : 2); |
| 4715 | } |
| 4716 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4717 | CXXMemberCallExpr *TheCall |
Abramo Bagnara | 6c572f1 | 2010-12-03 21:39:42 +0000 | [diff] [blame] | 4718 | = new (Context) CXXMemberCallExpr(Context, Fn, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4719 | NumArgs, ResultTy, VK, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4720 | RParenLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4721 | |
| 4722 | if (CheckCallReturnType(FPT->getResultType(), |
| 4723 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4724 | TheCall, 0)) |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4725 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 4726 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4727 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4728 | RParenLoc)) |
| 4729 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4730 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4731 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4732 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4733 | } |
| 4734 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4737 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4738 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4739 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4740 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4741 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4742 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 4743 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 4744 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4745 | RParenLoc, ExecConfig); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4748 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4749 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4750 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4751 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4752 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4753 | if (isa<DeclRefExpr>(NakedFn)) |
| 4754 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 4755 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4756 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 4757 | ExecConfig); |
| 4758 | } |
| 4759 | |
| 4760 | ExprResult |
| 4761 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 4762 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 4763 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4764 | if (!ConfigDecl) |
| 4765 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4766 | << "cudaConfigureCall"); |
| 4767 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4768 | |
| 4769 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 4770 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 4771 | |
| 4772 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4773 | } |
| 4774 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4775 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4776 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4777 | /// unary-convert to an expression of function-pointer or |
| 4778 | /// block-pointer type. |
| 4779 | /// |
| 4780 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4781 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4782 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4783 | SourceLocation LParenLoc, |
| 4784 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4785 | SourceLocation RParenLoc, |
| 4786 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4787 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 4788 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4789 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4790 | ExprResult Result = UsualUnaryConversions(Fn); |
| 4791 | if (Result.isInvalid()) |
| 4792 | return ExprError(); |
| 4793 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4794 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4795 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4796 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4797 | CallExpr *TheCall; |
| 4798 | if (Config) { |
| 4799 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4800 | cast<CallExpr>(Config), |
| 4801 | Args, NumArgs, |
| 4802 | Context.BoolTy, |
| 4803 | VK_RValue, |
| 4804 | RParenLoc); |
| 4805 | } else { |
| 4806 | TheCall = new (Context) CallExpr(Context, Fn, |
| 4807 | Args, NumArgs, |
| 4808 | Context.BoolTy, |
| 4809 | VK_RValue, |
| 4810 | RParenLoc); |
| 4811 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4812 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4813 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 4814 | |
| 4815 | // Bail out early if calling a builtin with custom typechecking. |
| 4816 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 4817 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 4818 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4819 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4820 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4821 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4822 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4823 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4824 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4825 | if (FuncT == 0) |
| 4826 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4827 | << Fn->getType() << Fn->getSourceRange()); |
| 4828 | } else if (const BlockPointerType *BPT = |
| 4829 | Fn->getType()->getAs<BlockPointerType>()) { |
| 4830 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 4831 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4832 | // Handle calls to expressions of unknown-any type. |
| 4833 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4834 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4835 | if (rewrite.isInvalid()) return ExprError(); |
| 4836 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 4837 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4838 | goto retry; |
| 4839 | } |
| 4840 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4841 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4842 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4843 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4844 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4845 | if (getLangOptions().CUDA) { |
| 4846 | if (Config) { |
| 4847 | // CUDA: Kernel calls must be to global functions |
| 4848 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4849 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 4850 | << FDecl->getName() << Fn->getSourceRange()); |
| 4851 | |
| 4852 | // CUDA: Kernel function must have 'void' return type |
| 4853 | if (!FuncT->getResultType()->isVoidType()) |
| 4854 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 4855 | << Fn->getType() << Fn->getSourceRange()); |
| 4856 | } |
| 4857 | } |
| 4858 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4859 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4860 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4861 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4862 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4863 | return ExprError(); |
| 4864 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4865 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4866 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4867 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4868 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4869 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4870 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4871 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4872 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4873 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4874 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4875 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4876 | if (FDecl) { |
| 4877 | // Check if we have too few/too many template arguments, based |
| 4878 | // on our knowledge of the function definition. |
| 4879 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4880 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4881 | const FunctionProtoType *Proto |
| 4882 | = Def->getType()->getAs<FunctionProtoType>(); |
| 4883 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4884 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4885 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4886 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4887 | |
| 4888 | // If the function we're calling isn't a function prototype, but we have |
| 4889 | // a function prototype from a prior declaratiom, use that prototype. |
| 4890 | if (!FDecl->hasPrototype()) |
| 4891 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4894 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4895 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4896 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4897 | |
| 4898 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4899 | InitializedEntity Entity |
| 4900 | = InitializedEntity::InitializeParameter(Context, |
| 4901 | Proto->getArgType(i)); |
| 4902 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4903 | SourceLocation(), |
| 4904 | Owned(Arg)); |
| 4905 | if (ArgE.isInvalid()) |
| 4906 | return true; |
| 4907 | |
| 4908 | Arg = ArgE.takeAs<Expr>(); |
| 4909 | |
| 4910 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4911 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 4912 | |
| 4913 | if (ArgE.isInvalid()) |
| 4914 | return true; |
| 4915 | |
| 4916 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4919 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4920 | Arg->getType(), |
| 4921 | PDiag(diag::err_call_incomplete_argument) |
| 4922 | << Arg->getSourceRange())) |
| 4923 | return ExprError(); |
| 4924 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4925 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4926 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4927 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4929 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4930 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4931 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4932 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4933 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4934 | // Check for sentinels |
| 4935 | if (NDecl) |
| 4936 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4938 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4939 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4940 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4941 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4943 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4944 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4945 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4946 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4947 | return ExprError(); |
| 4948 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4949 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4950 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4951 | } |
| 4952 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4953 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4954 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4955 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4956 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4957 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4958 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4959 | |
| 4960 | TypeSourceInfo *TInfo; |
| 4961 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4962 | if (!TInfo) |
| 4963 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4964 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4965 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4968 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4969 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4970 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4971 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4972 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4973 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4974 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 4975 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 4976 | << SourceRange(LParenLoc, |
| 4977 | literalExpr->getSourceRange().getEnd()))) |
| 4978 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4979 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4980 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 4981 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4982 | } else if (!literalType->isDependentType() && |
| 4983 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4984 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4985 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4986 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4987 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4988 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4989 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4990 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4991 | InitializationKind Kind |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4992 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4993 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4994 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4995 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4996 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4997 | &literalType); |
| 4998 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4999 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5000 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5001 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5002 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5003 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5004 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5005 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5006 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5007 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5008 | // In C, compound literals are l-values for some reason. |
| 5009 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5010 | |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5011 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5012 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5015 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5016 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5017 | SourceLocation RBraceLoc) { |
| 5018 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5019 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5020 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5021 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5022 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5023 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5024 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5025 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5026 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5027 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5030 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5031 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5032 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5033 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5034 | // Also, callers should have filtered out the invalid cases with |
| 5035 | // pointers. Everything else should be possible. |
| 5036 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5037 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5038 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5039 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5040 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5041 | switch (SrcTy->getScalarTypeKind()) { |
| 5042 | case Type::STK_MemberPointer: |
| 5043 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5044 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5045 | case Type::STK_Pointer: |
| 5046 | switch (DestTy->getScalarTypeKind()) { |
| 5047 | case Type::STK_Pointer: |
| 5048 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5049 | CK_AnyPointerToObjCPointerCast : |
| 5050 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5051 | case Type::STK_Bool: |
| 5052 | return CK_PointerToBoolean; |
| 5053 | case Type::STK_Integral: |
| 5054 | return CK_PointerToIntegral; |
| 5055 | case Type::STK_Floating: |
| 5056 | case Type::STK_FloatingComplex: |
| 5057 | case Type::STK_IntegralComplex: |
| 5058 | case Type::STK_MemberPointer: |
| 5059 | llvm_unreachable("illegal cast from pointer"); |
| 5060 | } |
| 5061 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5062 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5063 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5064 | case Type::STK_Integral: |
| 5065 | switch (DestTy->getScalarTypeKind()) { |
| 5066 | case Type::STK_Pointer: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5067 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5068 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5069 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5070 | case Type::STK_Bool: |
| 5071 | return CK_IntegralToBoolean; |
| 5072 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5073 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5074 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5075 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5076 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5077 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5078 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5079 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5080 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5081 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5082 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5083 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5084 | case Type::STK_MemberPointer: |
| 5085 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5086 | } |
| 5087 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5088 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5089 | case Type::STK_Floating: |
| 5090 | switch (DestTy->getScalarTypeKind()) { |
| 5091 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5092 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5093 | case Type::STK_Bool: |
| 5094 | return CK_FloatingToBoolean; |
| 5095 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5096 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5097 | case Type::STK_FloatingComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5098 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5099 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5100 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5101 | case Type::STK_IntegralComplex: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5102 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5103 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5104 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5105 | case Type::STK_Pointer: |
| 5106 | llvm_unreachable("valid float->pointer cast?"); |
| 5107 | case Type::STK_MemberPointer: |
| 5108 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5109 | } |
| 5110 | break; |
| 5111 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5112 | case Type::STK_FloatingComplex: |
| 5113 | switch (DestTy->getScalarTypeKind()) { |
| 5114 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5115 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5116 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5117 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5118 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5119 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5120 | if (S.Context.hasSameType(ET, DestTy)) |
| 5121 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5122 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5123 | return CK_FloatingCast; |
| 5124 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5125 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5126 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5127 | case Type::STK_Integral: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5128 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5129 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5130 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5131 | case Type::STK_Pointer: |
| 5132 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5133 | case Type::STK_MemberPointer: |
| 5134 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5135 | } |
| 5136 | break; |
| 5137 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5138 | case Type::STK_IntegralComplex: |
| 5139 | switch (DestTy->getScalarTypeKind()) { |
| 5140 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5141 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5142 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5143 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5144 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5145 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5146 | if (S.Context.hasSameType(ET, DestTy)) |
| 5147 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5148 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5149 | return CK_IntegralCast; |
| 5150 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5151 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5152 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5153 | case Type::STK_Floating: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5154 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5155 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5156 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5157 | case Type::STK_Pointer: |
| 5158 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5159 | case Type::STK_MemberPointer: |
| 5160 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5161 | } |
| 5162 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5163 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5164 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5165 | llvm_unreachable("Unhandled scalar cast"); |
| 5166 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5167 | } |
| 5168 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5169 | /// CheckCastTypes - Check type constraints for casting between types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5170 | ExprResult Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 5171 | Expr *castExpr, CastKind& Kind, ExprValueKind &VK, |
| 5172 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5173 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5174 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5175 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5176 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5177 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 5178 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5179 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5180 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5181 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5182 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5183 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5184 | // We only support r-value casts in C. |
| 5185 | VK = VK_RValue; |
| 5186 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5187 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5188 | // type needs to be scalar. |
| 5189 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5190 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5191 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5192 | if (castExprRes.isInvalid()) |
| 5193 | return ExprError(); |
| 5194 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5195 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5196 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5197 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5198 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5199 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5200 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5201 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5202 | if (castExprRes.isInvalid()) |
| 5203 | return ExprError(); |
| 5204 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5205 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5206 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5207 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5208 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5209 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5210 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5211 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5212 | (castType->isStructureType() || castType->isUnionType())) { |
| 5213 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5214 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5215 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5216 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5217 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5218 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5219 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5220 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5221 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5222 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5223 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5224 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5225 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5226 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5227 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5228 | castExpr->getType()) && |
| 5229 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5230 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5231 | << castExpr->getSourceRange(); |
| 5232 | break; |
| 5233 | } |
| 5234 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5235 | if (Field == FieldEnd) { |
| 5236 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5237 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5238 | return ExprError(); |
| 5239 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5240 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5241 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5242 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5243 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5244 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5245 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5246 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5247 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5248 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5249 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5250 | // The type we're casting to is known to be a scalar or vector. |
| 5251 | |
| 5252 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5253 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5254 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5255 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5256 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5257 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5258 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5259 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5260 | |
| 5261 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5262 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5263 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5264 | if (castType->isVectorType()) { |
| 5265 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5266 | VectorType::AltiVecVector && |
| 5267 | (castExpr->getType()->isIntegerType() || |
| 5268 | castExpr->getType()->isFloatingType())) { |
| 5269 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5270 | return Owned(castExpr); |
| 5271 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5272 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5273 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5274 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5275 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5276 | if (castExpr->getType()->isVectorType()) { |
| 5277 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5278 | return ExprError(); |
| 5279 | else |
| 5280 | return Owned(castExpr); |
| 5281 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5282 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5283 | // The source and target types are both scalars, i.e. |
| 5284 | // - arithmetic types (fundamental, enum, and complex) |
| 5285 | // - all kinds of pointers |
| 5286 | // Note that member pointers were filtered out with C++, above. |
| 5287 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5288 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5289 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5290 | return ExprError(); |
| 5291 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5292 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5293 | // If either type is a pointer, the other type has to be either an |
| 5294 | // integer or a pointer. |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5295 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5296 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5297 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5298 | castExprType->isArithmeticType()) { |
| 5299 | Diag(castExpr->getLocStart(), |
| 5300 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5301 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5302 | return ExprError(); |
| 5303 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5304 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5305 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5306 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5307 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5308 | return ExprError(); |
| 5309 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5310 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5311 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5312 | castExprRes = Owned(castExpr); |
| 5313 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5314 | if (castExprRes.isInvalid()) |
| 5315 | return ExprError(); |
| 5316 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5317 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5318 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5319 | CheckCastAlign(castExpr, castType, TyR); |
| 5320 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5321 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5324 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5325 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5326 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5327 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5328 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5329 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5330 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5331 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5332 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5333 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5334 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5335 | } else |
| 5336 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5337 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5338 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5339 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5340 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5341 | return false; |
| 5342 | } |
| 5343 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5344 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5345 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5346 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5347 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5348 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5349 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5350 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5351 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5352 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5353 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5354 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5355 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5356 | return ExprError(); |
| 5357 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5358 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5359 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5360 | } |
| 5361 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5362 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5363 | // conversion will take place first from scalar to elt type, and then |
| 5364 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5365 | if (SrcTy->isPointerType()) |
| 5366 | return Diag(R.getBegin(), |
| 5367 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5368 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5369 | |
| 5370 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5371 | ExprResult CastExprRes = Owned(CastExpr); |
| 5372 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5373 | if (CastExprRes.isInvalid()) |
| 5374 | return ExprError(); |
| 5375 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5376 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5377 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5378 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5381 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5382 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5383 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5384 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5385 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5386 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5387 | TypeSourceInfo *castTInfo; |
| 5388 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5389 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5390 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5391 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5392 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5393 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5394 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5395 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5396 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5397 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5400 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5401 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5402 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5403 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5404 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5405 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5406 | ExprResult CastResult = |
| 5407 | CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
| 5408 | Kind, VK, BasePath); |
| 5409 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5410 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5411 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5412 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5413 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5414 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5415 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5416 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5417 | } |
| 5418 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5419 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5420 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5421 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5422 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5423 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5424 | if (!E) |
| 5425 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5426 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5427 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5428 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5429 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5430 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5431 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5432 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5433 | if (Result.isInvalid()) return ExprError(); |
| 5434 | |
| 5435 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5438 | ExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5439 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5440 | SourceLocation RParenLoc, Expr *Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5441 | TypeSourceInfo *TInfo) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5442 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5443 | QualType Ty = TInfo->getType(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5444 | bool isVectorLiteral = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5445 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5446 | // Check for an altivec or OpenCL literal, |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5447 | // i.e. all the elements are integer constants. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5448 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5449 | if (PE->getNumExprs() == 0) { |
| 5450 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5451 | return ExprError(); |
| 5452 | } |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5453 | if (PE->getNumExprs() == 1) { |
| 5454 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5455 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5456 | } |
| 5457 | else |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5458 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5459 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5460 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5461 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5462 | // then handle it as such. |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5463 | if (isVectorLiteral) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5464 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5465 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5466 | // initializers must be one or must match the size of the vector. |
| 5467 | // If a single value is specified in the initializer then it will be |
| 5468 | // replicated to all the components of the vector |
| 5469 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5470 | VectorType::AltiVecVector) { |
| 5471 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5472 | // The number of initializers must be one or must match the size of the |
| 5473 | // vector. If a single value is specified in the initializer then it will |
| 5474 | // be replicated to all the components of the vector |
| 5475 | if (PE->getNumExprs() == 1) { |
| 5476 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5477 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5478 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5479 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5480 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5481 | } |
| 5482 | else if (PE->getNumExprs() < numElems) { |
| 5483 | Diag(PE->getExprLoc(), |
| 5484 | diag::err_incorrect_number_of_vector_initializers); |
| 5485 | return ExprError(); |
| 5486 | } |
| 5487 | else |
| 5488 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5489 | initExprs.push_back(PE->getExpr(i)); |
| 5490 | } |
| 5491 | else |
| 5492 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5493 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5494 | |
| 5495 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5496 | // braces instead of the original commas. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5497 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5498 | &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5499 | initExprs.size(), RParenLoc); |
| 5500 | E->setType(Ty); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5501 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5502 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5503 | // 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] | 5504 | // sequence of BinOp comma operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5505 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5506 | if (Result.isInvalid()) return ExprError(); |
| 5507 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5508 | } |
| 5509 | } |
| 5510 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5511 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5512 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5513 | MultiExprArg Val, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5514 | ParsedType TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5515 | unsigned nexprs = Val.size(); |
| 5516 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5517 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5518 | Expr *expr; |
| 5519 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5520 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5521 | else |
| 5522 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5523 | return Owned(expr); |
| 5524 | } |
| 5525 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5526 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5527 | /// constant and the other is not a pointer. |
| 5528 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5529 | SourceLocation QuestionLoc) { |
| 5530 | Expr *NullExpr = LHS; |
| 5531 | Expr *NonPointerExpr = RHS; |
| 5532 | Expr::NullPointerConstantKind NullKind = |
| 5533 | NullExpr->isNullPointerConstant(Context, |
| 5534 | Expr::NPC_ValueDependentIsNotNull); |
| 5535 | |
| 5536 | if (NullKind == Expr::NPCK_NotNull) { |
| 5537 | NullExpr = RHS; |
| 5538 | NonPointerExpr = LHS; |
| 5539 | NullKind = |
| 5540 | NullExpr->isNullPointerConstant(Context, |
| 5541 | Expr::NPC_ValueDependentIsNotNull); |
| 5542 | } |
| 5543 | |
| 5544 | if (NullKind == Expr::NPCK_NotNull) |
| 5545 | return false; |
| 5546 | |
| 5547 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5548 | // In this case, check to make sure that we got here from a "NULL" |
| 5549 | // string in the source code. |
| 5550 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5551 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5552 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5553 | return false; |
| 5554 | } |
| 5555 | |
| 5556 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5557 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5558 | << NonPointerExpr->getType() << DiagType |
| 5559 | << NonPointerExpr->getSourceRange(); |
| 5560 | return true; |
| 5561 | } |
| 5562 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5563 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5564 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5565 | /// C99 6.5.15 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5566 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5567 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5568 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5569 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5570 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5571 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5572 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5573 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5574 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5575 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5576 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5577 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5578 | // C++ is sufficiently different to merit its own checker. |
| 5579 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5580 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5581 | |
| 5582 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5583 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5584 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5585 | Cond = UsualUnaryConversions(Cond.take()); |
| 5586 | if (Cond.isInvalid()) |
| 5587 | return QualType(); |
| 5588 | LHS = UsualUnaryConversions(LHS.take()); |
| 5589 | if (LHS.isInvalid()) |
| 5590 | return QualType(); |
| 5591 | RHS = UsualUnaryConversions(RHS.take()); |
| 5592 | if (RHS.isInvalid()) |
| 5593 | return QualType(); |
| 5594 | |
| 5595 | QualType CondTy = Cond.get()->getType(); |
| 5596 | QualType LHSTy = LHS.get()->getType(); |
| 5597 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5598 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5599 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5600 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5601 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5602 | // Throw an error if its not either. |
| 5603 | if (getLangOptions().OpenCL) { |
| 5604 | if (!CondTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5605 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5606 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5607 | << CondTy; |
| 5608 | return QualType(); |
| 5609 | } |
| 5610 | } |
| 5611 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5612 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5613 | << CondTy; |
| 5614 | return QualType(); |
| 5615 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5616 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5617 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5618 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5619 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5620 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5621 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5622 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5623 | // attempt to implicity convert them to the vector type to act like the |
| 5624 | // built in select. |
| 5625 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5626 | // Both operands should be of scalar type. |
| 5627 | if (!LHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5628 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5629 | << CondTy; |
| 5630 | return QualType(); |
| 5631 | } |
| 5632 | if (!RHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5633 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5634 | << CondTy; |
| 5635 | return QualType(); |
| 5636 | } |
| 5637 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5638 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5639 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5640 | } |
| 5641 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5642 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5643 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5644 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5645 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5646 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5647 | return QualType(); |
| 5648 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5649 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5650 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5651 | // If both operands are the same structure or union type, the result is that |
| 5652 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5653 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5654 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5655 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5656 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5657 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5658 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5659 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5660 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5661 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5662 | // 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] | 5663 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5664 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5665 | if (!LHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5666 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5667 | << RHS.get()->getSourceRange(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5668 | if (!RHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5669 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5670 | << LHS.get()->getSourceRange(); |
| 5671 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 5672 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5673 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5674 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5675 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5676 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5677 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5678 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5679 | // promote the null to a pointer. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5680 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5681 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5682 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5683 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5684 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 5685 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5686 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5687 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5688 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5689 | // All objective-c pointer type analysis is done here. |
| 5690 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5691 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5692 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5693 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5694 | if (!compositeType.isNull()) |
| 5695 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5696 | |
| 5697 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5698 | // Handle block pointer types. |
| 5699 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5700 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5701 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5702 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5703 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 5704 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5705 | return destType; |
| 5706 | } |
| 5707 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5708 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5709 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5710 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5711 | // We have 2 block pointer types. |
| 5712 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5713 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5714 | return LHSTy; |
| 5715 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5716 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5717 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5718 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5719 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5720 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5721 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5722 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5723 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5724 | // In this situation, we assume void* type. No especially good |
| 5725 | // reason, but this is what gcc does, and we do have to pick |
| 5726 | // to get a consistent AST. |
| 5727 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5728 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5729 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5730 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5731 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5732 | // The block pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5733 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 5734 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5735 | return LHSTy; |
| 5736 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5737 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5738 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5739 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5740 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5741 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5742 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5743 | |
| 5744 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5745 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5746 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5747 | QualType destPointee |
| 5748 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5749 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5750 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5751 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5752 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5753 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5754 | return destType; |
| 5755 | } |
| 5756 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5757 | QualType destPointee |
| 5758 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5759 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5760 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5761 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5762 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5763 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5764 | return destType; |
| 5765 | } |
| 5766 | |
| 5767 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5768 | // Two identical pointer types are always compatible. |
| 5769 | return LHSTy; |
| 5770 | } |
| 5771 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5772 | rhptee.getUnqualifiedType())) { |
| 5773 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5774 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5775 | // In this situation, we assume void* type. No especially good |
| 5776 | // reason, but this is what gcc does, and we do have to pick |
| 5777 | // to get a consistent AST. |
| 5778 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5779 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5780 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5781 | return incompatTy; |
| 5782 | } |
| 5783 | // The pointer types are compatible. |
| 5784 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 5785 | // differently qualified versions of compatible types, the result type is |
| 5786 | // a pointer to an appropriately qualified version of the *composite* |
| 5787 | // type. |
| 5788 | // FIXME: Need to calculate the composite type. |
| 5789 | // FIXME: Need to add qualifiers |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5790 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 5791 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5792 | return LHSTy; |
| 5793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5794 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5795 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5796 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5797 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 5798 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5799 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5800 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5801 | return RHSTy; |
| 5802 | } |
| 5803 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 5804 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5805 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5806 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5807 | return LHSTy; |
| 5808 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5809 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5810 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5811 | // constant and the other is not a pointer type. In this case, the user most |
| 5812 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5813 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5814 | return QualType(); |
| 5815 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5816 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5817 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5818 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5819 | return QualType(); |
| 5820 | } |
| 5821 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5822 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5823 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5824 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5825 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5826 | QualType LHSTy = LHS.get()->getType(); |
| 5827 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5828 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5829 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5830 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5831 | // redefinition type if an attempt is made to access its fields. |
| 5832 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5833 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5834 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5835 | return LHSTy; |
| 5836 | } |
| 5837 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5838 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5839 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5840 | return RHSTy; |
| 5841 | } |
| 5842 | // And the same for struct objc_object* / id |
| 5843 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5844 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5845 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5846 | return LHSTy; |
| 5847 | } |
| 5848 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5849 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5850 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5851 | return RHSTy; |
| 5852 | } |
| 5853 | // And the same for struct objc_selector* / SEL |
| 5854 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5855 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5856 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5857 | return LHSTy; |
| 5858 | } |
| 5859 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5860 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5861 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5862 | return RHSTy; |
| 5863 | } |
| 5864 | // Check constraints for Objective-C object pointers types. |
| 5865 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5866 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5867 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5868 | // Two identical object pointer types are always compatible. |
| 5869 | return LHSTy; |
| 5870 | } |
| 5871 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 5872 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 5873 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5874 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5875 | // If both operands are interfaces and either operand can be |
| 5876 | // assigned to the other, use that type as the composite |
| 5877 | // type. This allows |
| 5878 | // xxx ? (A*) a : (B*) b |
| 5879 | // where B is a subclass of A. |
| 5880 | // |
| 5881 | // Additionally, as for assignment, if either type is 'id' |
| 5882 | // allow silent coercion. Finally, if the types are |
| 5883 | // incompatible then make sure to use 'id' as the composite |
| 5884 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5885 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5886 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5887 | // It could return the composite type. |
| 5888 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5889 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5890 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5891 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5892 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5893 | RHSTy->isObjCQualifiedIdType()) && |
| 5894 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5895 | // Need to handle "id<xx>" explicitly. |
| 5896 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5897 | // id. Currently localizing to here until clear this should be |
| 5898 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5899 | compositeType = Context.getObjCIdType(); |
| 5900 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5901 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5902 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5903 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5904 | ; |
| 5905 | else { |
| 5906 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5907 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5908 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5909 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5910 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5911 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5912 | return incompatTy; |
| 5913 | } |
| 5914 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5915 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 5916 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5917 | return compositeType; |
| 5918 | } |
| 5919 | // Check Objective-C object pointer types and 'void *' |
| 5920 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 5921 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5922 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5923 | QualType destPointee |
| 5924 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5925 | QualType destType = Context.getPointerType(destPointee); |
| 5926 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5927 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5928 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5929 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5930 | return destType; |
| 5931 | } |
| 5932 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 5933 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5934 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5935 | QualType destPointee |
| 5936 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5937 | QualType destType = Context.getPointerType(destPointee); |
| 5938 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5939 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5940 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5941 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5942 | return destType; |
| 5943 | } |
| 5944 | return QualType(); |
| 5945 | } |
| 5946 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5947 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5948 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5949 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5950 | SourceLocation ColonLoc, |
| 5951 | Expr *CondExpr, Expr *LHSExpr, |
| 5952 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5953 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5954 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5955 | OpaqueValueExpr *opaqueValue = 0; |
| 5956 | Expr *commonExpr = 0; |
| 5957 | if (LHSExpr == 0) { |
| 5958 | commonExpr = CondExpr; |
| 5959 | |
| 5960 | // We usually want to apply unary conversions *before* saving, except |
| 5961 | // in the special case of a C++ l-value conditional. |
| 5962 | if (!(getLangOptions().CPlusPlus |
| 5963 | && !commonExpr->isTypeDependent() |
| 5964 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5965 | && commonExpr->isGLValue() |
| 5966 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5967 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5968 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5969 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5970 | if (commonRes.isInvalid()) |
| 5971 | return ExprError(); |
| 5972 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5976 | commonExpr->getType(), |
| 5977 | commonExpr->getValueKind(), |
| 5978 | commonExpr->getObjectKind()); |
| 5979 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5980 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5981 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5982 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5983 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5984 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5985 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5986 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5987 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5988 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5989 | return ExprError(); |
| 5990 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5991 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5992 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5993 | LHS.take(), ColonLoc, |
| 5994 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5995 | |
| 5996 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5997 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 5998 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6001 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6002 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6003 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6004 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6005 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6006 | static Sema::AssignConvertType |
| 6007 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6008 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6009 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6010 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6011 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6012 | const Type *lhptee, *rhptee; |
| 6013 | Qualifiers lhq, rhq; |
| 6014 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6015 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6016 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6017 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6018 | |
| 6019 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6020 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6021 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6022 | Qualifiers lq; |
| 6023 | |
| 6024 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6025 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6026 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6027 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6028 | |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6029 | // It's okay to add or remove GC qualifiers when converting to |
| 6030 | // and from void*. |
| 6031 | else if (lhq.withoutObjCGCAttr().compatiblyIncludes(rhq.withoutObjCGCAttr()) |
| 6032 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6033 | ; // keep old |
| 6034 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6035 | // For GCC compatibility, other qualifier mismatches are treated |
| 6036 | // as still compatible in C. |
| 6037 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6038 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6039 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6040 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6041 | // 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] | 6042 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6043 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6044 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6045 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6046 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6047 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6048 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6049 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6050 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6051 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6052 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6053 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6054 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6055 | |
| 6056 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6057 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6058 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6059 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6060 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6061 | // 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] | 6062 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6063 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6064 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6065 | // Check if the pointee types are compatible ignoring the sign. |
| 6066 | // We explicitly check for char so that we catch "char" vs |
| 6067 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6068 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6069 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6070 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6071 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6072 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6073 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6074 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6075 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6076 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6077 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6078 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6079 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6080 | // takes priority over sign incompatibility because the sign |
| 6081 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6082 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6083 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6084 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6085 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6086 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6087 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6088 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6089 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6090 | // the eventual target type is the same and the pointers have the same |
| 6091 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6092 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6093 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6094 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6095 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6096 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6097 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6098 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6099 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6100 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6101 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6102 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6103 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6104 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6105 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6106 | } |
| 6107 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6108 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6109 | /// block pointer types are compatible or whether a block and normal pointer |
| 6110 | /// are compatible. It is more restrict than comparing two function pointer |
| 6111 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6112 | static Sema::AssignConvertType |
| 6113 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6114 | QualType rhsType) { |
| 6115 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6116 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6117 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6118 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6119 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6120 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6121 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6122 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6123 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6124 | // In C++, the types have to match exactly. |
| 6125 | if (S.getLangOptions().CPlusPlus) |
| 6126 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6127 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6128 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6129 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6130 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6131 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6132 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6133 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6134 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6135 | return Sema::IncompatibleBlockPointer; |
| 6136 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6137 | return ConvTy; |
| 6138 | } |
| 6139 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6140 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6141 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6142 | static Sema::AssignConvertType |
| 6143 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6144 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6145 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6146 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6147 | if (lhsType->isObjCBuiltinType()) { |
| 6148 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6149 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6150 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6151 | return Sema::IncompatiblePointer; |
| 6152 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6153 | } |
| 6154 | if (rhsType->isObjCBuiltinType()) { |
| 6155 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6156 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6157 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6158 | return Sema::IncompatiblePointer; |
| 6159 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6160 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6161 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6162 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6163 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6164 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6165 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6166 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6167 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6168 | |
| 6169 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6170 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6171 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6172 | return Sema::IncompatibleObjCQualifiedId; |
| 6173 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6176 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6177 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6178 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6179 | // Fake up an opaque expression. We don't actually care about what |
| 6180 | // cast operations are required, so if CheckAssignmentConstraints |
| 6181 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6182 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6183 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6184 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6185 | CastKind K = CK_Invalid; |
| 6186 | |
| 6187 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6188 | } |
| 6189 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6190 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6191 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6192 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6193 | /// |
| 6194 | /// int a, *pint; |
| 6195 | /// short *pshort; |
| 6196 | /// struct foo *pfoo; |
| 6197 | /// |
| 6198 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6199 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6200 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6201 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6202 | /// |
| 6203 | /// 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] | 6204 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6205 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6206 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6207 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6208 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6209 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6210 | QualType rhsType = rhs.get()->getType(); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6211 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6212 | // Get canonical types. We're not formatting these types, just comparing |
| 6213 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6214 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6215 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6216 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6217 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6218 | if (lhsType == rhsType) { |
| 6219 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6220 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6221 | } |
| 6222 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6223 | // If the left-hand side is a reference type, then we are in a |
| 6224 | // (rare!) case where we've allowed the use of references in C, |
| 6225 | // e.g., as a parameter type in a built-in function. In this case, |
| 6226 | // just make sure that the type referenced is compatible with the |
| 6227 | // right-hand side type. The caller is responsible for adjusting |
| 6228 | // lhsType so that the resulting expression does not have reference |
| 6229 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6230 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6231 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6232 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6233 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6234 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6235 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6236 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6237 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6238 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6239 | // to the same ExtVector type. |
| 6240 | if (lhsType->isExtVectorType()) { |
| 6241 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6242 | return Incompatible; |
| 6243 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6244 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6245 | // element type. |
| 6246 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6247 | if (elType != rhsType) { |
| 6248 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6249 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6250 | } |
| 6251 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6252 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6253 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6254 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6255 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6256 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6257 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6258 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6259 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6260 | // vector type and vice versa |
| 6261 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6262 | Kind = CK_BitCast; |
| 6263 | return Compatible; |
| 6264 | } |
| 6265 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6266 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6267 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6268 | // no bits are changed but the result type is different. |
| 6269 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6270 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6271 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6272 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6273 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6274 | } |
| 6275 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6276 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6277 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6278 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6279 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6280 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6281 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6282 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6283 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6284 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6285 | // Conversions to normal pointers. |
| 6286 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6287 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6288 | if (isa<PointerType>(rhsType)) { |
| 6289 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6290 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6291 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6292 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6293 | // int -> T* |
| 6294 | if (rhsType->isIntegerType()) { |
| 6295 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6296 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6297 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6298 | |
| 6299 | // C pointers are not compatible with ObjC object pointers, |
| 6300 | // with two exceptions: |
| 6301 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6302 | // - conversions to void* |
| 6303 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6304 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6305 | return Compatible; |
| 6306 | } |
| 6307 | |
| 6308 | // - conversions from 'Class' to the redefinition type |
| 6309 | if (rhsType->isObjCClassType() && |
| 6310 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6311 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6312 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6313 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6314 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6315 | Kind = CK_BitCast; |
| 6316 | return IncompatiblePointer; |
| 6317 | } |
| 6318 | |
| 6319 | // U^ -> void* |
| 6320 | if (rhsType->getAs<BlockPointerType>()) { |
| 6321 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6322 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6323 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6324 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6325 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6326 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6327 | return Incompatible; |
| 6328 | } |
| 6329 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6330 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6331 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6332 | // U^ -> T^ |
| 6333 | if (rhsType->isBlockPointerType()) { |
| 6334 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6335 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6336 | } |
| 6337 | |
| 6338 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6339 | if (rhsType->isIntegerType()) { |
| 6340 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6341 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6342 | } |
| 6343 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6344 | // id -> T^ |
| 6345 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6346 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6347 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6348 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6349 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6350 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6351 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6352 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6353 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6354 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6355 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6356 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6357 | return Incompatible; |
| 6358 | } |
| 6359 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6360 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6361 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6362 | // A* -> B* |
| 6363 | if (rhsType->isObjCObjectPointerType()) { |
| 6364 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6365 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6366 | } |
| 6367 | |
| 6368 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6369 | if (rhsType->isIntegerType()) { |
| 6370 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6371 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6374 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6375 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6376 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6377 | // - conversions from 'void*' |
| 6378 | if (rhsType->isVoidPointerType()) { |
| 6379 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6380 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
| 6383 | // - conversions to 'Class' from its redefinition type |
| 6384 | if (lhsType->isObjCClassType() && |
| 6385 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6386 | Kind = CK_BitCast; |
| 6387 | return Compatible; |
| 6388 | } |
| 6389 | |
| 6390 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6391 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6392 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6393 | |
| 6394 | // T^ -> A* |
| 6395 | if (rhsType->isBlockPointerType()) { |
| 6396 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6397 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6400 | return Incompatible; |
| 6401 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6402 | |
| 6403 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6404 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6405 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6406 | if (lhsType == Context.BoolTy) { |
| 6407 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6408 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6409 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6410 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6411 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6412 | if (lhsType->isIntegerType()) { |
| 6413 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6414 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6415 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6416 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6417 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6418 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6419 | |
| 6420 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6421 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6422 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6423 | if (lhsType == Context.BoolTy) { |
| 6424 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6425 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6426 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6427 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6428 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6429 | if (lhsType->isIntegerType()) { |
| 6430 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6431 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6432 | } |
| 6433 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6434 | return Incompatible; |
| 6435 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6436 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6437 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6438 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6439 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6440 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6441 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6442 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6443 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6444 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6445 | return Incompatible; |
| 6446 | } |
| 6447 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6448 | /// \brief Constructs a transparent union from an expression that is |
| 6449 | /// used to initialize the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6450 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6451 | QualType UnionType, FieldDecl *Field) { |
| 6452 | // Build an initializer list that designates the appropriate member |
| 6453 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6454 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6455 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6456 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6457 | SourceLocation()); |
| 6458 | Initializer->setType(UnionType); |
| 6459 | Initializer->setInitializedFieldInUnion(Field); |
| 6460 | |
| 6461 | // Build a compound literal constructing a value of the transparent |
| 6462 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6463 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6464 | EResult = S.Owned( |
| 6465 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6466 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6470 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6471 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6472 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6473 | // 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] | 6474 | // transparent_union GCC extension. |
| 6475 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6476 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6477 | return Incompatible; |
| 6478 | |
| 6479 | // The field to initialize within the transparent union. |
| 6480 | RecordDecl *UD = UT->getDecl(); |
| 6481 | FieldDecl *InitField = 0; |
| 6482 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6483 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6484 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6485 | it != itend; ++it) { |
| 6486 | if (it->getType()->isPointerType()) { |
| 6487 | // If the transparent union contains a pointer type, we allow: |
| 6488 | // 1) void pointer |
| 6489 | // 2) null pointer constant |
| 6490 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6491 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6492 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6493 | InitField = *it; |
| 6494 | break; |
| 6495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6496 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6497 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6498 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6499 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6500 | InitField = *it; |
| 6501 | break; |
| 6502 | } |
| 6503 | } |
| 6504 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6505 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6506 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6507 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6508 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6509 | InitField = *it; |
| 6510 | break; |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | if (!InitField) |
| 6515 | return Incompatible; |
| 6516 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6517 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6518 | return Compatible; |
| 6519 | } |
| 6520 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6521 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6522 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6523 | if (getLangOptions().CPlusPlus) { |
| 6524 | if (!lhsType->isRecordType()) { |
| 6525 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6526 | // expression is implicitly converted (C++ 4) to the |
| 6527 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6528 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6529 | lhsType.getUnqualifiedType(), |
| 6530 | AA_Assigning); |
| 6531 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6532 | return Incompatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6533 | rExpr = move(Res); |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6534 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6538 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6539 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6540 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6541 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6542 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6543 | if ((lhsType->isPointerType() || |
| 6544 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6545 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6546 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6547 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6548 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6549 | return Compatible; |
| 6550 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6551 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6552 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6553 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6554 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6555 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6556 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6557 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6558 | if (!lhsType->isReferenceType()) { |
| 6559 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 6560 | if (rExpr.isInvalid()) |
| 6561 | return Incompatible; |
| 6562 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6563 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6564 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6565 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6566 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6567 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6568 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6569 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6570 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6571 | // so that we can use references in built-in functions even in C. |
| 6572 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6573 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6574 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 6575 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6576 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6579 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6580 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6581 | << lex.get()->getType() << rex.get()->getType() |
| 6582 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6583 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6586 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6587 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6588 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6589 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6590 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6591 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6592 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6593 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6594 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6595 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6596 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6597 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6598 | // Handle the case of a vector & extvector type of the same size and element |
| 6599 | // 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] | 6600 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6601 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6602 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6603 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6604 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6605 | if (lhsType->isExtVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6606 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6607 | return lhsType; |
| 6608 | } |
| 6609 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6610 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6611 | return rhsType; |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6612 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 6613 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6614 | // vectors, the total size only needs to be the same. This is a |
| 6615 | // bitcast; no bits are changed but the result type is different. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6616 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6617 | return lhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6618 | } |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6619 | } |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6620 | } |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6621 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6622 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6623 | // Handle the case of equivalent AltiVec and GCC vector types |
| 6624 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 6625 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6626 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6627 | return rhsType; |
| 6628 | } |
| 6629 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6630 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6631 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6632 | bool swapped = false; |
| 6633 | if (rhsType->isExtVectorType()) { |
| 6634 | swapped = true; |
| 6635 | std::swap(rex, lex); |
| 6636 | std::swap(rhsType, lhsType); |
| 6637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6638 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6639 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6640 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6641 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 6642 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6643 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 6644 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6645 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6646 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6647 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6648 | if (swapped) std::swap(rex, lex); |
| 6649 | return lhsType; |
| 6650 | } |
| 6651 | } |
| 6652 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 6653 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6654 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 6655 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6656 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6657 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6658 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6659 | if (swapped) std::swap(rex, lex); |
| 6660 | return lhsType; |
| 6661 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6662 | } |
| 6663 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6664 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6665 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6666 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6667 | << lex.get()->getType() << rex.get()->getType() |
| 6668 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6669 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6670 | } |
| 6671 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6672 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6673 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 6674 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6675 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6676 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6677 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6678 | if (lex.isInvalid() || rex.isInvalid()) |
| 6679 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6680 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6681 | if (!lex.get()->getType()->isArithmeticType() || |
| 6682 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6683 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6684 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6685 | // Check for division by zero. |
| 6686 | if (isDiv && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6687 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 6688 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 6689 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6690 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6691 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6692 | } |
| 6693 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6694 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6695 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 6696 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 6697 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6698 | rex.get()->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6699 | return CheckVectorOperands(Loc, lex, rex); |
| 6700 | return InvalidOperands(Loc, lex, rex); |
| 6701 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6702 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6703 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6704 | if (lex.isInvalid() || rex.isInvalid()) |
| 6705 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6706 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6707 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6708 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6709 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6710 | // Check for remainder by zero. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6711 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 6712 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 6713 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6714 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6715 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6716 | } |
| 6717 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6718 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6719 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 6720 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6721 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6722 | if (CompLHSTy) *CompLHSTy = compType; |
| 6723 | return compType; |
| 6724 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6725 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6726 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6727 | if (lex.isInvalid() || rex.isInvalid()) |
| 6728 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6730 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6731 | if (lex.get()->getType()->isArithmeticType() && |
| 6732 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6733 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6734 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6735 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6736 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6737 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6738 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6739 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6740 | std::swap(PExp, IExp); |
| 6741 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6742 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6743 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6744 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6745 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6746 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6747 | // Check for arithmetic on pointers to incomplete types. |
| 6748 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6749 | if (getLangOptions().CPlusPlus) { |
| 6750 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6751 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6752 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6753 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6754 | |
| 6755 | // GNU extension: arithmetic on pointer to void |
| 6756 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6757 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6758 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6759 | if (getLangOptions().CPlusPlus) { |
| 6760 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6761 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6762 | return QualType(); |
| 6763 | } |
| 6764 | |
| 6765 | // GNU extension: arithmetic on pointer to function |
| 6766 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6767 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6768 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6769 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6770 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6771 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6772 | PExp->getType()->isObjCObjectPointerType()) && |
| 6773 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6774 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 6775 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6776 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6777 | return QualType(); |
| 6778 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6779 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6780 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6781 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6782 | << PointeeTy << PExp->getSourceRange(); |
| 6783 | return QualType(); |
| 6784 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6785 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6786 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6787 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6788 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6789 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6790 | if (LHSTy->isPromotableIntegerType()) |
| 6791 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6792 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6793 | *CompLHSTy = LHSTy; |
| 6794 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6795 | return PExp->getType(); |
| 6796 | } |
| 6797 | } |
| 6798 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6799 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6800 | } |
| 6801 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6802 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6803 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6804 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6805 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6806 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6807 | if (CompLHSTy) *CompLHSTy = compType; |
| 6808 | return compType; |
| 6809 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6810 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6811 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6812 | if (lex.isInvalid() || rex.isInvalid()) |
| 6813 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6814 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6815 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6816 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6817 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6818 | if (lex.get()->getType()->isArithmeticType() && |
| 6819 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6820 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6821 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6822 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6823 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6824 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6825 | if (lex.get()->getType()->isAnyPointerType()) { |
| 6826 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6827 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6828 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6829 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6830 | bool ComplainAboutVoid = false; |
| 6831 | Expr *ComplainAboutFunc = 0; |
| 6832 | if (lpointee->isVoidType()) { |
| 6833 | if (getLangOptions().CPlusPlus) { |
| 6834 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6835 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6836 | return QualType(); |
| 6837 | } |
| 6838 | |
| 6839 | // GNU C extension: arithmetic on pointer to void |
| 6840 | ComplainAboutVoid = true; |
| 6841 | } else if (lpointee->isFunctionType()) { |
| 6842 | if (getLangOptions().CPlusPlus) { |
| 6843 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6844 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6845 | return QualType(); |
| 6846 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6847 | |
| 6848 | // GNU C extension: arithmetic on pointer to function |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6849 | ComplainAboutFunc = lex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6850 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6851 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6852 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6853 | << lex.get()->getSourceRange() |
| 6854 | << lex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6855 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6856 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6857 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6858 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6859 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6860 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6861 | return QualType(); |
| 6862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6863 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6864 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6865 | if (rex.get()->getType()->isIntegerType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6866 | if (ComplainAboutVoid) |
| 6867 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6868 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6869 | if (ComplainAboutFunc) |
| 6870 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6871 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6872 | << ComplainAboutFunc->getSourceRange(); |
| 6873 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6874 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 6875 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6876 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6877 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6878 | // Handle pointer-pointer subtractions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6879 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6880 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6881 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6882 | // RHS must be a completely-type object type. |
| 6883 | // Handle the GNU void* extension. |
| 6884 | if (rpointee->isVoidType()) { |
| 6885 | if (getLangOptions().CPlusPlus) { |
| 6886 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6887 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6888 | return QualType(); |
| 6889 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6890 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6891 | ComplainAboutVoid = true; |
| 6892 | } else if (rpointee->isFunctionType()) { |
| 6893 | if (getLangOptions().CPlusPlus) { |
| 6894 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6895 | << rex.get()->getType() << rex.get()->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6896 | return QualType(); |
| 6897 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6898 | |
| 6899 | // GNU extension: arithmetic on pointer to function |
| 6900 | if (!ComplainAboutFunc) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6901 | ComplainAboutFunc = rex.get(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6902 | } else if (!rpointee->isDependentType() && |
| 6903 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6904 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6905 | << rex.get()->getSourceRange() |
| 6906 | << rex.get()->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6907 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6908 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6909 | if (getLangOptions().CPlusPlus) { |
| 6910 | // Pointee types must be the same: C++ [expr.add] |
| 6911 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6912 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6913 | << lex.get()->getType() << rex.get()->getType() |
| 6914 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6915 | return QualType(); |
| 6916 | } |
| 6917 | } else { |
| 6918 | // Pointee types must be compatible C99 6.5.6p3 |
| 6919 | if (!Context.typesAreCompatible( |
| 6920 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6921 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6922 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6923 | << lex.get()->getType() << rex.get()->getType() |
| 6924 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6925 | return QualType(); |
| 6926 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6927 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6928 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6929 | if (ComplainAboutVoid) |
| 6930 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6931 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6932 | if (ComplainAboutFunc) |
| 6933 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6934 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6935 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6936 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6937 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6938 | return Context.getPointerDiffType(); |
| 6939 | } |
| 6940 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6941 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6942 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6943 | } |
| 6944 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6945 | static bool isScopedEnumerationType(QualType T) { |
| 6946 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6947 | return ET->getDecl()->isScoped(); |
| 6948 | return false; |
| 6949 | } |
| 6950 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6951 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6952 | SourceLocation Loc, unsigned Opc, |
| 6953 | QualType LHSTy) { |
| 6954 | llvm::APSInt Right; |
| 6955 | // Check right/shifter operand |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6956 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6957 | return; |
| 6958 | |
| 6959 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6960 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6961 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6962 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6963 | return; |
| 6964 | } |
| 6965 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6966 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6967 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6968 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6969 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6970 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6971 | return; |
| 6972 | } |
| 6973 | if (Opc != BO_Shl) |
| 6974 | return; |
| 6975 | |
| 6976 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6977 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6978 | // integers have defined behavior modulo one more than the maximum value |
| 6979 | // representable in the result type, so never warn for those. |
| 6980 | llvm::APSInt Left; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6981 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6982 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 6983 | return; |
| 6984 | llvm::APInt ResultBits = |
| 6985 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6986 | if (LeftBits.uge(ResultBits)) |
| 6987 | return; |
| 6988 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6989 | Result = Result.shl(Right); |
| 6990 | |
| 6991 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6992 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6993 | // expected value. Thus we place this behind a different warning that can be |
| 6994 | // turned off separately if needed. |
| 6995 | if (LeftBits == ResultBits - 1) { |
| 6996 | S.Diag(Loc, diag::warn_shift_result_overrides_sign_bit) |
| 6997 | << Result.toString(10) << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6998 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6999 | return; |
| 7000 | } |
| 7001 | |
| 7002 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
| 7003 | << Result.toString(10) << Result.getMinSignedBits() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7004 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7005 | } |
| 7006 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7007 | // C99 6.5.7 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7008 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7009 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7010 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7011 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7012 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7013 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7014 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7015 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7016 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7017 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7018 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7019 | return InvalidOperands(Loc, lex, rex); |
| 7020 | } |
| 7021 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7022 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7023 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7024 | return CheckVectorOperands(Loc, lex, rex); |
| 7025 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7026 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7027 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7028 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7029 | // For the LHS, do usual unary conversions, but then reset them away |
| 7030 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7031 | ExprResult old_lex = lex; |
| 7032 | lex = UsualUnaryConversions(lex.take()); |
| 7033 | if (lex.isInvalid()) |
| 7034 | return QualType(); |
| 7035 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7036 | if (isCompAssign) lex = old_lex; |
| 7037 | |
| 7038 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7039 | rex = UsualUnaryConversions(rex.take()); |
| 7040 | if (rex.isInvalid()) |
| 7041 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7042 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7043 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7044 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7045 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7046 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7047 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7050 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7051 | if (DeclContext *DC = D->getDeclContext()) { |
| 7052 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7053 | return true; |
| 7054 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7055 | return FD->isFunctionTemplateSpecialization(); |
| 7056 | } |
| 7057 | return false; |
| 7058 | } |
| 7059 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7060 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7061 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7062 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7063 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7064 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7065 | // Handle vector comparisons separately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7066 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7067 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7068 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7069 | QualType lType = lex.get()->getType(); |
| 7070 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7071 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7072 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7073 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7074 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7075 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7076 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7077 | |
| 7078 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7079 | // Two different enums will raise a warning when compared. |
| 7080 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7081 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7082 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7083 | RHSEnumType->getDecl()->getIdentifier() && |
| 7084 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7085 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7086 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7087 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7088 | } |
| 7089 | } |
| 7090 | } |
| 7091 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7092 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7093 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7094 | !lex.get()->getLocStart().isMacroID() && |
| 7095 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7096 | // For non-floating point types, check for self-comparisons of the form |
| 7097 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7098 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7099 | // |
| 7100 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7101 | // expansion. Also don't warn about comparisons which are only self |
| 7102 | // comparisons within a template specialization. The warnings should catch |
| 7103 | // obvious cases in the definition of the template anyways. The idea is to |
| 7104 | // warn when the typed comparison operator will always evaluate to the same |
| 7105 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7106 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7107 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7108 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7109 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7110 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7111 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7112 | << (Opc == BO_EQ |
| 7113 | || Opc == BO_LE |
| 7114 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7115 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7116 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7117 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7118 | // what is it always going to eval to? |
| 7119 | char always_evals_to; |
| 7120 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7121 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7122 | always_evals_to = 0; // false |
| 7123 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7124 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7125 | always_evals_to = 1; // true |
| 7126 | break; |
| 7127 | default: |
| 7128 | // best we can say is 'a constant' |
| 7129 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7130 | break; |
| 7131 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7132 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7133 | << 1 // array |
| 7134 | << always_evals_to); |
| 7135 | } |
| 7136 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7137 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7138 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7139 | if (isa<CastExpr>(LHSStripped)) |
| 7140 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7141 | if (isa<CastExpr>(RHSStripped)) |
| 7142 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7143 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7144 | // Warn about comparisons against a string constant (unless the other |
| 7145 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7146 | Expr *literalString = 0; |
| 7147 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7148 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7149 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7150 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7151 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7152 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7153 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7154 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7155 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7156 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7157 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7158 | literalStringStripped = RHSStripped; |
| 7159 | } |
| 7160 | |
| 7161 | if (literalString) { |
| 7162 | std::string resultComparison; |
| 7163 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7164 | case BO_LT: resultComparison = ") < 0"; break; |
| 7165 | case BO_GT: resultComparison = ") > 0"; break; |
| 7166 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7167 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7168 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7169 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7170 | default: assert(false && "Invalid comparison operator"); |
| 7171 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7172 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7173 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7174 | PDiag(diag::warn_stringcompare) |
| 7175 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7176 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7177 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7178 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7179 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7180 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7181 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7182 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7183 | if (lex.isInvalid() || rex.isInvalid()) |
| 7184 | return QualType(); |
| 7185 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7186 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7187 | lex = UsualUnaryConversions(lex.take()); |
| 7188 | if (lex.isInvalid()) |
| 7189 | return QualType(); |
| 7190 | |
| 7191 | rex = UsualUnaryConversions(rex.take()); |
| 7192 | if (rex.isInvalid()) |
| 7193 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7194 | } |
| 7195 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7196 | lType = lex.get()->getType(); |
| 7197 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7198 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7199 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7200 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7201 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7202 | if (isRelational) { |
| 7203 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7204 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7205 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7206 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7207 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7208 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7209 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7210 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7211 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7212 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7213 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7214 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7215 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7216 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7217 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7218 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7219 | // All of the following pointer-related warnings are GCC extensions, except |
| 7220 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7221 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7222 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7223 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7224 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7225 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7226 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7227 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7228 | if (LCanPointeeTy == RCanPointeeTy) |
| 7229 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7230 | if (!isRelational && |
| 7231 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7232 | // Valid unless comparison between non-null pointer and function pointer |
| 7233 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7234 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7235 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7236 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7237 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7238 | Diag(Loc, |
| 7239 | isSFINAEContext()? |
| 7240 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7241 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7242 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7243 | |
| 7244 | if (isSFINAEContext()) |
| 7245 | return QualType(); |
| 7246 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7247 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7248 | return ResultTy; |
| 7249 | } |
| 7250 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7251 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7252 | // C++ [expr.rel]p2: |
| 7253 | // [...] Pointer conversions (4.10) and qualification |
| 7254 | // conversions (4.4) are performed on pointer operands (or on |
| 7255 | // a pointer operand and a null pointer constant) to bring |
| 7256 | // them to their composite pointer type. [...] |
| 7257 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7258 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7259 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7260 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7261 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7262 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7263 | if (T.isNull()) { |
| 7264 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7265 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7266 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7267 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7268 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7269 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7270 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7271 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7272 | } |
| 7273 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7274 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7275 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7276 | return ResultTy; |
| 7277 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7278 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7279 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7280 | RCanPointeeTy.getUnqualifiedType())) { |
| 7281 | // Valid unless a relational comparison of function pointers |
| 7282 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7283 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7284 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7285 | } |
| 7286 | } else if (!isRelational && |
| 7287 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7288 | // Valid unless comparison between non-null pointer and function pointer |
| 7289 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7290 | && !LHSIsNull && !RHSIsNull) { |
| 7291 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7292 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7293 | } |
| 7294 | } else { |
| 7295 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7296 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7297 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7298 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7299 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7300 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7301 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7302 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7303 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7304 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7305 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7307 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7308 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7309 | // Comparison of nullptr_t with itself. |
| 7310 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7311 | return ResultTy; |
| 7312 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7313 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7314 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7315 | if (RHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7316 | ((lType->isPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7317 | (!isRelational && lType->isMemberPointerType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7318 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7319 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7320 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7321 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7322 | return ResultTy; |
| 7323 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7324 | if (LHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7325 | ((rType->isPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7326 | (!isRelational && rType->isMemberPointerType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7327 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7328 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7329 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7330 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7331 | return ResultTy; |
| 7332 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7333 | |
| 7334 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7335 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7336 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7337 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7338 | // In addition, pointers to members can be compared, or a pointer to |
| 7339 | // member and a null pointer constant. Pointer to member conversions |
| 7340 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7341 | // them to a common type. If one operand is a null pointer constant, |
| 7342 | // the common type is the type of the other operand. Otherwise, the |
| 7343 | // common type is a pointer to member type similar (4.4) to the type |
| 7344 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7345 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7346 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7347 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7348 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7349 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7350 | if (T.isNull()) { |
| 7351 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7352 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7353 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7354 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7355 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7356 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7357 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7358 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7359 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7360 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7361 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7362 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7363 | return ResultTy; |
| 7364 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7365 | |
| 7366 | // Handle scoped enumeration types specifically, since they don't promote |
| 7367 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7368 | if (lex.get()->getType()->isEnumeralType() && |
| 7369 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7370 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7372 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7373 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7374 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7375 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7376 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7377 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7378 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7379 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7380 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7381 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7382 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7383 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7384 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7385 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7386 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7387 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7388 | if (!isRelational |
| 7389 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7390 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7391 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7392 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7393 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7394 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7395 | ->getPointeeType()->isVoidType()))) |
| 7396 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7397 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7398 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7399 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7400 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7401 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7402 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7403 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7404 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7405 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7406 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7407 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7408 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7409 | if (LPT || RPT) { |
| 7410 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7411 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7412 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7413 | if (!LPtrToVoid && !RPtrToVoid && |
| 7414 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7415 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7416 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7417 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7418 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7419 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7420 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7421 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7422 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7423 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7424 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7425 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7426 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7427 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7428 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7429 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7430 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7431 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7432 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7433 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7434 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7435 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7436 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7437 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7438 | bool isError = false; |
| 7439 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7440 | (RHSIsNull && rType->isIntegerType())) { |
| 7441 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7442 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7443 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7444 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7445 | else if (getLangOptions().CPlusPlus) { |
| 7446 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7447 | isError = true; |
| 7448 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7449 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7450 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7451 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7452 | Diag(Loc, DiagID) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7453 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7454 | if (isError) |
| 7455 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7456 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7457 | |
| 7458 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7459 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7460 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7461 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7462 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7463 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7464 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7465 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7466 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7467 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7468 | if (!isRelational && RHSIsNull |
| 7469 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7470 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7471 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7472 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7473 | if (!isRelational && LHSIsNull |
| 7474 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7475 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7476 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7477 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7478 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7479 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7482 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7483 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7484 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7485 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7486 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7487 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7488 | bool isRelational) { |
| 7489 | // Check to make sure we're operating on vectors of the same type and width, |
| 7490 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7491 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7492 | if (vType.isNull()) |
| 7493 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7494 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7495 | QualType lType = lex.get()->getType(); |
| 7496 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7497 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7498 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7499 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7500 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7501 | return Context.getLogicalOperationType(); |
| 7502 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7503 | // For non-floating point types, check for self-comparisons of the form |
| 7504 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7505 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7506 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7507 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7508 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7509 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7510 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7511 | PDiag(diag::warn_comparison_always) |
| 7512 | << 0 // self- |
| 7513 | << 2 // "a constant" |
| 7514 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7515 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7516 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7517 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7518 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7519 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7520 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7521 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7522 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7523 | // Return the type for the comparison, which is the same as vector type for |
| 7524 | // integer vectors, or an integer type of identical size and number of |
| 7525 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7526 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7527 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7528 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7529 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7530 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7531 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7532 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7533 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7534 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7535 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7536 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7537 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7538 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7539 | } |
| 7540 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7541 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7542 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7543 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7544 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7545 | rex.get()->getType()->hasIntegerRepresentation()) |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7546 | return CheckVectorOperands(Loc, lex, rex); |
| 7547 | |
| 7548 | return InvalidOperands(Loc, lex, rex); |
| 7549 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7550 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7551 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 7552 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 7553 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 7554 | return QualType(); |
| 7555 | lex = lexResult.take(); |
| 7556 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7557 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7558 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7559 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7560 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7561 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7562 | } |
| 7563 | |
| 7564 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7565 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7566 | |
| 7567 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7568 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7569 | // is a constant. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7570 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 7571 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7572 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7573 | !Loc.isMacroID()) { |
| 7574 | // If the RHS can be constant folded, and if it constant folds to something |
| 7575 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7576 | // happened to fold to true/false) then warn. |
| 7577 | Expr::EvalResult Result; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7578 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects && |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7579 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 7580 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7581 | << rex.get()->getSourceRange() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7582 | << (Opc == BO_LAnd ? "&&" : "||") |
| 7583 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7584 | } |
| 7585 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7586 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7587 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7588 | lex = UsualUnaryConversions(lex.take()); |
| 7589 | if (lex.isInvalid()) |
| 7590 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7591 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7592 | rex = UsualUnaryConversions(rex.take()); |
| 7593 | if (rex.isInvalid()) |
| 7594 | return QualType(); |
| 7595 | |
| 7596 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7597 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7598 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7599 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7600 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7601 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7602 | // The following is safe because we only use this method for |
| 7603 | // non-overloadable operands. |
| 7604 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7605 | // C++ [expr.log.and]p1 |
| 7606 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7607 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7608 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 7609 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7610 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7611 | lex = move(lexRes); |
| 7612 | |
| 7613 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 7614 | if (rexRes.isInvalid()) |
| 7615 | return InvalidOperands(Loc, lex, rex); |
| 7616 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7617 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7618 | // C++ [expr.log.and]p2 |
| 7619 | // C++ [expr.log.or]p2 |
| 7620 | // The result is a bool. |
| 7621 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7622 | } |
| 7623 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7624 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7625 | /// is a read-only property; return true if so. A readonly property expression |
| 7626 | /// depends on various declarations and thus must be treated specially. |
| 7627 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7628 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7629 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7630 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7631 | if (PropExpr->isImplicitProperty()) return false; |
| 7632 | |
| 7633 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7634 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 7635 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7636 | PropExpr->getBase()->getType(); |
| 7637 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7638 | if (const ObjCObjectPointerType *OPT = |
| 7639 | BaseType->getAsObjCInterfacePointerType()) |
| 7640 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7641 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7642 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7643 | } |
| 7644 | return false; |
| 7645 | } |
| 7646 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7647 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 7648 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7649 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 7650 | if (PropExpr->isImplicitProperty()) return false; |
| 7651 | |
| 7652 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7653 | QualType T = PDecl->getType(); |
| 7654 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 7655 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7656 | CanQualType CT = S.Context.getCanonicalType(T); |
| 7657 | return CT.isConstQualified(); |
| 7658 | } |
| 7659 | return false; |
| 7660 | } |
| 7661 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7662 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 7663 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 7664 | return false; |
| 7665 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 7666 | NamedDecl *Member = ME->getMemberDecl(); |
| 7667 | if (isa<FieldDecl>(Member)) { |
| 7668 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7669 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 7670 | return false; |
| 7671 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 7672 | } |
| 7673 | return false; |
| 7674 | } |
| 7675 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7676 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7677 | /// emit an error and return true. If so, return false. |
| 7678 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7679 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7680 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7681 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7682 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7683 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7684 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 7685 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7686 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7687 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7688 | if (IsLV == Expr::MLV_Valid) |
| 7689 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7690 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7691 | unsigned Diag = 0; |
| 7692 | bool NeedType = false; |
| 7693 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7694 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7695 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7696 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7697 | NeedType = true; |
| 7698 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7699 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7700 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7701 | NeedType = true; |
| 7702 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7703 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7704 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7705 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7706 | case Expr::MLV_Valid: |
| 7707 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7708 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7709 | case Expr::MLV_MemberFunction: |
| 7710 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7711 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7712 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7713 | case Expr::MLV_IncompleteType: |
| 7714 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7715 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7716 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7717 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7718 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7719 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7720 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7721 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7722 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7723 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7724 | case Expr::MLV_ReadonlyProperty: |
| 7725 | Diag = diag::error_readonly_property_assignment; |
| 7726 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7727 | case Expr::MLV_NoSetterProperty: |
| 7728 | Diag = diag::error_nosetter_property_assignment; |
| 7729 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7730 | case Expr::MLV_InvalidMessageExpression: |
| 7731 | Diag = diag::error_readonly_message_assignment; |
| 7732 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7733 | case Expr::MLV_SubObjCPropertySetting: |
| 7734 | Diag = diag::error_no_subobject_property_setting; |
| 7735 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7736 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7737 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7738 | SourceRange Assign; |
| 7739 | if (Loc != OrigLoc) |
| 7740 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7741 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7742 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7743 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7744 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7745 | return true; |
| 7746 | } |
| 7747 | |
| 7748 | |
| 7749 | |
| 7750 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7751 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7752 | SourceLocation Loc, |
| 7753 | QualType CompoundType) { |
| 7754 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 7755 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7756 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7757 | |
| 7758 | QualType LHSType = LHS->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7759 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7760 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7761 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7762 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7763 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7764 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 7765 | ExprResult LHSResult = Owned(LHS); |
| 7766 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7767 | if (LHSResult.isInvalid()) |
| 7768 | return QualType(); |
| 7769 | LHS = LHSResult.take(); |
| 7770 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7771 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7772 | if (RHS.isInvalid()) |
| 7773 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7774 | // Special case of NSObject attributes on c-style pointer types. |
| 7775 | if (ConvTy == IncompatiblePointer && |
| 7776 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7777 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7778 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7779 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7780 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7781 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7782 | if (ConvTy == Compatible && |
| 7783 | getLangOptions().ObjCNonFragileABI && |
| 7784 | LHSType->isObjCObjectType()) |
| 7785 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7786 | << LHSType; |
| 7787 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7788 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7789 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7790 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7791 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7792 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7793 | RHSCheck = ICE->getSubExpr(); |
| 7794 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7795 | if ((UO->getOpcode() == UO_Plus || |
| 7796 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7797 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7798 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7799 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7800 | // And there is a space or other character before the subexpr of the |
| 7801 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7802 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7803 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7804 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7805 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7806 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7807 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7808 | } |
| 7809 | } else { |
| 7810 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7811 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7812 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7813 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7814 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7815 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7816 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7817 | |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7818 | |
| 7819 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 7820 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 7821 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 7822 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 7823 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 7824 | // check. |
| 7825 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7826 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7827 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7828 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 7829 | !UO->getType().isVolatileQualified()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7830 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7831 | PDiag(diag::warn_indirection_through_null) |
| 7832 | << UO->getSubExpr()->getSourceRange()); |
| 7833 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7834 | PDiag(diag::note_indirection_through_null)); |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7835 | } |
| 7836 | |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7837 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7838 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7839 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7840 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7841 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7842 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7843 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7844 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7845 | // 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] | 7846 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7847 | return (getLangOptions().CPlusPlus |
| 7848 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7849 | } |
| 7850 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7851 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7852 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7853 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7854 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7855 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7856 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7857 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7858 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7859 | return QualType(); |
| 7860 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7861 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7862 | // operands, but not unary promotions. |
| 7863 | // 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] | 7864 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7865 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7866 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7867 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7868 | if (LHS.isInvalid()) |
| 7869 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7870 | |
| 7871 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7872 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7873 | if (RHS.isInvalid()) |
| 7874 | return QualType(); |
| 7875 | if (!RHS.get()->getType()->isVoidType()) |
| 7876 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7877 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7878 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7879 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7880 | } |
| 7881 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7882 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7883 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7884 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7885 | ExprValueKind &VK, |
| 7886 | SourceLocation OpLoc, |
| 7887 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7888 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7889 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7890 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7891 | QualType ResType = Op->getType(); |
| 7892 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7893 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7894 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7895 | // Decrement of bool is not allowed. |
| 7896 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7897 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7898 | return QualType(); |
| 7899 | } |
| 7900 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7901 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7902 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7903 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7904 | } else if (ResType->isAnyPointerType()) { |
| 7905 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7906 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7907 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7908 | if (PointeeTy->isVoidType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7909 | if (S.getLangOptions().CPlusPlus) { |
| 7910 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7911 | << Op->getSourceRange(); |
| 7912 | return QualType(); |
| 7913 | } |
| 7914 | |
| 7915 | // Pointer to void is a GNU extension in C. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7916 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7917 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7918 | if (S.getLangOptions().CPlusPlus) { |
| 7919 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7920 | << Op->getType() << Op->getSourceRange(); |
| 7921 | return QualType(); |
| 7922 | } |
| 7923 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7924 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7925 | << ResType << Op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7926 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 7927 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7928 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7929 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7930 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7931 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7932 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7933 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7934 | << PointeeTy << Op->getSourceRange(); |
| 7935 | return QualType(); |
| 7936 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7937 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7938 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7939 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7940 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7941 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7942 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7943 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7944 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7945 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7946 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7947 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7948 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7949 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7950 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7951 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7952 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7953 | // 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] | 7954 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7955 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7956 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7957 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7958 | // (in C or with postfix), the increment is the unqualified type of the |
| 7959 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7960 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7961 | VK = VK_LValue; |
| 7962 | return ResType; |
| 7963 | } else { |
| 7964 | VK = VK_RValue; |
| 7965 | return ResType.getUnqualifiedType(); |
| 7966 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7967 | } |
| 7968 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7969 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7970 | assert(E->getValueKind() == VK_LValue && |
| 7971 | E->getObjectKind() == OK_ObjCProperty); |
| 7972 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7973 | |
| 7974 | ExprValueKind VK = VK_RValue; |
| 7975 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7976 | if (const ObjCMethodDecl *GetterMethod = |
| 7977 | PRE->getImplicitPropertyGetter()) { |
| 7978 | QualType Result = GetterMethod->getResultType(); |
| 7979 | VK = Expr::getValueKindForType(Result); |
| 7980 | } |
| 7981 | else { |
| 7982 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7983 | << PRE->getBase()->getType(); |
| 7984 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
| 7987 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 7988 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7989 | |
| 7990 | ExprResult Result = MaybeBindToTemporary(E); |
| 7991 | if (!Result.isInvalid()) |
| 7992 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7993 | |
| 7994 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7995 | } |
| 7996 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7997 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 7998 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7999 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8000 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8001 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8002 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8003 | // If using property-dot syntax notation for assignment, and there is a |
| 8004 | // setter, RHS expression is being passed to the setter argument. So, |
| 8005 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8006 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8007 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8008 | LHSTy = (*P)->getType(); |
| 8009 | |
| 8010 | // Otherwise, if the getter returns an l-value, just call that. |
| 8011 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8012 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8013 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8014 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8015 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8016 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8017 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8018 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8019 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8020 | } |
| 8021 | |
| 8022 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8023 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8024 | InitializedEntity::InitializeParameter(Context, LHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8025 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8026 | if (!ArgE.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8027 | RHS = ArgE; |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8028 | } |
| 8029 | } |
| 8030 | |
| 8031 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8032 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8033 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8034 | /// where the declaration is needed for type checking. We only need to |
| 8035 | /// handle cases when the expression references a function designator |
| 8036 | /// or is an lvalue. Here are some examples: |
| 8037 | /// - &(x) => x |
| 8038 | /// - &*****f => f for f a function designator. |
| 8039 | /// - &s.xx => s |
| 8040 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8041 | /// - *(x + 1) -> x, if x is an array |
| 8042 | /// - &"123"[2] -> 0 |
| 8043 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8044 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8045 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8046 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8047 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8048 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8049 | // If this is an arrow operator, the address is an offset from |
| 8050 | // the base's value, so the object the base refers to is |
| 8051 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8052 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8053 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8054 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8055 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8056 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8057 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8058 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8059 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8060 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8061 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8062 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8063 | } |
| 8064 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8065 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8066 | case Stmt::UnaryOperatorClass: { |
| 8067 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8068 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8069 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8070 | case UO_Real: |
| 8071 | case UO_Imag: |
| 8072 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8073 | return getPrimaryDecl(UO->getSubExpr()); |
| 8074 | default: |
| 8075 | return 0; |
| 8076 | } |
| 8077 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8078 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8079 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8080 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8081 | // If the result of an implicit cast is an l-value, we care about |
| 8082 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8083 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8084 | default: |
| 8085 | return 0; |
| 8086 | } |
| 8087 | } |
| 8088 | |
| 8089 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8090 | /// 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] | 8091 | /// 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] | 8092 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8093 | /// 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] | 8094 | /// 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] | 8095 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8096 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8097 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8098 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8099 | return S.Context.DependentTy; |
| 8100 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8101 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8102 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8103 | return S.Context.UnknownAnyTy; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8104 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8105 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8106 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8107 | // Make sure to ignore parentheses in subsequent checks |
| 8108 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8109 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8110 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8111 | // Implement C99-only parts of addressof rules. |
| 8112 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8113 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8114 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8115 | // (assuming the deref expression is valid). |
| 8116 | return uOp->getSubExpr()->getType(); |
| 8117 | } |
| 8118 | // Technically, there should be a check for array subscript |
| 8119 | // expressions here, but the result of one is always an lvalue anyway. |
| 8120 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8121 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8122 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8123 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8124 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8125 | bool sfinae = S.isSFINAEContext(); |
| 8126 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8127 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8128 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8129 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8130 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8131 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8132 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8133 | } else if (lval == Expr::LV_MemberFunction) { |
| 8134 | // If it's an instance method, make a member pointer. |
| 8135 | // The expression must have exactly the form &A::foo. |
| 8136 | |
| 8137 | // If the underlying expression isn't a decl ref, give up. |
| 8138 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8139 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8140 | << OrigOp->getSourceRange(); |
| 8141 | return QualType(); |
| 8142 | } |
| 8143 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8144 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8145 | |
| 8146 | // The id-expression was parenthesized. |
| 8147 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8148 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8149 | << OrigOp->getSourceRange(); |
| 8150 | |
| 8151 | // The method was named without a qualifier. |
| 8152 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8153 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8154 | << op->getSourceRange(); |
| 8155 | } |
| 8156 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8157 | return S.Context.getMemberPointerType(op->getType(), |
| 8158 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8159 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8160 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8161 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8162 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8163 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8164 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8165 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8166 | return QualType(); |
| 8167 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8168 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8169 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8170 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8171 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8172 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8173 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8174 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8175 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8176 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8177 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8178 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8179 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8180 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8181 | << "property expression" << op->getSourceRange(); |
| 8182 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8183 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8184 | // 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] | 8185 | // with the register storage-class specifier. |
| 8186 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8187 | // in C++ it is not error to take address of a register |
| 8188 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8189 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8190 | !S.getLangOptions().CPlusPlus) { |
| 8191 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8192 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8193 | return QualType(); |
| 8194 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8195 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8196 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8197 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8198 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8199 | // Could be a pointer to member, though, if there is an explicit |
| 8200 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8201 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8202 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8203 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8204 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8205 | S.Diag(OpLoc, |
| 8206 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8207 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8208 | return QualType(); |
| 8209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8210 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8211 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8212 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8213 | return S.Context.getMemberPointerType(op->getType(), |
| 8214 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8215 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8216 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8217 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8218 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8219 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8220 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8221 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8222 | // Taking the address of a void variable is technically illegal, but we |
| 8223 | // allow it in cases which are otherwise valid. |
| 8224 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8225 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8226 | } |
| 8227 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8228 | // 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] | 8229 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8230 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8231 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8232 | } |
| 8233 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8234 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8235 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8236 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8237 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8238 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8239 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8240 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8241 | if (ConvResult.isInvalid()) |
| 8242 | return QualType(); |
| 8243 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8244 | QualType OpTy = Op->getType(); |
| 8245 | QualType Result; |
| 8246 | |
| 8247 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8248 | // is an incomplete type or void. It would be possible to warn about |
| 8249 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8250 | // warning is unlikely to catch any mistakes. |
| 8251 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8252 | Result = PT->getPointeeType(); |
| 8253 | else if (const ObjCObjectPointerType *OPT = |
| 8254 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8255 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8256 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8257 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8258 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8259 | if (PR.take() != Op) |
| 8260 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8261 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8262 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8263 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8264 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8265 | << OpTy << Op->getSourceRange(); |
| 8266 | return QualType(); |
| 8267 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8268 | |
| 8269 | // Dereferences are usually l-values... |
| 8270 | VK = VK_LValue; |
| 8271 | |
| 8272 | // ...except that certain expressions are never l-values in C. |
| 8273 | if (!S.getLangOptions().CPlusPlus && |
| 8274 | IsCForbiddenLValueType(S.Context, Result)) |
| 8275 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8276 | |
| 8277 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8278 | } |
| 8279 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8280 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8281 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8282 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8283 | switch (Kind) { |
| 8284 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8285 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8286 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8287 | case tok::star: Opc = BO_Mul; break; |
| 8288 | case tok::slash: Opc = BO_Div; break; |
| 8289 | case tok::percent: Opc = BO_Rem; break; |
| 8290 | case tok::plus: Opc = BO_Add; break; |
| 8291 | case tok::minus: Opc = BO_Sub; break; |
| 8292 | case tok::lessless: Opc = BO_Shl; break; |
| 8293 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8294 | case tok::lessequal: Opc = BO_LE; break; |
| 8295 | case tok::less: Opc = BO_LT; break; |
| 8296 | case tok::greaterequal: Opc = BO_GE; break; |
| 8297 | case tok::greater: Opc = BO_GT; break; |
| 8298 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8299 | case tok::equalequal: Opc = BO_EQ; break; |
| 8300 | case tok::amp: Opc = BO_And; break; |
| 8301 | case tok::caret: Opc = BO_Xor; break; |
| 8302 | case tok::pipe: Opc = BO_Or; break; |
| 8303 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8304 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8305 | case tok::equal: Opc = BO_Assign; break; |
| 8306 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8307 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8308 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8309 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8310 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8311 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8312 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8313 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8314 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8315 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8316 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8317 | } |
| 8318 | return Opc; |
| 8319 | } |
| 8320 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8321 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8322 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8323 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8324 | switch (Kind) { |
| 8325 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8326 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8327 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8328 | case tok::amp: Opc = UO_AddrOf; break; |
| 8329 | case tok::star: Opc = UO_Deref; break; |
| 8330 | case tok::plus: Opc = UO_Plus; break; |
| 8331 | case tok::minus: Opc = UO_Minus; break; |
| 8332 | case tok::tilde: Opc = UO_Not; break; |
| 8333 | case tok::exclaim: Opc = UO_LNot; break; |
| 8334 | case tok::kw___real: Opc = UO_Real; break; |
| 8335 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8336 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8337 | } |
| 8338 | return Opc; |
| 8339 | } |
| 8340 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8341 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8342 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8343 | /// suppressed in the event of macro expansions. |
| 8344 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8345 | SourceLocation OpLoc) { |
| 8346 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8347 | return; |
| 8348 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8349 | return; |
| 8350 | lhs = lhs->IgnoreParenImpCasts(); |
| 8351 | rhs = rhs->IgnoreParenImpCasts(); |
| 8352 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8353 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8354 | if (!LeftDeclRef || !RightDeclRef || |
| 8355 | LeftDeclRef->getLocation().isMacroID() || |
| 8356 | RightDeclRef->getLocation().isMacroID()) |
| 8357 | return; |
| 8358 | const ValueDecl *LeftDecl = |
| 8359 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8360 | const ValueDecl *RightDecl = |
| 8361 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8362 | if (LeftDecl != RightDecl) |
| 8363 | return; |
| 8364 | if (LeftDecl->getType().isVolatileQualified()) |
| 8365 | return; |
| 8366 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8367 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8368 | return; |
| 8369 | |
| 8370 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8371 | << LeftDeclRef->getType() |
| 8372 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8373 | } |
| 8374 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8375 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8376 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8377 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8378 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8379 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8380 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8381 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8382 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8383 | // The following two variables are used for compound assignment operators |
| 8384 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8385 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8386 | ExprValueKind VK = VK_RValue; |
| 8387 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8388 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8389 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8390 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8391 | // But since an assignment can trigger target based overload, exclude it in |
| 8392 | // our blind search. i.e: |
| 8393 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8394 | // f<int> == 0; // resolve f<int> blindly |
| 8395 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8396 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8397 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8398 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8399 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8400 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8401 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8402 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8403 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8404 | } |
| 8405 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8406 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8407 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8408 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8409 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8410 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8411 | VK = lhs.get()->getValueKind(); |
| 8412 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8413 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8414 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8415 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8416 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8417 | case BO_PtrMemD: |
| 8418 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8419 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8420 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8421 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8422 | case BO_Mul: |
| 8423 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8424 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8425 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8426 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8427 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8428 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 8429 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8430 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8431 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 8432 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8433 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8434 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 8435 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8436 | case BO_Shl: |
| 8437 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8438 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8439 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8440 | case BO_LE: |
| 8441 | case BO_LT: |
| 8442 | case BO_GE: |
| 8443 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8444 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8445 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8446 | case BO_EQ: |
| 8447 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8448 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8449 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8450 | case BO_And: |
| 8451 | case BO_Xor: |
| 8452 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8453 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 8454 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8455 | case BO_LAnd: |
| 8456 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8457 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8458 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8459 | case BO_MulAssign: |
| 8460 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8461 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8462 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8463 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8464 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8465 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8466 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8467 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8468 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 8469 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8470 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8471 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8472 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8473 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8474 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8475 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8476 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8477 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8478 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8479 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8480 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8481 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8482 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8483 | case BO_ShlAssign: |
| 8484 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8485 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8486 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8487 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8488 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8489 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8490 | case BO_AndAssign: |
| 8491 | case BO_XorAssign: |
| 8492 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8493 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8494 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8495 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8496 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8497 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8498 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8499 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8500 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 8501 | VK = rhs.get()->getValueKind(); |
| 8502 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8503 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8504 | break; |
| 8505 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8506 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8507 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8508 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8509 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 8510 | ResultTy, VK, OK, OpLoc)); |
| 8511 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8512 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8513 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8514 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8515 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 8516 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8517 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8518 | } |
| 8519 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8520 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 8521 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8522 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 8523 | const PartialDiagnostic &PD, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8524 | const PartialDiagnostic &FirstNote, |
| 8525 | SourceRange FirstParenRange, |
| 8526 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8527 | SourceRange SecondParenRange) { |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8528 | Self.Diag(Loc, PD); |
| 8529 | |
| 8530 | if (!FirstNote.getDiagID()) |
| 8531 | return; |
| 8532 | |
| 8533 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 8534 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8535 | // We can't display the parentheses, so just return. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8536 | return; |
| 8537 | } |
| 8538 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8539 | Self.Diag(Loc, FirstNote) |
| 8540 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8541 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8542 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8543 | if (!SecondNote.getDiagID()) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8544 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8545 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8546 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 8547 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8548 | // We can't display the parentheses, so just dig the |
| 8549 | // warning/error and return. |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8550 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8551 | return; |
| 8552 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8553 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8554 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8555 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 8556 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8557 | } |
| 8558 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8559 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8560 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8561 | /// comparison operators have higher precedence. The most typical example of |
| 8562 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8563 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8564 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8565 | typedef BinaryOperator BinOp; |
| 8566 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8567 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8568 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8569 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8570 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8571 | rhsopc = BO->getOpcode(); |
| 8572 | |
| 8573 | // Subs are not binary operators. |
| 8574 | if (lhsopc == -1 && rhsopc == -1) |
| 8575 | return; |
| 8576 | |
| 8577 | // Bitwise operations are sometimes used as eager logical ops. |
| 8578 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8579 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8580 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8581 | return; |
| 8582 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8583 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8584 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8585 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8586 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8587 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8588 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8589 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8590 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 8591 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8592 | << BinOp::getOpcodeStr(lhsopc), |
| 8593 | lhs->getSourceRange()); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8594 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8595 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8596 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8597 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8598 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8599 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8600 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8601 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 8602 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8603 | << BinOp::getOpcodeStr(rhsopc), |
| 8604 | rhs->getSourceRange()); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8605 | } |
| 8606 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8607 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8608 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8609 | /// in parentheses. |
| 8610 | static void |
| 8611 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| 8612 | Expr *E) { |
| 8613 | assert(isa<BinaryOperator>(E) && |
| 8614 | cast<BinaryOperator>(E)->getOpcode() == BO_LAnd); |
| 8615 | SuggestParentheses(Self, OpLoc, |
| 8616 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| 8617 | << E->getSourceRange(), |
| 8618 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| 8619 | E->getSourceRange(), |
| 8620 | Self.PDiag(0), SourceRange()); |
| 8621 | } |
| 8622 | |
| 8623 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8624 | /// 'true'. |
| 8625 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8626 | bool Res; |
| 8627 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8628 | } |
| 8629 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8630 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8631 | /// 'false'. |
| 8632 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8633 | bool Res; |
| 8634 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8635 | } |
| 8636 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8637 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8638 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8639 | Expr *OrLHS, Expr *OrRHS) { |
| 8640 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8641 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8642 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 8643 | if (EvaluatesAsFalse(S, OrRHS)) |
| 8644 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8645 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8646 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8647 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8648 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8649 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8650 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8651 | // "a || b && 1", but warn now. |
| 8652 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8653 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8654 | } |
| 8655 | } |
| 8656 | } |
| 8657 | } |
| 8658 | |
| 8659 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8660 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8661 | Expr *OrLHS, Expr *OrRHS) { |
| 8662 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8663 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8664 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 8665 | if (EvaluatesAsFalse(S, OrLHS)) |
| 8666 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8667 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8668 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8669 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8670 | } |
| 8671 | } |
| 8672 | } |
| 8673 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8674 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8675 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8676 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8677 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8678 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8679 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8680 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 8681 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8682 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8683 | // 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] | 8684 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8685 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 8686 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8687 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8688 | } |
| 8689 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8690 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8691 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8692 | tok::TokenKind Kind, |
| 8693 | Expr *lhs, Expr *rhs) { |
| 8694 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8695 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8696 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8697 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8698 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8699 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8700 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8701 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8702 | } |
| 8703 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8704 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8705 | BinaryOperatorKind Opc, |
| 8706 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8707 | if (getLangOptions().CPlusPlus) { |
| 8708 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8709 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8710 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8711 | UseBuiltinOperator = false; |
| 8712 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8713 | UseBuiltinOperator = true; |
| 8714 | } else { |
| 8715 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 8716 | !rhs->getType()->isOverloadableType(); |
| 8717 | } |
| 8718 | |
| 8719 | if (!UseBuiltinOperator) { |
| 8720 | // Find all of the overloaded operators visible from this |
| 8721 | // point. We perform both an operator-name lookup from the local |
| 8722 | // scope and an argument-dependent lookup based on the types of |
| 8723 | // the arguments. |
| 8724 | UnresolvedSet<16> Functions; |
| 8725 | OverloadedOperatorKind OverOp |
| 8726 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8727 | if (S && OverOp != OO_None) |
| 8728 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 8729 | Functions); |
| 8730 | |
| 8731 | // Build the (potentially-overloaded, potentially-dependent) |
| 8732 | // binary operation. |
| 8733 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 8734 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8735 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8736 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8737 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8738 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8739 | } |
| 8740 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8741 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8742 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8743 | Expr *InputExpr) { |
| 8744 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8745 | ExprValueKind VK = VK_RValue; |
| 8746 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8747 | QualType resultType; |
| 8748 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8749 | case UO_PreInc: |
| 8750 | case UO_PreDec: |
| 8751 | case UO_PostInc: |
| 8752 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8753 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8754 | Opc == UO_PreInc || |
| 8755 | Opc == UO_PostInc, |
| 8756 | Opc == UO_PreInc || |
| 8757 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8758 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8759 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8760 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8761 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8762 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8763 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8764 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8765 | Input = move(resolved); |
| 8766 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8767 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8768 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8769 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8770 | case UO_Plus: |
| 8771 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8772 | Input = UsualUnaryConversions(Input.take()); |
| 8773 | if (Input.isInvalid()) return ExprError(); |
| 8774 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8775 | if (resultType->isDependentType()) |
| 8776 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8777 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8778 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8779 | break; |
| 8780 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8781 | resultType->isEnumeralType()) |
| 8782 | break; |
| 8783 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8784 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8785 | resultType->isPointerType()) |
| 8786 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8787 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8788 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8789 | if (Input.isInvalid()) return ExprError(); |
| 8790 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8791 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8792 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8793 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8794 | << resultType << Input.get()->getSourceRange()); |
| 8795 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8796 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8797 | Input = UsualUnaryConversions(Input.take()); |
| 8798 | if (Input.isInvalid()) return ExprError(); |
| 8799 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8800 | if (resultType->isDependentType()) |
| 8801 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8802 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8803 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8804 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8805 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8806 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8807 | else if (resultType->hasIntegerRepresentation()) |
| 8808 | break; |
| 8809 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8810 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8811 | if (Input.isInvalid()) return ExprError(); |
| 8812 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8813 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8814 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8815 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8816 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8817 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8818 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8819 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8820 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8821 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8822 | if (Input.isInvalid()) return ExprError(); |
| 8823 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8824 | if (resultType->isDependentType()) |
| 8825 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8826 | if (resultType->isScalarType()) { |
| 8827 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8828 | if (Context.getLangOptions().CPlusPlus) { |
| 8829 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8830 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8831 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8832 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8833 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8834 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8835 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8836 | if (Input.isInvalid()) return ExprError(); |
| 8837 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8838 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8839 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8840 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8841 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8842 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8843 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8844 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8845 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8846 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8847 | case UO_Real: |
| 8848 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8849 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8850 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8851 | if (Input.isInvalid()) return ExprError(); |
| 8852 | if (Input.get()->getValueKind() != VK_RValue && |
| 8853 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8854 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8855 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8856 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8857 | resultType = Input.get()->getType(); |
| 8858 | VK = Input.get()->getValueKind(); |
| 8859 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8860 | break; |
| 8861 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8862 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8863 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8864 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8865 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8866 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8867 | } |
| 8868 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8869 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8870 | UnaryOperatorKind Opc, |
| 8871 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8872 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8873 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8874 | // Find all of the overloaded operators visible from this |
| 8875 | // point. We perform both an operator-name lookup from the local |
| 8876 | // scope and an argument-dependent lookup based on the types of |
| 8877 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8878 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8879 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8880 | if (S && OverOp != OO_None) |
| 8881 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8882 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8883 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8884 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8885 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8886 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8887 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8888 | } |
| 8889 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8890 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8891 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8892 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8893 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8894 | } |
| 8895 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8896 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8897 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8898 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8899 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8900 | // 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] | 8901 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8902 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8903 | } |
| 8904 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8905 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8906 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8907 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8908 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8909 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8910 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8911 | bool isFileScope |
| 8912 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8913 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8914 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8915 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8916 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8917 | // example, it is not possible to goto into a stmt expression apparently. |
| 8918 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8919 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8920 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8921 | // as the type of the stmtexpr. |
| 8922 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8923 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8924 | if (!Compound->body_empty()) { |
| 8925 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8926 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8927 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8928 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8929 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8930 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8931 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8932 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8933 | // Do function/array conversion on the last expression, but not |
| 8934 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8935 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8936 | if (LastExpr.isInvalid()) |
| 8937 | return ExprError(); |
| 8938 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8939 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8940 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
| 8941 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8942 | InitializedEntity::InitializeResult(LPLoc, |
| 8943 | Ty, |
| 8944 | false), |
| 8945 | SourceLocation(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8946 | LastExpr); |
| 8947 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8948 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8949 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8950 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8951 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8952 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8953 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8954 | StmtExprMayBindToTemp = true; |
| 8955 | } |
| 8956 | } |
| 8957 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8958 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8959 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8960 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8961 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8962 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8963 | if (StmtExprMayBindToTemp) |
| 8964 | return MaybeBindToTemporary(ResStmtExpr); |
| 8965 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8966 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8967 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8968 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8969 | TypeSourceInfo *TInfo, |
| 8970 | OffsetOfComponent *CompPtr, |
| 8971 | unsigned NumComponents, |
| 8972 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8973 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8974 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8975 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8976 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8977 | // We must have at least one component that refers to the type, and the first |
| 8978 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8979 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8980 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8981 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8982 | << ArgTy << TypeRange); |
| 8983 | |
| 8984 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8985 | // with an incomplete type would be ill-formed. |
| 8986 | if (!Dependent |
| 8987 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8988 | PDiag(diag::err_offsetof_incomplete_type) |
| 8989 | << TypeRange)) |
| 8990 | return ExprError(); |
| 8991 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8992 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8993 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8994 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8995 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8996 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8997 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8998 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8999 | |
| 9000 | bool DidWarnAboutNonPOD = false; |
| 9001 | QualType CurrentType = ArgTy; |
| 9002 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9003 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9004 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9005 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9006 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9007 | if (OC.isBrackets) { |
| 9008 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9009 | if (!CurrentType->isDependentType()) { |
| 9010 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9011 | if(!AT) |
| 9012 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9013 | << CurrentType); |
| 9014 | CurrentType = AT->getElementType(); |
| 9015 | } else |
| 9016 | CurrentType = Context.DependentTy; |
| 9017 | |
| 9018 | // The expression must be an integral expression. |
| 9019 | // FIXME: An integral constant expression? |
| 9020 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9021 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9022 | !Idx->getType()->isIntegerType()) |
| 9023 | return ExprError(Diag(Idx->getLocStart(), |
| 9024 | diag::err_typecheck_subscript_not_integer) |
| 9025 | << Idx->getSourceRange()); |
| 9026 | |
| 9027 | // Record this array index. |
| 9028 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9029 | Exprs.push_back(Idx); |
| 9030 | continue; |
| 9031 | } |
| 9032 | |
| 9033 | // Offset of a field. |
| 9034 | if (CurrentType->isDependentType()) { |
| 9035 | // We have the offset of a field, but we can't look into the dependent |
| 9036 | // type. Just record the identifier of the field. |
| 9037 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9038 | CurrentType = Context.DependentTy; |
| 9039 | continue; |
| 9040 | } |
| 9041 | |
| 9042 | // We need to have a complete type to look into. |
| 9043 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9044 | diag::err_offsetof_incomplete_type)) |
| 9045 | return ExprError(); |
| 9046 | |
| 9047 | // Look for the designated field. |
| 9048 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9049 | if (!RC) |
| 9050 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9051 | << CurrentType); |
| 9052 | RecordDecl *RD = RC->getDecl(); |
| 9053 | |
| 9054 | // C++ [lib.support.types]p5: |
| 9055 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9056 | // International Standard. type shall be a POD structure or a POD union |
| 9057 | // (clause 9). |
| 9058 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9059 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9060 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9061 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9062 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9063 | << CurrentType)) |
| 9064 | DidWarnAboutNonPOD = true; |
| 9065 | } |
| 9066 | |
| 9067 | // Look for the field. |
| 9068 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9069 | LookupQualifiedName(R, RD); |
| 9070 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9071 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9072 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9073 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9074 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9075 | } |
| 9076 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9077 | if (!MemberDecl) |
| 9078 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9079 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9080 | OC.LocEnd)); |
| 9081 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9082 | // C99 7.17p3: |
| 9083 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9084 | // |
| 9085 | // We diagnose this as an error. |
| 9086 | if (MemberDecl->getBitWidth()) { |
| 9087 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9088 | << MemberDecl->getDeclName() |
| 9089 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9090 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9091 | return ExprError(); |
| 9092 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9093 | |
| 9094 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9095 | if (IndirectMemberDecl) |
| 9096 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9097 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9098 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9099 | // the base class indirections. |
| 9100 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9101 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9102 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9103 | CXXBasePath &Path = Paths.front(); |
| 9104 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9105 | B != BEnd; ++B) |
| 9106 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9107 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9108 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9109 | if (IndirectMemberDecl) { |
| 9110 | for (IndirectFieldDecl::chain_iterator FI = |
| 9111 | IndirectMemberDecl->chain_begin(), |
| 9112 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9113 | assert(isa<FieldDecl>(*FI)); |
| 9114 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9115 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9116 | } |
| 9117 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9118 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9119 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9120 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9121 | } |
| 9122 | |
| 9123 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9124 | TInfo, Comps.data(), Comps.size(), |
| 9125 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9126 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9127 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9128 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9129 | SourceLocation BuiltinLoc, |
| 9130 | SourceLocation TypeLoc, |
| 9131 | ParsedType argty, |
| 9132 | OffsetOfComponent *CompPtr, |
| 9133 | unsigned NumComponents, |
| 9134 | SourceLocation RPLoc) { |
| 9135 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9136 | TypeSourceInfo *ArgTInfo; |
| 9137 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9138 | if (ArgTy.isNull()) |
| 9139 | return ExprError(); |
| 9140 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9141 | if (!ArgTInfo) |
| 9142 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9143 | |
| 9144 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9145 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9146 | } |
| 9147 | |
| 9148 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9149 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9150 | Expr *CondExpr, |
| 9151 | Expr *LHSExpr, Expr *RHSExpr, |
| 9152 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9153 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9154 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9155 | ExprValueKind VK = VK_RValue; |
| 9156 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9157 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9158 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9159 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9160 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9161 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9162 | } else { |
| 9163 | // The conditional expression is required to be a constant expression. |
| 9164 | llvm::APSInt condEval(32); |
| 9165 | SourceLocation ExpLoc; |
| 9166 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9167 | return ExprError(Diag(ExpLoc, |
| 9168 | diag::err_typecheck_choose_expr_requires_constant) |
| 9169 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9170 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9171 | // 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] | 9172 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9173 | |
| 9174 | resType = ActiveExpr->getType(); |
| 9175 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9176 | VK = ActiveExpr->getValueKind(); |
| 9177 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9178 | } |
| 9179 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9180 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9181 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9182 | resType->isDependentType(), |
| 9183 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9184 | } |
| 9185 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9186 | //===----------------------------------------------------------------------===// |
| 9187 | // Clang Extensions. |
| 9188 | //===----------------------------------------------------------------------===// |
| 9189 | |
| 9190 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9191 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9192 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9193 | PushBlockScope(BlockScope, Block); |
| 9194 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9195 | if (BlockScope) |
| 9196 | PushDeclContext(BlockScope, Block); |
| 9197 | else |
| 9198 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9199 | } |
| 9200 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9201 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9202 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9203 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9204 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9205 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9206 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9207 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9208 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9209 | // GetTypeForDeclarator always produces a function type for a block |
| 9210 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9211 | // unless the function was written with a typedef. |
| 9212 | assert(T->isFunctionType() && |
| 9213 | "GetTypeForDeclarator made a non-function block signature"); |
| 9214 | |
| 9215 | // Look for an explicit signature in that function type. |
| 9216 | FunctionProtoTypeLoc ExplicitSignature; |
| 9217 | |
| 9218 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9219 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9220 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9221 | |
| 9222 | // Check whether that explicit signature was synthesized by |
| 9223 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9224 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9225 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9226 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9227 | // This would be much cheaper if we stored TypeLocs instead of |
| 9228 | // TypeSourceInfos. |
| 9229 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9230 | unsigned Size = Result.getFullDataSize(); |
| 9231 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9232 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9233 | |
| 9234 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9235 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9236 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9237 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9238 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9239 | CurBlock->FunctionType = T; |
| 9240 | |
| 9241 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9242 | QualType RetTy = Fn->getResultType(); |
| 9243 | bool isVariadic = |
| 9244 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9245 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9246 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9247 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9248 | // Don't allow returning a objc interface by value. |
| 9249 | if (RetTy->isObjCObjectType()) { |
| 9250 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9251 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9252 | return; |
| 9253 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9254 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9255 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9256 | // return type. TODO: what should we do with declarators like: |
| 9257 | // ^ * { ... } |
| 9258 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9259 | if (RetTy != Context.DependentTy) |
| 9260 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9261 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9262 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9263 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9264 | if (ExplicitSignature) { |
| 9265 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9266 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9267 | if (Param->getIdentifier() == 0 && |
| 9268 | !Param->isImplicit() && |
| 9269 | !Param->isInvalidDecl() && |
| 9270 | !getLangOptions().CPlusPlus) |
| 9271 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9272 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9273 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9274 | |
| 9275 | // Fake up parameter variables if we have a typedef, like |
| 9276 | // ^ fntype { ... } |
| 9277 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9278 | for (FunctionProtoType::arg_type_iterator |
| 9279 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9280 | ParmVarDecl *Param = |
| 9281 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9282 | ParamInfo.getSourceRange().getBegin(), |
| 9283 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9284 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9285 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9286 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9287 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9288 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9289 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9290 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9291 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9292 | CurBlock->TheDecl->param_end(), |
| 9293 | /*CheckParameterNames=*/false); |
| 9294 | } |
| 9295 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9296 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9297 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9298 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9299 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9300 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9301 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9302 | // FIXME: remove the attribute. |
| 9303 | } |
| 9304 | |
| 9305 | // Put the parameter variables in scope. We can bail out immediately |
| 9306 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9307 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9308 | return; |
| 9309 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9310 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9311 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9312 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9313 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9314 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9315 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9316 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9317 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9318 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9319 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9320 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9321 | } |
| 9322 | |
| 9323 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9324 | /// is invoked to pop the information about the block from the action impl. |
| 9325 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9326 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9327 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9328 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9329 | } |
| 9330 | |
| 9331 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9332 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9333 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9334 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9335 | // If blocks are disabled, emit an error. |
| 9336 | if (!LangOpts.Blocks) |
| 9337 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9338 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9339 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9340 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9341 | PopDeclContext(); |
| 9342 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9343 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9344 | if (!BSI->ReturnType.isNull()) |
| 9345 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9346 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9347 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9348 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9349 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9350 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9351 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9352 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9353 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9354 | // If the user wrote a function type in some form, try to use that. |
| 9355 | if (!BSI->FunctionType.isNull()) { |
| 9356 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9357 | |
| 9358 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9359 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9360 | |
| 9361 | // Turn protoless block types into nullary block types. |
| 9362 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9363 | FunctionProtoType::ExtProtoInfo EPI; |
| 9364 | EPI.ExtInfo = Ext; |
| 9365 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9366 | |
| 9367 | // Otherwise, if we don't need to change anything about the function type, |
| 9368 | // preserve its sugar structure. |
| 9369 | } else if (FTy->getResultType() == RetTy && |
| 9370 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9371 | BlockTy = BSI->FunctionType; |
| 9372 | |
| 9373 | // Otherwise, make the minimal modifications to the function type. |
| 9374 | } else { |
| 9375 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9376 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9377 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9378 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9379 | BlockTy = Context.getFunctionType(RetTy, |
| 9380 | FPT->arg_type_begin(), |
| 9381 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9382 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9383 | } |
| 9384 | |
| 9385 | // If we don't have a function type, just build one from nothing. |
| 9386 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9387 | FunctionProtoType::ExtProtoInfo EPI; |
Eli Friedman | a49218e | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 9388 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, false, 0, CC_Default); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9389 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9390 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9391 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9392 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9393 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9394 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9395 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9396 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9397 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9398 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9399 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9400 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9401 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9402 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9403 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9404 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9405 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9406 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9407 | } |
| 9408 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9409 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9410 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9411 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9412 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9413 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9414 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9415 | } |
| 9416 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9417 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9418 | Expr *E, TypeSourceInfo *TInfo, |
| 9419 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9420 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9421 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9422 | // Get the va_list type |
| 9423 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9424 | if (VaListType->isArrayType()) { |
| 9425 | // Deal with implicit array decay; for example, on x86-64, |
| 9426 | // va_list is an array, but it's supposed to decay to |
| 9427 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9428 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9429 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9430 | ExprResult Result = UsualUnaryConversions(E); |
| 9431 | if (Result.isInvalid()) |
| 9432 | return ExprError(); |
| 9433 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9434 | } else { |
| 9435 | // Otherwise, the va_list argument must be an l-value because |
| 9436 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9437 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9438 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9439 | return ExprError(); |
| 9440 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9441 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9442 | if (!E->isTypeDependent() && |
| 9443 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9444 | return ExprError(Diag(E->getLocStart(), |
| 9445 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9446 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9447 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9448 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9449 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9450 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9451 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9452 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9453 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9454 | } |
| 9455 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9456 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9457 | // The type of __null will be int or long, depending on the size of |
| 9458 | // pointers on the target. |
| 9459 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9460 | unsigned pw = Context.Target.getPointerWidth(0); |
| 9461 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9462 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9463 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9464 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9465 | else if (pw == Context.Target.getLongLongWidth()) |
| 9466 | Ty = Context.LongLongTy; |
| 9467 | else { |
| 9468 | assert(!"I don't know size of pointer!"); |
| 9469 | Ty = Context.IntTy; |
| 9470 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9471 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9472 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9473 | } |
| 9474 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9475 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9476 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9477 | if (!SemaRef.getLangOptions().ObjC1) |
| 9478 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9479 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9480 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9481 | if (!PT) |
| 9482 | return; |
| 9483 | |
| 9484 | // Check if the destination is of type 'id'. |
| 9485 | if (!PT->isObjCIdType()) { |
| 9486 | // Check if the destination is the 'NSString' interface. |
| 9487 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9488 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9489 | return; |
| 9490 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9491 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9492 | // Strip off any parens and casts. |
| 9493 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 9494 | if (!SL || SL->isWide()) |
| 9495 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9496 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9497 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9498 | } |
| 9499 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9500 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9501 | SourceLocation Loc, |
| 9502 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9503 | Expr *SrcExpr, AssignmentAction Action, |
| 9504 | bool *Complained) { |
| 9505 | if (Complained) |
| 9506 | *Complained = false; |
| 9507 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9508 | // Decode the result (notice that AST's are still created for extensions). |
| 9509 | bool isInvalid = false; |
| 9510 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9511 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9512 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9513 | switch (ConvTy) { |
| 9514 | default: assert(0 && "Unknown conversion type"); |
| 9515 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9516 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9517 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9518 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9519 | case IntToPointer: |
| 9520 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9521 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9522 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9523 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9524 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 9525 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9526 | case IncompatiblePointerSign: |
| 9527 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9528 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9529 | case FunctionVoidPointer: |
| 9530 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9531 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9532 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9533 | // Perform array-to-pointer decay if necessary. |
| 9534 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9535 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9536 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9537 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9538 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9539 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9540 | break; |
| 9541 | } |
| 9542 | |
| 9543 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9544 | // fallthrough |
| 9545 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9546 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9547 | // If the qualifiers lost were because we were applying the |
| 9548 | // (deprecated) C++ conversion from a string literal to a char* |
| 9549 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9550 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9551 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9552 | // bit of refactoring (so that the second argument is an |
| 9553 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9554 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9555 | // C++ semantics. |
| 9556 | if (getLangOptions().CPlusPlus && |
| 9557 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9558 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9559 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9560 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9561 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9562 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9563 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9564 | case IntToBlockPointer: |
| 9565 | DiagKind = diag::err_int_to_block_pointer; |
| 9566 | break; |
| 9567 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9568 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9569 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9570 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9571 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9572 | // it can give a more specific diagnostic. |
| 9573 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9574 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9575 | case IncompatibleVectors: |
| 9576 | DiagKind = diag::warn_incompatible_vectors; |
| 9577 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9578 | case Incompatible: |
| 9579 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9580 | isInvalid = true; |
| 9581 | break; |
| 9582 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9583 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9584 | QualType FirstType, SecondType; |
| 9585 | switch (Action) { |
| 9586 | case AA_Assigning: |
| 9587 | case AA_Initializing: |
| 9588 | // The destination type comes first. |
| 9589 | FirstType = DstType; |
| 9590 | SecondType = SrcType; |
| 9591 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9592 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9593 | case AA_Returning: |
| 9594 | case AA_Passing: |
| 9595 | case AA_Converting: |
| 9596 | case AA_Sending: |
| 9597 | case AA_Casting: |
| 9598 | // The source type comes first. |
| 9599 | FirstType = SrcType; |
| 9600 | SecondType = DstType; |
| 9601 | break; |
| 9602 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9603 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9604 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9605 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9606 | if (Complained) |
| 9607 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9608 | return isInvalid; |
| 9609 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9610 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9611 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9612 | llvm::APSInt ICEResult; |
| 9613 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9614 | if (Result) |
| 9615 | *Result = ICEResult; |
| 9616 | return false; |
| 9617 | } |
| 9618 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9619 | Expr::EvalResult EvalResult; |
| 9620 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9621 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9622 | EvalResult.HasSideEffects) { |
| 9623 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9624 | |
| 9625 | if (EvalResult.Diag) { |
| 9626 | // We only show the note if it's not the usual "invalid subexpression" |
| 9627 | // or if it's actually in a subexpression. |
| 9628 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9629 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9630 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9631 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9632 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9633 | return true; |
| 9634 | } |
| 9635 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9636 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9637 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9638 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9639 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9640 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9641 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9642 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9643 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9644 | if (Result) |
| 9645 | *Result = EvalResult.Val.getInt(); |
| 9646 | return false; |
| 9647 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9648 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9649 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9650 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9651 | ExprEvalContexts.push_back( |
| 9652 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9653 | } |
| 9654 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9655 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9656 | Sema::PopExpressionEvaluationContext() { |
| 9657 | // Pop the current expression evaluation context off the stack. |
| 9658 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9659 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9660 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9661 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9662 | if (Rec.PotentiallyReferenced) { |
| 9663 | // Mark any remaining declarations in the current position of the stack |
| 9664 | // as "referenced". If they were not meant to be referenced, semantic |
| 9665 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9666 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9667 | I = Rec.PotentiallyReferenced->begin(), |
| 9668 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9669 | I != IEnd; ++I) |
| 9670 | MarkDeclarationReferenced(I->first, I->second); |
| 9671 | } |
| 9672 | |
| 9673 | if (Rec.PotentiallyDiagnosed) { |
| 9674 | // Emit any pending diagnostics. |
| 9675 | for (PotentiallyEmittedDiagnostics::iterator |
| 9676 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9677 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9678 | I != IEnd; ++I) |
| 9679 | Diag(I->first, I->second); |
| 9680 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9681 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9682 | |
| 9683 | // When are coming out of an unevaluated context, clear out any |
| 9684 | // temporaries that we may have created as part of the evaluation of |
| 9685 | // the expression in that context: they aren't relevant because they |
| 9686 | // will never be constructed. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9687 | if (Rec.Context == Unevaluated && |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9688 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 9689 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9690 | ExprTemporaries.end()); |
| 9691 | |
| 9692 | // Destroy the popped expression evaluation record. |
| 9693 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9694 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9695 | |
| 9696 | /// \brief Note that the given declaration was referenced in the source code. |
| 9697 | /// |
| 9698 | /// This routine should be invoke whenever a given declaration is referenced |
| 9699 | /// in the source code, and where that reference occurred. If this declaration |
| 9700 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9701 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9702 | /// |
| 9703 | /// \param Loc the location where the declaration was referenced. |
| 9704 | /// |
| 9705 | /// \param D the declaration that has been referenced by the source code. |
| 9706 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9707 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9708 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9709 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9710 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9711 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 9712 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 9713 | // template or not. The reason for this is that unevaluated expressions |
| 9714 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 9715 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9716 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9717 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9718 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9719 | return; |
| 9720 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9721 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9722 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9723 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9724 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9725 | // Do not mark anything as "used" within a dependent context; wait for |
| 9726 | // an instantiation. |
| 9727 | if (CurContext->isDependentContext()) |
| 9728 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9729 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9730 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9731 | case Unevaluated: |
| 9732 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9733 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9734 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9735 | case PotentiallyEvaluated: |
| 9736 | // We are in a potentially-evaluated expression, so this declaration is |
| 9737 | // "used"; handle this below. |
| 9738 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9739 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9740 | case PotentiallyPotentiallyEvaluated: |
| 9741 | // We are in an expression that may be potentially evaluated; queue this |
| 9742 | // declaration reference until we know whether the expression is |
| 9743 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9744 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9745 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9746 | |
| 9747 | case PotentiallyEvaluatedIfUsed: |
| 9748 | // Referenced declarations will only be used if the construct in the |
| 9749 | // containing expression is used. |
| 9750 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9751 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9752 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9753 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9754 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9755 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 9756 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9757 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 9758 | return; |
| 9759 | if (!Constructor->isUsed(false)) |
| 9760 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9761 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 9762 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9763 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9764 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 9765 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9766 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9767 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9768 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9769 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9770 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9771 | if (Destructor->isVirtual()) |
| 9772 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9773 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 9774 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 9775 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9776 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9777 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9778 | } else if (MethodDecl->isVirtual()) |
| 9779 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9780 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9781 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9782 | // Recursive functions should be marked when used from another function. |
| 9783 | if (CurContext == Function) return; |
| 9784 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9785 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9786 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9787 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9788 | bool AlreadyInstantiated = false; |
| 9789 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9790 | = Function->getTemplateSpecializationInfo()) { |
| 9791 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9792 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9793 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9794 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9795 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9796 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9797 | = Function->getMemberSpecializationInfo()) { |
| 9798 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9799 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9800 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9801 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9802 | AlreadyInstantiated = true; |
| 9803 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9804 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9805 | if (!AlreadyInstantiated) { |
| 9806 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9807 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9808 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9809 | Loc)); |
| 9810 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9811 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9812 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9813 | } else { |
| 9814 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9815 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9816 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9817 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9818 | MarkDeclarationReferenced(Loc, *i); |
| 9819 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9820 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9821 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9822 | // Keep track of used but undefined functions. |
| 9823 | if (!Function->isPure() && !Function->hasBody() && |
| 9824 | Function->getLinkage() != ExternalLinkage) { |
| 9825 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9826 | if (old.isInvalid()) old = Loc; |
| 9827 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9828 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9829 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9830 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9832 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9833 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9834 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9835 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9836 | Var->getInstantiatedFromStaticDataMember()) { |
| 9837 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9838 | assert(MSInfo && "Missing member specialization information?"); |
| 9839 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9840 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9841 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9842 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9843 | } |
| 9844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9845 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9846 | // Keep track of used but undefined variables. We make a hole in |
| 9847 | // the warning for static const data members with in-line |
| 9848 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9849 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9850 | && Var->getLinkage() != ExternalLinkage |
| 9851 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9852 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9853 | if (old.isInvalid()) old = Loc; |
| 9854 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9855 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9856 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9857 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9858 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9859 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9860 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9861 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9862 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9863 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9864 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9865 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9866 | Sema &S; |
| 9867 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9868 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9869 | public: |
| 9870 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9871 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9872 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9873 | |
| 9874 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9875 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9876 | }; |
| 9877 | } |
| 9878 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9879 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9880 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9881 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9882 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9883 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9884 | |
| 9885 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9886 | } |
| 9887 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9888 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9889 | if (ClassTemplateSpecializationDecl *Spec |
| 9890 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9891 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9892 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9893 | } |
| 9894 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9895 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
| 9898 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9899 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9900 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9901 | } |
| 9902 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9903 | namespace { |
| 9904 | /// \brief Helper class that marks all of the declarations referenced by |
| 9905 | /// potentially-evaluated subexpressions as "referenced". |
| 9906 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9907 | Sema &S; |
| 9908 | |
| 9909 | public: |
| 9910 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9911 | |
| 9912 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9913 | |
| 9914 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9915 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9916 | } |
| 9917 | |
| 9918 | void VisitMemberExpr(MemberExpr *E) { |
| 9919 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9920 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9921 | } |
| 9922 | |
| 9923 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9924 | if (E->getConstructor()) |
| 9925 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9926 | if (E->getOperatorNew()) |
| 9927 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9928 | if (E->getOperatorDelete()) |
| 9929 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9930 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9931 | } |
| 9932 | |
| 9933 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9934 | if (E->getOperatorDelete()) |
| 9935 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9936 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9937 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9938 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9939 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9940 | S.LookupDestructor(Record)); |
| 9941 | } |
| 9942 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9943 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9944 | } |
| 9945 | |
| 9946 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9947 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9948 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9949 | } |
| 9950 | |
| 9951 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9952 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9953 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9954 | |
| 9955 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9956 | Visit(E->getExpr()); |
| 9957 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9958 | }; |
| 9959 | } |
| 9960 | |
| 9961 | /// \brief Mark any declarations that appear within this expression or any |
| 9962 | /// potentially-evaluated subexpressions as "referenced". |
| 9963 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9964 | EvaluatedExprMarker(*this).Visit(E); |
| 9965 | } |
| 9966 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9967 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9968 | /// of the program being compiled. |
| 9969 | /// |
| 9970 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9971 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9972 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9973 | /// expressions, code used only during overload resolution, etc., are not |
| 9974 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9975 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9976 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9977 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9978 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9979 | /// This routine should be used for all diagnostics that describe the run-time |
| 9980 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9981 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9982 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9983 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9984 | const PartialDiagnostic &PD) { |
| 9985 | switch (ExprEvalContexts.back().Context ) { |
| 9986 | case Unevaluated: |
| 9987 | // The argument will never be evaluated, so don't complain. |
| 9988 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9989 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9990 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9991 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9992 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9993 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9994 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9995 | } |
| 9996 | else |
| 9997 | Diag(Loc, PD); |
| 9998 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9999 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10000 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10001 | case PotentiallyPotentiallyEvaluated: |
| 10002 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10003 | break; |
| 10004 | } |
| 10005 | |
| 10006 | return false; |
| 10007 | } |
| 10008 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10009 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10010 | CallExpr *CE, FunctionDecl *FD) { |
| 10011 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10012 | return false; |
| 10013 | |
| 10014 | PartialDiagnostic Note = |
| 10015 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10016 | << FD->getDeclName() : PDiag(); |
| 10017 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10018 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10019 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10020 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10021 | PDiag(diag::err_call_function_incomplete_return) |
| 10022 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10023 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10024 | << CE->getSourceRange(), |
| 10025 | std::make_pair(NoteLoc, Note))) |
| 10026 | return true; |
| 10027 | |
| 10028 | return false; |
| 10029 | } |
| 10030 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10031 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10032 | // will prevent this condition from triggering, which is what we want. |
| 10033 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10034 | SourceLocation Loc; |
| 10035 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10036 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10037 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10038 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10039 | if (isa<BinaryOperator>(E)) { |
| 10040 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10041 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10042 | return; |
| 10043 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10044 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10045 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10046 | // Greylist some idioms by putting them into a warning subcategory. |
| 10047 | if (ObjCMessageExpr *ME |
| 10048 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10049 | Selector Sel = ME->getSelector(); |
| 10050 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10051 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10052 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10053 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10054 | |
| 10055 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10056 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10057 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10058 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10059 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10060 | Loc = Op->getOperatorLoc(); |
| 10061 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10062 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10063 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10064 | return; |
| 10065 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10066 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10067 | Loc = Op->getOperatorLoc(); |
| 10068 | } else { |
| 10069 | // Not an assignment. |
| 10070 | return; |
| 10071 | } |
| 10072 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10073 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10074 | |
| 10075 | if (IsOrAssign) |
| 10076 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10077 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10078 | else |
| 10079 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10080 | << FixItHint::CreateReplacement(Loc, "=="); |
| 10081 | |
Fariborz Jahanian | 81ab3cf | 2011-04-13 20:31:26 +0000 | [diff] [blame] | 10082 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10083 | SourceLocation Close = E->getSourceRange().getEnd(); |
Fariborz Jahanian | 6027461 | 2011-04-13 22:18:37 +0000 | [diff] [blame] | 10084 | SourceLocation LocForEndOfToken = |
| 10085 | Close.isMacroID() ? Close : PP.getLocForEndOfToken(Close); |
| 10086 | Diag(Loc, diag::note_condition_assign_silence) |
| 10087 | << FixItHint::CreateInsertion(Open, "(") |
| 10088 | << FixItHint::CreateInsertion(LocForEndOfToken, ")"); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10089 | } |
| 10090 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10091 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10092 | /// that the user intended an assignment used as condition. |
| 10093 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10094 | // Don't warn if the parens came from a macro. |
| 10095 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10096 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10097 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10098 | // Don't warn for dependent expressions. |
| 10099 | if (parenE->isTypeDependent()) |
| 10100 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10101 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10102 | Expr *E = parenE->IgnoreParens(); |
| 10103 | |
| 10104 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10105 | if (opE->getOpcode() == BO_EQ && |
| 10106 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10107 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10108 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10109 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10110 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
| 10111 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10112 | << FixItHint::CreateReplacement(Loc, "="); |
| 10113 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10114 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10115 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10116 | } |
| 10117 | } |
| 10118 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10119 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10120 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10121 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10122 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10123 | |
| 10124 | if (!E->isTypeDependent()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10125 | if (E->isBoundMemberFunction(Context)) { |
| 10126 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10127 | << E->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10128 | return ExprError(); |
| 10129 | } |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10130 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10131 | if (getLangOptions().CPlusPlus) |
| 10132 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10133 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10134 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10135 | if (ERes.isInvalid()) |
| 10136 | return ExprError(); |
| 10137 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10138 | |
| 10139 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10140 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10141 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10142 | << T << E->getSourceRange(); |
| 10143 | return ExprError(); |
| 10144 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10145 | } |
| 10146 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10147 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10148 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10149 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10150 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10151 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10152 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10153 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10154 | |
| 10155 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10156 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10157 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10158 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10159 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10160 | /// to have an appropriate type. |
| 10161 | struct RebuildUnknownAnyFunction |
| 10162 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10163 | |
| 10164 | Sema &S; |
| 10165 | |
| 10166 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10167 | |
| 10168 | ExprResult VisitStmt(Stmt *S) { |
| 10169 | llvm_unreachable("unexpected statement!"); |
| 10170 | return ExprError(); |
| 10171 | } |
| 10172 | |
| 10173 | ExprResult VisitExpr(Expr *expr) { |
| 10174 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10175 | << expr->getSourceRange(); |
| 10176 | return ExprError(); |
| 10177 | } |
| 10178 | |
| 10179 | /// Rebuild an expression which simply semantically wraps another |
| 10180 | /// expression which it shares the type and value kind of. |
| 10181 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10182 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10183 | if (subResult.isInvalid()) return ExprError(); |
| 10184 | |
| 10185 | Expr *subExpr = subResult.take(); |
| 10186 | expr->setSubExpr(subExpr); |
| 10187 | expr->setType(subExpr->getType()); |
| 10188 | expr->setValueKind(subExpr->getValueKind()); |
| 10189 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10190 | return expr; |
| 10191 | } |
| 10192 | |
| 10193 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10194 | return rebuildSugarExpr(paren); |
| 10195 | } |
| 10196 | |
| 10197 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10198 | return rebuildSugarExpr(op); |
| 10199 | } |
| 10200 | |
| 10201 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10202 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10203 | if (subResult.isInvalid()) return ExprError(); |
| 10204 | |
| 10205 | Expr *subExpr = subResult.take(); |
| 10206 | op->setSubExpr(subExpr); |
| 10207 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10208 | assert(op->getValueKind() == VK_RValue); |
| 10209 | assert(op->getObjectKind() == OK_Ordinary); |
| 10210 | return op; |
| 10211 | } |
| 10212 | |
| 10213 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10214 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10215 | |
| 10216 | expr->setType(decl->getType()); |
| 10217 | |
| 10218 | assert(expr->getValueKind() == VK_RValue); |
| 10219 | if (S.getLangOptions().CPlusPlus && |
| 10220 | !(isa<CXXMethodDecl>(decl) && |
| 10221 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10222 | expr->setValueKind(VK_LValue); |
| 10223 | |
| 10224 | return expr; |
| 10225 | } |
| 10226 | |
| 10227 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10228 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10229 | } |
| 10230 | |
| 10231 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10232 | return resolveDecl(ref, ref->getDecl()); |
| 10233 | } |
| 10234 | }; |
| 10235 | } |
| 10236 | |
| 10237 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10238 | /// to have a function type. |
| 10239 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10240 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10241 | if (result.isInvalid()) return ExprError(); |
| 10242 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10243 | } |
| 10244 | |
| 10245 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10246 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10247 | /// into one which resolves the type directly on the referring |
| 10248 | /// expression. Strict preservation of the original source |
| 10249 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10250 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10251 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10252 | |
| 10253 | Sema &S; |
| 10254 | |
| 10255 | /// The current destination type. |
| 10256 | QualType DestType; |
| 10257 | |
| 10258 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10259 | : S(S), DestType(castType) {} |
| 10260 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10261 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10262 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10263 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10264 | } |
| 10265 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10266 | ExprResult VisitExpr(Expr *expr) { |
| 10267 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10268 | << expr->getSourceRange(); |
| 10269 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10270 | } |
| 10271 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10272 | ExprResult VisitCallExpr(CallExpr *call); |
| 10273 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10274 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10275 | /// Rebuild an expression which simply semantically wraps another |
| 10276 | /// expression which it shares the type and value kind of. |
| 10277 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10278 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10279 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10280 | Expr *subExpr = subResult.take(); |
| 10281 | expr->setSubExpr(subExpr); |
| 10282 | expr->setType(subExpr->getType()); |
| 10283 | expr->setValueKind(subExpr->getValueKind()); |
| 10284 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10285 | return expr; |
| 10286 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10287 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10288 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10289 | return rebuildSugarExpr(paren); |
| 10290 | } |
| 10291 | |
| 10292 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10293 | return rebuildSugarExpr(op); |
| 10294 | } |
| 10295 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10296 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10297 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10298 | if (!ptr) { |
| 10299 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10300 | << op->getSourceRange(); |
| 10301 | return ExprError(); |
| 10302 | } |
| 10303 | assert(op->getValueKind() == VK_RValue); |
| 10304 | assert(op->getObjectKind() == OK_Ordinary); |
| 10305 | op->setType(DestType); |
| 10306 | |
| 10307 | // Build the sub-expression as if it were an object of the pointee type. |
| 10308 | DestType = ptr->getPointeeType(); |
| 10309 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10310 | if (subResult.isInvalid()) return ExprError(); |
| 10311 | op->setSubExpr(subResult.take()); |
| 10312 | return op; |
| 10313 | } |
| 10314 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10315 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10316 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10317 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10318 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10319 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10320 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10321 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10322 | |
| 10323 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10324 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10325 | } |
| 10326 | }; |
| 10327 | } |
| 10328 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10329 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10330 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10331 | Expr *callee = call->getCallee(); |
| 10332 | |
| 10333 | enum FnKind { |
| 10334 | FK_Function, |
| 10335 | FK_FunctionPointer, |
| 10336 | FK_BlockPointer |
| 10337 | }; |
| 10338 | |
| 10339 | FnKind kind; |
| 10340 | QualType type = callee->getType(); |
| 10341 | if (type->isFunctionType()) { |
| 10342 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10343 | kind = FK_Function; |
| 10344 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10345 | type = ptr->getPointeeType(); |
| 10346 | kind = FK_FunctionPointer; |
| 10347 | } else { |
| 10348 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 10349 | kind = FK_BlockPointer; |
| 10350 | } |
| 10351 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 10352 | |
| 10353 | // Verify that this is a legal result type of a function. |
| 10354 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10355 | unsigned diagID = diag::err_func_returning_array_function; |
| 10356 | if (kind == FK_BlockPointer) |
| 10357 | diagID = diag::err_block_returning_array_function; |
| 10358 | |
| 10359 | S.Diag(call->getExprLoc(), diagID) |
| 10360 | << DestType->isFunctionType() << DestType; |
| 10361 | return ExprError(); |
| 10362 | } |
| 10363 | |
| 10364 | // Otherwise, go ahead and set DestType as the call's result. |
| 10365 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 10366 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 10367 | assert(call->getObjectKind() == OK_Ordinary); |
| 10368 | |
| 10369 | // Rebuild the function type, replacing the result type with DestType. |
| 10370 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 10371 | DestType = S.Context.getFunctionType(DestType, |
| 10372 | proto->arg_type_begin(), |
| 10373 | proto->getNumArgs(), |
| 10374 | proto->getExtProtoInfo()); |
| 10375 | else |
| 10376 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 10377 | fnType->getExtInfo()); |
| 10378 | |
| 10379 | // Rebuild the appropriate pointer-to-function type. |
| 10380 | switch (kind) { |
| 10381 | case FK_Function: |
| 10382 | // Nothing to do. |
| 10383 | break; |
| 10384 | |
| 10385 | case FK_FunctionPointer: |
| 10386 | DestType = S.Context.getPointerType(DestType); |
| 10387 | break; |
| 10388 | |
| 10389 | case FK_BlockPointer: |
| 10390 | DestType = S.Context.getBlockPointerType(DestType); |
| 10391 | break; |
| 10392 | } |
| 10393 | |
| 10394 | // Finally, we can recurse. |
| 10395 | ExprResult calleeResult = Visit(callee); |
| 10396 | if (!calleeResult.isUsable()) return ExprError(); |
| 10397 | call->setCallee(calleeResult.take()); |
| 10398 | |
| 10399 | // Bind a temporary if necessary. |
| 10400 | return S.MaybeBindToTemporary(call); |
| 10401 | } |
| 10402 | |
| 10403 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10404 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 10405 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10406 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10407 | // Verify that this is a legal result type of a call. |
| 10408 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10409 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 10410 | << DestType->isFunctionType() << DestType; |
| 10411 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10412 | } |
| 10413 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10414 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 10415 | method->setResultType(DestType); |
| 10416 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10417 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10418 | msg->setType(DestType.getNonReferenceType()); |
| 10419 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10420 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10421 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10422 | } |
| 10423 | |
| 10424 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10425 | // 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] | 10426 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10427 | assert(ice->getValueKind() == VK_RValue); |
| 10428 | assert(ice->getObjectKind() == OK_Ordinary); |
| 10429 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10430 | ice->setType(DestType); |
| 10431 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10432 | // Rebuild the sub-expression as the pointee (function) type. |
| 10433 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 10434 | |
| 10435 | ExprResult result = Visit(ice->getSubExpr()); |
| 10436 | if (!result.isUsable()) return ExprError(); |
| 10437 | |
| 10438 | ice->setSubExpr(result.take()); |
| 10439 | return S.Owned(ice); |
| 10440 | } |
| 10441 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10442 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10443 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10444 | QualType type = DestType; |
| 10445 | |
| 10446 | // We know how to make this work for certain kinds of decls: |
| 10447 | |
| 10448 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10449 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
| 10450 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10451 | if (method->isInstance()) valueKind = VK_RValue; |
| 10452 | |
| 10453 | // This is true because FunctionDecls must always have function |
| 10454 | // type, so we can't be resolving the entire thing at once. |
| 10455 | assert(type->isFunctionType()); |
| 10456 | |
| 10457 | // Function references aren't l-values in C. |
| 10458 | if (!S.getLangOptions().CPlusPlus) |
| 10459 | valueKind = VK_RValue; |
| 10460 | |
| 10461 | // - variables |
| 10462 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10463 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 10464 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10465 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10466 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10467 | << decl << expr->getSourceRange(); |
| 10468 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10469 | } |
| 10470 | |
| 10471 | // - nothing else |
| 10472 | } else { |
| 10473 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10474 | << decl << expr->getSourceRange(); |
| 10475 | return ExprError(); |
| 10476 | } |
| 10477 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10478 | decl->setType(DestType); |
| 10479 | expr->setType(type); |
| 10480 | expr->setValueKind(valueKind); |
| 10481 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10482 | } |
| 10483 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10484 | /// Check a cast of an unknown-any type. We intentionally only |
| 10485 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10486 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 10487 | Expr *castExpr, CastKind &castKind, |
| 10488 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10489 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10490 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 10491 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10492 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10493 | castExpr = result.take(); |
| 10494 | VK = castExpr->getValueKind(); |
| 10495 | castKind = CK_NoOp; |
| 10496 | |
| 10497 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10498 | } |
| 10499 | |
| 10500 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 10501 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10502 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10503 | while (true) { |
| 10504 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10505 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10506 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10507 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10508 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10509 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10510 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10511 | } |
| 10512 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10513 | SourceLocation loc; |
| 10514 | NamedDecl *d; |
| 10515 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 10516 | loc = ref->getLocation(); |
| 10517 | d = ref->getDecl(); |
| 10518 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 10519 | loc = mem->getMemberLoc(); |
| 10520 | d = mem->getMemberDecl(); |
| 10521 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 10522 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10523 | loc = msg->getSelectorLoc(); |
| 10524 | d = msg->getMethodDecl(); |
| 10525 | assert(d && "unknown method returning __unknown_any?"); |
| 10526 | } else { |
| 10527 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10528 | << e->getSourceRange(); |
| 10529 | return ExprError(); |
| 10530 | } |
| 10531 | |
| 10532 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10533 | |
| 10534 | // Never recoverable. |
| 10535 | return ExprError(); |
| 10536 | } |
| 10537 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10538 | /// Check for operands with placeholder types and complain if found. |
| 10539 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10540 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10541 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10542 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10543 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10544 | // Overloaded expressions. |
| 10545 | if (type == Context.OverloadTy) |
| 10546 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10547 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10548 | QualType(), |
| 10549 | diag::err_ovl_unresolvable); |
| 10550 | |
| 10551 | // Expressions of unknown type. |
| 10552 | if (type == Context.UnknownAnyTy) |
| 10553 | return diagnoseUnknownAnyExpr(*this, E); |
| 10554 | |
| 10555 | assert(!type->isPlaceholderType()); |
| 10556 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10557 | } |