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). |
| 259 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 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()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | ImpCastExprToType(E, Context.getPointerType(Ty), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 265 | CK_FunctionToPointerDecay); |
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()) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 279 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 280 | CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 281 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 282 | } |
| 283 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 284 | void Sema::DefaultLvalueConversion(Expr *&E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 285 | // C++ [conv.lval]p1: |
| 286 | // A glvalue of a non-function, non-array type T can be |
| 287 | // converted to a prvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 288 | if (!E->isGLValue()) return; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 289 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 290 | QualType T = E->getType(); |
| 291 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 292 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 293 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 294 | if (E->getObjectKind() == OK_ObjCProperty) { |
| 295 | ConvertPropertyForRValue(E); |
| 296 | if (!E->isGLValue()) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 297 | return; |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 298 | } |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 299 | |
| 300 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 301 | // expressions of certain types in C++. |
| 302 | if (getLangOptions().CPlusPlus && |
| 303 | (E->getType() == Context.OverloadTy || |
| 304 | T->isDependentType() || |
| 305 | T->isRecordType())) |
| 306 | return; |
| 307 | |
| 308 | // The C standard is actually really unclear on this point, and |
| 309 | // DR106 tells us what the result should be but not why. It's |
| 310 | // generally best to say that void types just doesn't undergo |
| 311 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 312 | // 'void' type are never l-values, but qualified void can be. |
| 313 | if (T->isVoidType()) |
| 314 | return; |
| 315 | |
| 316 | // C++ [conv.lval]p1: |
| 317 | // [...] If T is a non-class type, the type of the prvalue is the |
| 318 | // cv-unqualified version of T. Otherwise, the type of the |
| 319 | // rvalue is T. |
| 320 | // |
| 321 | // C99 6.3.2.1p2: |
| 322 | // If the lvalue has qualified type, the value has the unqualified |
| 323 | // version of the type of the lvalue; otherwise, the value has the |
| 324 | // type of the lvalue. |
| 325 | if (T.hasQualifiers()) |
| 326 | T = T.getUnqualifiedType(); |
| 327 | |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 328 | CheckArrayAccess(E); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 329 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 330 | E = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 331 | E, 0, VK_RValue); |
| 332 | } |
| 333 | |
| 334 | void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) { |
| 335 | DefaultFunctionArrayConversion(E); |
| 336 | DefaultLvalueConversion(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 340 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 341 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 342 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 343 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 344 | /// In these instances, this routine should *not* be called. |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 345 | Expr *Sema::UsualUnaryConversions(Expr *&E) { |
| 346 | // First, convert to an r-value. |
| 347 | DefaultFunctionArrayLvalueConversion(E); |
| 348 | |
| 349 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 350 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 351 | |
| 352 | // Try to perform integral promotions if the object has a theoretically |
| 353 | // promotable type. |
| 354 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 355 | // C99 6.3.1.1p2: |
| 356 | // |
| 357 | // The following may be used in an expression wherever an int or |
| 358 | // unsigned int may be used: |
| 359 | // - an object or expression with an integer type whose integer |
| 360 | // conversion rank is less than or equal to the rank of int |
| 361 | // and unsigned int. |
| 362 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 363 | // |
| 364 | // If an int can represent all values of the original type, the |
| 365 | // value is converted to an int; otherwise, it is converted to an |
| 366 | // unsigned int. These are called the integer promotions. All |
| 367 | // other types are unchanged by the integer promotions. |
| 368 | |
| 369 | QualType PTy = Context.isPromotableBitField(E); |
| 370 | if (!PTy.isNull()) { |
| 371 | ImpCastExprToType(E, PTy, CK_IntegralCast); |
| 372 | return E; |
| 373 | } |
| 374 | if (Ty->isPromotableIntegerType()) { |
| 375 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 376 | ImpCastExprToType(E, PT, CK_IntegralCast); |
| 377 | return E; |
| 378 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 379 | } |
| 380 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 381 | return E; |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 384 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | /// 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] | 386 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 387 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 388 | QualType Ty = Expr->getType(); |
| 389 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 391 | UsualUnaryConversions(Expr); |
| 392 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 393 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 394 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 395 | return ImpCastExprToType(Expr, Context.DoubleTy, CK_FloatingCast); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 398 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 399 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 400 | /// interfaces passed by value. This returns true if the argument type is |
| 401 | /// completely illegal. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 402 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT, |
| 403 | FunctionDecl *FDecl) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 404 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 406 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 407 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 408 | // etc. |
| 409 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
| 410 | return false; |
| 411 | |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 412 | if (Expr->getType()->isObjCObjectType() && |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 413 | DiagRuntimeBehavior(Expr->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 414 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 415 | << Expr->getType() << CT)) |
| 416 | return true; |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 418 | if (!Expr->getType()->isPODType() && |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 419 | DiagRuntimeBehavior(Expr->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 420 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 421 | << Expr->getType() << CT)) |
| 422 | return true; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 423 | |
| 424 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 427 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 428 | /// 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] | 429 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 430 | /// responsible for emitting appropriate error diagnostics. |
| 431 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 432 | /// GCC. |
| 433 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 434 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 435 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 436 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 437 | |
| 438 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 439 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 441 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 442 | QualType lhs = |
| 443 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 445 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 446 | |
| 447 | // If both types are identical, no conversion is needed. |
| 448 | if (lhs == rhs) |
| 449 | return lhs; |
| 450 | |
| 451 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 452 | // The caller can deal with this (e.g. pointer + int). |
| 453 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 454 | return lhs; |
| 455 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 456 | // Apply unary and bitfield promotions to the LHS's type. |
| 457 | QualType lhs_unpromoted = lhs; |
| 458 | if (lhs->isPromotableIntegerType()) |
| 459 | lhs = Context.getPromotedIntegerType(lhs); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 460 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 461 | if (!LHSBitfieldPromoteTy.isNull()) |
| 462 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 463 | if (lhs != lhs_unpromoted && !isCompAssign) |
| 464 | ImpCastExprToType(lhsExpr, lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 465 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 466 | // If both types are identical, no conversion is needed. |
| 467 | if (lhs == rhs) |
| 468 | return lhs; |
| 469 | |
| 470 | // At this point, we have two different arithmetic types. |
| 471 | |
| 472 | // Handle complex types first (C99 6.3.1.8p1). |
| 473 | bool LHSComplexFloat = lhs->isComplexType(); |
| 474 | bool RHSComplexFloat = rhs->isComplexType(); |
| 475 | if (LHSComplexFloat || RHSComplexFloat) { |
| 476 | // if we have an integer operand, the result is the complex type. |
| 477 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 478 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 479 | if (rhs->isIntegerType()) { |
| 480 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 481 | ImpCastExprToType(rhsExpr, fp, CK_IntegralToFloating); |
| 482 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
| 483 | } else { |
| 484 | assert(rhs->isComplexIntegerType()); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 485 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 486 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 487 | return lhs; |
| 488 | } |
| 489 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 490 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 491 | if (!isCompAssign) { |
| 492 | // int -> float -> _Complex float |
| 493 | if (lhs->isIntegerType()) { |
| 494 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 495 | ImpCastExprToType(lhsExpr, fp, CK_IntegralToFloating); |
| 496 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 497 | } else { |
| 498 | assert(lhs->isComplexIntegerType()); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 499 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 502 | return rhs; |
| 503 | } |
| 504 | |
| 505 | // This handles complex/complex, complex/float, or float/complex. |
| 506 | // When both operands are complex, the shorter operand is converted to the |
| 507 | // type of the longer, and that is the type of the result. This corresponds |
| 508 | // to what is done when combining two real floating-point operands. |
| 509 | // The fun begins when size promotion occur across type domains. |
| 510 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 511 | // floating-point type, the less precise type is converted, within it's |
| 512 | // real or complex domain, to the precision of the other type. For example, |
| 513 | // when combining a "long double" with a "double _Complex", the |
| 514 | // "double _Complex" is promoted to "long double _Complex". |
| 515 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 516 | |
| 517 | // If both are complex, just cast to the more precise type. |
| 518 | if (LHSComplexFloat && RHSComplexFloat) { |
| 519 | if (order > 0) { |
| 520 | // _Complex float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 521 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 522 | return lhs; |
| 523 | |
| 524 | } else if (order < 0) { |
| 525 | // _Complex float -> _Complex double |
| 526 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 527 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 528 | return rhs; |
| 529 | } |
| 530 | return lhs; |
| 531 | } |
| 532 | |
| 533 | // If just the LHS is complex, the RHS needs to be converted, |
| 534 | // and the LHS might need to be promoted. |
| 535 | if (LHSComplexFloat) { |
| 536 | if (order > 0) { // LHS is wider |
| 537 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 538 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 539 | ImpCastExprToType(rhsExpr, fp, CK_FloatingCast); |
| 540 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 541 | return lhs; |
| 542 | } |
| 543 | |
| 544 | // RHS is at least as wide. Find its corresponding complex type. |
| 545 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 546 | |
| 547 | // double -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 548 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 549 | |
| 550 | // _Complex float -> _Complex double |
| 551 | if (!isCompAssign && order < 0) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 552 | ImpCastExprToType(lhsExpr, result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 553 | |
| 554 | return result; |
| 555 | } |
| 556 | |
| 557 | // Just the RHS is complex, so the LHS needs to be converted |
| 558 | // and the RHS might need to be promoted. |
| 559 | assert(RHSComplexFloat); |
| 560 | |
| 561 | if (order < 0) { // RHS is wider |
| 562 | // float -> _Complex double |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 563 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 564 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 565 | ImpCastExprToType(lhsExpr, fp, CK_FloatingCast); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 566 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 567 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 568 | return rhs; |
| 569 | } |
| 570 | |
| 571 | // LHS is at least as wide. Find its corresponding complex type. |
| 572 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 573 | |
| 574 | // double -> _Complex double |
| 575 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 576 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 577 | |
| 578 | // _Complex float -> _Complex double |
| 579 | if (order > 0) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 580 | ImpCastExprToType(rhsExpr, result, CK_FloatingComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 581 | |
| 582 | return result; |
| 583 | } |
| 584 | |
| 585 | // Now handle "real" floating types (i.e. float, double, long double). |
| 586 | bool LHSFloat = lhs->isRealFloatingType(); |
| 587 | bool RHSFloat = rhs->isRealFloatingType(); |
| 588 | if (LHSFloat || RHSFloat) { |
| 589 | // If we have two real floating types, convert the smaller operand |
| 590 | // to the bigger result. |
| 591 | if (LHSFloat && RHSFloat) { |
| 592 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 593 | if (order > 0) { |
| 594 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingCast); |
| 595 | return lhs; |
| 596 | } |
| 597 | |
| 598 | assert(order < 0 && "illegal float comparison"); |
| 599 | if (!isCompAssign) |
| 600 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingCast); |
| 601 | return rhs; |
| 602 | } |
| 603 | |
| 604 | // If we have an integer operand, the result is the real floating type. |
| 605 | if (LHSFloat) { |
| 606 | if (rhs->isIntegerType()) { |
| 607 | // Convert rhs to the lhs floating point type. |
| 608 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralToFloating); |
| 609 | return lhs; |
| 610 | } |
| 611 | |
| 612 | // Convert both sides to the appropriate complex float. |
| 613 | assert(rhs->isComplexIntegerType()); |
| 614 | QualType result = Context.getComplexType(lhs); |
| 615 | |
| 616 | // _Complex int -> _Complex float |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 617 | ImpCastExprToType(rhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 618 | |
| 619 | // float -> _Complex float |
| 620 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 621 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 622 | |
| 623 | return result; |
| 624 | } |
| 625 | |
| 626 | assert(RHSFloat); |
| 627 | if (lhs->isIntegerType()) { |
| 628 | // Convert lhs to the rhs floating point type. |
| 629 | if (!isCompAssign) |
| 630 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralToFloating); |
| 631 | return rhs; |
| 632 | } |
| 633 | |
| 634 | // Convert both sides to the appropriate complex float. |
| 635 | assert(lhs->isComplexIntegerType()); |
| 636 | QualType result = Context.getComplexType(rhs); |
| 637 | |
| 638 | // _Complex int -> _Complex float |
| 639 | if (!isCompAssign) |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 640 | ImpCastExprToType(lhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 641 | |
| 642 | // float -> _Complex float |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 643 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 644 | |
| 645 | return result; |
| 646 | } |
| 647 | |
| 648 | // Handle GCC complex int extension. |
| 649 | // FIXME: if the operands are (int, _Complex long), we currently |
| 650 | // don't promote the complex. Also, signedness? |
| 651 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 652 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 653 | if (lhsComplexInt && rhsComplexInt) { |
| 654 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 655 | rhsComplexInt->getElementType()); |
| 656 | assert(order && "inequal types with equal element ordering"); |
| 657 | if (order > 0) { |
| 658 | // _Complex int -> _Complex long |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 659 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 660 | return lhs; |
| 661 | } |
| 662 | |
| 663 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 664 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 665 | return rhs; |
| 666 | } else if (lhsComplexInt) { |
| 667 | // int -> _Complex int |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 668 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 669 | return lhs; |
| 670 | } else if (rhsComplexInt) { |
| 671 | // int -> _Complex int |
| 672 | if (!isCompAssign) |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 673 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 674 | return rhs; |
| 675 | } |
| 676 | |
| 677 | // Finally, we have two differing integer types. |
| 678 | // The rules for this case are in C99 6.3.1.8 |
| 679 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 680 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 681 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 682 | if (lhsSigned == rhsSigned) { |
| 683 | // Same signedness; use the higher-ranked type |
| 684 | if (compare >= 0) { |
| 685 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 686 | return lhs; |
| 687 | } else if (!isCompAssign) |
| 688 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 689 | return rhs; |
| 690 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 691 | // The unsigned type has greater than or equal rank to the |
| 692 | // signed type, so use the unsigned type |
| 693 | if (rhsSigned) { |
| 694 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 695 | return lhs; |
| 696 | } else if (!isCompAssign) |
| 697 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 698 | return rhs; |
| 699 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 700 | // The two types are different widths; if we are here, that |
| 701 | // means the signed type is larger than the unsigned type, so |
| 702 | // use the signed type. |
| 703 | if (lhsSigned) { |
| 704 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 705 | return lhs; |
| 706 | } else if (!isCompAssign) |
| 707 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 708 | return rhs; |
| 709 | } else { |
| 710 | // The signed type is higher-ranked than the unsigned type, |
| 711 | // but isn't actually any bigger (like unsigned int and long |
| 712 | // on most 32-bit systems). Use the unsigned type corresponding |
| 713 | // to the signed type. |
| 714 | QualType result = |
| 715 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 716 | ImpCastExprToType(rhsExpr, result, CK_IntegralCast); |
| 717 | if (!isCompAssign) |
| 718 | ImpCastExprToType(lhsExpr, result, CK_IntegralCast); |
| 719 | return result; |
| 720 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 723 | //===----------------------------------------------------------------------===// |
| 724 | // Semantic Analysis for various Expression Types |
| 725 | //===----------------------------------------------------------------------===// |
| 726 | |
| 727 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 728 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 730 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 731 | /// multiple tokens. However, the common case is that StringToks points to one |
| 732 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 733 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 734 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 735 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 736 | assert(NumStringToks && "Must have at least one string!"); |
| 737 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 738 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 739 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 740 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 741 | |
| 742 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 743 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 744 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 745 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 746 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 747 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 748 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 749 | |
| 750 | // 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] | 751 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 752 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 753 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 754 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 755 | // the nul terminator character as well as the string length for pascal |
| 756 | // strings. |
| 757 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 758 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 759 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 761 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 762 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 763 | Literal.GetStringLength(), |
| 764 | Literal.AnyWide, StrTy, |
| 765 | &StringTokLocs[0], |
| 766 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | } |
| 768 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 769 | enum CaptureResult { |
| 770 | /// No capture is required. |
| 771 | CR_NoCapture, |
| 772 | |
| 773 | /// A capture is required. |
| 774 | CR_Capture, |
| 775 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 776 | /// A by-ref capture is required. |
| 777 | CR_CaptureByRef, |
| 778 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 779 | /// An error occurred when trying to capture the given variable. |
| 780 | CR_Error |
| 781 | }; |
| 782 | |
| 783 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 784 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 785 | /// \param var - the variable referenced |
| 786 | /// \param DC - the context which we couldn't capture through |
| 787 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 788 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 789 | VarDecl *var, DeclContext *DC) { |
| 790 | switch (S.ExprEvalContexts.back().Context) { |
| 791 | case Sema::Unevaluated: |
| 792 | // The argument will never be evaluated, so don't complain. |
| 793 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 794 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 795 | case Sema::PotentiallyEvaluated: |
| 796 | case Sema::PotentiallyEvaluatedIfUsed: |
| 797 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 798 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 799 | case Sema::PotentiallyPotentiallyEvaluated: |
| 800 | // FIXME: delay these! |
| 801 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 803 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 804 | // Don't diagnose about capture if we're not actually in code right |
| 805 | // now; in general, there are more appropriate places that will |
| 806 | // diagnose this. |
| 807 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 808 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 809 | // Certain madnesses can happen with parameter declarations, which |
| 810 | // we want to ignore. |
| 811 | if (isa<ParmVarDecl>(var)) { |
| 812 | // - If the parameter still belongs to the translation unit, then |
| 813 | // we're actually just using one parameter in the declaration of |
| 814 | // the next. This is useful in e.g. VLAs. |
| 815 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 816 | return CR_NoCapture; |
| 817 | |
| 818 | // - This particular madness can happen in ill-formed default |
| 819 | // arguments; claim it's okay and let downstream code handle it. |
| 820 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 821 | return CR_NoCapture; |
| 822 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 823 | |
| 824 | DeclarationName functionName; |
| 825 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 826 | functionName = fn->getDeclName(); |
| 827 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 828 | |
| 829 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 830 | << var->getIdentifier() << functionName; |
| 831 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 832 | << var->getIdentifier(); |
| 833 | |
| 834 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | } |
| 836 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 837 | /// There is a well-formed capture at a particular scope level; |
| 838 | /// propagate it through all the nested blocks. |
| 839 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 840 | const BlockDecl::Capture &capture) { |
| 841 | VarDecl *var = capture.getVariable(); |
| 842 | |
| 843 | // Update all the inner blocks with the capture information. |
| 844 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 845 | i != e; ++i) { |
| 846 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 847 | innerBlock->Captures.push_back( |
| 848 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 849 | /*nested*/ true, capture.getCopyExpr())); |
| 850 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 851 | } |
| 852 | |
| 853 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 854 | } |
| 855 | |
| 856 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 857 | /// given value in the current context requires a variable capture. |
| 858 | /// |
| 859 | /// This also keeps the captures set in the BlockScopeInfo records |
| 860 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 861 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 862 | ValueDecl *value) { |
| 863 | // Only variables ever require capture. |
| 864 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 865 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 866 | |
| 867 | // Fast path: variables from the current context never require capture. |
| 868 | DeclContext *DC = S.CurContext; |
| 869 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 870 | |
| 871 | // Only variables with local storage require capture. |
| 872 | // FIXME: What about 'const' variables in C++? |
| 873 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 874 | |
| 875 | // Otherwise, we need to capture. |
| 876 | |
| 877 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 878 | do { |
| 879 | // Only blocks (and eventually C++0x closures) can capture; other |
| 880 | // scopes don't work. |
| 881 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 882 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 883 | |
| 884 | BlockScopeInfo *blockScope = |
| 885 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 886 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 887 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 888 | // Check whether we've already captured it in this block. If so, |
| 889 | // we're done. |
| 890 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 891 | return propagateCapture(S, functionScopesIndex, |
| 892 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 893 | |
| 894 | functionScopesIndex--; |
| 895 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 896 | } while (var->getDeclContext() != DC); |
| 897 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 898 | // Okay, we descended all the way to the block that defines the variable. |
| 899 | // Actually try to capture it. |
| 900 | QualType type = var->getType(); |
| 901 | |
| 902 | // Prohibit variably-modified types. |
| 903 | if (type->isVariablyModifiedType()) { |
| 904 | S.Diag(loc, diag::err_ref_vm_type); |
| 905 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 906 | return CR_Error; |
| 907 | } |
| 908 | |
| 909 | // Prohibit arrays, even in __block variables, but not references to |
| 910 | // them. |
| 911 | if (type->isArrayType()) { |
| 912 | S.Diag(loc, diag::err_ref_array_type); |
| 913 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 914 | return CR_Error; |
| 915 | } |
| 916 | |
| 917 | S.MarkDeclarationReferenced(loc, var); |
| 918 | |
| 919 | // The BlocksAttr indicates the variable is bound by-reference. |
| 920 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 921 | |
| 922 | // Build a copy expression. |
| 923 | Expr *copyExpr = 0; |
| 924 | if (!byRef && S.getLangOptions().CPlusPlus && |
| 925 | !type->isDependentType() && type->isStructureOrClassType()) { |
| 926 | // According to the blocks spec, the capture of a variable from |
| 927 | // the stack requires a const copy constructor. This is not true |
| 928 | // of the copy/move done to move a __block variable to the heap. |
| 929 | type.addConst(); |
| 930 | |
| 931 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 932 | ExprResult result = |
| 933 | S.PerformCopyInitialization( |
| 934 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 935 | type, false), |
| 936 | loc, S.Owned(declRef)); |
| 937 | |
| 938 | // Build a full-expression copy expression if initialization |
| 939 | // succeeded and used a non-trivial constructor. Recover from |
| 940 | // errors by pretending that the copy isn't necessary. |
| 941 | if (!result.isInvalid() && |
| 942 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 943 | result = S.MaybeCreateExprWithCleanups(result); |
| 944 | copyExpr = result.take(); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | // We're currently at the declarer; go back to the closure. |
| 949 | functionScopesIndex++; |
| 950 | BlockScopeInfo *blockScope = |
| 951 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 952 | |
| 953 | // Build a valid capture in this scope. |
| 954 | blockScope->Captures.push_back( |
| 955 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 956 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 957 | |
| 958 | // Propagate that to inner captures if necessary. |
| 959 | return propagateCapture(S, functionScopesIndex, |
| 960 | blockScope->Captures.back()); |
| 961 | } |
| 962 | |
| 963 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 964 | const DeclarationNameInfo &NameInfo, |
| 965 | bool byRef) { |
| 966 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 967 | |
| 968 | VarDecl *var = cast<VarDecl>(vd); |
| 969 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 970 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 971 | |
| 972 | QualType exprType = var->getType().getNonReferenceType(); |
| 973 | |
| 974 | BlockDeclRefExpr *BDRE; |
| 975 | if (!byRef) { |
| 976 | // The variable will be bound by copy; make it const within the |
| 977 | // closure, but record that this was done in the expression. |
| 978 | bool constAdded = !exprType.isConstQualified(); |
| 979 | exprType.addConst(); |
| 980 | |
| 981 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 982 | NameInfo.getLoc(), false, |
| 983 | constAdded); |
| 984 | } else { |
| 985 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 986 | NameInfo.getLoc(), true); |
| 987 | } |
| 988 | |
| 989 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 990 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 991 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 992 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 993 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 994 | SourceLocation Loc, |
| 995 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 996 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 997 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 998 | } |
| 999 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1000 | /// BuildDeclRefExpr - Build an expression that references a |
| 1001 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1002 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1003 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1004 | const DeclarationNameInfo &NameInfo, |
| 1005 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1006 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1007 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1008 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1009 | SS? SS->getWithLocInContext(Context) |
| 1010 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1011 | D, NameInfo, Ty, VK); |
| 1012 | |
| 1013 | // Just in case we're building an illegal pointer-to-member. |
| 1014 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1015 | E->setObjectKind(OK_BitField); |
| 1016 | |
| 1017 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1020 | static ExprResult |
| 1021 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1022 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1023 | DeclAccessPair FoundDecl, |
| 1024 | const DeclarationNameInfo &MemberNameInfo); |
| 1025 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1026 | ExprResult |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1027 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1028 | SourceLocation loc, |
| 1029 | IndirectFieldDecl *indirectField, |
| 1030 | Expr *baseObjectExpr, |
| 1031 | SourceLocation opLoc) { |
| 1032 | // First, build the expression that refers to the base object. |
| 1033 | |
| 1034 | bool baseObjectIsPointer = false; |
| 1035 | Qualifiers baseQuals; |
| 1036 | |
| 1037 | // Case 1: the base of the indirect field is not a field. |
| 1038 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1039 | CXXScopeSpec EmptySS; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1040 | if (baseVariable) { |
| 1041 | assert(baseVariable->getType()->isRecordType()); |
| 1042 | |
| 1043 | // In principle we could have a member access expression that |
| 1044 | // accesses an anonymous struct/union that's a static member of |
| 1045 | // the base object's class. However, under the current standard, |
| 1046 | // static data members cannot be anonymous structs or unions. |
| 1047 | // Supporting this is as easy as building a MemberExpr here. |
| 1048 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1049 | |
| 1050 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1051 | |
| 1052 | ExprResult result = |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1053 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1054 | if (result.isInvalid()) return ExprError(); |
| 1055 | |
| 1056 | baseObjectExpr = result.take(); |
| 1057 | baseObjectIsPointer = false; |
| 1058 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1059 | |
| 1060 | // Case 2: the base of the indirect field is a field and the user |
| 1061 | // wrote a member expression. |
| 1062 | } else if (baseObjectExpr) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1063 | // The caller provided the base object expression. Determine |
| 1064 | // whether its a pointer and whether it adds any qualifiers to the |
| 1065 | // anonymous struct/union fields we're looking into. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1066 | QualType objectType = baseObjectExpr->getType(); |
| 1067 | |
| 1068 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1069 | baseObjectIsPointer = true; |
| 1070 | objectType = ptr->getPointeeType(); |
| 1071 | } else { |
| 1072 | baseObjectIsPointer = false; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1073 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1074 | baseQuals = objectType.getQualifiers(); |
| 1075 | |
| 1076 | // Case 3: the base of the indirect field is a field and we should |
| 1077 | // build an implicit member access. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1078 | } else { |
| 1079 | // We've found a member of an anonymous struct/union that is |
| 1080 | // inside a non-anonymous struct/union, so in a well-formed |
| 1081 | // program our base object expression is "this". |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1082 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 1083 | if (!method) { |
| 1084 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1085 | << indirectField->getDeclName(); |
| 1086 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1089 | // Our base object expression is "this". |
| 1090 | baseObjectExpr = |
| 1091 | new (Context) CXXThisExpr(loc, method->getThisType(Context), |
| 1092 | /*isImplicit=*/ true); |
| 1093 | baseObjectIsPointer = true; |
| 1094 | baseQuals = Qualifiers::fromCVRMask(method->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | // Build the implicit member references to the field of the |
| 1098 | // anonymous struct/union. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1099 | Expr *result = baseObjectExpr; |
| 1100 | IndirectFieldDecl::chain_iterator |
| 1101 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1102 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1103 | // Build the first member access in the chain with full information. |
| 1104 | if (!baseVariable) { |
| 1105 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1106 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1107 | // FIXME: use the real found-decl info! |
| 1108 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1109 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1110 | // Make a nameInfo that properly uses the anonymous name. |
| 1111 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1112 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1113 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1114 | EmptySS, field, foundDecl, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1115 | memberNameInfo).take(); |
| 1116 | baseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1117 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1118 | // FIXME: check qualified member access |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1121 | // In all cases, we should now skip the first declaration in the chain. |
| 1122 | ++FI; |
| 1123 | |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1124 | while (FI != FEnd) { |
| 1125 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1126 | |
| 1127 | // FIXME: these are somewhat meaningless |
| 1128 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1129 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1130 | |
| 1131 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | f584832 | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1132 | (FI == FEnd? SS : EmptySS), field, |
| 1133 | foundDecl, memberNameInfo) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1134 | .take(); |
| 1135 | } |
| 1136 | |
| 1137 | return Owned(result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1140 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1141 | /// possibly a list of template arguments. |
| 1142 | /// |
| 1143 | /// If this produces template arguments, it is permitted to call |
| 1144 | /// DecomposeTemplateName. |
| 1145 | /// |
| 1146 | /// This actually loses a lot of source location information for |
| 1147 | /// non-standard name kinds; we should consider preserving that in |
| 1148 | /// some way. |
| 1149 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1150 | const UnqualifiedId &Id, |
| 1151 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1152 | DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1153 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1154 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1155 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1156 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1157 | |
| 1158 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1159 | Id.TemplateId->getTemplateArgs(), |
| 1160 | Id.TemplateId->NumArgs); |
| 1161 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1162 | TemplateArgsPtr.release(); |
| 1163 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1164 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1165 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1166 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1167 | TemplateArgs = &Buffer; |
| 1168 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1169 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1170 | TemplateArgs = 0; |
| 1171 | } |
| 1172 | } |
| 1173 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1174 | /// Determines if the given class is provably not derived from all of |
| 1175 | /// the prospective base classes. |
| 1176 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1177 | CXXRecordDecl *Record, |
| 1178 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1179 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1180 | return false; |
| 1181 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1182 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1183 | if (!RD) return false; |
| 1184 | Record = cast<CXXRecordDecl>(RD); |
| 1185 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1186 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1187 | E = Record->bases_end(); I != E; ++I) { |
| 1188 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1189 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1190 | if (!BaseRT) return false; |
| 1191 | |
| 1192 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1193 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1194 | return false; |
| 1195 | } |
| 1196 | |
| 1197 | return true; |
| 1198 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1199 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1200 | enum IMAKind { |
| 1201 | /// The reference is definitely not an instance member access. |
| 1202 | IMA_Static, |
| 1203 | |
| 1204 | /// The reference may be an implicit instance member access. |
| 1205 | IMA_Mixed, |
| 1206 | |
| 1207 | /// The reference may be to an instance member, but it is invalid if |
| 1208 | /// so, because the context is not an instance method. |
| 1209 | IMA_Mixed_StaticContext, |
| 1210 | |
| 1211 | /// The reference may be to an instance member, but it is invalid if |
| 1212 | /// so, because the context is from an unrelated class. |
| 1213 | IMA_Mixed_Unrelated, |
| 1214 | |
| 1215 | /// The reference is definitely an implicit instance member access. |
| 1216 | IMA_Instance, |
| 1217 | |
| 1218 | /// The reference may be to an unresolved using declaration. |
| 1219 | IMA_Unresolved, |
| 1220 | |
| 1221 | /// The reference may be to an unresolved using declaration and the |
| 1222 | /// context is not an instance method. |
| 1223 | IMA_Unresolved_StaticContext, |
| 1224 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1225 | /// All possible referrents are instance members and the current |
| 1226 | /// context is not an instance method. |
| 1227 | IMA_Error_StaticContext, |
| 1228 | |
| 1229 | /// All possible referrents are instance members of an unrelated |
| 1230 | /// class. |
| 1231 | IMA_Error_Unrelated |
| 1232 | }; |
| 1233 | |
| 1234 | /// The given lookup names class member(s) and is not being used for |
| 1235 | /// an address-of-member expression. Classify the type of access |
| 1236 | /// according to whether it's possible that this reference names an |
| 1237 | /// instance member. This is best-effort; it is okay to |
| 1238 | /// conservatively answer "yes", in which case some errors will simply |
| 1239 | /// not be caught until template-instantiation. |
| 1240 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1241 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1242 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1243 | |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1244 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1245 | bool isStaticContext = |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1246 | (!isa<CXXMethodDecl>(DC) || |
| 1247 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1248 | |
| 1249 | if (R.isUnresolvableResult()) |
| 1250 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1251 | |
| 1252 | // Collect all the declaring classes of instance members we find. |
| 1253 | bool hasNonInstance = false; |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1254 | bool hasField = false; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1255 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1256 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1257 | NamedDecl *D = *I; |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1258 | |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1259 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1260 | if (dyn_cast<FieldDecl>(D)) |
| 1261 | hasField = true; |
| 1262 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1263 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1264 | Classes.insert(R->getCanonicalDecl()); |
| 1265 | } |
| 1266 | else |
| 1267 | hasNonInstance = true; |
| 1268 | } |
| 1269 | |
| 1270 | // If we didn't find any instance members, it can't be an implicit |
| 1271 | // member reference. |
| 1272 | if (Classes.empty()) |
| 1273 | return IMA_Static; |
| 1274 | |
| 1275 | // If the current context is not an instance method, it can't be |
| 1276 | // an implicit member reference. |
Sebastian Redl | f978000 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1277 | if (isStaticContext) { |
| 1278 | if (hasNonInstance) |
| 1279 | return IMA_Mixed_StaticContext; |
| 1280 | |
| 1281 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1282 | // C++0x [expr.prim.general]p10: |
| 1283 | // An id-expression that denotes a non-static data member or non-static |
| 1284 | // member function of a class can only be used: |
| 1285 | // (...) |
| 1286 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1287 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1288 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1289 | if (isUnevaluatedExpression) |
| 1290 | return IMA_Mixed_StaticContext; |
| 1291 | } |
| 1292 | |
| 1293 | return IMA_Error_StaticContext; |
| 1294 | } |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1295 | |
| 1296 | // If we can prove that the current context is unrelated to all the |
| 1297 | // declaring classes, it can't be an implicit member reference (in |
| 1298 | // which case it's an error if any of those members are selected). |
| 1299 | if (IsProvablyNotDerivedFrom(SemaRef, |
John McCall | ea1471e | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1300 | cast<CXXMethodDecl>(DC)->getParent(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1301 | Classes)) |
| 1302 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1303 | |
| 1304 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1305 | } |
| 1306 | |
| 1307 | /// Diagnose a reference to a field with no object available. |
| 1308 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1309 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1310 | NamedDecl *rep, |
| 1311 | const DeclarationNameInfo &nameInfo) { |
| 1312 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1313 | SourceRange Range(Loc); |
| 1314 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1315 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1316 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1317 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1318 | if (MD->isStatic()) { |
| 1319 | // "invalid use of member 'x' in static member function" |
| 1320 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1321 | << Range << nameInfo.getName(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1322 | return; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1327 | << nameInfo.getName() << Range; |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1334 | /// Diagnose an empty lookup. |
| 1335 | /// |
| 1336 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1337 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1338 | CorrectTypoContext CTC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1339 | DeclarationName Name = R.getLookupName(); |
| 1340 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1341 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1342 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1343 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1344 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1345 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1346 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1347 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1348 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1349 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1350 | // If the original lookup was an unqualified lookup, fake an |
| 1351 | // unqualified lookup. This is useful when (for example) the |
| 1352 | // original lookup would not have found something because it was a |
| 1353 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1354 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1355 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1356 | if (isa<CXXRecordDecl>(DC)) { |
| 1357 | LookupQualifiedName(R, DC); |
| 1358 | |
| 1359 | if (!R.empty()) { |
| 1360 | // Don't give errors about ambiguities in this lookup. |
| 1361 | R.suppressDiagnostics(); |
| 1362 | |
| 1363 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1364 | bool isInstance = CurMethod && |
| 1365 | CurMethod->isInstance() && |
| 1366 | DC == CurMethod->getParent(); |
| 1367 | |
| 1368 | // Give a code modification hint to insert 'this->'. |
| 1369 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1370 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1371 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1372 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1373 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1374 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1375 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1376 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1377 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1378 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1379 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1380 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1381 | R.getNameLoc(), DepThisType, false); |
| 1382 | TemplateArgumentListInfo TList; |
| 1383 | if (ULE->hasExplicitTemplateArgs()) |
| 1384 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1385 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1386 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1387 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1388 | CXXDependentScopeMemberExpr *DepExpr = |
| 1389 | CXXDependentScopeMemberExpr::Create( |
| 1390 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1391 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1392 | R.getLookupNameInfo(), &TList); |
| 1393 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1394 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1395 | // FIXME: we should be able to handle this case too. It is correct |
| 1396 | // to add this-> here. This is a workaround for PR7947. |
| 1397 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1398 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1399 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1400 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1401 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1402 | |
| 1403 | // Do we really want to note all of these? |
| 1404 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1405 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1406 | |
| 1407 | // Tell the callee to try to recover. |
| 1408 | return false; |
| 1409 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1410 | |
| 1411 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1415 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1416 | DeclarationName Corrected; |
Daniel Dunbar | dc32cdf | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1417 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1418 | if (!R.empty()) { |
| 1419 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1420 | if (SS.isEmpty()) |
| 1421 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1422 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1423 | R.getLookupName().getAsString()); |
| 1424 | else |
| 1425 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1426 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1427 | << SS.getRange() |
| 1428 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1429 | R.getLookupName().getAsString()); |
| 1430 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1431 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1432 | << ND->getDeclName(); |
| 1433 | |
| 1434 | // Tell the callee to try to recover. |
| 1435 | return false; |
| 1436 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1437 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1438 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1439 | // FIXME: If we ended up with a typo for a type name or |
| 1440 | // Objective-C class name, we're in trouble because the parser |
| 1441 | // is in the wrong place to recover. Suggest the typo |
| 1442 | // correction, but don't make it a fix-it since we're not going |
| 1443 | // to recover well anyway. |
| 1444 | if (SS.isEmpty()) |
| 1445 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1446 | else |
| 1447 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1448 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1449 | << SS.getRange(); |
| 1450 | |
| 1451 | // Don't try to recover; it won't work. |
| 1452 | return true; |
| 1453 | } |
| 1454 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1455 | // 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] | 1456 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1457 | if (SS.isEmpty()) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1458 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1459 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1460 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1461 | << Name << computeDeclContext(SS, false) << Corrected |
| 1462 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1463 | return true; |
| 1464 | } |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1465 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | // Emit a special diagnostic for failed member lookups. |
| 1469 | // FIXME: computing the declaration context might fail here (?) |
| 1470 | if (!SS.isEmpty()) { |
| 1471 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1472 | << Name << computeDeclContext(SS, false) |
| 1473 | << SS.getRange(); |
| 1474 | return true; |
| 1475 | } |
| 1476 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1477 | // Give up, we can't recover. |
| 1478 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1482 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1483 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1484 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1485 | if (!IDecl) |
| 1486 | return 0; |
| 1487 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1488 | if (!ClassImpDecl) |
| 1489 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1490 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1491 | if (!property) |
| 1492 | return 0; |
| 1493 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1494 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1495 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1496 | return 0; |
| 1497 | return property; |
| 1498 | } |
| 1499 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1500 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1501 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1502 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1503 | if (!IDecl) |
| 1504 | return false; |
| 1505 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1506 | if (!ClassImpDecl) |
| 1507 | return false; |
| 1508 | if (ObjCPropertyImplDecl *PIDecl |
| 1509 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1510 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1511 | PIDecl->getPropertyIvarDecl()) |
| 1512 | return false; |
| 1513 | |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1517 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1518 | LookupResult &Lookup, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1519 | IdentifierInfo *II, |
| 1520 | SourceLocation NameLoc) { |
| 1521 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1522 | bool LookForIvars; |
| 1523 | if (Lookup.empty()) |
| 1524 | LookForIvars = true; |
| 1525 | else if (CurMeth->isClassMethod()) |
| 1526 | LookForIvars = false; |
| 1527 | else |
| 1528 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1529 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1530 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1531 | if (!LookForIvars) |
| 1532 | return 0; |
| 1533 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1534 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1535 | if (!IDecl) |
| 1536 | return 0; |
| 1537 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1538 | if (!ClassImpDecl) |
| 1539 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1540 | bool DynamicImplSeen = false; |
| 1541 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1542 | if (!property) |
| 1543 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1544 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1545 | DynamicImplSeen = |
| 1546 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1547 | // property implementation has a designated ivar. No need to assume a new |
| 1548 | // one. |
| 1549 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1550 | return 0; |
| 1551 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1552 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1553 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1554 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1555 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1556 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1557 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1558 | (Expr *)0, true); |
| 1559 | ClassImpDecl->addDecl(Ivar); |
| 1560 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1561 | property->setPropertyIvarDecl(Ivar); |
| 1562 | return Ivar; |
| 1563 | } |
| 1564 | return 0; |
| 1565 | } |
| 1566 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1567 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1568 | CXXScopeSpec &SS, |
| 1569 | UnqualifiedId &Id, |
| 1570 | bool HasTrailingLParen, |
| 1571 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1572 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1573 | "cannot be direct & operand and have a trailing lparen"); |
| 1574 | |
| 1575 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1576 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1577 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1578 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1579 | |
| 1580 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1581 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1582 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1583 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1584 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1585 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1586 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1587 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1588 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1589 | // C++ [temp.dep.expr]p3: |
| 1590 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1591 | // -- an identifier that was declared with a dependent type, |
| 1592 | // (note: handled after lookup) |
| 1593 | // -- a template-id that is dependent, |
| 1594 | // (note: handled in BuildTemplateIdExpr) |
| 1595 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1596 | // -- a nested-name-specifier that contains a class-name that |
| 1597 | // names a dependent type. |
| 1598 | // Determine whether this is a member of an unknown specialization; |
| 1599 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1600 | bool DependentID = false; |
| 1601 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1602 | Name.getCXXNameType()->isDependentType()) { |
| 1603 | DependentID = true; |
| 1604 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1605 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1606 | if (RequireCompleteDeclContext(SS, DC)) |
| 1607 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1608 | } else { |
| 1609 | DependentID = true; |
| 1610 | } |
| 1611 | } |
| 1612 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1613 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1614 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1615 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1616 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1617 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1618 | // Perform the required lookup. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1619 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1620 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1621 | // Lookup the template name again to correctly establish the context in |
| 1622 | // which it was found. This is really unfortunate as we already did the |
| 1623 | // lookup to determine that it was a template name in the first place. If |
| 1624 | // this becomes a performance hit, we can work harder to preserve those |
| 1625 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1626 | bool MemberOfUnknownSpecialization; |
| 1627 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1628 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1629 | |
| 1630 | if (MemberOfUnknownSpecialization || |
| 1631 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1632 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1633 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1634 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1635 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1636 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1638 | // If the result might be in a dependent base class, this is a dependent |
| 1639 | // id-expression. |
| 1640 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1641 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1642 | TemplateArgs); |
| 1643 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1644 | // If this reference is in an Objective-C method, then we need to do |
| 1645 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1646 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1647 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1648 | if (E.isInvalid()) |
| 1649 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1651 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1652 | return Owned(Ex); |
| 1653 | |
| 1654 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1655 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1656 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1657 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) { |
| 1658 | if (const ObjCPropertyDecl *Property = |
| 1659 | canSynthesizeProvisionalIvar(II)) { |
| 1660 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1661 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1662 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1663 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1664 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1665 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1666 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1667 | // for further use, this must be set to false if in class method. |
| 1668 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1669 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1670 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1671 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1672 | if (R.isAmbiguous()) |
| 1673 | return ExprError(); |
| 1674 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1675 | // Determine whether this name might be a candidate for |
| 1676 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1677 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1678 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1679 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1680 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1681 | // in C90, extension in C99, forbidden in C++). |
| 1682 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1683 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1684 | if (D) R.addDecl(D); |
| 1685 | } |
| 1686 | |
| 1687 | // If this name wasn't predeclared and if this is not a function |
| 1688 | // call, diagnose the problem. |
| 1689 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1690 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1691 | return ExprError(); |
| 1692 | |
| 1693 | assert(!R.empty() && |
| 1694 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1695 | |
| 1696 | // If we found an Objective-C instance variable, let |
| 1697 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1698 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1699 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1700 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1701 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1702 | assert(E.isInvalid() || E.get()); |
| 1703 | return move(E); |
| 1704 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | } |
| 1706 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1707 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1708 | // This is guaranteed from this point on. |
| 1709 | assert(!R.empty() || ADL); |
| 1710 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1711 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1712 | // C++ [class.mfct.non-static]p3: |
| 1713 | // When an id-expression that is not part of a class member access |
| 1714 | // syntax and not used to form a pointer to member is used in the |
| 1715 | // body of a non-static member function of class X, if name lookup |
| 1716 | // resolves the name in the id-expression to a non-static non-type |
| 1717 | // member of some class C, the id-expression is transformed into a |
| 1718 | // class member access expression using (*this) as the |
| 1719 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1720 | // |
| 1721 | // But we don't actually need to do this for '&' operands if R |
| 1722 | // resolved to a function or overloaded function set, because the |
| 1723 | // expression is ill-formed if it actually works out to be a |
| 1724 | // non-static member function: |
| 1725 | // |
| 1726 | // C++ [expr.ref]p4: |
| 1727 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1728 | // [t]he expression can be used only as the left-hand operand of a |
| 1729 | // member function call. |
| 1730 | // |
| 1731 | // There are other safeguards against such uses, but it's important |
| 1732 | // to get this right here so that we don't end up making a |
| 1733 | // spuriously dependent expression if we're inside a dependent |
| 1734 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1735 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1736 | bool MightBeImplicitMember; |
| 1737 | if (!isAddressOfOperand) |
| 1738 | MightBeImplicitMember = true; |
| 1739 | else if (!SS.isEmpty()) |
| 1740 | MightBeImplicitMember = false; |
| 1741 | else if (R.isOverloadedResult()) |
| 1742 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1743 | else if (R.isUnresolvableResult()) |
| 1744 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1745 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1746 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1747 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1748 | |
| 1749 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1750 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1753 | if (TemplateArgs) |
| 1754 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1755 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1756 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1757 | } |
| 1758 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1759 | /// Builds an expression which might be an implicit member expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1760 | ExprResult |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1761 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1762 | LookupResult &R, |
| 1763 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1764 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1765 | case IMA_Instance: |
| 1766 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1767 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1768 | case IMA_Mixed: |
| 1769 | case IMA_Mixed_Unrelated: |
| 1770 | case IMA_Unresolved: |
| 1771 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1772 | |
| 1773 | case IMA_Static: |
| 1774 | case IMA_Mixed_StaticContext: |
| 1775 | case IMA_Unresolved_StaticContext: |
| 1776 | if (TemplateArgs) |
| 1777 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1778 | return BuildDeclarationNameExpr(SS, R, false); |
| 1779 | |
| 1780 | case IMA_Error_StaticContext: |
| 1781 | case IMA_Error_Unrelated: |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1782 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 1783 | R.getLookupNameInfo()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1784 | return ExprError(); |
| 1785 | } |
| 1786 | |
| 1787 | llvm_unreachable("unexpected instance member access kind"); |
| 1788 | return ExprError(); |
| 1789 | } |
| 1790 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1791 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1792 | /// declaration name, generally during template instantiation. |
| 1793 | /// There's a large number of things which don't need to be done along |
| 1794 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1795 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1796 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1797 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1798 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1799 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1800 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1801 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1802 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1803 | return ExprError(); |
| 1804 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1805 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1806 | LookupQualifiedName(R, DC); |
| 1807 | |
| 1808 | if (R.isAmbiguous()) |
| 1809 | return ExprError(); |
| 1810 | |
| 1811 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1812 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1813 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1814 | return ExprError(); |
| 1815 | } |
| 1816 | |
| 1817 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1818 | } |
| 1819 | |
| 1820 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1821 | /// detected that we're currently inside an ObjC method. Perform some |
| 1822 | /// additional lookup. |
| 1823 | /// |
| 1824 | /// Ideally, most of this would be done by lookup, but there's |
| 1825 | /// actually quite a lot of extra work involved. |
| 1826 | /// |
| 1827 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1828 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1829 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1830 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1831 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1832 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1833 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1834 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1835 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1836 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1837 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1838 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1839 | |
| 1840 | // If we're in a class method, we don't normally want to look for |
| 1841 | // ivars. But if we don't find anything else, and there's an |
| 1842 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1843 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1844 | |
| 1845 | bool LookForIvars; |
| 1846 | if (Lookup.empty()) |
| 1847 | LookForIvars = true; |
| 1848 | else if (IsClassMethod) |
| 1849 | LookForIvars = false; |
| 1850 | else |
| 1851 | LookForIvars = (Lookup.isSingleResult() && |
| 1852 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1853 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1855 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1856 | ObjCInterfaceDecl *ClassDeclared; |
| 1857 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1858 | // Diagnose using an ivar in a class method. |
| 1859 | if (IsClassMethod) |
| 1860 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1861 | << IV->getDeclName()); |
| 1862 | |
| 1863 | // If we're referencing an invalid decl, just return this as a silent |
| 1864 | // error node. The error diagnostic was already emitted on the decl. |
| 1865 | if (IV->isInvalidDecl()) |
| 1866 | return ExprError(); |
| 1867 | |
| 1868 | // Check if referencing a field with __attribute__((deprecated)). |
| 1869 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1870 | return ExprError(); |
| 1871 | |
| 1872 | // Diagnose the use of an ivar outside of the declaring class. |
| 1873 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1874 | ClassDeclared != IFace) |
| 1875 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1876 | |
| 1877 | // FIXME: This should use a new expr for a direct reference, don't |
| 1878 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1879 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1880 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1881 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1882 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1883 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1884 | SelfName, false, false); |
| 1885 | if (SelfExpr.isInvalid()) |
| 1886 | return ExprError(); |
| 1887 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1888 | Expr *SelfE = SelfExpr.take(); |
| 1889 | DefaultLvalueConversion(SelfE); |
| 1890 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1891 | MarkDeclarationReferenced(Loc, IV); |
| 1892 | return Owned(new (Context) |
| 1893 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1894 | SelfE, true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1895 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1896 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1897 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1898 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1899 | ObjCInterfaceDecl *ClassDeclared; |
| 1900 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1901 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1902 | IFace == ClassDeclared) |
| 1903 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1904 | } |
| 1905 | } |
| 1906 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1907 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1908 | // FIXME. Consolidate this with similar code in LookupName. |
| 1909 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1910 | if (!(getLangOptions().CPlusPlus && |
| 1911 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1912 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1913 | S, Lookup.isForRedeclaration(), |
| 1914 | Lookup.getNameLoc()); |
| 1915 | if (D) Lookup.addDecl(D); |
| 1916 | } |
| 1917 | } |
| 1918 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1919 | // Sentinel value saying that we didn't do anything special. |
| 1920 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1921 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1922 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1923 | /// \brief Cast a base object to a member's actual type. |
| 1924 | /// |
| 1925 | /// Logically this happens in three phases: |
| 1926 | /// |
| 1927 | /// * First we cast from the base type to the naming class. |
| 1928 | /// The naming class is the class into which we were looking |
| 1929 | /// when we found the member; it's the qualifier type if a |
| 1930 | /// qualifier was provided, and otherwise it's the base type. |
| 1931 | /// |
| 1932 | /// * Next we cast from the naming class to the declaring class. |
| 1933 | /// If the member we found was brought into a class's scope by |
| 1934 | /// a using declaration, this is that class; otherwise it's |
| 1935 | /// the class declaring the member. |
| 1936 | /// |
| 1937 | /// * Finally we cast from the declaring class to the "true" |
| 1938 | /// declaring class of the member. This conversion does not |
| 1939 | /// obey access control. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1940 | bool |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1941 | Sema::PerformObjectMemberConversion(Expr *&From, |
| 1942 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1943 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1944 | NamedDecl *Member) { |
| 1945 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1946 | if (!RD) |
| 1947 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1949 | QualType DestRecordType; |
| 1950 | QualType DestType; |
| 1951 | QualType FromRecordType; |
| 1952 | QualType FromType = From->getType(); |
| 1953 | bool PointerConversions = false; |
| 1954 | if (isa<FieldDecl>(Member)) { |
| 1955 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1957 | if (FromType->getAs<PointerType>()) { |
| 1958 | DestType = Context.getPointerType(DestRecordType); |
| 1959 | FromRecordType = FromType->getPointeeType(); |
| 1960 | PointerConversions = true; |
| 1961 | } else { |
| 1962 | DestType = DestRecordType; |
| 1963 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1964 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1965 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1966 | if (Method->isStatic()) |
| 1967 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1968 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1969 | DestType = Method->getThisType(Context); |
| 1970 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1972 | if (FromType->getAs<PointerType>()) { |
| 1973 | FromRecordType = FromType->getPointeeType(); |
| 1974 | PointerConversions = true; |
| 1975 | } else { |
| 1976 | FromRecordType = FromType; |
| 1977 | DestType = DestRecordType; |
| 1978 | } |
| 1979 | } else { |
| 1980 | // No conversion necessary. |
| 1981 | return false; |
| 1982 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1984 | if (DestType->isDependentType() || FromType->isDependentType()) |
| 1985 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1987 | // If the unqualified types are the same, no conversion is necessary. |
| 1988 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1989 | return false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1990 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1991 | SourceRange FromRange = From->getSourceRange(); |
| 1992 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1993 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1994 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1996 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1997 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1998 | // class name. |
| 1999 | // |
| 2000 | // If the member was a qualified name and the qualified referred to a |
| 2001 | // specific base subobject type, we'll cast to that intermediate type |
| 2002 | // first and then to the object in which the member is declared. That allows |
| 2003 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2004 | // |
| 2005 | // class Base { public: int x; }; |
| 2006 | // class Derived1 : public Base { }; |
| 2007 | // class Derived2 : public Base { }; |
| 2008 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2009 | // |
| 2010 | // void VeryDerived::f() { |
| 2011 | // x = 17; // error: ambiguous base subobjects |
| 2012 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2013 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2014 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2015 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2016 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2017 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2018 | |
| 2019 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2020 | |
| 2021 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2022 | // type of the object type, in which case we just ignore it. |
| 2023 | // Otherwise build the appropriate casts. |
| 2024 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2025 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2026 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2027 | FromLoc, FromRange, &BasePath)) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2028 | return true; |
| 2029 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2030 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2031 | QType = Context.getPointerType(QType); |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2032 | ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2033 | VK, &BasePath); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2034 | |
| 2035 | FromType = QType; |
| 2036 | FromRecordType = QRecordType; |
| 2037 | |
| 2038 | // If the qualifier type was the same as the destination type, |
| 2039 | // we're done. |
| 2040 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 2041 | return false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2042 | } |
| 2043 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2044 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2045 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2046 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2047 | // If we actually found the member through a using declaration, cast |
| 2048 | // down to the using declaration's type. |
| 2049 | // |
| 2050 | // Pointer equality is fine here because only one declaration of a |
| 2051 | // class ever has member declarations. |
| 2052 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2053 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2054 | QualType URecordType = Context.getTypeDeclType( |
| 2055 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2056 | |
| 2057 | // We only need to do this if the naming-class to declaring-class |
| 2058 | // conversion is non-trivial. |
| 2059 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2060 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2061 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2062 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2063 | FromLoc, FromRange, &BasePath)) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2064 | return true; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2065 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2066 | QualType UType = URecordType; |
| 2067 | if (PointerConversions) |
| 2068 | UType = Context.getPointerType(UType); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2069 | ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2070 | VK, &BasePath); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2071 | FromType = UType; |
| 2072 | FromRecordType = URecordType; |
| 2073 | } |
| 2074 | |
| 2075 | // We don't do access control for the conversion from the |
| 2076 | // declaring class to the true declaring class. |
| 2077 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2078 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2079 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2080 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2081 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2082 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2083 | IgnoreAccess)) |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2084 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2085 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2086 | ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2087 | VK, &BasePath); |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2088 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2089 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2091 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2093 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2094 | DeclAccessPair FoundDecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2095 | const DeclarationNameInfo &MemberNameInfo, |
| 2096 | QualType Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2097 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2098 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2099 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2100 | Member, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2101 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2104 | static ExprResult |
| 2105 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2106 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2107 | DeclAccessPair FoundDecl, |
| 2108 | const DeclarationNameInfo &MemberNameInfo) { |
| 2109 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2110 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2111 | // that *x is always an l-value), except that if the base isn't |
| 2112 | // an ordinary object then we must have an rvalue. |
| 2113 | ExprValueKind VK = VK_LValue; |
| 2114 | ExprObjectKind OK = OK_Ordinary; |
| 2115 | if (!IsArrow) { |
| 2116 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2117 | VK = BaseExpr->getValueKind(); |
| 2118 | else |
| 2119 | VK = VK_RValue; |
| 2120 | } |
| 2121 | if (VK != VK_RValue && Field->isBitField()) |
| 2122 | OK = OK_BitField; |
| 2123 | |
| 2124 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2125 | QualType MemberType = Field->getType(); |
| 2126 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2127 | MemberType = Ref->getPointeeType(); |
| 2128 | VK = VK_LValue; |
| 2129 | } else { |
| 2130 | QualType BaseType = BaseExpr->getType(); |
| 2131 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2132 | |
| 2133 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2134 | |
| 2135 | // GC attributes are never picked up by members. |
| 2136 | BaseQuals.removeObjCGCAttr(); |
| 2137 | |
| 2138 | // CVR attributes from the base are picked up by members, |
| 2139 | // except that 'mutable' members don't pick up 'const'. |
| 2140 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2141 | |
| 2142 | Qualifiers MemberQuals |
| 2143 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2144 | |
| 2145 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2146 | assert(!MemberQuals.hasAddressSpace()); |
| 2147 | |
| 2148 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2149 | if (Combined != MemberQuals) |
| 2150 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2151 | } |
| 2152 | |
| 2153 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
| 2154 | if (S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2155 | FoundDecl, Field)) |
| 2156 | return ExprError(); |
| 2157 | return S.Owned(BuildMemberExpr(S.Context, BaseExpr, IsArrow, SS, |
| 2158 | Field, FoundDecl, MemberNameInfo, |
| 2159 | MemberType, VK, OK)); |
| 2160 | } |
| 2161 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2162 | /// Builds an implicit member access expression. The current context |
| 2163 | /// is known to be an instance method, and the given unqualified lookup |
| 2164 | /// set is known to contain only instance members, at least one of which |
| 2165 | /// is from an appropriate type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2166 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2167 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2168 | LookupResult &R, |
| 2169 | const TemplateArgumentListInfo *TemplateArgs, |
| 2170 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2171 | assert(!R.empty() && !R.isAmbiguous()); |
| 2172 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2173 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2175 | // We may have found a field within an anonymous union or struct |
| 2176 | // (C++ [class.union]). |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2177 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2178 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2179 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2180 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2181 | // If this is known to be an instance access, go ahead and build an |
| 2182 | // implicit 'this' expression now. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2183 | // 'this' expression now. |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2184 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 2185 | assert(method && "didn't correctly pre-flight capture of 'this'"); |
| 2186 | |
| 2187 | QualType thisType = method->getThisType(Context); |
| 2188 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2189 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2190 | SourceLocation Loc = R.getNameLoc(); |
| 2191 | if (SS.getRange().isValid()) |
| 2192 | Loc = SS.getRange().getBegin(); |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2193 | baseExpr = new (Context) CXXThisExpr(loc, thisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2196 | return BuildMemberReferenceExpr(baseExpr, thisType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2197 | /*OpLoc*/ SourceLocation(), |
| 2198 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2199 | SS, |
| 2200 | /*FirstQualifierInScope*/ 0, |
| 2201 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2204 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2205 | const LookupResult &R, |
| 2206 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2207 | // Only when used directly as the postfix-expression of a call. |
| 2208 | if (!HasTrailingLParen) |
| 2209 | return false; |
| 2210 | |
| 2211 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2212 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2213 | return false; |
| 2214 | |
| 2215 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2216 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2217 | return false; |
| 2218 | |
| 2219 | // Turn off ADL when we find certain kinds of declarations during |
| 2220 | // normal lookup: |
| 2221 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2222 | NamedDecl *D = *I; |
| 2223 | |
| 2224 | // C++0x [basic.lookup.argdep]p3: |
| 2225 | // -- a declaration of a class member |
| 2226 | // Since using decls preserve this property, we check this on the |
| 2227 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2228 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2229 | return false; |
| 2230 | |
| 2231 | // C++0x [basic.lookup.argdep]p3: |
| 2232 | // -- a block-scope function declaration that is not a |
| 2233 | // using-declaration |
| 2234 | // NOTE: we also trigger this for function templates (in fact, we |
| 2235 | // don't check the decl type at all, since all other decl types |
| 2236 | // turn off ADL anyway). |
| 2237 | if (isa<UsingShadowDecl>(D)) |
| 2238 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2239 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2240 | return false; |
| 2241 | |
| 2242 | // C++0x [basic.lookup.argdep]p3: |
| 2243 | // -- a declaration that is neither a function or a function |
| 2244 | // template |
| 2245 | // And also for builtin functions. |
| 2246 | if (isa<FunctionDecl>(D)) { |
| 2247 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2248 | |
| 2249 | // But also builtin functions. |
| 2250 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2251 | return false; |
| 2252 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2253 | return false; |
| 2254 | } |
| 2255 | |
| 2256 | return true; |
| 2257 | } |
| 2258 | |
| 2259 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2260 | /// Diagnoses obvious problems with the use of the given declaration |
| 2261 | /// as an expression. This is only actually called for lookups that |
| 2262 | /// were not overloaded, and it doesn't promise that the declaration |
| 2263 | /// will in fact be used. |
| 2264 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 2265 | if (isa<TypedefDecl>(D)) { |
| 2266 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2267 | return true; |
| 2268 | } |
| 2269 | |
| 2270 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2271 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2272 | return true; |
| 2273 | } |
| 2274 | |
| 2275 | if (isa<NamespaceDecl>(D)) { |
| 2276 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2277 | return true; |
| 2278 | } |
| 2279 | |
| 2280 | return false; |
| 2281 | } |
| 2282 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2283 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2284 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2285 | LookupResult &R, |
| 2286 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2287 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2288 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2289 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2290 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2291 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2292 | |
| 2293 | // We only need to check the declaration if there's exactly one |
| 2294 | // result, because in the overloaded case the results can only be |
| 2295 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2296 | if (R.isSingleResult() && |
| 2297 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2298 | return ExprError(); |
| 2299 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2300 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2301 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2302 | // we've picked a target. |
| 2303 | R.suppressDiagnostics(); |
| 2304 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2305 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2306 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2307 | SS.getWithLocInContext(Context), |
| 2308 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2309 | NeedsADL, R.isOverloadedResult(), |
| 2310 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2311 | |
| 2312 | return Owned(ULE); |
| 2313 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2314 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2315 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2316 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2317 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2318 | const DeclarationNameInfo &NameInfo, |
| 2319 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2320 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2321 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2322 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2323 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2324 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2325 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2326 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2328 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2329 | // Specifically diagnose references to class templates that are missing |
| 2330 | // a template argument list. |
| 2331 | Diag(Loc, diag::err_template_decl_ref) |
| 2332 | << Template << SS.getRange(); |
| 2333 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2334 | return ExprError(); |
| 2335 | } |
| 2336 | |
| 2337 | // Make sure that we're referring to a value. |
| 2338 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2339 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2340 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2341 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2342 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2343 | return ExprError(); |
| 2344 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2345 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2346 | // Check whether this declaration can be used. Note that we suppress |
| 2347 | // this check when we're going to perform argument-dependent lookup |
| 2348 | // on this function name, because this might not be the function |
| 2349 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2350 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2351 | return ExprError(); |
| 2352 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2353 | // Only create DeclRefExpr's for valid Decl's. |
| 2354 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2355 | return ExprError(); |
| 2356 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2357 | // Handle members of anonymous structs and unions. If we got here, |
| 2358 | // and the reference is to a class member indirect field, then this |
| 2359 | // must be the subject of a pointer-to-member expression. |
| 2360 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2361 | if (!indirectField->isCXXClassMember()) |
| 2362 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2363 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2365 | // If the identifier reference is inside a block, and it refers to a value |
| 2366 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2367 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2368 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2369 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2370 | // We do not do this for things like enum constants, global variables, etc, |
| 2371 | // as they do not get snapshotted. |
| 2372 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2373 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2374 | case CR_Error: |
| 2375 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2376 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2377 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2378 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2379 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2380 | |
| 2381 | case CR_CaptureByRef: |
| 2382 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2383 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2384 | |
| 2385 | case CR_NoCapture: { |
| 2386 | // If this reference is not in a block or if the referenced |
| 2387 | // variable is within the block, create a normal DeclRefExpr. |
| 2388 | |
| 2389 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2390 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2391 | |
| 2392 | switch (D->getKind()) { |
| 2393 | // Ignore all the non-ValueDecl kinds. |
| 2394 | #define ABSTRACT_DECL(kind) |
| 2395 | #define VALUE(type, base) |
| 2396 | #define DECL(type, base) \ |
| 2397 | case Decl::type: |
| 2398 | #include "clang/AST/DeclNodes.inc" |
| 2399 | llvm_unreachable("invalid value decl kind"); |
| 2400 | return ExprError(); |
| 2401 | |
| 2402 | // These shouldn't make it here. |
| 2403 | case Decl::ObjCAtDefsField: |
| 2404 | case Decl::ObjCIvar: |
| 2405 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2406 | return ExprError(); |
| 2407 | |
| 2408 | // Enum constants are always r-values and never references. |
| 2409 | // Unresolved using declarations are dependent. |
| 2410 | case Decl::EnumConstant: |
| 2411 | case Decl::UnresolvedUsingValue: |
| 2412 | valueKind = VK_RValue; |
| 2413 | break; |
| 2414 | |
| 2415 | // Fields and indirect fields that got here must be for |
| 2416 | // pointer-to-member expressions; we just call them l-values for |
| 2417 | // internal consistency, because this subexpression doesn't really |
| 2418 | // exist in the high-level semantics. |
| 2419 | case Decl::Field: |
| 2420 | case Decl::IndirectField: |
| 2421 | assert(getLangOptions().CPlusPlus && |
| 2422 | "building reference to field in C?"); |
| 2423 | |
| 2424 | // These can't have reference type in well-formed programs, but |
| 2425 | // for internal consistency we do this anyway. |
| 2426 | type = type.getNonReferenceType(); |
| 2427 | valueKind = VK_LValue; |
| 2428 | break; |
| 2429 | |
| 2430 | // Non-type template parameters are either l-values or r-values |
| 2431 | // depending on the type. |
| 2432 | case Decl::NonTypeTemplateParm: { |
| 2433 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2434 | type = reftype->getPointeeType(); |
| 2435 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2436 | break; |
| 2437 | } |
| 2438 | |
| 2439 | // For non-references, we need to strip qualifiers just in case |
| 2440 | // the template parameter was declared as 'const int' or whatever. |
| 2441 | valueKind = VK_RValue; |
| 2442 | type = type.getUnqualifiedType(); |
| 2443 | break; |
| 2444 | } |
| 2445 | |
| 2446 | case Decl::Var: |
| 2447 | // In C, "extern void blah;" is valid and is an r-value. |
| 2448 | if (!getLangOptions().CPlusPlus && |
| 2449 | !type.hasQualifiers() && |
| 2450 | type->isVoidType()) { |
| 2451 | valueKind = VK_RValue; |
| 2452 | break; |
| 2453 | } |
| 2454 | // fallthrough |
| 2455 | |
| 2456 | case Decl::ImplicitParam: |
| 2457 | case Decl::ParmVar: |
| 2458 | // These are always l-values. |
| 2459 | valueKind = VK_LValue; |
| 2460 | type = type.getNonReferenceType(); |
| 2461 | break; |
| 2462 | |
| 2463 | case Decl::Function: { |
| 2464 | // Functions are l-values in C++. |
| 2465 | if (getLangOptions().CPlusPlus) { |
| 2466 | valueKind = VK_LValue; |
| 2467 | break; |
| 2468 | } |
| 2469 | |
| 2470 | // C99 DR 316 says that, if a function type comes from a |
| 2471 | // function definition (without a prototype), that type is only |
| 2472 | // used for checking compatibility. Therefore, when referencing |
| 2473 | // the function, we pretend that we don't have the full function |
| 2474 | // type. |
| 2475 | if (!cast<FunctionDecl>(VD)->hasPrototype()) |
| 2476 | if (const FunctionProtoType *proto = type->getAs<FunctionProtoType>()) |
| 2477 | type = Context.getFunctionNoProtoType(proto->getResultType(), |
| 2478 | proto->getExtInfo()); |
| 2479 | |
| 2480 | // Functions are r-values in C. |
| 2481 | valueKind = VK_RValue; |
| 2482 | break; |
| 2483 | } |
| 2484 | |
| 2485 | case Decl::CXXMethod: |
| 2486 | // C++ methods are l-values if static, r-values if non-static. |
| 2487 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2488 | valueKind = VK_LValue; |
| 2489 | break; |
| 2490 | } |
| 2491 | // fallthrough |
| 2492 | |
| 2493 | case Decl::CXXConversion: |
| 2494 | case Decl::CXXDestructor: |
| 2495 | case Decl::CXXConstructor: |
| 2496 | valueKind = VK_RValue; |
| 2497 | break; |
| 2498 | } |
| 2499 | |
| 2500 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2501 | } |
| 2502 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2503 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2504 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2505 | llvm_unreachable("unknown capture result"); |
| 2506 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2509 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2510 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2511 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2512 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2513 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2514 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2515 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2516 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2517 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2518 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2520 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2521 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2522 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2523 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2524 | if (!currentDecl && getCurBlock()) |
| 2525 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2526 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2527 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2528 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2529 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2530 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2531 | QualType ResTy; |
| 2532 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2533 | ResTy = Context.DependentTy; |
| 2534 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2535 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2536 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2537 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2538 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2539 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2540 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2541 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2544 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2545 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2546 | bool Invalid = false; |
| 2547 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2548 | if (Invalid) |
| 2549 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2550 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2551 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2552 | PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2553 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2554 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2556 | QualType Ty; |
| 2557 | if (!getLangOptions().CPlusPlus) |
| 2558 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2559 | else if (Literal.isWide()) |
| 2560 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2561 | else if (Literal.isMultiChar()) |
| 2562 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2563 | else |
| 2564 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2565 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2566 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2567 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2568 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2571 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2572 | // 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] | 2573 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2574 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2575 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2576 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2577 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2578 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2579 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2581 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2582 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2583 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2584 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2585 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2586 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2587 | bool Invalid = false; |
| 2588 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2589 | if (Invalid) |
| 2590 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2591 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2593 | Tok.getLocation(), PP); |
| 2594 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2595 | return ExprError(); |
| 2596 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2597 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2599 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2600 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2601 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2602 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2603 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2604 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2605 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2606 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2607 | |
| 2608 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2609 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2610 | using llvm::APFloat; |
| 2611 | APFloat Val(Format); |
| 2612 | |
| 2613 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2614 | |
| 2615 | // Overflow is always an error, but underflow is only an error if |
| 2616 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2617 | if ((result & APFloat::opOverflow) || |
| 2618 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2619 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2620 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2621 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2622 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2623 | APFloat::getLargest(Format).toString(buffer); |
| 2624 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2625 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2626 | APFloat::getSmallest(Format).toString(buffer); |
| 2627 | } |
| 2628 | |
| 2629 | Diag(Tok.getLocation(), diagnostic) |
| 2630 | << Ty |
| 2631 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2632 | } |
| 2633 | |
| 2634 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2635 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2636 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2637 | if (Ty == Context.DoubleTy) { |
| 2638 | if (getLangOptions().SinglePrecisionConstants) { |
| 2639 | ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); |
| 2640 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2641 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
| 2642 | ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); |
| 2643 | } |
| 2644 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2645 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2646 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2647 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2648 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2649 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2650 | // long long is a C99 feature. |
| 2651 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2652 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2653 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2655 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2656 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2657 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2658 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2659 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2660 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2661 | Ty = Context.UnsignedLongLongTy; |
| 2662 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2663 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2664 | } else { |
| 2665 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2666 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2668 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2669 | // be an unsigned int. |
| 2670 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2671 | |
| 2672 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2673 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2674 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2675 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2676 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2677 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2678 | // Does it fit in a unsigned int? |
| 2679 | if (ResultVal.isIntN(IntSize)) { |
| 2680 | // Does it fit in a signed int? |
| 2681 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2682 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2684 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2685 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2687 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2689 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2690 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2691 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2693 | // Does it fit in a unsigned long? |
| 2694 | if (ResultVal.isIntN(LongSize)) { |
| 2695 | // Does it fit in a signed long? |
| 2696 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2697 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2698 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2699 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2700 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2701 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2704 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2705 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2706 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2707 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2708 | // Does it fit in a unsigned long long? |
| 2709 | if (ResultVal.isIntN(LongLongSize)) { |
| 2710 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2711 | // To be compatible with MSVC, hex integer literals ending with the |
| 2712 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2713 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2714 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2715 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2716 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2717 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2718 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2721 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2722 | // If we still couldn't decide a type, we probably have something that |
| 2723 | // 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] | 2724 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2726 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2727 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2728 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2729 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2730 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2731 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2732 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2733 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2734 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2735 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2736 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2737 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2739 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2740 | |
| 2741 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2744 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2745 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2746 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2747 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 2751 | /// See C99 6.3.2.1p[2-4] for more details. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2752 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2753 | SourceLocation OpLoc, |
| 2754 | SourceRange ExprRange, |
| 2755 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2756 | if (exprType->isDependentType()) |
| 2757 | return false; |
| 2758 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2759 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2760 | // the result is the size of the referenced type." |
| 2761 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2762 | // result shall be the alignment of the referenced type." |
| 2763 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2764 | exprType = Ref->getPointeeType(); |
| 2765 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2766 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2767 | // scalar or vector data type argument..." |
| 2768 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2769 | // type (C99 6.2.5p18) or void. |
| 2770 | if (ExprKind == UETT_VecStep) { |
| 2771 | if (!(exprType->isArithmeticType() || exprType->isVoidType() || |
| 2772 | exprType->isVectorType())) { |
| 2773 | Diag(OpLoc, diag::err_vecstep_non_scalar_vector_type) |
| 2774 | << exprType << ExprRange; |
| 2775 | return true; |
| 2776 | } |
| 2777 | } |
| 2778 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2779 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2780 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2781 | // alignof(function) is allowed as an extension. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2782 | if (ExprKind == UETT_SizeOf) |
| 2783 | Diag(OpLoc, diag::ext_sizeof_function_type) |
| 2784 | << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2785 | return false; |
| 2786 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2787 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2788 | // Allow sizeof(void)/alignof(void) as an extension. vec_step(void) is not |
| 2789 | // 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] | 2790 | if (exprType->isVoidType()) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2791 | if (ExprKind != UETT_VecStep) |
| 2792 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 2793 | << ExprKind << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2794 | return false; |
| 2795 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2797 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2798 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2799 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2800 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2801 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2802 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2803 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2804 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2805 | << exprType << (ExprKind == UETT_SizeOf) |
| 2806 | << ExprRange; |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2807 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 2808 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2810 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2813 | static bool CheckAlignOfExpr(Sema &S, Expr *E, SourceLocation OpLoc, |
| 2814 | SourceRange ExprRange) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2815 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2816 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2818 | if (isa<DeclRefExpr>(E)) |
| 2819 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2820 | |
| 2821 | // Cannot know anything else if the expression is dependent. |
| 2822 | if (E->isTypeDependent()) |
| 2823 | return false; |
| 2824 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2825 | if (E->getBitField()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2826 | S. Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2827 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2828 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2829 | |
| 2830 | // Alignment of a field access is always okay, so long as it isn't a |
| 2831 | // bit-field. |
| 2832 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2833 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2834 | return false; |
| 2835 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2836 | return S.CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 2837 | UETT_AlignOf); |
| 2838 | } |
| 2839 | |
| 2840 | bool Sema::CheckVecStepExpr(Expr *E, SourceLocation OpLoc, |
| 2841 | SourceRange ExprRange) { |
| 2842 | E = E->IgnoreParens(); |
| 2843 | |
| 2844 | // Cannot know anything else if the expression is dependent. |
| 2845 | if (E->isTypeDependent()) |
| 2846 | return false; |
| 2847 | |
| 2848 | return CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 2849 | UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2850 | } |
| 2851 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2852 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2853 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2854 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2855 | SourceLocation OpLoc, |
| 2856 | UnaryExprOrTypeTrait ExprKind, |
| 2857 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2858 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2859 | return ExprError(); |
| 2860 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2861 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2862 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2863 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2864 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2865 | return ExprError(); |
| 2866 | |
| 2867 | // 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] | 2868 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2869 | Context.getSizeType(), |
| 2870 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | /// \brief Build a sizeof or alignof expression given an expression |
| 2874 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2875 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2876 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2877 | UnaryExprOrTypeTrait ExprKind, |
| 2878 | SourceRange R) { |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2879 | // Verify that the operand is valid. |
| 2880 | bool isInvalid = false; |
| 2881 | if (E->isTypeDependent()) { |
| 2882 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2883 | } else if (ExprKind == UETT_AlignOf) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2884 | isInvalid = CheckAlignOfExpr(*this, E, OpLoc, R); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2885 | } else if (ExprKind == UETT_VecStep) { |
| 2886 | isInvalid = CheckVecStepExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2887 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2888 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 2889 | isInvalid = true; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2890 | } else if (E->getType()->isPlaceholderType()) { |
| 2891 | ExprResult PE = CheckPlaceholderExpr(E, OpLoc); |
| 2892 | if (PE.isInvalid()) return ExprError(); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2893 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind, R); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2894 | } else { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2895 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, R, |
| 2896 | UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | if (isInvalid) |
| 2900 | return ExprError(); |
| 2901 | |
| 2902 | // 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] | 2903 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, E, |
| 2904 | Context.getSizeType(), |
| 2905 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2908 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2909 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2910 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2911 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2912 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2913 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2914 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2915 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2916 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2917 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2918 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2919 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2920 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2921 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2922 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2923 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2924 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2925 | ExprResult Result |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2926 | = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind, |
| 2927 | ArgEx->getSourceRange()); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2928 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2929 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2932 | static QualType CheckRealImagOperand(Sema &S, Expr *&V, SourceLocation Loc, |
| 2933 | bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2934 | if (V->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2935 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2936 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2937 | // _Real and _Imag are only l-values for normal l-values. |
| 2938 | if (V->getObjectKind() != OK_Ordinary) |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2939 | S.DefaultLvalueConversion(V); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2940 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2941 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2942 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2943 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2945 | // Otherwise they pass through real integer and floating point types here. |
| 2946 | if (V->getType()->isArithmeticType()) |
| 2947 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2949 | // Test for placeholders. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2950 | ExprResult PR = S.CheckPlaceholderExpr(V, Loc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2951 | if (PR.isInvalid()) return QualType(); |
| 2952 | if (PR.take() != V) { |
| 2953 | V = PR.take(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2954 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2957 | // Reject anything else. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2958 | S.Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2959 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2960 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2964 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2965 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2966 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2967 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2968 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2969 | switch (Kind) { |
| 2970 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2971 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2972 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2973 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2974 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2975 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2978 | /// Expressions of certain arbitrary types are forbidden by C from |
| 2979 | /// having l-value type. These are: |
| 2980 | /// - 'void', but not qualified void |
| 2981 | /// - function types |
| 2982 | /// |
| 2983 | /// The exact rule here is C99 6.3.2.1: |
| 2984 | /// An lvalue is an expression with an object type or an incomplete |
| 2985 | /// type other than void. |
| 2986 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 2987 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 2988 | T->isFunctionType()); |
| 2989 | } |
| 2990 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2991 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2992 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2993 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2994 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2995 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2996 | if (Result.isInvalid()) return ExprError(); |
| 2997 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2998 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2999 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3000 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3001 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3002 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3003 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3004 | Context.DependentTy, |
| 3005 | VK_LValue, OK_Ordinary, |
| 3006 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3009 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3010 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3011 | LHSExp->getType()->isEnumeralType() || |
| 3012 | RHSExp->getType()->isRecordType() || |
| 3013 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3014 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3017 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3021 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3022 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3023 | Expr *Idx, SourceLocation RLoc) { |
| 3024 | Expr *LHSExp = Base; |
| 3025 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3027 | // Perform default conversions. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3028 | if (!LHSExp->getType()->getAs<VectorType>()) |
| 3029 | DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3030 | DefaultFunctionArrayLvalueConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3032 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3033 | ExprValueKind VK = VK_LValue; |
| 3034 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3035 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3036 | // 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] | 3037 | // 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] | 3038 | // 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] | 3039 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3040 | Expr *BaseExpr, *IndexExpr; |
| 3041 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3042 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3043 | BaseExpr = LHSExp; |
| 3044 | IndexExpr = RHSExp; |
| 3045 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3046 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3047 | BaseExpr = LHSExp; |
| 3048 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3049 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3050 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3051 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3052 | BaseExpr = RHSExp; |
| 3053 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3054 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3056 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3057 | BaseExpr = LHSExp; |
| 3058 | IndexExpr = RHSExp; |
| 3059 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3061 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3062 | // Handle the uncommon case of "123[Ptr]". |
| 3063 | BaseExpr = RHSExp; |
| 3064 | IndexExpr = LHSExp; |
| 3065 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3066 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3067 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3068 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3069 | VK = LHSExp->getValueKind(); |
| 3070 | if (VK != VK_RValue) |
| 3071 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3072 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3073 | // FIXME: need to deal with const... |
| 3074 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3075 | } else if (LHSTy->isArrayType()) { |
| 3076 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3077 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3078 | // wasn't promoted because of the C90 rule that doesn't |
| 3079 | // allow promoting non-lvalue arrays. Warn, then |
| 3080 | // force the promotion here. |
| 3081 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3082 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3083 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3084 | CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3085 | LHSTy = LHSExp->getType(); |
| 3086 | |
| 3087 | BaseExpr = LHSExp; |
| 3088 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3089 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3090 | } else if (RHSTy->isArrayType()) { |
| 3091 | // Same as previous, except for 123[f().a] case |
| 3092 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3093 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3094 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3095 | CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3096 | RHSTy = RHSExp->getType(); |
| 3097 | |
| 3098 | BaseExpr = RHSExp; |
| 3099 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3100 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3101 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3102 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3103 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3104 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3105 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3106 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3107 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3108 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3109 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3110 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3111 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3112 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3113 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3114 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3115 | // 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] | 3116 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3117 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3118 | // incomplete types are not object types. |
| 3119 | if (ResultType->isFunctionType()) { |
| 3120 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3121 | << ResultType << BaseExpr->getSourceRange(); |
| 3122 | return ExprError(); |
| 3123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3125 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3126 | // GNU extension: subscripting on pointer to void |
| 3127 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3128 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3129 | |
| 3130 | // C forbids expressions of unqualified void type from being l-values. |
| 3131 | // See IsCForbiddenLValueType. |
| 3132 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3133 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3134 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3135 | PDiag(diag::err_subscript_incomplete_type) |
| 3136 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3137 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3138 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3139 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3140 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3141 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3142 | << ResultType << BaseExpr->getSourceRange(); |
| 3143 | return ExprError(); |
| 3144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3146 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3147 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3148 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3149 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3150 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3153 | /// Check an ext-vector component access expression. |
| 3154 | /// |
| 3155 | /// VK should be set in advance to the value kind of the base |
| 3156 | /// expression. |
| 3157 | static QualType |
| 3158 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3159 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3160 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3161 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3162 | // see FIXME there. |
| 3163 | // |
| 3164 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3165 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3166 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3167 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3168 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3169 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3170 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3171 | // 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] | 3172 | // special names that indicate a subset of exactly half the elements are |
| 3173 | // to be selected. |
| 3174 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3175 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3176 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3177 | // 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] | 3178 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3179 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3180 | bool HasRepeated = false; |
| 3181 | bool HasIndex[16] = {}; |
| 3182 | |
| 3183 | int Idx; |
| 3184 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3185 | // Check that we've found one of the special components, or that the component |
| 3186 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3187 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3188 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3189 | HalvingSwizzle = true; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3190 | } else if (!HexSwizzle && |
| 3191 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3192 | do { |
| 3193 | if (HasIndex[Idx]) HasRepeated = true; |
| 3194 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3195 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3196 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3197 | } else { |
| 3198 | if (HexSwizzle) compStr++; |
| 3199 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3200 | if (HasIndex[Idx]) HasRepeated = true; |
| 3201 | HasIndex[Idx] = true; |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3202 | compStr++; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3203 | } |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3204 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3205 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3206 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3207 | // We didn't get to the end of the string. This means the component names |
| 3208 | // 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] | 3209 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3210 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3211 | return QualType(); |
| 3212 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3213 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3214 | // Ensure no component accessor exceeds the width of the vector type it |
| 3215 | // operates on. |
| 3216 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3217 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3218 | |
| 3219 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3220 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3221 | |
| 3222 | while (*compStr) { |
| 3223 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3224 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3225 | << baseType << SourceRange(CompLoc); |
| 3226 | return QualType(); |
| 3227 | } |
| 3228 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3229 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3230 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3231 | // 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] | 3232 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3233 | // 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] | 3234 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3235 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3236 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3237 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3238 | if (HexSwizzle) |
| 3239 | CompSize--; |
| 3240 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3241 | if (CompSize == 1) |
| 3242 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3243 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3244 | if (HasRepeated) VK = VK_RValue; |
| 3245 | |
| 3246 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3247 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3248 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3249 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3250 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3251 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3252 | } |
| 3253 | 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] | 3254 | } |
| 3255 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3256 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3257 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3258 | const Selector &Sel, |
| 3259 | ASTContext &Context) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3260 | if (Member) |
| 3261 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3262 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3263 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3264 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3266 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3267 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3268 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3269 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3270 | return D; |
| 3271 | } |
| 3272 | return 0; |
| 3273 | } |
| 3274 | |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3275 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3276 | IdentifierInfo *Member, |
| 3277 | const Selector &Sel, |
| 3278 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3279 | // Check protocols on qualified interfaces. |
| 3280 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3281 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3282 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3283 | if (Member) |
| 3284 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3285 | GDecl = PD; |
| 3286 | break; |
| 3287 | } |
| 3288 | // 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] | 3289 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3290 | GDecl = OMD; |
| 3291 | break; |
| 3292 | } |
| 3293 | } |
| 3294 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3295 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3296 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3297 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f2ad2c9 | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3298 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3299 | Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3300 | if (GDecl) |
| 3301 | return GDecl; |
| 3302 | } |
| 3303 | } |
| 3304 | return GDecl; |
| 3305 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3306 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3307 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3308 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3309 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3310 | const CXXScopeSpec &SS, |
| 3311 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3312 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3313 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3314 | // Even in dependent contexts, try to diagnose base expressions with |
| 3315 | // obviously wrong types, e.g.: |
| 3316 | // |
| 3317 | // T* t; |
| 3318 | // t.f; |
| 3319 | // |
| 3320 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3321 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3322 | // allows this, while still reporting an error if T is a struct pointer. |
| 3323 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3324 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3325 | if (PT && (!getLangOptions().ObjC1 || |
| 3326 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3327 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3328 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3329 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3330 | return ExprError(); |
| 3331 | } |
| 3332 | } |
| 3333 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3334 | assert(BaseType->isDependentType() || |
| 3335 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3336 | isDependentScopeSpecifier(SS)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3337 | |
| 3338 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3339 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3340 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3341 | IsArrow, OpLoc, |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3342 | SS.getWithLocInContext(Context), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3343 | FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3344 | NameInfo, TemplateArgs)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | /// We know that the given qualified member reference points only to |
| 3348 | /// declarations which do not belong to the static type of the base |
| 3349 | /// expression. Diagnose the problem. |
| 3350 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3351 | Expr *BaseExpr, |
| 3352 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3353 | const CXXScopeSpec &SS, |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3354 | NamedDecl *rep, |
| 3355 | const DeclarationNameInfo &nameInfo) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3356 | // If this is an implicit member access, use a different set of |
| 3357 | // diagnostics. |
| 3358 | if (!BaseExpr) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3359 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3360 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3361 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3362 | << SS.getRange() << rep << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | // Check whether the declarations we found through a nested-name |
| 3366 | // specifier in a member expression are actually members of the base |
| 3367 | // type. The restriction here is: |
| 3368 | // |
| 3369 | // C++ [expr.ref]p2: |
| 3370 | // ... In these cases, the id-expression shall name a |
| 3371 | // member of the class or of one of its base classes. |
| 3372 | // |
| 3373 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3374 | // an unrelated class, and for us to find an overload set including |
| 3375 | // decls from classes which are not superclasses, as long as the decl |
| 3376 | // we actually pick through overload resolution is from a superclass. |
| 3377 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3378 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3379 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3380 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3381 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3382 | if (!BaseRT) { |
| 3383 | // We can't check this yet because the base type is still |
| 3384 | // dependent. |
| 3385 | assert(BaseType->isDependentType()); |
| 3386 | return false; |
| 3387 | } |
| 3388 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3389 | |
| 3390 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3391 | // If this is an implicit member reference and we find a |
| 3392 | // non-instance member, it's not an error. |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3393 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3394 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3395 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3396 | // 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] | 3397 | DeclContext *DC = (*I)->getDeclContext(); |
| 3398 | while (DC->isTransparentContext()) |
| 3399 | DC = DC->getParent(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 9d4bb94 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3401 | if (!DC->isRecord()) |
| 3402 | continue; |
| 3403 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3404 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 0246376 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3405 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3408 | return false; |
| 3409 | } |
| 3410 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3411 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3412 | R.getRepresentativeDecl(), |
| 3413 | R.getLookupNameInfo()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3414 | return true; |
| 3415 | } |
| 3416 | |
| 3417 | static bool |
| 3418 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3419 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3420 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3421 | bool HasTemplateArgs) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3422 | RecordDecl *RDecl = RTy->getDecl(); |
| 3423 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3424 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3425 | << BaseRange)) |
| 3426 | return true; |
| 3427 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3428 | if (HasTemplateArgs) { |
| 3429 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3430 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3431 | |
| 3432 | bool MOUS; |
| 3433 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3434 | return false; |
| 3435 | } |
| 3436 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3437 | DeclContext *DC = RDecl; |
| 3438 | if (SS.isSet()) { |
| 3439 | // If the member name was a qualified-id, look into the |
| 3440 | // nested-name-specifier. |
| 3441 | DC = SemaRef.computeDeclContext(SS, false); |
| 3442 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3443 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3444 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3445 | << SS.getRange() << DC; |
| 3446 | return true; |
| 3447 | } |
| 3448 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3449 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3450 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3451 | if (!isa<TypeDecl>(DC)) { |
| 3452 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3453 | << DC << SS.getRange(); |
| 3454 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3455 | } |
| 3456 | } |
| 3457 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3458 | // The record definition is complete, now look up the member. |
| 3459 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3461 | if (!R.empty()) |
| 3462 | return false; |
| 3463 | |
| 3464 | // We didn't find anything with the given name, so try to correct |
| 3465 | // for typos. |
| 3466 | DeclarationName Name = R.getLookupName(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3467 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3468 | !R.empty() && |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3469 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3470 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3471 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3472 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3473 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3474 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3475 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3476 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3477 | return false; |
| 3478 | } else { |
| 3479 | R.clear(); |
Douglas Gregor | 12eb5d6 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3480 | R.setLookupName(Name); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3483 | return false; |
| 3484 | } |
| 3485 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3486 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3487 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3488 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3489 | CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3490 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3491 | const DeclarationNameInfo &NameInfo, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3492 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3493 | if (BaseType->isDependentType() || |
| 3494 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3495 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3496 | IsArrow, OpLoc, |
| 3497 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3498 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3499 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3500 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3501 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3502 | // Implicit member accesses. |
| 3503 | if (!Base) { |
| 3504 | QualType RecordTy = BaseType; |
| 3505 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3506 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3507 | RecordTy->getAs<RecordType>(), |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3508 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3509 | return ExprError(); |
| 3510 | |
| 3511 | // Explicit member accesses. |
| 3512 | } else { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3513 | ExprResult Result = |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3514 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3515 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3516 | |
| 3517 | if (Result.isInvalid()) { |
| 3518 | Owned(Base); |
| 3519 | return ExprError(); |
| 3520 | } |
| 3521 | |
| 3522 | if (Result.get()) |
| 3523 | return move(Result); |
Sebastian Redl | f3e6337 | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3524 | |
| 3525 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3526 | BaseType = Base->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3527 | } |
| 3528 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3529 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3530 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3531 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3534 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3535 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3536 | SourceLocation OpLoc, bool IsArrow, |
| 3537 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3538 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3539 | LookupResult &R, |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3540 | const TemplateArgumentListInfo *TemplateArgs, |
| 3541 | bool SuppressQualifierCheck) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3542 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3543 | if (IsArrow) { |
| 3544 | assert(BaseType->isPointerType()); |
| 3545 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3546 | } |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3547 | R.setBaseObjectType(BaseType); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3548 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3549 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3550 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3551 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3552 | |
| 3553 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3554 | return ExprError(); |
| 3555 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3556 | if (R.empty()) { |
| 3557 | // Rederive where we looked up. |
| 3558 | DeclContext *DC = (SS.isSet() |
| 3559 | ? computeDeclContext(SS, false) |
| 3560 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3561 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3562 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3563 | << MemberName << DC |
| 3564 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3565 | return ExprError(); |
| 3566 | } |
| 3567 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3568 | // Diagnose lookups that find only declarations from a non-base |
| 3569 | // type. This is possible for either qualified lookups (which may |
| 3570 | // have been qualified with an unrelated type) or implicit member |
| 3571 | // expressions (which were found with unqualified lookup and thus |
| 3572 | // may have come from an enclosing scope). Note that it's okay for |
| 3573 | // lookup to find declarations from a non-base type as long as those |
| 3574 | // aren't the ones picked by overload resolution. |
| 3575 | if ((SS.isSet() || !BaseExpr || |
| 3576 | (isa<CXXThisExpr>(BaseExpr) && |
| 3577 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | 06a9f36 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3578 | !SuppressQualifierCheck && |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3579 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3580 | return ExprError(); |
| 3581 | |
| 3582 | // Construct an unresolved result if we in fact got an unresolved |
| 3583 | // result. |
| 3584 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3585 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3586 | // pick a member. |
| 3587 | R.suppressDiagnostics(); |
| 3588 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3589 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3590 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3591 | BaseExpr, BaseExprType, |
| 3592 | IsArrow, OpLoc, |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3593 | SS.getWithLocInContext(Context), |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3594 | MemberNameInfo, |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3595 | TemplateArgs, R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3596 | |
| 3597 | return Owned(MemExpr); |
| 3598 | } |
| 3599 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3600 | assert(R.isSingleResult()); |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3601 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3602 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 3603 | |
| 3604 | // FIXME: diagnose the presence of template arguments now. |
| 3605 | |
| 3606 | // If the decl being referenced had an error, return an error for this |
| 3607 | // sub-expr without emitting another error, in order to avoid cascading |
| 3608 | // error cases. |
| 3609 | if (MemberDecl->isInvalidDecl()) |
| 3610 | return ExprError(); |
| 3611 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3612 | // Handle the implicit-member-access case. |
| 3613 | if (!BaseExpr) { |
| 3614 | // 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] | 3615 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3616 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3617 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 3618 | SourceLocation Loc = R.getNameLoc(); |
| 3619 | if (SS.getRange().isValid()) |
| 3620 | Loc = SS.getRange().getBegin(); |
| 3621 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3624 | bool ShouldCheckUse = true; |
| 3625 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 3626 | // Don't diagnose the use of a virtual member function unless it's |
| 3627 | // explicitly qualified. |
| 3628 | if (MD->isVirtual() && !SS.isSet()) |
| 3629 | ShouldCheckUse = false; |
| 3630 | } |
| 3631 | |
| 3632 | // Check the use of this member. |
| 3633 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 3634 | Owned(BaseExpr); |
| 3635 | return ExprError(); |
| 3636 | } |
| 3637 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3638 | // Perform a property load on the base regardless of whether we |
| 3639 | // actually need it for the declaration. |
| 3640 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) |
| 3641 | ConvertPropertyForRValue(BaseExpr); |
| 3642 | |
John McCall | dfa1edb | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3643 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 3644 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 3645 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3646 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3647 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 3648 | // We may have found a field within an anonymous union or struct |
| 3649 | // (C++ [class.union]). |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3650 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3651 | BaseExpr, OpLoc); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3652 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3653 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 3654 | MarkDeclarationReferenced(MemberLoc, Var); |
| 3655 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3656 | Var, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3657 | Var->getType().getNonReferenceType(), |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3658 | VK_LValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3661 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3662 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3663 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3664 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3665 | MemberFn->getType(), |
| 3666 | MemberFn->isInstance() ? VK_RValue : VK_LValue, |
| 3667 | OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3668 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3669 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3670 | |
| 3671 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 3672 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3673 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3674 | Enum, FoundDecl, MemberNameInfo, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3675 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | Owned(BaseExpr); |
| 3679 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3680 | // We found something that we didn't expect. Complain. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3681 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3682 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3683 | << MemberName << BaseType << int(IsArrow); |
| 3684 | else |
| 3685 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 3686 | << MemberName << BaseType << int(IsArrow); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3688 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 3689 | << MemberName; |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3690 | R.suppressDiagnostics(); |
Douglas Gregor | b0fd483 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3691 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3694 | /// Given that normal member access failed on the given expression, |
| 3695 | /// and given that the expression's type involves builtin-id or |
| 3696 | /// builtin-Class, decide whether substituting in the redefinition |
| 3697 | /// types would be profitable. The redefinition type is whatever |
| 3698 | /// this translation unit tried to typedef to id/Class; we store |
| 3699 | /// it to the side and then re-use it in places like this. |
| 3700 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, Expr *&base) { |
| 3701 | const ObjCObjectPointerType *opty |
| 3702 | = base->getType()->getAs<ObjCObjectPointerType>(); |
| 3703 | if (!opty) return false; |
| 3704 | |
| 3705 | const ObjCObjectType *ty = opty->getObjectType(); |
| 3706 | |
| 3707 | QualType redef; |
| 3708 | if (ty->isObjCId()) { |
| 3709 | redef = S.Context.ObjCIdRedefinitionType; |
| 3710 | } else if (ty->isObjCClass()) { |
| 3711 | redef = S.Context.ObjCClassRedefinitionType; |
| 3712 | } else { |
| 3713 | return false; |
| 3714 | } |
| 3715 | |
| 3716 | // Do the substitution as long as the redefinition type isn't just a |
| 3717 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 3718 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 3719 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 3720 | return false; |
| 3721 | |
| 3722 | S.ImpCastExprToType(base, redef, CK_BitCast); |
| 3723 | return true; |
| 3724 | } |
| 3725 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3726 | /// Look up the given member of the given non-type-dependent |
| 3727 | /// expression. This can return in one of two ways: |
| 3728 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 3729 | /// assume that lookup was performed and the results written into |
| 3730 | /// the provided structure. It will take over from there. |
| 3731 | /// * Otherwise, the returned expression will be produced in place of |
| 3732 | /// an ordinary member expression. |
| 3733 | /// |
| 3734 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 3735 | /// fixed for ObjC++. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3736 | ExprResult |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3737 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3738 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3739 | CXXScopeSpec &SS, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3740 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3741 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3742 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 3743 | // Perform default conversions. |
| 3744 | DefaultFunctionArrayConversion(BaseExpr); |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3745 | if (IsArrow) DefaultLvalueConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3746 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 3747 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3748 | assert(!BaseType->isDependentType()); |
| 3749 | |
| 3750 | DeclarationName MemberName = R.getLookupName(); |
| 3751 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3752 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3753 | // For later type-checking purposes, turn arrow accesses into dot |
| 3754 | // accesses. The only access type we support that doesn't follow |
| 3755 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 3756 | // and those never use arrows, so this is unaffected. |
| 3757 | if (IsArrow) { |
| 3758 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3759 | BaseType = Ptr->getPointeeType(); |
| 3760 | else if (const ObjCObjectPointerType *Ptr |
| 3761 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3762 | BaseType = Ptr->getPointeeType(); |
| 3763 | else if (BaseType->isRecordType()) { |
| 3764 | // Recover from arrow accesses to records, e.g.: |
| 3765 | // struct MyRecord foo; |
| 3766 | // foo->bar |
| 3767 | // This is actually well-formed in C++ if MyRecord has an |
| 3768 | // overloaded operator->, but that should have been dealt with |
| 3769 | // by now. |
| 3770 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3771 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 3772 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 3773 | IsArrow = false; |
| 3774 | } else { |
| 3775 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 3776 | << BaseType << BaseExpr->getSourceRange(); |
| 3777 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3781 | // Handle field access to simple records. |
| 3782 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| 3783 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 3784 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 3785 | return ExprError(); |
| 3786 | |
| 3787 | // Returning valid-but-null is how we indicate to the caller that |
| 3788 | // the lookup result was filled in. |
| 3789 | return Owned((Expr*) 0); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3790 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3791 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3792 | // Handle ivar access to Objective-C objects. |
| 3793 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3794 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3795 | |
| 3796 | // There are three cases for the base type: |
| 3797 | // - builtin id (qualified or unqualified) |
| 3798 | // - builtin Class (qualified or unqualified) |
| 3799 | // - an interface |
| 3800 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 3801 | if (!IDecl) { |
| 3802 | // There's an implicit 'isa' ivar on all objects. |
| 3803 | // But we only actually find it this way on objects of type 'id', |
| 3804 | // apparently. |
| 3805 | if (OTy->isObjCId() && Member->isStr("isa")) |
| 3806 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, IsArrow, MemberLoc, |
| 3807 | Context.getObjCClassType())); |
| 3808 | |
| 3809 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3810 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3811 | ObjCImpDecl, HasTemplateArgs); |
| 3812 | goto fail; |
| 3813 | } |
| 3814 | |
| 3815 | ObjCInterfaceDecl *ClassDeclared; |
| 3816 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 3817 | |
| 3818 | if (!IV) { |
| 3819 | // Attempt to correct for typos in ivar names. |
| 3820 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3821 | LookupMemberName); |
| 3822 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 3823 | IsArrow ? CTC_ObjCIvarLookup |
| 3824 | : CTC_ObjCPropertyLookup) && |
| 3825 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 3826 | Diag(R.getNameLoc(), |
| 3827 | diag::err_typecheck_member_reference_ivar_suggest) |
| 3828 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 3829 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3830 | IV->getNameAsString()); |
| 3831 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 3832 | << IV->getDeclName(); |
| 3833 | } else { |
| 3834 | Res.clear(); |
| 3835 | Res.setLookupName(Member); |
| 3836 | |
| 3837 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 3838 | << IDecl->getDeclName() << MemberName |
| 3839 | << BaseExpr->getSourceRange(); |
| 3840 | return ExprError(); |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | // If the decl being referenced had an error, return an error for this |
| 3845 | // sub-expr without emitting another error, in order to avoid cascading |
| 3846 | // error cases. |
| 3847 | if (IV->isInvalidDecl()) |
| 3848 | return ExprError(); |
| 3849 | |
| 3850 | // Check whether we can reference this field. |
| 3851 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 3852 | return ExprError(); |
| 3853 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 3854 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 3855 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 3856 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 3857 | ClassOfMethodDecl = MD->getClassInterface(); |
| 3858 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 3859 | // Case of a c-function declared inside an objc implementation. |
| 3860 | // FIXME: For a c-style function nested inside an objc implementation |
| 3861 | // class, there is no implementation context available, so we pass |
| 3862 | // down the context as argument to this routine. Ideally, this context |
| 3863 | // need be passed down in the AST node and somehow calculated from the |
| 3864 | // AST for a function decl. |
| 3865 | if (ObjCImplementationDecl *IMPD = |
| 3866 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 3867 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 3868 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 3869 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 3870 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 3871 | } |
| 3872 | |
| 3873 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 3874 | if (ClassDeclared != IDecl || |
| 3875 | ClassOfMethodDecl != ClassDeclared) |
| 3876 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 3877 | << IV->getDeclName(); |
| 3878 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 3879 | // @protected |
| 3880 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 3881 | << IV->getDeclName(); |
| 3882 | } |
| 3883 | |
| 3884 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 3885 | MemberLoc, BaseExpr, |
| 3886 | IsArrow)); |
| 3887 | } |
| 3888 | |
| 3889 | // Objective-C property access. |
| 3890 | const ObjCObjectPointerType *OPT; |
| 3891 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 3892 | // This actually uses the base as an r-value. |
| 3893 | DefaultLvalueConversion(BaseExpr); |
| 3894 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr->getType())); |
| 3895 | |
| 3896 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3897 | |
| 3898 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 3899 | |
| 3900 | // id, with and without qualifiers. |
| 3901 | if (OT->isObjCId()) { |
| 3902 | // Check protocols on qualified interfaces. |
| 3903 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 3904 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 3905 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 3906 | // Check the use of this declaration |
| 3907 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3908 | return ExprError(); |
| 3909 | |
| 3910 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 3911 | VK_LValue, |
| 3912 | OK_ObjCProperty, |
| 3913 | MemberLoc, |
| 3914 | BaseExpr)); |
| 3915 | } |
| 3916 | |
| 3917 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3918 | // Check the use of this method. |
| 3919 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3920 | return ExprError(); |
| 3921 | Selector SetterSel = |
| 3922 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3923 | PP.getSelectorTable(), Member); |
| 3924 | ObjCMethodDecl *SMD = 0; |
| 3925 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 3926 | SetterSel, Context)) |
| 3927 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 3928 | QualType PType = OMD->getSendResultType(); |
| 3929 | |
| 3930 | ExprValueKind VK = VK_LValue; |
| 3931 | if (!getLangOptions().CPlusPlus && |
| 3932 | IsCForbiddenLValueType(Context, PType)) |
| 3933 | VK = VK_RValue; |
| 3934 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 3935 | |
| 3936 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 3937 | VK, OK, |
| 3938 | MemberLoc, BaseExpr)); |
| 3939 | } |
| 3940 | } |
Fariborz Jahanian | 4eb7f69 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 3941 | // Use of id.member can only be for a property reference. Do not |
| 3942 | // use the 'id' redefinition in this case. |
| 3943 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3944 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3945 | ObjCImpDecl, HasTemplateArgs); |
| 3946 | |
| 3947 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 3948 | << MemberName << BaseType); |
| 3949 | } |
| 3950 | |
| 3951 | // 'Class', unqualified only. |
| 3952 | if (OT->isObjCClass()) { |
| 3953 | // Only works in a method declaration (??!). |
| 3954 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 3955 | if (!MD) { |
| 3956 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3957 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3958 | ObjCImpDecl, HasTemplateArgs); |
| 3959 | |
| 3960 | goto fail; |
| 3961 | } |
| 3962 | |
| 3963 | // Also must look for a getter name which uses property syntax. |
| 3964 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3965 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 3966 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3967 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 3968 | // Check the use of this method. |
| 3969 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3970 | return ExprError(); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3971 | } else |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3972 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3973 | // If we found a getter then this may be a valid dot-reference, we |
| 3974 | // will look for the matching setter, in case it is needed. |
| 3975 | Selector SetterSel = |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3976 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3977 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3978 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 3979 | if (!Setter) { |
| 3980 | // If this reference is in an @implementation, also check for 'private' |
| 3981 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3982 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3983 | } |
| 3984 | // Look through local category implementations associated with the class. |
| 3985 | if (!Setter) |
| 3986 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3987 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3988 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3989 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3990 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3991 | if (Getter || Setter) { |
| 3992 | QualType PType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3993 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3994 | ExprValueKind VK = VK_LValue; |
| 3995 | if (Getter) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3996 | PType = Getter->getSendResultType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3997 | if (!getLangOptions().CPlusPlus && |
| 3998 | IsCForbiddenLValueType(Context, PType)) |
| 3999 | VK = VK_RValue; |
| 4000 | } else { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4001 | // Get the expression type from Setter's incoming parameter. |
| 4002 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4003 | } |
| 4004 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4005 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4006 | // FIXME: we must check that the setter has property type. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4007 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4008 | PType, VK, OK, |
| 4009 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4010 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4011 | |
| 4012 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4013 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4014 | ObjCImpDecl, HasTemplateArgs); |
| 4015 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4016 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4017 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4018 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4019 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4020 | // Normal property access. |
| 4021 | return HandleExprPropertyRefExpr(OPT, BaseExpr, MemberName, MemberLoc, |
| 4022 | SourceLocation(), QualType(), false); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4023 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4024 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4025 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4026 | if (BaseType->isExtVectorType()) { |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4027 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4028 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4029 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr->getValueKind()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4030 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4031 | Member, MemberLoc); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4032 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4033 | return ExprError(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4034 | |
| 4035 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr, |
| 4036 | *Member, MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4037 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4038 | |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4039 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4040 | // not just a pointer to builtin-sel again. |
| 4041 | if (IsArrow && |
| 4042 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4043 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
| 4044 | ImpCastExprToType(BaseExpr, Context.ObjCSelRedefinitionType, CK_BitCast); |
| 4045 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4046 | ObjCImpDecl, HasTemplateArgs); |
| 4047 | } |
| 4048 | |
| 4049 | // Failure cases. |
| 4050 | fail: |
| 4051 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4052 | // Recover from dot accesses to pointers, e.g.: |
| 4053 | // type *foo; |
| 4054 | // foo.bar |
| 4055 | // This is actually well-formed in two cases: |
| 4056 | // - 'type' is an Objective C type |
| 4057 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4058 | // the appropriate pointer type |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4059 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4060 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4061 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4062 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4063 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 4064 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4065 | |
| 4066 | // Recurse as an -> access. |
| 4067 | IsArrow = true; |
| 4068 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4069 | ObjCImpDecl, HasTemplateArgs); |
| 4070 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4073 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4074 | // because they forgot parentheses to call that function. |
| 4075 | bool TryCall = false; |
| 4076 | bool Overloaded = false; |
| 4077 | UnresolvedSet<8> AllOverloads; |
| 4078 | if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(BaseExpr)) { |
| 4079 | AllOverloads.append(Overloads->decls_begin(), Overloads->decls_end()); |
| 4080 | TryCall = true; |
| 4081 | Overloaded = true; |
| 4082 | } else if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(BaseExpr)) { |
| 4083 | if (FunctionDecl* Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 4084 | AllOverloads.addDecl(Fun); |
| 4085 | TryCall = true; |
| 4086 | } |
| 4087 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4088 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4089 | if (TryCall) { |
| 4090 | // Plunder the overload set for something that would make the member |
| 4091 | // expression valid. |
| 4092 | UnresolvedSet<4> ViableOverloads; |
| 4093 | bool HasViableZeroArgOverload = false; |
| 4094 | for (OverloadExpr::decls_iterator it = AllOverloads.begin(), |
| 4095 | DeclsEnd = AllOverloads.end(); it != DeclsEnd; ++it) { |
Matt Beaumont-Gay | fbe5994 | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4096 | // Our overload set may include TemplateDecls, which we'll ignore for the |
| 4097 | // purposes of determining whether we can issue a '()' fixit. |
| 4098 | if (const FunctionDecl *OverloadDecl = dyn_cast<FunctionDecl>(*it)) { |
| 4099 | QualType ResultTy = OverloadDecl->getResultType(); |
| 4100 | if ((!IsArrow && ResultTy->isRecordType()) || |
| 4101 | (IsArrow && ResultTy->isPointerType() && |
| 4102 | ResultTy->getPointeeType()->isRecordType())) { |
| 4103 | ViableOverloads.addDecl(*it); |
| 4104 | if (OverloadDecl->getMinRequiredArguments() == 0) { |
| 4105 | HasViableZeroArgOverload = true; |
| 4106 | } |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4107 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4108 | } |
| 4109 | } |
| 4110 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4111 | if (!HasViableZeroArgOverload || ViableOverloads.size() != 1) { |
| 4112 | Diag(BaseExpr->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | fbe5994 | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4113 | << (AllOverloads.size() > 1) << 0 |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4114 | << BaseExpr->getSourceRange(); |
| 4115 | int ViableOverloadCount = ViableOverloads.size(); |
| 4116 | int I; |
| 4117 | for (I = 0; I < ViableOverloadCount; ++I) { |
| 4118 | // FIXME: Magic number for max shown overloads stolen from |
| 4119 | // OverloadCandidateSet::NoteCandidates. |
| 4120 | if (I >= 4 && Diags.getShowOverloads() == Diagnostic::Ovl_Best) { |
| 4121 | break; |
| 4122 | } |
| 4123 | Diag(ViableOverloads[I].getDecl()->getSourceRange().getBegin(), |
| 4124 | diag::note_member_ref_possible_intended_overload); |
| 4125 | } |
| 4126 | if (I != ViableOverloadCount) { |
| 4127 | Diag(BaseExpr->getExprLoc(), diag::note_ovl_too_many_candidates) |
| 4128 | << int(ViableOverloadCount - I); |
| 4129 | } |
| 4130 | return ExprError(); |
| 4131 | } |
| 4132 | } else { |
| 4133 | // We don't have an expression that's convenient to get a Decl from, but we |
| 4134 | // can at least check if the type is "function of 0 arguments which returns |
| 4135 | // an acceptable type". |
| 4136 | const FunctionType *Fun = NULL; |
| 4137 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 4138 | if ((Fun = Ptr->getPointeeType()->getAs<FunctionType>())) { |
| 4139 | TryCall = true; |
| 4140 | } |
| 4141 | } else if ((Fun = BaseType->getAs<FunctionType>())) { |
| 4142 | TryCall = true; |
| 4143 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4144 | |
| 4145 | if (TryCall) { |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4146 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Fun)) { |
| 4147 | if (FPT->getNumArgs() == 0) { |
| 4148 | QualType ResultTy = Fun->getResultType(); |
| 4149 | TryCall = (!IsArrow && ResultTy->isRecordType()) || |
| 4150 | (IsArrow && ResultTy->isPointerType() && |
| 4151 | ResultTy->getPointeeType()->isRecordType()); |
| 4152 | } |
Matt Beaumont-Gay | 26ae5dd | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 4153 | } |
John McCall | 028d397 | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
| 4156 | |
Matt Beaumont-Gay | 65b34d7 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4157 | if (TryCall) { |
| 4158 | // At this point, we know BaseExpr looks like it's potentially callable with |
| 4159 | // 0 arguments, and that it returns something of a reasonable type, so we |
| 4160 | // can emit a fixit and carry on pretending that BaseExpr was actually a |
| 4161 | // CallExpr. |
| 4162 | SourceLocation ParenInsertionLoc = |
| 4163 | PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 4164 | Diag(BaseExpr->getExprLoc(), diag::err_member_reference_needs_call) |
| 4165 | << int(Overloaded) << 1 |
| 4166 | << BaseExpr->getSourceRange() |
| 4167 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4168 | ExprResult NewBase = ActOnCallExpr(0, BaseExpr, ParenInsertionLoc, |
| 4169 | MultiExprArg(*this, 0, 0), |
| 4170 | ParenInsertionLoc); |
| 4171 | if (NewBase.isInvalid()) |
| 4172 | return ExprError(); |
| 4173 | BaseExpr = NewBase.takeAs<Expr>(); |
| 4174 | DefaultFunctionArrayConversion(BaseExpr); |
| 4175 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4176 | ObjCImpDecl, HasTemplateArgs); |
| 4177 | } |
| 4178 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4179 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 4180 | << BaseType << BaseExpr->getSourceRange(); |
| 4181 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4182 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4185 | /// The main callback when the parser finds something like |
| 4186 | /// expression . [nested-name-specifier] identifier |
| 4187 | /// expression -> [nested-name-specifier] identifier |
| 4188 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4189 | /// possibilities, including destructor and operator references. |
| 4190 | /// |
| 4191 | /// \param OpKind either tok::arrow or tok::period |
| 4192 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4193 | /// is used to diagnose mis-uses of special members that can |
| 4194 | /// only be called |
| 4195 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4196 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4197 | /// aren't properly put in the context chain |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4198 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 5e3c67b | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4199 | SourceLocation OpLoc, |
| 4200 | tok::TokenKind OpKind, |
| 4201 | CXXScopeSpec &SS, |
| 4202 | UnqualifiedId &Id, |
| 4203 | Decl *ObjCImpDecl, |
| 4204 | bool HasTrailingLParen) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4205 | if (SS.isSet() && SS.isInvalid()) |
| 4206 | return ExprError(); |
| 4207 | |
Francois Pichet | dbee341 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4208 | // Warn about the explicit constructor calls Microsoft extension. |
| 4209 | if (getLangOptions().Microsoft && |
| 4210 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4211 | Diag(Id.getSourceRange().getBegin(), |
| 4212 | diag::ext_ms_explicit_constructor_call); |
| 4213 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4214 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4215 | |
| 4216 | // Decompose the name into its component parts. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4217 | DeclarationNameInfo NameInfo; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4218 | const TemplateArgumentListInfo *TemplateArgs; |
| 4219 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4220 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4221 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4222 | DeclarationName Name = NameInfo.getName(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4223 | bool IsArrow = (OpKind == tok::arrow); |
| 4224 | |
| 4225 | NamedDecl *FirstQualifierInScope |
| 4226 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4227 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4228 | |
| 4229 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4230 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4231 | if (Result.isInvalid()) return ExprError(); |
| 4232 | Base = Result.take(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4233 | |
Douglas Gregor | 01e56ae | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4234 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4235 | isDependentScopeSpecifier(SS)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4236 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4237 | IsArrow, OpLoc, |
| 4238 | SS, FirstQualifierInScope, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4239 | NameInfo, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4240 | } else { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4241 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4242 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 4243 | SS, ObjCImpDecl, TemplateArgs != 0); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4244 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4245 | if (Result.isInvalid()) { |
| 4246 | Owned(Base); |
| 4247 | return ExprError(); |
| 4248 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4249 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4250 | if (Result.get()) { |
| 4251 | // The only way a reference to a destructor can be used is to |
| 4252 | // immediately call it, which falls into this case. If the |
| 4253 | // next token is not a '(', produce a diagnostic and build the |
| 4254 | // call now. |
| 4255 | if (!HasTrailingLParen && |
| 4256 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4257 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4258 | |
John McCall | ad00b77 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4259 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4262 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4263 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4264 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4270 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4271 | FunctionDecl *FD, |
| 4272 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4273 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4274 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4275 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4276 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4277 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4278 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4279 | return ExprError(); |
| 4280 | } |
| 4281 | |
| 4282 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4283 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4284 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4285 | // Instantiate the expression. |
| 4286 | MultiLevelTemplateArgumentList ArgList |
| 4287 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4288 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4289 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4290 | = ArgList.getInnermost(); |
| 4291 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4292 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4293 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4294 | ExprResult Result; |
| 4295 | { |
| 4296 | // C++ [dcl.fct.default]p5: |
| 4297 | // The names in the [default argument] expression are bound, and |
| 4298 | // the semantic constraints are checked, at the point where the |
| 4299 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4300 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4301 | Result = SubstExpr(UninstExpr, ArgList); |
| 4302 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4303 | if (Result.isInvalid()) |
| 4304 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4305 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4306 | // Check the expression as an initializer for the parameter. |
| 4307 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4308 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4309 | InitializationKind Kind |
| 4310 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4311 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4312 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4314 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4315 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4316 | MultiExprArg(*this, &ResultE, 1)); |
| 4317 | if (Result.isInvalid()) |
| 4318 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4319 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4320 | // Build the default argument expression. |
| 4321 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4322 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4325 | // If the default expression creates temporaries, we need to |
| 4326 | // push them to the current stack of expression temporaries so they'll |
| 4327 | // be properly destroyed. |
| 4328 | // FIXME: We should really be rebuilding the default argument with new |
| 4329 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4330 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4331 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4332 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4333 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4334 | ExprTemporaries.push_back(Temporary); |
| 4335 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4336 | |
| 4337 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4338 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4339 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4340 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4341 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4344 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4345 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4346 | /// function prototype Proto. Call is the call expression itself, and |
| 4347 | /// Fn is the function expression. For a C++ member function, this |
| 4348 | /// routine does not attempt to convert the object argument. Returns |
| 4349 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4350 | bool |
| 4351 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4352 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4353 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4354 | Expr **Args, unsigned NumArgs, |
| 4355 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4356 | // Bail out early if calling a builtin with custom typechecking. |
| 4357 | // We don't need to do this in the |
| 4358 | if (FDecl) |
| 4359 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4360 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4361 | return false; |
| 4362 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4363 | // 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] | 4364 | // assignment, to the types of the corresponding parameter, ... |
| 4365 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4366 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4368 | // If too few arguments are available (and we don't have default |
| 4369 | // arguments for the remaining parameters), don't make the call. |
| 4370 | if (NumArgs < NumArgsInProto) { |
| 4371 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4372 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4373 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4374 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4375 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | // If too many are passed and not variadic, error on the extras and drop |
| 4379 | // them. |
| 4380 | if (NumArgs > NumArgsInProto) { |
| 4381 | if (!Proto->isVariadic()) { |
| 4382 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4383 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4384 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4385 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4386 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4387 | Args[NumArgs-1]->getLocEnd()); |
| 4388 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4389 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4390 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4391 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4392 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4393 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4394 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4395 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4396 | if (Fn->getType()->isBlockPointerType()) |
| 4397 | CallType = VariadicBlock; // Block |
| 4398 | else if (isa<MemberExpr>(Fn)) |
| 4399 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4400 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4401 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4402 | if (Invalid) |
| 4403 | return true; |
| 4404 | unsigned TotalNumArgs = AllArgs.size(); |
| 4405 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4406 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4407 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4408 | return false; |
| 4409 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4410 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4411 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4412 | FunctionDecl *FDecl, |
| 4413 | const FunctionProtoType *Proto, |
| 4414 | unsigned FirstProtoArg, |
| 4415 | Expr **Args, unsigned NumArgs, |
| 4416 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4417 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4418 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4419 | unsigned NumArgsToCheck = NumArgs; |
| 4420 | bool Invalid = false; |
| 4421 | if (NumArgs != NumArgsInProto) |
| 4422 | // Use default arguments for missing arguments |
| 4423 | NumArgsToCheck = NumArgsInProto; |
| 4424 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4425 | // 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] | 4426 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4427 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4429 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4430 | if (ArgIx < NumArgs) { |
| 4431 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4432 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4433 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4434 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4435 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4436 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4437 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4438 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4439 | // Pass the argument |
| 4440 | ParmVarDecl *Param = 0; |
| 4441 | if (FDecl && i < FDecl->getNumParams()) |
| 4442 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4443 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4444 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4445 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4446 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4447 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4448 | SourceLocation(), |
| 4449 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4450 | if (ArgE.isInvalid()) |
| 4451 | return true; |
| 4452 | |
| 4453 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4454 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4455 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4456 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4457 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4458 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4459 | if (ArgExpr.isInvalid()) |
| 4460 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4461 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4462 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4463 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4464 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4465 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4466 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4467 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4468 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4469 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4470 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4471 | Expr *Arg = Args[i]; |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4472 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType, FDecl); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4473 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4474 | } |
| 4475 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4476 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4477 | } |
| 4478 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4479 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4480 | /// This provides the location of the left/right parens and a list of comma |
| 4481 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4482 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4483 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4484 | MultiExprArg args, SourceLocation RParenLoc, |
| 4485 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4486 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4487 | |
| 4488 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4489 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4490 | if (Result.isInvalid()) return ExprError(); |
| 4491 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4492 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4493 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4495 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4496 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4497 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4498 | if (NumArgs > 0) { |
| 4499 | // Pseudo-destructor calls should not have any arguments. |
| 4500 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4501 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4502 | SourceRange(Args[0]->getLocStart(), |
| 4503 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4505 | NumArgs = 0; |
| 4506 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4508 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4509 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4510 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4511 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4512 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4513 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4514 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4515 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4516 | bool Dependent = false; |
| 4517 | if (Fn->isTypeDependent()) |
| 4518 | Dependent = true; |
| 4519 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4520 | Dependent = true; |
| 4521 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4522 | if (Dependent) { |
| 4523 | if (ExecConfig) { |
| 4524 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4525 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4526 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4527 | } else { |
| 4528 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4529 | Context.DependentTy, VK_RValue, |
| 4530 | RParenLoc)); |
| 4531 | } |
| 4532 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4533 | |
| 4534 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4535 | if (Fn->getType()->isRecordType()) |
| 4536 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4537 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4538 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4539 | Expr *NakedFn = Fn->IgnoreParens(); |
| 4540 | |
| 4541 | // Determine whether this is a call to an unresolved member function. |
| 4542 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4543 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 4544 | // an unresolved using declaration), it has to be an overloaded |
| 4545 | // function set, which means it must contain either multiple |
| 4546 | // declarations (all methods or method templates) or a single |
| 4547 | // method template. |
| 4548 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 2b147f0 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4549 | isa<FunctionTemplateDecl>( |
| 4550 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 4551 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4552 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4553 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4554 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4555 | } |
| 4556 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4557 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4558 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4559 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4560 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4561 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4562 | RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4563 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4564 | |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4565 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4566 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4567 | if (BO->getOpcode() == BO_PtrMemD || |
| 4568 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | 5f970ee | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 4569 | if (const FunctionProtoType *FPT |
| 4570 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4571 | QualType ResultTy = FPT->getCallResultType(Context); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4572 | ExprValueKind VK = Expr::getValueKindForType(FPT->getResultType()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | fdc13a0 | 2011-02-04 12:57:49 +0000 | [diff] [blame] | 4574 | // Check that the object type isn't more qualified than the |
| 4575 | // member function we're calling. |
| 4576 | Qualifiers FuncQuals = Qualifiers::fromCVRMask(FPT->getTypeQuals()); |
| 4577 | Qualifiers ObjectQuals |
| 4578 | = BO->getOpcode() == BO_PtrMemD |
| 4579 | ? BO->getLHS()->getType().getQualifiers() |
| 4580 | : BO->getLHS()->getType()->getAs<PointerType>() |
| 4581 | ->getPointeeType().getQualifiers(); |
| 4582 | |
| 4583 | Qualifiers Difference = ObjectQuals - FuncQuals; |
| 4584 | Difference.removeObjCGCAttr(); |
| 4585 | Difference.removeAddressSpace(); |
| 4586 | if (Difference) { |
| 4587 | std::string QualsString = Difference.getAsString(); |
| 4588 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 4589 | << BO->getType().getUnqualifiedType() |
| 4590 | << QualsString |
| 4591 | << (QualsString.find(' ') == std::string::npos? 1 : 2); |
| 4592 | } |
| 4593 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4594 | CXXMemberCallExpr *TheCall |
Abramo Bagnara | 6c572f1 | 2010-12-03 21:39:42 +0000 | [diff] [blame] | 4595 | = new (Context) CXXMemberCallExpr(Context, Fn, Args, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4596 | NumArgs, ResultTy, VK, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4597 | RParenLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4598 | |
| 4599 | if (CheckCallReturnType(FPT->getResultType(), |
| 4600 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4601 | TheCall, 0)) |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4602 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 4603 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4604 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4605 | RParenLoc)) |
| 4606 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4607 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4608 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4609 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4610 | } |
| 4611 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4614 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4615 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4616 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4617 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4618 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4619 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 4620 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 4621 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4622 | RParenLoc, ExecConfig); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4625 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4626 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4627 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4628 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4629 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4630 | if (isa<DeclRefExpr>(NakedFn)) |
| 4631 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 4632 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4633 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 4634 | ExecConfig); |
| 4635 | } |
| 4636 | |
| 4637 | ExprResult |
| 4638 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 4639 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 4640 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4641 | if (!ConfigDecl) |
| 4642 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4643 | << "cudaConfigureCall"); |
| 4644 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4645 | |
| 4646 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 4647 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 4648 | |
| 4649 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4652 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4653 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4654 | /// unary-convert to an expression of function-pointer or |
| 4655 | /// block-pointer type. |
| 4656 | /// |
| 4657 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4658 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4659 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4660 | SourceLocation LParenLoc, |
| 4661 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4662 | SourceLocation RParenLoc, |
| 4663 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4664 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 4665 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4666 | // Promote the function operand. |
| 4667 | UsualUnaryConversions(Fn); |
| 4668 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4669 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4670 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4671 | CallExpr *TheCall; |
| 4672 | if (Config) { |
| 4673 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4674 | cast<CallExpr>(Config), |
| 4675 | Args, NumArgs, |
| 4676 | Context.BoolTy, |
| 4677 | VK_RValue, |
| 4678 | RParenLoc); |
| 4679 | } else { |
| 4680 | TheCall = new (Context) CallExpr(Context, Fn, |
| 4681 | Args, NumArgs, |
| 4682 | Context.BoolTy, |
| 4683 | VK_RValue, |
| 4684 | RParenLoc); |
| 4685 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4686 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4687 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 4688 | |
| 4689 | // Bail out early if calling a builtin with custom typechecking. |
| 4690 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 4691 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 4692 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4693 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4694 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4695 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4696 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4697 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4698 | if (FuncT == 0) |
| 4699 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4700 | << Fn->getType() << Fn->getSourceRange()); |
| 4701 | } else if (const BlockPointerType *BPT = |
| 4702 | Fn->getType()->getAs<BlockPointerType>()) { |
| 4703 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 4704 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4705 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4706 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4707 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4708 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4709 | if (getLangOptions().CUDA) { |
| 4710 | if (Config) { |
| 4711 | // CUDA: Kernel calls must be to global functions |
| 4712 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4713 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 4714 | << FDecl->getName() << Fn->getSourceRange()); |
| 4715 | |
| 4716 | // CUDA: Kernel function must have 'void' return type |
| 4717 | if (!FuncT->getResultType()->isVoidType()) |
| 4718 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 4719 | << Fn->getType() << Fn->getSourceRange()); |
| 4720 | } |
| 4721 | } |
| 4722 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4723 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4724 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4725 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4726 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4727 | return ExprError(); |
| 4728 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4729 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4730 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4731 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4733 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4734 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4735 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4736 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4737 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4738 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4740 | if (FDecl) { |
| 4741 | // Check if we have too few/too many template arguments, based |
| 4742 | // on our knowledge of the function definition. |
| 4743 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4744 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4745 | const FunctionProtoType *Proto |
| 4746 | = Def->getType()->getAs<FunctionProtoType>(); |
| 4747 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4748 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4749 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4750 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4751 | |
| 4752 | // If the function we're calling isn't a function prototype, but we have |
| 4753 | // a function prototype from a prior declaratiom, use that prototype. |
| 4754 | if (!FDecl->hasPrototype()) |
| 4755 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4758 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4759 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4760 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4761 | |
| 4762 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4763 | InitializedEntity Entity |
| 4764 | = InitializedEntity::InitializeParameter(Context, |
| 4765 | Proto->getArgType(i)); |
| 4766 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4767 | SourceLocation(), |
| 4768 | Owned(Arg)); |
| 4769 | if (ArgE.isInvalid()) |
| 4770 | return true; |
| 4771 | |
| 4772 | Arg = ArgE.takeAs<Expr>(); |
| 4773 | |
| 4774 | } else { |
| 4775 | DefaultArgumentPromotion(Arg); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4776 | } |
| 4777 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4778 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4779 | Arg->getType(), |
| 4780 | PDiag(diag::err_call_incomplete_argument) |
| 4781 | << Arg->getSourceRange())) |
| 4782 | return ExprError(); |
| 4783 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4784 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4785 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4786 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4788 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4789 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4790 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4791 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4792 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4793 | // Check for sentinels |
| 4794 | if (NDecl) |
| 4795 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4796 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4797 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4798 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4799 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4800 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4802 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4803 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4804 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4805 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4806 | return ExprError(); |
| 4807 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4808 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4809 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4810 | } |
| 4811 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4812 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4813 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4814 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4815 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4816 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4817 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4818 | |
| 4819 | TypeSourceInfo *TInfo; |
| 4820 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4821 | if (!TInfo) |
| 4822 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4823 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4824 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4825 | } |
| 4826 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4827 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4828 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4829 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4830 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4831 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4832 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4833 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 4834 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 4835 | << SourceRange(LParenLoc, |
| 4836 | literalExpr->getSourceRange().getEnd()))) |
| 4837 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4838 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4839 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 4840 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4841 | } else if (!literalType->isDependentType() && |
| 4842 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4843 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4844 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4845 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4846 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4847 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4848 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4849 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4850 | InitializationKind Kind |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4851 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4852 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4853 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4854 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4855 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4856 | &literalType); |
| 4857 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4858 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4859 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4860 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4861 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4862 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4863 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4864 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4865 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4866 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4867 | // In C, compound literals are l-values for some reason. |
| 4868 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 4869 | |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 4870 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4871 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4872 | } |
| 4873 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4874 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4875 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4876 | SourceLocation RBraceLoc) { |
| 4877 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4878 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 4879 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4880 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4881 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4882 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4883 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 4884 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4885 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4886 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4887 | } |
| 4888 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4889 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4890 | /// except the final cast and returning the kind required. |
| 4891 | static CastKind PrepareScalarCast(Sema &S, Expr *&Src, QualType DestTy) { |
| 4892 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4893 | // Also, callers should have filtered out the invalid cases with |
| 4894 | // pointers. Everything else should be possible. |
| 4895 | |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4896 | QualType SrcTy = Src->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4897 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4898 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4899 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4900 | switch (SrcTy->getScalarTypeKind()) { |
| 4901 | case Type::STK_MemberPointer: |
| 4902 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4903 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4904 | case Type::STK_Pointer: |
| 4905 | switch (DestTy->getScalarTypeKind()) { |
| 4906 | case Type::STK_Pointer: |
| 4907 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4908 | CK_AnyPointerToObjCPointerCast : |
| 4909 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4910 | case Type::STK_Bool: |
| 4911 | return CK_PointerToBoolean; |
| 4912 | case Type::STK_Integral: |
| 4913 | return CK_PointerToIntegral; |
| 4914 | case Type::STK_Floating: |
| 4915 | case Type::STK_FloatingComplex: |
| 4916 | case Type::STK_IntegralComplex: |
| 4917 | case Type::STK_MemberPointer: |
| 4918 | llvm_unreachable("illegal cast from pointer"); |
| 4919 | } |
| 4920 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4921 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4922 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4923 | case Type::STK_Integral: |
| 4924 | switch (DestTy->getScalarTypeKind()) { |
| 4925 | case Type::STK_Pointer: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4926 | if (Src->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4927 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4928 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4929 | case Type::STK_Bool: |
| 4930 | return CK_IntegralToBoolean; |
| 4931 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4932 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4933 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4934 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4935 | case Type::STK_IntegralComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4936 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4937 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4938 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4939 | case Type::STK_FloatingComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4940 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4941 | CK_IntegralToFloating); |
| 4942 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4943 | case Type::STK_MemberPointer: |
| 4944 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4945 | } |
| 4946 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4947 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4948 | case Type::STK_Floating: |
| 4949 | switch (DestTy->getScalarTypeKind()) { |
| 4950 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4951 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4952 | case Type::STK_Bool: |
| 4953 | return CK_FloatingToBoolean; |
| 4954 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4955 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4956 | case Type::STK_FloatingComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4957 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4958 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4959 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4960 | case Type::STK_IntegralComplex: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4961 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4962 | CK_FloatingToIntegral); |
| 4963 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4964 | case Type::STK_Pointer: |
| 4965 | llvm_unreachable("valid float->pointer cast?"); |
| 4966 | case Type::STK_MemberPointer: |
| 4967 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4968 | } |
| 4969 | break; |
| 4970 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4971 | case Type::STK_FloatingComplex: |
| 4972 | switch (DestTy->getScalarTypeKind()) { |
| 4973 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4974 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4975 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4976 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4977 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4978 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4979 | if (S.Context.hasSameType(ET, DestTy)) |
| 4980 | return CK_FloatingComplexToReal; |
| 4981 | S.ImpCastExprToType(Src, ET, CK_FloatingComplexToReal); |
| 4982 | return CK_FloatingCast; |
| 4983 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4984 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4985 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4986 | case Type::STK_Integral: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4987 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4988 | CK_FloatingComplexToReal); |
| 4989 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4990 | case Type::STK_Pointer: |
| 4991 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4992 | case Type::STK_MemberPointer: |
| 4993 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4994 | } |
| 4995 | break; |
| 4996 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4997 | case Type::STK_IntegralComplex: |
| 4998 | switch (DestTy->getScalarTypeKind()) { |
| 4999 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5000 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5001 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5002 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5003 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5004 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5005 | if (S.Context.hasSameType(ET, DestTy)) |
| 5006 | return CK_IntegralComplexToReal; |
| 5007 | S.ImpCastExprToType(Src, ET, CK_IntegralComplexToReal); |
| 5008 | return CK_IntegralCast; |
| 5009 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5010 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5011 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5012 | case Type::STK_Floating: |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5013 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5014 | CK_IntegralComplexToReal); |
| 5015 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5016 | case Type::STK_Pointer: |
| 5017 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5018 | case Type::STK_MemberPointer: |
| 5019 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5020 | } |
| 5021 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5022 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5023 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5024 | llvm_unreachable("Unhandled scalar cast"); |
| 5025 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5028 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5029 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 5030 | Expr *&castExpr, CastKind& Kind, ExprValueKind &VK, |
| 5031 | CXXCastPath &BasePath, bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5032 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5033 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 5034 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5035 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5036 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5037 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5038 | // We only support r-value casts in C. |
| 5039 | VK = VK_RValue; |
| 5040 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5041 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5042 | // type needs to be scalar. |
| 5043 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5044 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| 5045 | IgnoredValueConversions(castExpr); |
| 5046 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5047 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5048 | Kind = CK_ToVoid; |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5049 | return false; |
| 5050 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5051 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5052 | DefaultFunctionArrayLvalueConversion(castExpr); |
| 5053 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5054 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5055 | diag::err_typecheck_cast_to_incomplete)) |
| 5056 | return true; |
| 5057 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5058 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5059 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5060 | (castType->isStructureType() || castType->isUnionType())) { |
| 5061 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5062 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5063 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5064 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5065 | Kind = CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5066 | return false; |
| 5067 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5068 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5069 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5070 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5071 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5072 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5073 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5074 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5075 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5076 | castExpr->getType()) && |
| 5077 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5078 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5079 | << castExpr->getSourceRange(); |
| 5080 | break; |
| 5081 | } |
| 5082 | } |
| 5083 | if (Field == FieldEnd) |
| 5084 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 5085 | << castExpr->getType() << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5086 | Kind = CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5087 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5088 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5089 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5090 | // Reject any other conversions to non-scalar types. |
| 5091 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 5092 | << castType << castExpr->getSourceRange(); |
| 5093 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5094 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5095 | // The type we're casting to is known to be a scalar or vector. |
| 5096 | |
| 5097 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5098 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5099 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5100 | return Diag(castExpr->getLocStart(), |
| 5101 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5102 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5103 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5104 | |
| 5105 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5106 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5107 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5108 | if (castType->isVectorType()) { |
| 5109 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5110 | VectorType::AltiVecVector && |
| 5111 | (castExpr->getType()->isIntegerType() || |
| 5112 | castExpr->getType()->isFloatingType())) { |
| 5113 | Kind = CK_VectorSplat; |
| 5114 | return false; |
| 5115 | } else |
| 5116 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 5117 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5118 | if (castExpr->getType()->isVectorType()) |
| 5119 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 5120 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5121 | // The source and target types are both scalars, i.e. |
| 5122 | // - arithmetic types (fundamental, enum, and complex) |
| 5123 | // - all kinds of pointers |
| 5124 | // Note that member pointers were filtered out with C++, above. |
| 5125 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5126 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 5127 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5128 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5129 | // If either type is a pointer, the other type has to be either an |
| 5130 | // integer or a pointer. |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5131 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5132 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5133 | if (!castExprType->isIntegralType(Context) && |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 5134 | castExprType->isArithmeticType()) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5135 | return Diag(castExpr->getLocStart(), |
| 5136 | diag::err_cast_pointer_from_non_pointer_int) |
| 5137 | << castExprType << castExpr->getSourceRange(); |
| 5138 | } else if (!castExpr->getType()->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5139 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5140 | return Diag(castExpr->getLocStart(), |
| 5141 | diag::err_cast_pointer_to_non_pointer_int) |
| 5142 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5143 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5144 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5145 | Kind = PrepareScalarCast(*this, castExpr, castType); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5146 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5147 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5148 | CheckCastAlign(castExpr, castType, TyR); |
| 5149 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5150 | return false; |
| 5151 | } |
| 5152 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5153 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5154 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5155 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5156 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5157 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5158 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5159 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5160 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5161 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5162 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5163 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5164 | } else |
| 5165 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5166 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5167 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5168 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5169 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5170 | return false; |
| 5171 | } |
| 5172 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5173 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5174 | CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5175 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5176 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5177 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5178 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5179 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5180 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5181 | if (SrcTy->isVectorType()) { |
| 5182 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 5183 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 5184 | << DestTy << SrcTy << R; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5185 | Kind = CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5186 | return false; |
| 5187 | } |
| 5188 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5189 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5190 | // conversion will take place first from scalar to elt type, and then |
| 5191 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5192 | if (SrcTy->isPointerType()) |
| 5193 | return Diag(R.getBegin(), |
| 5194 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5195 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5196 | |
| 5197 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 5198 | ImpCastExprToType(CastExpr, DestElemTy, |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5199 | PrepareScalarCast(*this, CastExpr, DestElemTy)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5200 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5201 | Kind = CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5202 | return false; |
| 5203 | } |
| 5204 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5205 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5206 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5207 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5208 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5209 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5210 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5211 | TypeSourceInfo *castTInfo; |
| 5212 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5213 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5214 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5216 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5217 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5218 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5219 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5220 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5221 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5224 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5225 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5226 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5227 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5228 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5229 | CXXCastPath BasePath; |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5230 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5231 | Kind, VK, BasePath)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5232 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5233 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5234 | return Owned(CStyleCastExpr::Create(Context, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 5235 | Ty->getType().getNonLValueExprType(Context), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5236 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5237 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5240 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5241 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5242 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5243 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5244 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5245 | if (!E) |
| 5246 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5247 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5248 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5249 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5250 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5251 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5252 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5253 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5254 | if (Result.isInvalid()) return ExprError(); |
| 5255 | |
| 5256 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5257 | } |
| 5258 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5259 | ExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5260 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5261 | SourceLocation RParenLoc, Expr *Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5262 | TypeSourceInfo *TInfo) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5263 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5264 | QualType Ty = TInfo->getType(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5265 | bool isVectorLiteral = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5266 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5267 | // Check for an altivec or OpenCL literal, |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5268 | // i.e. all the elements are integer constants. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5269 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5270 | if (PE->getNumExprs() == 0) { |
| 5271 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5272 | return ExprError(); |
| 5273 | } |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5274 | if (PE->getNumExprs() == 1) { |
| 5275 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5276 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5277 | } |
| 5278 | else |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5279 | isVectorLiteral = true; |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5280 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5281 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5282 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 8bb59a8 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5283 | // then handle it as such. |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5284 | if (isVectorLiteral) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5285 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame^] | 5286 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5287 | // initializers must be one or must match the size of the vector. |
| 5288 | // If a single value is specified in the initializer then it will be |
| 5289 | // replicated to all the components of the vector |
| 5290 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5291 | VectorType::AltiVecVector) { |
| 5292 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5293 | // The number of initializers must be one or must match the size of the |
| 5294 | // vector. If a single value is specified in the initializer then it will |
| 5295 | // be replicated to all the components of the vector |
| 5296 | if (PE->getNumExprs() == 1) { |
| 5297 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 5298 | Expr *Literal = PE->getExpr(0); |
| 5299 | ImpCastExprToType(Literal, ElemTy, |
| 5300 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5301 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal); |
| 5302 | } |
| 5303 | else if (PE->getNumExprs() < numElems) { |
| 5304 | Diag(PE->getExprLoc(), |
| 5305 | diag::err_incorrect_number_of_vector_initializers); |
| 5306 | return ExprError(); |
| 5307 | } |
| 5308 | else |
| 5309 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5310 | initExprs.push_back(PE->getExpr(i)); |
| 5311 | } |
| 5312 | else |
| 5313 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5314 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5315 | |
| 5316 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5317 | // braces instead of the original commas. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5318 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5319 | &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5320 | initExprs.size(), RParenLoc); |
| 5321 | E->setType(Ty); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5322 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5323 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5324 | // 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] | 5325 | // sequence of BinOp comma operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5326 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5327 | if (Result.isInvalid()) return ExprError(); |
| 5328 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5332 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5333 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5334 | MultiExprArg Val, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5335 | ParsedType TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5336 | unsigned nexprs = Val.size(); |
| 5337 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5338 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5339 | Expr *expr; |
| 5340 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5341 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5342 | else |
| 5343 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5344 | return Owned(expr); |
| 5345 | } |
| 5346 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5347 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5348 | /// constant and the other is not a pointer. |
| 5349 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5350 | SourceLocation QuestionLoc) { |
| 5351 | Expr *NullExpr = LHS; |
| 5352 | Expr *NonPointerExpr = RHS; |
| 5353 | Expr::NullPointerConstantKind NullKind = |
| 5354 | NullExpr->isNullPointerConstant(Context, |
| 5355 | Expr::NPC_ValueDependentIsNotNull); |
| 5356 | |
| 5357 | if (NullKind == Expr::NPCK_NotNull) { |
| 5358 | NullExpr = RHS; |
| 5359 | NonPointerExpr = LHS; |
| 5360 | NullKind = |
| 5361 | NullExpr->isNullPointerConstant(Context, |
| 5362 | Expr::NPC_ValueDependentIsNotNull); |
| 5363 | } |
| 5364 | |
| 5365 | if (NullKind == Expr::NPCK_NotNull) |
| 5366 | return false; |
| 5367 | |
| 5368 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5369 | // In this case, check to make sure that we got here from a "NULL" |
| 5370 | // string in the source code. |
| 5371 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5372 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5373 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5374 | return false; |
| 5375 | } |
| 5376 | |
| 5377 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5378 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5379 | << NonPointerExpr->getType() << DiagType |
| 5380 | << NonPointerExpr->getSourceRange(); |
| 5381 | return true; |
| 5382 | } |
| 5383 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5384 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5385 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5386 | /// C99 6.5.15 |
| 5387 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5388 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5389 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5390 | |
| 5391 | // If either LHS or RHS are overloaded functions, try to resolve them. |
| 5392 | if (LHS->getType() == Context.OverloadTy || |
| 5393 | RHS->getType() == Context.OverloadTy) { |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5394 | ExprResult LHSResult = CheckPlaceholderExpr(LHS, QuestionLoc); |
| 5395 | if (LHSResult.isInvalid()) |
| 5396 | return QualType(); |
| 5397 | |
| 5398 | ExprResult RHSResult = CheckPlaceholderExpr(RHS, QuestionLoc); |
| 5399 | if (RHSResult.isInvalid()) |
| 5400 | return QualType(); |
| 5401 | |
| 5402 | LHS = LHSResult.take(); |
| 5403 | RHS = RHSResult.take(); |
| 5404 | } |
| 5405 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5406 | // C++ is sufficiently different to merit its own checker. |
| 5407 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5408 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5409 | |
| 5410 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5411 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5412 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5413 | UsualUnaryConversions(Cond); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5414 | UsualUnaryConversions(LHS); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5415 | UsualUnaryConversions(RHS); |
| 5416 | QualType CondTy = Cond->getType(); |
| 5417 | QualType LHSTy = LHS->getType(); |
| 5418 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5419 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5420 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5421 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5422 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5423 | // Throw an error if its not either. |
| 5424 | if (getLangOptions().OpenCL) { |
| 5425 | if (!CondTy->isVectorType()) { |
| 5426 | Diag(Cond->getLocStart(), |
| 5427 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5428 | << CondTy; |
| 5429 | return QualType(); |
| 5430 | } |
| 5431 | } |
| 5432 | else { |
| 5433 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5434 | << CondTy; |
| 5435 | return QualType(); |
| 5436 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5437 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5438 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5439 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5440 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5441 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5442 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5443 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5444 | // attempt to implicity convert them to the vector type to act like the |
| 5445 | // built in select. |
| 5446 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5447 | // Both operands should be of scalar type. |
| 5448 | if (!LHSTy->isScalarType()) { |
| 5449 | Diag(LHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5450 | << CondTy; |
| 5451 | return QualType(); |
| 5452 | } |
| 5453 | if (!RHSTy->isScalarType()) { |
| 5454 | Diag(RHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5455 | << CondTy; |
| 5456 | return QualType(); |
| 5457 | } |
| 5458 | // Implicity convert these scalars to the type of the condition. |
| 5459 | ImpCastExprToType(LHS, CondTy, CK_IntegralCast); |
| 5460 | ImpCastExprToType(RHS, CondTy, CK_IntegralCast); |
| 5461 | } |
| 5462 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5463 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5464 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5465 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5466 | UsualArithmeticConversions(LHS, RHS); |
| 5467 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5468 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5469 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5470 | // If both operands are the same structure or union type, the result is that |
| 5471 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5472 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5473 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5474 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5475 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5476 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5477 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5478 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5479 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5480 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5481 | // 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] | 5482 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5483 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5484 | if (!LHSTy->isVoidType()) |
| 5485 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5486 | << RHS->getSourceRange(); |
| 5487 | if (!RHSTy->isVoidType()) |
| 5488 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5489 | << LHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5490 | ImpCastExprToType(LHS, Context.VoidTy, CK_ToVoid); |
| 5491 | ImpCastExprToType(RHS, Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5492 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5493 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5494 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5495 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5496 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5497 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5498 | // promote the null to a pointer. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5499 | ImpCastExprToType(RHS, LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5500 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5501 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5502 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5503 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5504 | ImpCastExprToType(LHS, RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5505 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5506 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5507 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5508 | // All objective-c pointer type analysis is done here. |
| 5509 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5510 | QuestionLoc); |
| 5511 | if (!compositeType.isNull()) |
| 5512 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5513 | |
| 5514 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5515 | // Handle block pointer types. |
| 5516 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5517 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5518 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5519 | QualType destType = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5520 | ImpCastExprToType(LHS, destType, CK_BitCast); |
| 5521 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5522 | return destType; |
| 5523 | } |
| 5524 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5525 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5526 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5527 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5528 | // We have 2 block pointer types. |
| 5529 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5530 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5531 | return LHSTy; |
| 5532 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5533 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5534 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5535 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5536 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5537 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5538 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5539 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5540 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5541 | // In this situation, we assume void* type. No especially good |
| 5542 | // reason, but this is what gcc does, and we do have to pick |
| 5543 | // to get a consistent AST. |
| 5544 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5545 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5546 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5547 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5548 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5549 | // The block pointer types are compatible. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5550 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5551 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5552 | return LHSTy; |
| 5553 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5554 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5555 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5556 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5557 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5558 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5559 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5560 | |
| 5561 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5562 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5563 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5564 | QualType destPointee |
| 5565 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5566 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5567 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5568 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5569 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5570 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5571 | return destType; |
| 5572 | } |
| 5573 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5574 | QualType destPointee |
| 5575 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5576 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5577 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5578 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5579 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5580 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5581 | return destType; |
| 5582 | } |
| 5583 | |
| 5584 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5585 | // Two identical pointer types are always compatible. |
| 5586 | return LHSTy; |
| 5587 | } |
| 5588 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5589 | rhptee.getUnqualifiedType())) { |
| 5590 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 5591 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5592 | // In this situation, we assume void* type. No especially good |
| 5593 | // reason, but this is what gcc does, and we do have to pick |
| 5594 | // to get a consistent AST. |
| 5595 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5596 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5597 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5598 | return incompatTy; |
| 5599 | } |
| 5600 | // The pointer types are compatible. |
| 5601 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 5602 | // differently qualified versions of compatible types, the result type is |
| 5603 | // a pointer to an appropriately qualified version of the *composite* |
| 5604 | // type. |
| 5605 | // FIXME: Need to calculate the composite type. |
| 5606 | // FIXME: Need to add qualifiers |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5607 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5608 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5609 | return LHSTy; |
| 5610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5611 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5612 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5613 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5614 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 5615 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5616 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5617 | ImpCastExprToType(LHS, RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5618 | return RHSTy; |
| 5619 | } |
| 5620 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 5621 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5622 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5623 | ImpCastExprToType(RHS, LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5624 | return LHSTy; |
| 5625 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5626 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5627 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5628 | // constant and the other is not a pointer type. In this case, the user most |
| 5629 | // likely forgot to take the address of the other expression. |
| 5630 | if (DiagnoseConditionalForNull(LHS, RHS, QuestionLoc)) |
| 5631 | return QualType(); |
| 5632 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5633 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5634 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 5635 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5636 | return QualType(); |
| 5637 | } |
| 5638 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5639 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5640 | /// two objective-c pointer types of the two input expressions. |
| 5641 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 5642 | SourceLocation QuestionLoc) { |
| 5643 | QualType LHSTy = LHS->getType(); |
| 5644 | QualType RHSTy = RHS->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5645 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5646 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5647 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5648 | // redefinition type if an attempt is made to access its fields. |
| 5649 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5650 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5651 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5652 | return LHSTy; |
| 5653 | } |
| 5654 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5655 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5656 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5657 | return RHSTy; |
| 5658 | } |
| 5659 | // And the same for struct objc_object* / id |
| 5660 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5661 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5662 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5663 | return LHSTy; |
| 5664 | } |
| 5665 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5666 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5667 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5668 | return RHSTy; |
| 5669 | } |
| 5670 | // And the same for struct objc_selector* / SEL |
| 5671 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5672 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5673 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5674 | return LHSTy; |
| 5675 | } |
| 5676 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5677 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5678 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5679 | return RHSTy; |
| 5680 | } |
| 5681 | // Check constraints for Objective-C object pointers types. |
| 5682 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5683 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5684 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5685 | // Two identical object pointer types are always compatible. |
| 5686 | return LHSTy; |
| 5687 | } |
| 5688 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 5689 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 5690 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5691 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5692 | // If both operands are interfaces and either operand can be |
| 5693 | // assigned to the other, use that type as the composite |
| 5694 | // type. This allows |
| 5695 | // xxx ? (A*) a : (B*) b |
| 5696 | // where B is a subclass of A. |
| 5697 | // |
| 5698 | // Additionally, as for assignment, if either type is 'id' |
| 5699 | // allow silent coercion. Finally, if the types are |
| 5700 | // incompatible then make sure to use 'id' as the composite |
| 5701 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5702 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5703 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5704 | // It could return the composite type. |
| 5705 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5706 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5707 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5708 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5709 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5710 | RHSTy->isObjCQualifiedIdType()) && |
| 5711 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5712 | // Need to handle "id<xx>" explicitly. |
| 5713 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5714 | // id. Currently localizing to here until clear this should be |
| 5715 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5716 | compositeType = Context.getObjCIdType(); |
| 5717 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5718 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5719 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5720 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5721 | ; |
| 5722 | else { |
| 5723 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5724 | << LHSTy << RHSTy |
| 5725 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5726 | QualType incompatTy = Context.getObjCIdType(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5727 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5728 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5729 | return incompatTy; |
| 5730 | } |
| 5731 | // The object pointer types are compatible. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5732 | ImpCastExprToType(LHS, compositeType, CK_BitCast); |
| 5733 | ImpCastExprToType(RHS, compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5734 | return compositeType; |
| 5735 | } |
| 5736 | // Check Objective-C object pointer types and 'void *' |
| 5737 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 5738 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5739 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5740 | QualType destPointee |
| 5741 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5742 | QualType destType = Context.getPointerType(destPointee); |
| 5743 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5744 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5745 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5746 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5747 | return destType; |
| 5748 | } |
| 5749 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 5750 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5751 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5752 | QualType destPointee |
| 5753 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5754 | QualType destType = Context.getPointerType(destPointee); |
| 5755 | // Add qualifiers if necessary. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5756 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5757 | // Promote to void*. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5758 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5759 | return destType; |
| 5760 | } |
| 5761 | return QualType(); |
| 5762 | } |
| 5763 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5764 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5765 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5766 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5767 | SourceLocation ColonLoc, |
| 5768 | Expr *CondExpr, Expr *LHSExpr, |
| 5769 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5770 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5771 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5772 | OpaqueValueExpr *opaqueValue = 0; |
| 5773 | Expr *commonExpr = 0; |
| 5774 | if (LHSExpr == 0) { |
| 5775 | commonExpr = CondExpr; |
| 5776 | |
| 5777 | // We usually want to apply unary conversions *before* saving, except |
| 5778 | // in the special case of a C++ l-value conditional. |
| 5779 | if (!(getLangOptions().CPlusPlus |
| 5780 | && !commonExpr->isTypeDependent() |
| 5781 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5782 | && commonExpr->isGLValue() |
| 5783 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5784 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5785 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
| 5786 | UsualUnaryConversions(commonExpr); |
| 5787 | } |
| 5788 | |
| 5789 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5790 | commonExpr->getType(), |
| 5791 | commonExpr->getValueKind(), |
| 5792 | commonExpr->getObjectKind()); |
| 5793 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5794 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5795 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5796 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5797 | ExprObjectKind OK = OK_Ordinary; |
Fariborz Jahanian | 1fb019b | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 5798 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, RHSExpr, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5799 | VK, OK, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5800 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5801 | return ExprError(); |
| 5802 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5803 | if (!commonExpr) |
| 5804 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
| 5805 | LHSExpr, ColonLoc, |
| 5806 | RHSExpr, result, VK, OK)); |
| 5807 | |
| 5808 | return Owned(new (Context) |
| 5809 | BinaryConditionalOperator(commonExpr, opaqueValue, CondExpr, LHSExpr, |
| 5810 | RHSExpr, QuestionLoc, ColonLoc, result, VK, OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5811 | } |
| 5812 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5813 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5814 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5815 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5816 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5817 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5818 | static Sema::AssignConvertType |
| 5819 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5820 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5821 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5822 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5823 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5824 | const Type *lhptee, *rhptee; |
| 5825 | Qualifiers lhq, rhq; |
| 5826 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 5827 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5828 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5829 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5830 | |
| 5831 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5832 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5833 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5834 | Qualifiers lq; |
| 5835 | |
| 5836 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5837 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5838 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5839 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5840 | |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5841 | // It's okay to add or remove GC qualifiers when converting to |
| 5842 | // and from void*. |
| 5843 | else if (lhq.withoutObjCGCAttr().compatiblyIncludes(rhq.withoutObjCGCAttr()) |
| 5844 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5845 | ; // keep old |
| 5846 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5847 | // For GCC compatibility, other qualifier mismatches are treated |
| 5848 | // as still compatible in C. |
| 5849 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5850 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5851 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5852 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5853 | // 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] | 5854 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5855 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5856 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5857 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5858 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5859 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5860 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5861 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5862 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5863 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5864 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5865 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5866 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5867 | |
| 5868 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5869 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5870 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5871 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5872 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5873 | // 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] | 5874 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5875 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5876 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5877 | // Check if the pointee types are compatible ignoring the sign. |
| 5878 | // We explicitly check for char so that we catch "char" vs |
| 5879 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5880 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5881 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5882 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5883 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5884 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5885 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5886 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5887 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5888 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5889 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5890 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5891 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5892 | // takes priority over sign incompatibility because the sign |
| 5893 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5894 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5895 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5896 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5897 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5898 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5899 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5900 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5901 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5902 | // the eventual target type is the same and the pointers have the same |
| 5903 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5904 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5905 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5906 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5907 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5908 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5909 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5910 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5911 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5912 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5913 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5914 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5915 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5916 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5917 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5918 | } |
| 5919 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5920 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5921 | /// block pointer types are compatible or whether a block and normal pointer |
| 5922 | /// are compatible. It is more restrict than comparing two function pointer |
| 5923 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5924 | static Sema::AssignConvertType |
| 5925 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5926 | QualType rhsType) { |
| 5927 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5928 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5929 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5930 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5931 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5932 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5933 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5934 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5935 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5936 | // In C++, the types have to match exactly. |
| 5937 | if (S.getLangOptions().CPlusPlus) |
| 5938 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5939 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5940 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5941 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5942 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5943 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5944 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5945 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5946 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5947 | return Sema::IncompatibleBlockPointer; |
| 5948 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5949 | return ConvTy; |
| 5950 | } |
| 5951 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5952 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5953 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5954 | static Sema::AssignConvertType |
| 5955 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5956 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5957 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5958 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5959 | if (lhsType->isObjCBuiltinType()) { |
| 5960 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5961 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5962 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5963 | return Sema::IncompatiblePointer; |
| 5964 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5965 | } |
| 5966 | if (rhsType->isObjCBuiltinType()) { |
| 5967 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5968 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5969 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5970 | return Sema::IncompatiblePointer; |
| 5971 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5972 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5973 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5974 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5975 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5976 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5977 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5978 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5979 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5980 | |
| 5981 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5982 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5983 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5984 | return Sema::IncompatibleObjCQualifiedId; |
| 5985 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5986 | } |
| 5987 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5988 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5989 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5990 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5991 | // Fake up an opaque expression. We don't actually care about what |
| 5992 | // cast operations are required, so if CheckAssignmentConstraints |
| 5993 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5994 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5995 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5996 | Expr *rhsPtr = &rhs; |
| 5997 | CastKind K = CK_Invalid; |
| 5998 | |
| 5999 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6000 | } |
| 6001 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6002 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6003 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6004 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6005 | /// |
| 6006 | /// int a, *pint; |
| 6007 | /// short *pshort; |
| 6008 | /// struct foo *pfoo; |
| 6009 | /// |
| 6010 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6011 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6012 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6013 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6014 | /// |
| 6015 | /// 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] | 6016 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6017 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6018 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6019 | Sema::AssignConvertType |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6020 | Sema::CheckAssignmentConstraints(QualType lhsType, Expr *&rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6021 | CastKind &Kind) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6022 | QualType rhsType = rhs->getType(); |
| 6023 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6024 | // Get canonical types. We're not formatting these types, just comparing |
| 6025 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6026 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6027 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6028 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6029 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6030 | if (lhsType == rhsType) { |
| 6031 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6032 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6035 | // If the left-hand side is a reference type, then we are in a |
| 6036 | // (rare!) case where we've allowed the use of references in C, |
| 6037 | // e.g., as a parameter type in a built-in function. In this case, |
| 6038 | // just make sure that the type referenced is compatible with the |
| 6039 | // right-hand side type. The caller is responsible for adjusting |
| 6040 | // lhsType so that the resulting expression does not have reference |
| 6041 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6042 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6043 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6044 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6045 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6046 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6047 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6048 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6049 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6050 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6051 | // to the same ExtVector type. |
| 6052 | if (lhsType->isExtVectorType()) { |
| 6053 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6054 | return Incompatible; |
| 6055 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6056 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6057 | // element type. |
| 6058 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6059 | if (elType != rhsType) { |
| 6060 | Kind = PrepareScalarCast(*this, rhs, elType); |
| 6061 | ImpCastExprToType(rhs, elType, Kind); |
| 6062 | } |
| 6063 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6064 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6065 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6067 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6068 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6069 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6070 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6071 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6072 | // vector type and vice versa |
| 6073 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6074 | Kind = CK_BitCast; |
| 6075 | return Compatible; |
| 6076 | } |
| 6077 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6078 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6079 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6080 | // no bits are changed but the result type is different. |
| 6081 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6082 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6083 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6084 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6085 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6086 | } |
| 6087 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6088 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6089 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6090 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6091 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6092 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6093 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6094 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6095 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6096 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6097 | // Conversions to normal pointers. |
| 6098 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6099 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6100 | if (isa<PointerType>(rhsType)) { |
| 6101 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6102 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6103 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6104 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6105 | // int -> T* |
| 6106 | if (rhsType->isIntegerType()) { |
| 6107 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6108 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6109 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6110 | |
| 6111 | // C pointers are not compatible with ObjC object pointers, |
| 6112 | // with two exceptions: |
| 6113 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6114 | // - conversions to void* |
| 6115 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6116 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6117 | return Compatible; |
| 6118 | } |
| 6119 | |
| 6120 | // - conversions from 'Class' to the redefinition type |
| 6121 | if (rhsType->isObjCClassType() && |
| 6122 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6123 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6124 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6125 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6126 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6127 | Kind = CK_BitCast; |
| 6128 | return IncompatiblePointer; |
| 6129 | } |
| 6130 | |
| 6131 | // U^ -> void* |
| 6132 | if (rhsType->getAs<BlockPointerType>()) { |
| 6133 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6134 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6135 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6136 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6137 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6138 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6139 | return Incompatible; |
| 6140 | } |
| 6141 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6142 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6143 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6144 | // U^ -> T^ |
| 6145 | if (rhsType->isBlockPointerType()) { |
| 6146 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6147 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6148 | } |
| 6149 | |
| 6150 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6151 | if (rhsType->isIntegerType()) { |
| 6152 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6153 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6154 | } |
| 6155 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6156 | // id -> T^ |
| 6157 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6158 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6159 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6160 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6161 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6162 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6163 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6164 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6165 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6166 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6167 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6168 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6169 | return Incompatible; |
| 6170 | } |
| 6171 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6172 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6173 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6174 | // A* -> B* |
| 6175 | if (rhsType->isObjCObjectPointerType()) { |
| 6176 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6177 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
| 6180 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6181 | if (rhsType->isIntegerType()) { |
| 6182 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6183 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6184 | } |
| 6185 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6186 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6187 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6188 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6189 | // - conversions from 'void*' |
| 6190 | if (rhsType->isVoidPointerType()) { |
| 6191 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6192 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | // - conversions to 'Class' from its redefinition type |
| 6196 | if (lhsType->isObjCClassType() && |
| 6197 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6198 | Kind = CK_BitCast; |
| 6199 | return Compatible; |
| 6200 | } |
| 6201 | |
| 6202 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6203 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6204 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6205 | |
| 6206 | // T^ -> A* |
| 6207 | if (rhsType->isBlockPointerType()) { |
| 6208 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6209 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6210 | } |
| 6211 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6212 | return Incompatible; |
| 6213 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6214 | |
| 6215 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6216 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6217 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6218 | if (lhsType == Context.BoolTy) { |
| 6219 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6220 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6221 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6222 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6223 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6224 | if (lhsType->isIntegerType()) { |
| 6225 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6226 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6227 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6228 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6229 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6230 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6233 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6234 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6235 | if (lhsType == Context.BoolTy) { |
| 6236 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6237 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6238 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6239 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6240 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6241 | if (lhsType->isIntegerType()) { |
| 6242 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6243 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6244 | } |
| 6245 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6246 | return Incompatible; |
| 6247 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6248 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6249 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6250 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6251 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6252 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6253 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6254 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6255 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6257 | return Incompatible; |
| 6258 | } |
| 6259 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6260 | /// \brief Constructs a transparent union from an expression that is |
| 6261 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6262 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6263 | QualType UnionType, FieldDecl *Field) { |
| 6264 | // Build an initializer list that designates the appropriate member |
| 6265 | // of the transparent union. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6266 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6267 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6268 | SourceLocation()); |
| 6269 | Initializer->setType(UnionType); |
| 6270 | Initializer->setInitializedFieldInUnion(Field); |
| 6271 | |
| 6272 | // Build a compound literal constructing a value of the transparent |
| 6273 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6274 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 6275 | E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6276 | VK_RValue, Initializer, false); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6277 | } |
| 6278 | |
| 6279 | Sema::AssignConvertType |
| 6280 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 6281 | QualType FromType = rExpr->getType(); |
| 6282 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6283 | // 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] | 6284 | // transparent_union GCC extension. |
| 6285 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6286 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6287 | return Incompatible; |
| 6288 | |
| 6289 | // The field to initialize within the transparent union. |
| 6290 | RecordDecl *UD = UT->getDecl(); |
| 6291 | FieldDecl *InitField = 0; |
| 6292 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6293 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6294 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6295 | it != itend; ++it) { |
| 6296 | if (it->getType()->isPointerType()) { |
| 6297 | // If the transparent union contains a pointer type, we allow: |
| 6298 | // 1) void pointer |
| 6299 | // 2) null pointer constant |
| 6300 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6301 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6302 | ImpCastExprToType(rExpr, it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6303 | InitField = *it; |
| 6304 | break; |
| 6305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6306 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6307 | if (rExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6308 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6309 | ImpCastExprToType(rExpr, it->getType(), CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6310 | InitField = *it; |
| 6311 | break; |
| 6312 | } |
| 6313 | } |
| 6314 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6315 | Expr *rhs = rExpr; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6316 | CastKind Kind = CK_Invalid; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6317 | if (CheckAssignmentConstraints(it->getType(), rhs, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6318 | == Compatible) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6319 | ImpCastExprToType(rhs, it->getType(), Kind); |
| 6320 | rExpr = rhs; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6321 | InitField = *it; |
| 6322 | break; |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | if (!InitField) |
| 6327 | return Incompatible; |
| 6328 | |
| 6329 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 6330 | return Compatible; |
| 6331 | } |
| 6332 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6333 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6334 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6335 | if (getLangOptions().CPlusPlus) { |
| 6336 | if (!lhsType->isRecordType()) { |
| 6337 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6338 | // expression is implicitly converted (C++ 4) to the |
| 6339 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 6340 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6341 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6342 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6343 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6344 | } |
| 6345 | |
| 6346 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6347 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6348 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6349 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6350 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6351 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6352 | if ((lhsType->isPointerType() || |
| 6353 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6354 | lhsType->isBlockPointerType()) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6355 | && rExpr->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6356 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6357 | ImpCastExprToType(rExpr, lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6358 | return Compatible; |
| 6359 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6360 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6361 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6362 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6363 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6364 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6365 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6366 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6367 | if (!lhsType->isReferenceType()) |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6368 | DefaultFunctionArrayLvalueConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6369 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6370 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6371 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6372 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6373 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6374 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6375 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6376 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6377 | // so that we can use references in built-in functions even in C. |
| 6378 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6379 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6380 | if (result != Incompatible && rExpr->getType() != lhsType) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6381 | ImpCastExprToType(rExpr, lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6382 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6383 | } |
| 6384 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6385 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6386 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6387 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6388 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6389 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6392 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6393 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6394 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6395 | QualType lhsType = |
| 6396 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 6397 | QualType rhsType = |
| 6398 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6399 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6400 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6401 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6402 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6403 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6404 | // Handle the case of a vector & extvector type of the same size and element |
| 6405 | // 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] | 6406 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6407 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6408 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6409 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6410 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6411 | if (lhsType->isExtVectorType()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6412 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6413 | return lhsType; |
| 6414 | } |
| 6415 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6416 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 26bcf67 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6417 | return rhsType; |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6418 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 6419 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6420 | // vectors, the total size only needs to be the same. This is a |
| 6421 | // bitcast; no bits are changed but the result type is different. |
| 6422 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
| 6423 | return lhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6424 | } |
Eric Christopher | e84f9eb | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6425 | } |
Chandler Carruth | 629f9e4 | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6426 | } |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6427 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6429 | // Handle the case of equivalent AltiVec and GCC vector types |
| 6430 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 6431 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6432 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6433 | return rhsType; |
| 6434 | } |
| 6435 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6436 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6437 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6438 | bool swapped = false; |
| 6439 | if (rhsType->isExtVectorType()) { |
| 6440 | swapped = true; |
| 6441 | std::swap(rex, lex); |
| 6442 | std::swap(rhsType, lhsType); |
| 6443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6444 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6445 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6446 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6447 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 6448 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6449 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 6450 | if (order > 0) |
| 6451 | ImpCastExprToType(rex, EltTy, CK_IntegralCast); |
| 6452 | if (order >= 0) { |
| 6453 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6454 | if (swapped) std::swap(rex, lex); |
| 6455 | return lhsType; |
| 6456 | } |
| 6457 | } |
| 6458 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 6459 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6460 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 6461 | if (order > 0) |
| 6462 | ImpCastExprToType(rex, EltTy, CK_FloatingCast); |
| 6463 | if (order >= 0) { |
| 6464 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6465 | if (swapped) std::swap(rex, lex); |
| 6466 | return lhsType; |
| 6467 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6468 | } |
| 6469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6470 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6471 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6472 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6473 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6474 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6475 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6476 | } |
| 6477 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6478 | QualType Sema::CheckMultiplyDivideOperands( |
| 6479 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 6480 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6481 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6482 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6483 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6484 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6485 | if (!lex->getType()->isArithmeticType() || |
| 6486 | !rex->getType()->isArithmeticType()) |
| 6487 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6488 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6489 | // Check for division by zero. |
| 6490 | if (isDiv && |
| 6491 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 6492 | DiagRuntimeBehavior(Loc, rex, PDiag(diag::warn_division_by_zero) |
| 6493 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6494 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6495 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6496 | } |
| 6497 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6498 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6500 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6501 | if (lex->getType()->hasIntegerRepresentation() && |
| 6502 | rex->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6503 | return CheckVectorOperands(Loc, lex, rex); |
| 6504 | return InvalidOperands(Loc, lex, rex); |
| 6505 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6506 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6507 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6508 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6509 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 6510 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6511 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6512 | // Check for remainder by zero. |
| 6513 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 6514 | DiagRuntimeBehavior(Loc, rex, PDiag(diag::warn_remainder_by_zero) |
| 6515 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6516 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6517 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6518 | } |
| 6519 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6520 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6521 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6522 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6523 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6524 | if (CompLHSTy) *CompLHSTy = compType; |
| 6525 | return compType; |
| 6526 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6527 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6528 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6529 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6530 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6531 | if (lex->getType()->isArithmeticType() && |
| 6532 | rex->getType()->isArithmeticType()) { |
| 6533 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6534 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6535 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6536 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6537 | // Put any potential pointer into PExp |
| 6538 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6539 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6540 | std::swap(PExp, IExp); |
| 6541 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6542 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6543 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6544 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6545 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6546 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6547 | // Check for arithmetic on pointers to incomplete types. |
| 6548 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6549 | if (getLangOptions().CPlusPlus) { |
| 6550 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6551 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6552 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6553 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6554 | |
| 6555 | // GNU extension: arithmetic on pointer to void |
| 6556 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6557 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6558 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6559 | if (getLangOptions().CPlusPlus) { |
| 6560 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 6561 | << lex->getType() << lex->getSourceRange(); |
| 6562 | return QualType(); |
| 6563 | } |
| 6564 | |
| 6565 | // GNU extension: arithmetic on pointer to function |
| 6566 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 6567 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6568 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6569 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6570 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6571 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6572 | PExp->getType()->isObjCObjectPointerType()) && |
| 6573 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6574 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 6575 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6576 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6577 | return QualType(); |
| 6578 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6579 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6580 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6581 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6582 | << PointeeTy << PExp->getSourceRange(); |
| 6583 | return QualType(); |
| 6584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6585 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6586 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6587 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 6588 | if (LHSTy.isNull()) { |
| 6589 | LHSTy = lex->getType(); |
| 6590 | if (LHSTy->isPromotableIntegerType()) |
| 6591 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6592 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6593 | *CompLHSTy = LHSTy; |
| 6594 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6595 | return PExp->getType(); |
| 6596 | } |
| 6597 | } |
| 6598 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6599 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6600 | } |
| 6601 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6602 | // C99 6.5.6 |
| 6603 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6604 | SourceLocation Loc, QualType* CompLHSTy) { |
| 6605 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6606 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6607 | if (CompLHSTy) *CompLHSTy = compType; |
| 6608 | return compType; |
| 6609 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6610 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6611 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6612 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6613 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6614 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6615 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6616 | if (lex->getType()->isArithmeticType() |
| 6617 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6618 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6619 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6621 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6622 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6623 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 6624 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6625 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6626 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6627 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6628 | bool ComplainAboutVoid = false; |
| 6629 | Expr *ComplainAboutFunc = 0; |
| 6630 | if (lpointee->isVoidType()) { |
| 6631 | if (getLangOptions().CPlusPlus) { |
| 6632 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6633 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6634 | return QualType(); |
| 6635 | } |
| 6636 | |
| 6637 | // GNU C extension: arithmetic on pointer to void |
| 6638 | ComplainAboutVoid = true; |
| 6639 | } else if (lpointee->isFunctionType()) { |
| 6640 | if (getLangOptions().CPlusPlus) { |
| 6641 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6642 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6643 | return QualType(); |
| 6644 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6645 | |
| 6646 | // GNU C extension: arithmetic on pointer to function |
| 6647 | ComplainAboutFunc = lex; |
| 6648 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6649 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6650 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6651 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6652 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6653 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6654 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6655 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6656 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6657 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6658 | << lpointee << lex->getSourceRange(); |
| 6659 | return QualType(); |
| 6660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6661 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6662 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6663 | if (rex->getType()->isIntegerType()) { |
| 6664 | if (ComplainAboutVoid) |
| 6665 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6666 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6667 | if (ComplainAboutFunc) |
| 6668 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6669 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6670 | << ComplainAboutFunc->getSourceRange(); |
| 6671 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6672 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6673 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6674 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6675 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6676 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6677 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6678 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6679 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6680 | // RHS must be a completely-type object type. |
| 6681 | // Handle the GNU void* extension. |
| 6682 | if (rpointee->isVoidType()) { |
| 6683 | if (getLangOptions().CPlusPlus) { |
| 6684 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6685 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6686 | return QualType(); |
| 6687 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6688 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6689 | ComplainAboutVoid = true; |
| 6690 | } else if (rpointee->isFunctionType()) { |
| 6691 | if (getLangOptions().CPlusPlus) { |
| 6692 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6693 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6694 | return QualType(); |
| 6695 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6696 | |
| 6697 | // GNU extension: arithmetic on pointer to function |
| 6698 | if (!ComplainAboutFunc) |
| 6699 | ComplainAboutFunc = rex; |
| 6700 | } else if (!rpointee->isDependentType() && |
| 6701 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6702 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 6703 | << rex->getSourceRange() |
| 6704 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6705 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6706 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6707 | if (getLangOptions().CPlusPlus) { |
| 6708 | // Pointee types must be the same: C++ [expr.add] |
| 6709 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6710 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6711 | << lex->getType() << rex->getType() |
| 6712 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6713 | return QualType(); |
| 6714 | } |
| 6715 | } else { |
| 6716 | // Pointee types must be compatible C99 6.5.6p3 |
| 6717 | if (!Context.typesAreCompatible( |
| 6718 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6719 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6720 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6721 | << lex->getType() << rex->getType() |
| 6722 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6723 | return QualType(); |
| 6724 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6725 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6726 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6727 | if (ComplainAboutVoid) |
| 6728 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6729 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6730 | if (ComplainAboutFunc) |
| 6731 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6732 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6733 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6734 | |
| 6735 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6736 | return Context.getPointerDiffType(); |
| 6737 | } |
| 6738 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6739 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6740 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6743 | static bool isScopedEnumerationType(QualType T) { |
| 6744 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6745 | return ET->getDecl()->isScoped(); |
| 6746 | return false; |
| 6747 | } |
| 6748 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6749 | static void DiagnoseBadShiftValues(Sema& S, Expr *&lex, Expr *&rex, |
| 6750 | SourceLocation Loc, unsigned Opc, |
| 6751 | QualType LHSTy) { |
| 6752 | llvm::APSInt Right; |
| 6753 | // Check right/shifter operand |
| 6754 | if (rex->isValueDependent() || !rex->isIntegerConstantExpr(Right, S.Context)) |
| 6755 | return; |
| 6756 | |
| 6757 | if (Right.isNegative()) { |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6758 | S.DiagRuntimeBehavior(Loc, rex, |
| 6759 | S.PDiag(diag::warn_shift_negative) |
| 6760 | << rex->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6761 | return; |
| 6762 | } |
| 6763 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 6764 | S.Context.getTypeSize(lex->getType())); |
| 6765 | if (Right.uge(LeftBits)) { |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6766 | S.DiagRuntimeBehavior(Loc, rex, |
| 6767 | S.PDiag(diag::warn_shift_gt_typewidth) |
| 6768 | << rex->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6769 | return; |
| 6770 | } |
| 6771 | if (Opc != BO_Shl) |
| 6772 | return; |
| 6773 | |
| 6774 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6775 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6776 | // integers have defined behavior modulo one more than the maximum value |
| 6777 | // representable in the result type, so never warn for those. |
| 6778 | llvm::APSInt Left; |
Chandler Carruth | 6c3c3f5 | 2011-02-24 00:03:53 +0000 | [diff] [blame] | 6779 | if (lex->isValueDependent() || !lex->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6780 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 6781 | return; |
| 6782 | llvm::APInt ResultBits = |
| 6783 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6784 | if (LeftBits.uge(ResultBits)) |
| 6785 | return; |
| 6786 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6787 | Result = Result.shl(Right); |
| 6788 | |
| 6789 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6790 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6791 | // expected value. Thus we place this behind a different warning that can be |
| 6792 | // turned off separately if needed. |
| 6793 | if (LeftBits == ResultBits - 1) { |
| 6794 | S.Diag(Loc, diag::warn_shift_result_overrides_sign_bit) |
| 6795 | << Result.toString(10) << LHSTy |
| 6796 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6797 | return; |
| 6798 | } |
| 6799 | |
| 6800 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
| 6801 | << Result.toString(10) << Result.getMinSignedBits() << LHSTy |
| 6802 | << Left.getBitWidth() << lex->getSourceRange() << rex->getSourceRange(); |
| 6803 | } |
| 6804 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6805 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6806 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6807 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6808 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6809 | if (!lex->getType()->hasIntegerRepresentation() || |
| 6810 | !rex->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6811 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6812 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6813 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6814 | // hasIntegerRepresentation() above instead of this. |
| 6815 | if (isScopedEnumerationType(lex->getType()) || |
| 6816 | isScopedEnumerationType(rex->getType())) { |
| 6817 | return InvalidOperands(Loc, lex, rex); |
| 6818 | } |
| 6819 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6820 | // Vector shifts promote their scalar inputs to vector type. |
| 6821 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 6822 | return CheckVectorOperands(Loc, lex, rex); |
| 6823 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6824 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6825 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6826 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6827 | // For the LHS, do usual unary conversions, but then reset them away |
| 6828 | // if this is a compound assignment. |
| 6829 | Expr *old_lex = lex; |
| 6830 | UsualUnaryConversions(lex); |
| 6831 | QualType LHSTy = lex->getType(); |
| 6832 | if (isCompAssign) lex = old_lex; |
| 6833 | |
| 6834 | // The RHS is simpler. |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6835 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6836 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6837 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6838 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6839 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6840 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6841 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6842 | } |
| 6843 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6844 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6845 | if (DeclContext *DC = D->getDeclContext()) { |
| 6846 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6847 | return true; |
| 6848 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6849 | return FD->isFunctionTemplateSpecialization(); |
| 6850 | } |
| 6851 | return false; |
| 6852 | } |
| 6853 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6854 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6855 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6856 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6857 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6858 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6859 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6860 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6861 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6862 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6863 | QualType lType = lex->getType(); |
| 6864 | QualType rType = rex->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6865 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6866 | Expr *LHSStripped = lex->IgnoreParenImpCasts(); |
| 6867 | Expr *RHSStripped = rex->IgnoreParenImpCasts(); |
| 6868 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6869 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6870 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6871 | |
| 6872 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6873 | // Two different enums will raise a warning when compared. |
| 6874 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 6875 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 6876 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 6877 | RHSEnumType->getDecl()->getIdentifier() && |
| 6878 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 6879 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6880 | << LHSStrippedType << RHSStrippedType |
| 6881 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6882 | } |
| 6883 | } |
| 6884 | } |
| 6885 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6886 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6887 | !(lType->isBlockPointerType() && isRelational) && |
| 6888 | !lex->getLocStart().isMacroID() && |
| 6889 | !rex->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6890 | // For non-floating point types, check for self-comparisons of the form |
| 6891 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6892 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6893 | // |
| 6894 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6895 | // expansion. Also don't warn about comparisons which are only self |
| 6896 | // comparisons within a template specialization. The warnings should catch |
| 6897 | // obvious cases in the definition of the template anyways. The idea is to |
| 6898 | // warn when the typed comparison operator will always evaluate to the same |
| 6899 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6900 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6901 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6902 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6903 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6904 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6905 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6906 | << (Opc == BO_EQ |
| 6907 | || Opc == BO_LE |
| 6908 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6909 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6910 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6911 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6912 | // what is it always going to eval to? |
| 6913 | char always_evals_to; |
| 6914 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6915 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6916 | always_evals_to = 0; // false |
| 6917 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6918 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6919 | always_evals_to = 1; // true |
| 6920 | break; |
| 6921 | default: |
| 6922 | // best we can say is 'a constant' |
| 6923 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6924 | break; |
| 6925 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6926 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6927 | << 1 // array |
| 6928 | << always_evals_to); |
| 6929 | } |
| 6930 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6931 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6932 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6933 | if (isa<CastExpr>(LHSStripped)) |
| 6934 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6935 | if (isa<CastExpr>(RHSStripped)) |
| 6936 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6937 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6938 | // Warn about comparisons against a string constant (unless the other |
| 6939 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6940 | Expr *literalString = 0; |
| 6941 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6942 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6943 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6944 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6945 | literalString = lex; |
| 6946 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6947 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6948 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6949 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6950 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6951 | literalString = rex; |
| 6952 | literalStringStripped = RHSStripped; |
| 6953 | } |
| 6954 | |
| 6955 | if (literalString) { |
| 6956 | std::string resultComparison; |
| 6957 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6958 | case BO_LT: resultComparison = ") < 0"; break; |
| 6959 | case BO_GT: resultComparison = ") > 0"; break; |
| 6960 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6961 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6962 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6963 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6964 | default: assert(false && "Invalid comparison operator"); |
| 6965 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6966 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6967 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6968 | PDiag(diag::warn_stringcompare) |
| 6969 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6970 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6971 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6972 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6973 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6974 | // C99 6.5.8p3 / C99 6.5.9p4 |
| 6975 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 6976 | UsualArithmeticConversions(lex, rex); |
| 6977 | else { |
| 6978 | UsualUnaryConversions(lex); |
| 6979 | UsualUnaryConversions(rex); |
| 6980 | } |
| 6981 | |
| 6982 | lType = lex->getType(); |
| 6983 | rType = rex->getType(); |
| 6984 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6985 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6986 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6987 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6988 | if (isRelational) { |
| 6989 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6990 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6991 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6992 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6993 | if (lType->hasFloatingRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6994 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6995 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6996 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6997 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6998 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6999 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7000 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7001 | Expr::NPC_ValueDependentIsNull); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7002 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7003 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7004 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7005 | // All of the following pointer-related warnings are GCC extensions, except |
| 7006 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7007 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7008 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7009 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7010 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7011 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7012 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7013 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7014 | if (LCanPointeeTy == RCanPointeeTy) |
| 7015 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7016 | if (!isRelational && |
| 7017 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7018 | // Valid unless comparison between non-null pointer and function pointer |
| 7019 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7020 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7021 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7022 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7023 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7024 | Diag(Loc, |
| 7025 | isSFINAEContext()? |
| 7026 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7027 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7028 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7029 | |
| 7030 | if (isSFINAEContext()) |
| 7031 | return QualType(); |
| 7032 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7033 | ImpCastExprToType(rex, lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7034 | return ResultTy; |
| 7035 | } |
| 7036 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7037 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7038 | // C++ [expr.rel]p2: |
| 7039 | // [...] Pointer conversions (4.10) and qualification |
| 7040 | // conversions (4.4) are performed on pointer operands (or on |
| 7041 | // a pointer operand and a null pointer constant) to bring |
| 7042 | // them to their composite pointer type. [...] |
| 7043 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7044 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7045 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7046 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7047 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7048 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7049 | if (T.isNull()) { |
| 7050 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 7051 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 7052 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7053 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7054 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7055 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7056 | << lType << rType << T |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7057 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7058 | } |
| 7059 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7060 | ImpCastExprToType(lex, T, CK_BitCast); |
| 7061 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7062 | return ResultTy; |
| 7063 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7064 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7065 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7066 | RCanPointeeTy.getUnqualifiedType())) { |
| 7067 | // Valid unless a relational comparison of function pointers |
| 7068 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7069 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 7070 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 7071 | } |
| 7072 | } else if (!isRelational && |
| 7073 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7074 | // Valid unless comparison between non-null pointer and function pointer |
| 7075 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7076 | && !LHSIsNull && !RHSIsNull) { |
| 7077 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 7078 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 7079 | } |
| 7080 | } else { |
| 7081 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7082 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7083 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7084 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7085 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7086 | if (LHSIsNull && !RHSIsNull) |
| 7087 | ImpCastExprToType(lex, rType, CK_BitCast); |
| 7088 | else |
| 7089 | ImpCastExprToType(rex, lType, CK_BitCast); |
| 7090 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7091 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7093 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7094 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7095 | // Comparison of nullptr_t with itself. |
| 7096 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7097 | return ResultTy; |
| 7098 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7099 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7100 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7101 | if (RHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7102 | ((lType->isPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7103 | (!isRelational && lType->isMemberPointerType()))) { |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7104 | ImpCastExprToType(rex, lType, |
| 7105 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7106 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7107 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7108 | return ResultTy; |
| 7109 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7110 | if (LHSIsNull && |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7111 | ((rType->isPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7112 | (!isRelational && rType->isMemberPointerType()))) { |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7113 | ImpCastExprToType(lex, rType, |
| 7114 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7115 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7116 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7117 | return ResultTy; |
| 7118 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7119 | |
| 7120 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7121 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7122 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7123 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7124 | // In addition, pointers to members can be compared, or a pointer to |
| 7125 | // member and a null pointer constant. Pointer to member conversions |
| 7126 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7127 | // them to a common type. If one operand is a null pointer constant, |
| 7128 | // the common type is the type of the other operand. Otherwise, the |
| 7129 | // common type is a pointer to member type similar (4.4) to the type |
| 7130 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7131 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7132 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7133 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7134 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7135 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7136 | if (T.isNull()) { |
| 7137 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7138 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7139 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7140 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7141 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7142 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7143 | << lType << rType << T |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7144 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7146 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7147 | ImpCastExprToType(lex, T, CK_BitCast); |
| 7148 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7149 | return ResultTy; |
| 7150 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7151 | |
| 7152 | // Handle scoped enumeration types specifically, since they don't promote |
| 7153 | // to integers. |
| 7154 | if (lex->getType()->isEnumeralType() && |
| 7155 | Context.hasSameUnqualifiedType(lex->getType(), rex->getType())) |
| 7156 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7157 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7158 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7159 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7160 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7161 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7162 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7163 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7164 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7165 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7166 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7167 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7168 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7169 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7170 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7171 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7172 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7173 | if (!isRelational |
| 7174 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7175 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7176 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7177 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7178 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7179 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7180 | ->getPointeeType()->isVoidType()))) |
| 7181 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 7182 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7183 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7184 | if (LHSIsNull && !RHSIsNull) |
| 7185 | ImpCastExprToType(lex, rType, CK_BitCast); |
| 7186 | else |
| 7187 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7188 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7189 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7190 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7191 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7192 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7193 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7194 | if (LPT || RPT) { |
| 7195 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7196 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7197 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7198 | if (!LPtrToVoid && !RPtrToVoid && |
| 7199 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7200 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7201 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7202 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7203 | if (LHSIsNull && !RHSIsNull) |
| 7204 | ImpCastExprToType(lex, rType, CK_BitCast); |
| 7205 | else |
| 7206 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7207 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7208 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7209 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7210 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7211 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 7212 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7213 | if (LHSIsNull && !RHSIsNull) |
| 7214 | ImpCastExprToType(lex, rType, CK_BitCast); |
| 7215 | else |
| 7216 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7217 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7218 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7219 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7220 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7221 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7222 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7223 | bool isError = false; |
| 7224 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7225 | (RHSIsNull && rType->isIntegerType())) { |
| 7226 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7227 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7228 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7229 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7230 | else if (getLangOptions().CPlusPlus) { |
| 7231 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7232 | isError = true; |
| 7233 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7234 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7235 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7236 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7237 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 7238 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7239 | if (isError) |
| 7240 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7241 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7242 | |
| 7243 | if (lType->isIntegerType()) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7244 | ImpCastExprToType(lex, rType, |
| 7245 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7246 | else |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7247 | ImpCastExprToType(rex, lType, |
| 7248 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7249 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7250 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7251 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7252 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7253 | if (!isRelational && RHSIsNull |
| 7254 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7255 | ImpCastExprToType(rex, lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7256 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7257 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7258 | if (!isRelational && LHSIsNull |
| 7259 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7260 | ImpCastExprToType(lex, rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7261 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7262 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7263 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7264 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7265 | } |
| 7266 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7267 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7268 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7269 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7270 | /// types. |
| 7271 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7272 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7273 | bool isRelational) { |
| 7274 | // Check to make sure we're operating on vectors of the same type and width, |
| 7275 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7276 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7277 | if (vType.isNull()) |
| 7278 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7279 | |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 7280 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7281 | // bool for C++, int for C |
| 7282 | if (getLangOptions().AltiVec) |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7283 | return Context.getLogicalOperationType(); |
Anton Yartsev | aa4fe05 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 7284 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7285 | QualType lType = lex->getType(); |
| 7286 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7287 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7288 | // For non-floating point types, check for self-comparisons of the form |
| 7289 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7290 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7291 | if (!lType->hasFloatingRepresentation()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7292 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 7293 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 7294 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7295 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7296 | PDiag(diag::warn_comparison_always) |
| 7297 | << 0 // self- |
| 7298 | << 2 // "a constant" |
| 7299 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7300 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7301 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7302 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7303 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7304 | assert (rType->hasFloatingRepresentation()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7305 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7306 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7307 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7308 | // Return the type for the comparison, which is the same as vector type for |
| 7309 | // integer vectors, or an integer type of identical size and number of |
| 7310 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7311 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7312 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7313 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7314 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7315 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7316 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7317 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7318 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7319 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7320 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7321 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7322 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7323 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7324 | } |
| 7325 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7326 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7327 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7328 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 7329 | if (lex->getType()->hasIntegerRepresentation() && |
| 7330 | rex->getType()->hasIntegerRepresentation()) |
| 7331 | return CheckVectorOperands(Loc, lex, rex); |
| 7332 | |
| 7333 | return InvalidOperands(Loc, lex, rex); |
| 7334 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7335 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7336 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7337 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7338 | if (lex->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7339 | rex->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7340 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7341 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7342 | } |
| 7343 | |
| 7344 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7345 | Expr *&lex, Expr *&rex, SourceLocation Loc, unsigned Opc) { |
| 7346 | |
| 7347 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7348 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7349 | // is a constant. |
| 7350 | if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() && |
Eli Friedman | 787b094 | 2010-07-27 19:14:53 +0000 | [diff] [blame] | 7351 | rex->getType()->isIntegerType() && !rex->isValueDependent() && |
Chris Lattner | 23ef3e4 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7352 | // Don't warn in macros. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7353 | !Loc.isMacroID()) { |
| 7354 | // If the RHS can be constant folded, and if it constant folds to something |
| 7355 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7356 | // happened to fold to true/false) then warn. |
| 7357 | Expr::EvalResult Result; |
| 7358 | if (rex->Evaluate(Result, Context) && !Result.HasSideEffects && |
| 7359 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 7360 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 7361 | << rex->getSourceRange() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7362 | << (Opc == BO_LAnd ? "&&" : "||") |
| 7363 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7364 | } |
| 7365 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7366 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7367 | if (!Context.getLangOptions().CPlusPlus) { |
| 7368 | UsualUnaryConversions(lex); |
| 7369 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7370 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7371 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 7372 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7373 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7374 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7375 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7376 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7377 | // The following is safe because we only use this method for |
| 7378 | // non-overloadable operands. |
| 7379 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7380 | // C++ [expr.log.and]p1 |
| 7381 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7382 | // The operands are both contextually converted to type bool. |
| 7383 | if (PerformContextuallyConvertToBool(lex) || |
| 7384 | PerformContextuallyConvertToBool(rex)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7385 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7386 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7387 | // C++ [expr.log.and]p2 |
| 7388 | // C++ [expr.log.or]p2 |
| 7389 | // The result is a bool. |
| 7390 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7393 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7394 | /// is a read-only property; return true if so. A readonly property expression |
| 7395 | /// depends on various declarations and thus must be treated specially. |
| 7396 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7397 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7398 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7399 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7400 | if (PropExpr->isImplicitProperty()) return false; |
| 7401 | |
| 7402 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7403 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 7404 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7405 | PropExpr->getBase()->getType(); |
| 7406 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7407 | if (const ObjCObjectPointerType *OPT = |
| 7408 | BaseType->getAsObjCInterfacePointerType()) |
| 7409 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7410 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7411 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7412 | } |
| 7413 | return false; |
| 7414 | } |
| 7415 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7416 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 7417 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 7418 | return false; |
| 7419 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 7420 | NamedDecl *Member = ME->getMemberDecl(); |
| 7421 | if (isa<FieldDecl>(Member)) { |
| 7422 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7423 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 7424 | return false; |
| 7425 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 7426 | } |
| 7427 | return false; |
| 7428 | } |
| 7429 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7430 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7431 | /// emit an error and return true. If so, return false. |
| 7432 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7433 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7434 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7435 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7436 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7437 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7438 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7439 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7440 | if (IsLV == Expr::MLV_Valid) |
| 7441 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7442 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7443 | unsigned Diag = 0; |
| 7444 | bool NeedType = false; |
| 7445 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7446 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7447 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7448 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7449 | NeedType = true; |
| 7450 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7451 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7452 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7453 | NeedType = true; |
| 7454 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7455 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7456 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7457 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7458 | case Expr::MLV_Valid: |
| 7459 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7460 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7461 | case Expr::MLV_MemberFunction: |
| 7462 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7463 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7464 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7465 | case Expr::MLV_IncompleteType: |
| 7466 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7467 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7468 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7469 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7470 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7471 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7472 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7473 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7474 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7475 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7476 | case Expr::MLV_ReadonlyProperty: |
| 7477 | Diag = diag::error_readonly_property_assignment; |
| 7478 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7479 | case Expr::MLV_NoSetterProperty: |
| 7480 | Diag = diag::error_nosetter_property_assignment; |
| 7481 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7482 | case Expr::MLV_InvalidMessageExpression: |
| 7483 | Diag = diag::error_readonly_message_assignment; |
| 7484 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7485 | case Expr::MLV_SubObjCPropertySetting: |
| 7486 | Diag = diag::error_no_subobject_property_setting; |
| 7487 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7488 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7489 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7490 | SourceRange Assign; |
| 7491 | if (Loc != OrigLoc) |
| 7492 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7493 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7494 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7495 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7496 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7497 | return true; |
| 7498 | } |
| 7499 | |
| 7500 | |
| 7501 | |
| 7502 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7503 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 7504 | SourceLocation Loc, |
| 7505 | QualType CompoundType) { |
| 7506 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 7507 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7508 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7509 | |
| 7510 | QualType LHSType = LHS->getType(); |
| 7511 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7512 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7513 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7514 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7515 | // Simple assignment "x = y". |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7516 | if (LHS->getObjectKind() == OK_ObjCProperty) |
| 7517 | ConvertPropertyForLValue(LHS, RHS, LHSTy); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7518 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7519 | // Special case of NSObject attributes on c-style pointer types. |
| 7520 | if (ConvTy == IncompatiblePointer && |
| 7521 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7522 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7523 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7524 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7525 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7526 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7527 | if (ConvTy == Compatible && |
| 7528 | getLangOptions().ObjCNonFragileABI && |
| 7529 | LHSType->isObjCObjectType()) |
| 7530 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7531 | << LHSType; |
| 7532 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7533 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7534 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7535 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7536 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7537 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7538 | RHSCheck = ICE->getSubExpr(); |
| 7539 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7540 | if ((UO->getOpcode() == UO_Plus || |
| 7541 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7542 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7543 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7544 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7545 | // And there is a space or other character before the subexpr of the |
| 7546 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7547 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7548 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7549 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7550 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7551 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7552 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7553 | } |
| 7554 | } else { |
| 7555 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7556 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7557 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7558 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7559 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7560 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7561 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7562 | |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7563 | |
| 7564 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 7565 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 7566 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 7567 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 7568 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 7569 | // check. |
| 7570 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7571 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7572 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 7573 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 7574 | !UO->getType().isVolatileQualified()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7575 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7576 | PDiag(diag::warn_indirection_through_null) |
| 7577 | << UO->getSubExpr()->getSourceRange()); |
| 7578 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 7579 | PDiag(diag::note_indirection_through_null)); |
Chris Lattner | 8b5dec3 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7580 | } |
| 7581 | |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7582 | // Check for trivial buffer overflows. |
Ted Kremenek | 3aea4da | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 7583 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 7584 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7585 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7586 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7587 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7588 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7589 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7590 | // 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] | 7591 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7592 | return (getLangOptions().CPlusPlus |
| 7593 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7594 | } |
| 7595 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7596 | // C99 6.5.17 |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7597 | static QualType CheckCommaOperands(Sema &S, Expr *&LHS, Expr *&RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7598 | SourceLocation Loc) { |
| 7599 | S.DiagnoseUnusedExprResult(LHS); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7600 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7601 | ExprResult LHSResult = S.CheckPlaceholderExpr(LHS, Loc); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7602 | if (LHSResult.isInvalid()) |
| 7603 | return QualType(); |
| 7604 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7605 | ExprResult RHSResult = S.CheckPlaceholderExpr(RHS, Loc); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7606 | if (RHSResult.isInvalid()) |
| 7607 | return QualType(); |
| 7608 | RHS = RHSResult.take(); |
| 7609 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7610 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7611 | // operands, but not unary promotions. |
| 7612 | // 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] | 7613 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7614 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7615 | // containing site to determine what should be done with the RHS. |
| 7616 | S.IgnoredValueConversions(LHS); |
| 7617 | |
| 7618 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7619 | S.DefaultFunctionArrayLvalueConversion(RHS); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7620 | if (!RHS->getType()->isVoidType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7621 | S.RequireCompleteType(Loc, RHS->getType(), diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7622 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7623 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7624 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7625 | } |
| 7626 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7627 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7628 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7629 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7630 | ExprValueKind &VK, |
| 7631 | SourceLocation OpLoc, |
| 7632 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7633 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7634 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7635 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7636 | QualType ResType = Op->getType(); |
| 7637 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7638 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7639 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7640 | // Decrement of bool is not allowed. |
| 7641 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7642 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7643 | return QualType(); |
| 7644 | } |
| 7645 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7646 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7647 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7648 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7649 | } else if (ResType->isAnyPointerType()) { |
| 7650 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7651 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7652 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7653 | if (PointeeTy->isVoidType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7654 | if (S.getLangOptions().CPlusPlus) { |
| 7655 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7656 | << Op->getSourceRange(); |
| 7657 | return QualType(); |
| 7658 | } |
| 7659 | |
| 7660 | // Pointer to void is a GNU extension in C. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7661 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7662 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7663 | if (S.getLangOptions().CPlusPlus) { |
| 7664 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7665 | << Op->getType() << Op->getSourceRange(); |
| 7666 | return QualType(); |
| 7667 | } |
| 7668 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7669 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7670 | << ResType << Op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7671 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 7672 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7673 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7674 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7675 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7676 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7677 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7678 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7679 | << PointeeTy << Op->getSourceRange(); |
| 7680 | return QualType(); |
| 7681 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7682 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7683 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7684 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7685 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7686 | } else if (ResType->isPlaceholderType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7687 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7688 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7689 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7690 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7691 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7692 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7693 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7694 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7695 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7696 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7697 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7698 | // 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] | 7699 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7700 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7701 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7702 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7703 | // (in C or with postfix), the increment is the unqualified type of the |
| 7704 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7705 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7706 | VK = VK_LValue; |
| 7707 | return ResType; |
| 7708 | } else { |
| 7709 | VK = VK_RValue; |
| 7710 | return ResType.getUnqualifiedType(); |
| 7711 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7712 | } |
| 7713 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7714 | void Sema::ConvertPropertyForRValue(Expr *&E) { |
| 7715 | assert(E->getValueKind() == VK_LValue && |
| 7716 | E->getObjectKind() == OK_ObjCProperty); |
| 7717 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7718 | |
| 7719 | ExprValueKind VK = VK_RValue; |
| 7720 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7721 | if (const ObjCMethodDecl *GetterMethod = |
| 7722 | PRE->getImplicitPropertyGetter()) { |
| 7723 | QualType Result = GetterMethod->getResultType(); |
| 7724 | VK = Expr::getValueKindForType(Result); |
| 7725 | } |
| 7726 | else { |
| 7727 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7728 | << PRE->getBase()->getType(); |
| 7729 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
| 7732 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 7733 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7734 | |
| 7735 | ExprResult Result = MaybeBindToTemporary(E); |
| 7736 | if (!Result.isInvalid()) |
| 7737 | E = Result.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7738 | } |
| 7739 | |
| 7740 | void Sema::ConvertPropertyForLValue(Expr *&LHS, Expr *&RHS, QualType &LHSTy) { |
| 7741 | assert(LHS->getValueKind() == VK_LValue && |
| 7742 | LHS->getObjectKind() == OK_ObjCProperty); |
| 7743 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 7744 | |
| 7745 | if (PRE->isImplicitProperty()) { |
| 7746 | // If using property-dot syntax notation for assignment, and there is a |
| 7747 | // setter, RHS expression is being passed to the setter argument. So, |
| 7748 | // type conversion (and comparison) is RHS to setter's argument type. |
| 7749 | if (const ObjCMethodDecl *SetterMD = PRE->getImplicitPropertySetter()) { |
| 7750 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7751 | LHSTy = (*P)->getType(); |
| 7752 | |
| 7753 | // Otherwise, if the getter returns an l-value, just call that. |
| 7754 | } else { |
| 7755 | QualType Result = PRE->getImplicitPropertyGetter()->getResultType(); |
| 7756 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7757 | if (VK == VK_LValue) { |
| 7758 | LHS = ImplicitCastExpr::Create(Context, LHS->getType(), |
| 7759 | CK_GetObjCProperty, LHS, 0, VK); |
| 7760 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7761 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7762 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7763 | } |
| 7764 | |
| 7765 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7766 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7767 | InitializedEntity::InitializeParameter(Context, LHSTy); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7768 | Expr *Arg = RHS; |
| 7769 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), |
| 7770 | Owned(Arg)); |
| 7771 | if (!ArgE.isInvalid()) |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7772 | RHS = ArgE.takeAs<Expr>(); |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7773 | } |
| 7774 | } |
| 7775 | |
| 7776 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7777 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7778 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7779 | /// where the declaration is needed for type checking. We only need to |
| 7780 | /// handle cases when the expression references a function designator |
| 7781 | /// or is an lvalue. Here are some examples: |
| 7782 | /// - &(x) => x |
| 7783 | /// - &*****f => f for f a function designator. |
| 7784 | /// - &s.xx => s |
| 7785 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7786 | /// - *(x + 1) -> x, if x is an array |
| 7787 | /// - &"123"[2] -> 0 |
| 7788 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7789 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7790 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7791 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7792 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7793 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7794 | // If this is an arrow operator, the address is an offset from |
| 7795 | // the base's value, so the object the base refers to is |
| 7796 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7797 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7798 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7799 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7800 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7801 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7802 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7803 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7804 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7805 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7806 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7807 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7808 | } |
| 7809 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7810 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7811 | case Stmt::UnaryOperatorClass: { |
| 7812 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7813 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7814 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7815 | case UO_Real: |
| 7816 | case UO_Imag: |
| 7817 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7818 | return getPrimaryDecl(UO->getSubExpr()); |
| 7819 | default: |
| 7820 | return 0; |
| 7821 | } |
| 7822 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7823 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7824 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7825 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7826 | // If the result of an implicit cast is an l-value, we care about |
| 7827 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7828 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7829 | default: |
| 7830 | return 0; |
| 7831 | } |
| 7832 | } |
| 7833 | |
| 7834 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7835 | /// 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] | 7836 | /// 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] | 7837 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7838 | /// 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] | 7839 | /// 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] | 7840 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7841 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7842 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7843 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7844 | return S.Context.DependentTy; |
| 7845 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7846 | return S.Context.OverloadTy; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7847 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7848 | ExprResult PR = S.CheckPlaceholderExpr(OrigOp, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7849 | if (PR.isInvalid()) return QualType(); |
| 7850 | OrigOp = PR.take(); |
| 7851 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7852 | // Make sure to ignore parentheses in subsequent checks |
| 7853 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7854 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7855 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7856 | // Implement C99-only parts of addressof rules. |
| 7857 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7858 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7859 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7860 | // (assuming the deref expression is valid). |
| 7861 | return uOp->getSubExpr()->getType(); |
| 7862 | } |
| 7863 | // Technically, there should be a check for array subscript |
| 7864 | // expressions here, but the result of one is always an lvalue anyway. |
| 7865 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7866 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7867 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7868 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7869 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7870 | bool sfinae = S.isSFINAEContext(); |
| 7871 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7872 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7873 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7874 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7875 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7876 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7877 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7878 | } else if (lval == Expr::LV_MemberFunction) { |
| 7879 | // If it's an instance method, make a member pointer. |
| 7880 | // The expression must have exactly the form &A::foo. |
| 7881 | |
| 7882 | // If the underlying expression isn't a decl ref, give up. |
| 7883 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7884 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7885 | << OrigOp->getSourceRange(); |
| 7886 | return QualType(); |
| 7887 | } |
| 7888 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7889 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7890 | |
| 7891 | // The id-expression was parenthesized. |
| 7892 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7893 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7894 | << OrigOp->getSourceRange(); |
| 7895 | |
| 7896 | // The method was named without a qualifier. |
| 7897 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7898 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7899 | << op->getSourceRange(); |
| 7900 | } |
| 7901 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7902 | return S.Context.getMemberPointerType(op->getType(), |
| 7903 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7904 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7905 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7906 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7907 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7908 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7909 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7910 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7911 | return QualType(); |
| 7912 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7913 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7914 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7915 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7916 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7917 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7918 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7919 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7920 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7921 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7922 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7923 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7924 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7925 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7926 | << "property expression" << op->getSourceRange(); |
| 7927 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7928 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7929 | // 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] | 7930 | // with the register storage-class specifier. |
| 7931 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7932 | // in C++ it is not error to take address of a register |
| 7933 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7934 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7935 | !S.getLangOptions().CPlusPlus) { |
| 7936 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7937 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7938 | return QualType(); |
| 7939 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7940 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7941 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7942 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7943 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7944 | // Could be a pointer to member, though, if there is an explicit |
| 7945 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7946 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7947 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7948 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7949 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7950 | S.Diag(OpLoc, |
| 7951 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7952 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7953 | return QualType(); |
| 7954 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7955 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7956 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7957 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7958 | return S.Context.getMemberPointerType(op->getType(), |
| 7959 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7960 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7961 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7962 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7963 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7964 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7965 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7966 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7967 | // Taking the address of a void variable is technically illegal, but we |
| 7968 | // allow it in cases which are otherwise valid. |
| 7969 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7970 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7971 | } |
| 7972 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7973 | // 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] | 7974 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7975 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7976 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7977 | } |
| 7978 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7979 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7980 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7981 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7982 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7983 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7984 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7985 | S.UsualUnaryConversions(Op); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7986 | QualType OpTy = Op->getType(); |
| 7987 | QualType Result; |
| 7988 | |
| 7989 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7990 | // is an incomplete type or void. It would be possible to warn about |
| 7991 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7992 | // warning is unlikely to catch any mistakes. |
| 7993 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7994 | Result = PT->getPointeeType(); |
| 7995 | else if (const ObjCObjectPointerType *OPT = |
| 7996 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7997 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7998 | else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7999 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8000 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8001 | if (PR.take() != Op) |
| 8002 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8003 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8004 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8005 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8006 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8007 | << OpTy << Op->getSourceRange(); |
| 8008 | return QualType(); |
| 8009 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8010 | |
| 8011 | // Dereferences are usually l-values... |
| 8012 | VK = VK_LValue; |
| 8013 | |
| 8014 | // ...except that certain expressions are never l-values in C. |
| 8015 | if (!S.getLangOptions().CPlusPlus && |
| 8016 | IsCForbiddenLValueType(S.Context, Result)) |
| 8017 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8018 | |
| 8019 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8020 | } |
| 8021 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8022 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8023 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8024 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8025 | switch (Kind) { |
| 8026 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8027 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8028 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8029 | case tok::star: Opc = BO_Mul; break; |
| 8030 | case tok::slash: Opc = BO_Div; break; |
| 8031 | case tok::percent: Opc = BO_Rem; break; |
| 8032 | case tok::plus: Opc = BO_Add; break; |
| 8033 | case tok::minus: Opc = BO_Sub; break; |
| 8034 | case tok::lessless: Opc = BO_Shl; break; |
| 8035 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8036 | case tok::lessequal: Opc = BO_LE; break; |
| 8037 | case tok::less: Opc = BO_LT; break; |
| 8038 | case tok::greaterequal: Opc = BO_GE; break; |
| 8039 | case tok::greater: Opc = BO_GT; break; |
| 8040 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8041 | case tok::equalequal: Opc = BO_EQ; break; |
| 8042 | case tok::amp: Opc = BO_And; break; |
| 8043 | case tok::caret: Opc = BO_Xor; break; |
| 8044 | case tok::pipe: Opc = BO_Or; break; |
| 8045 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8046 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8047 | case tok::equal: Opc = BO_Assign; break; |
| 8048 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8049 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8050 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8051 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8052 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8053 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8054 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8055 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8056 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8057 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8058 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8059 | } |
| 8060 | return Opc; |
| 8061 | } |
| 8062 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8063 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8064 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8065 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8066 | switch (Kind) { |
| 8067 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8068 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8069 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8070 | case tok::amp: Opc = UO_AddrOf; break; |
| 8071 | case tok::star: Opc = UO_Deref; break; |
| 8072 | case tok::plus: Opc = UO_Plus; break; |
| 8073 | case tok::minus: Opc = UO_Minus; break; |
| 8074 | case tok::tilde: Opc = UO_Not; break; |
| 8075 | case tok::exclaim: Opc = UO_LNot; break; |
| 8076 | case tok::kw___real: Opc = UO_Real; break; |
| 8077 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8078 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8079 | } |
| 8080 | return Opc; |
| 8081 | } |
| 8082 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8083 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8084 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8085 | /// suppressed in the event of macro expansions. |
| 8086 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8087 | SourceLocation OpLoc) { |
| 8088 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8089 | return; |
| 8090 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8091 | return; |
| 8092 | lhs = lhs->IgnoreParenImpCasts(); |
| 8093 | rhs = rhs->IgnoreParenImpCasts(); |
| 8094 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8095 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8096 | if (!LeftDeclRef || !RightDeclRef || |
| 8097 | LeftDeclRef->getLocation().isMacroID() || |
| 8098 | RightDeclRef->getLocation().isMacroID()) |
| 8099 | return; |
| 8100 | const ValueDecl *LeftDecl = |
| 8101 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8102 | const ValueDecl *RightDecl = |
| 8103 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8104 | if (LeftDecl != RightDecl) |
| 8105 | return; |
| 8106 | if (LeftDecl->getType().isVolatileQualified()) |
| 8107 | return; |
| 8108 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8109 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8110 | return; |
| 8111 | |
| 8112 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8113 | << LeftDeclRef->getType() |
| 8114 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8115 | } |
| 8116 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8117 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8118 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8119 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8120 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8121 | BinaryOperatorKind Opc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8122 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8123 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8124 | // The following two variables are used for compound assignment operators |
| 8125 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8126 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8127 | ExprValueKind VK = VK_RValue; |
| 8128 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8129 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8130 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8131 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8132 | // But since an assignment can trigger target based overload, exclude it in |
| 8133 | // our blind search. i.e: |
| 8134 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8135 | // f<int> == 0; // resolve f<int> blindly |
| 8136 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8137 | if (Opc != BO_Assign) { |
| 8138 | if (lhs->getType() == Context.OverloadTy) { |
| 8139 | ExprResult resolvedLHS = |
| 8140 | ResolveAndFixSingleFunctionTemplateSpecialization(lhs); |
| 8141 | if (resolvedLHS.isUsable()) lhs = resolvedLHS.release(); |
| 8142 | } |
| 8143 | if (rhs->getType() == Context.OverloadTy) { |
| 8144 | ExprResult resolvedRHS = |
| 8145 | ResolveAndFixSingleFunctionTemplateSpecialization(rhs); |
| 8146 | if (resolvedRHS.isUsable()) rhs = resolvedRHS.release(); |
| 8147 | } |
| 8148 | } |
| 8149 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8150 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8151 | case BO_Assign: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8152 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8153 | if (getLangOptions().CPlusPlus && |
| 8154 | lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8155 | VK = lhs->getValueKind(); |
| 8156 | OK = lhs->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8157 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8158 | if (!ResultTy.isNull()) |
| 8159 | DiagnoseSelfAssignment(*this, lhs, rhs, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8160 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8161 | case BO_PtrMemD: |
| 8162 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8163 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8164 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8165 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8166 | case BO_Mul: |
| 8167 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8168 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8169 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8170 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8171 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8172 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 8173 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8174 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8175 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 8176 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8177 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8178 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 8179 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8180 | case BO_Shl: |
| 8181 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8182 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8183 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8184 | case BO_LE: |
| 8185 | case BO_LT: |
| 8186 | case BO_GE: |
| 8187 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8188 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8189 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8190 | case BO_EQ: |
| 8191 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8192 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8193 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8194 | case BO_And: |
| 8195 | case BO_Xor: |
| 8196 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8197 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 8198 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8199 | case BO_LAnd: |
| 8200 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8201 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8202 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8203 | case BO_MulAssign: |
| 8204 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8205 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8206 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8207 | CompLHSTy = CompResultTy; |
| 8208 | if (!CompResultTy.isNull()) |
| 8209 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8210 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8211 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8212 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 8213 | CompLHSTy = CompResultTy; |
| 8214 | if (!CompResultTy.isNull()) |
| 8215 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8216 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8217 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8218 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 8219 | if (!CompResultTy.isNull()) |
| 8220 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8221 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8222 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8223 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 8224 | if (!CompResultTy.isNull()) |
| 8225 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8226 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8227 | case BO_ShlAssign: |
| 8228 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8229 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8230 | CompLHSTy = CompResultTy; |
| 8231 | if (!CompResultTy.isNull()) |
| 8232 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8233 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8234 | case BO_AndAssign: |
| 8235 | case BO_XorAssign: |
| 8236 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8237 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8238 | CompLHSTy = CompResultTy; |
| 8239 | if (!CompResultTy.isNull()) |
| 8240 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8241 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8242 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8243 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8244 | if (getLangOptions().CPlusPlus) { |
| 8245 | VK = rhs->getValueKind(); |
| 8246 | OK = rhs->getObjectKind(); |
| 8247 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8248 | break; |
| 8249 | } |
| 8250 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8251 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8252 | if (CompResultTy.isNull()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8253 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, |
| 8254 | VK, OK, OpLoc)); |
| 8255 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8256 | if (getLangOptions().CPlusPlus && lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8257 | VK = VK_LValue; |
| 8258 | OK = lhs->getObjectKind(); |
| 8259 | } |
| 8260 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
| 8261 | VK, OK, CompLHSTy, |
| 8262 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8263 | } |
| 8264 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8265 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 8266 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8267 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 8268 | const PartialDiagnostic &PD, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8269 | const PartialDiagnostic &FirstNote, |
| 8270 | SourceRange FirstParenRange, |
| 8271 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8272 | SourceRange SecondParenRange) { |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8273 | Self.Diag(Loc, PD); |
| 8274 | |
| 8275 | if (!FirstNote.getDiagID()) |
| 8276 | return; |
| 8277 | |
| 8278 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 8279 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8280 | // We can't display the parentheses, so just return. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8281 | return; |
| 8282 | } |
| 8283 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8284 | Self.Diag(Loc, FirstNote) |
| 8285 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8286 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8287 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8288 | if (!SecondNote.getDiagID()) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8289 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8290 | |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8291 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 8292 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8293 | // We can't display the parentheses, so just dig the |
| 8294 | // warning/error and return. |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8295 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8296 | return; |
| 8297 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8298 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8299 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8300 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 8301 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8302 | } |
| 8303 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8304 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8305 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8306 | /// comparison operators have higher precedence. The most typical example of |
| 8307 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8308 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8309 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8310 | typedef BinaryOperator BinOp; |
| 8311 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8312 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8313 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8314 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8315 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8316 | rhsopc = BO->getOpcode(); |
| 8317 | |
| 8318 | // Subs are not binary operators. |
| 8319 | if (lhsopc == -1 && rhsopc == -1) |
| 8320 | return; |
| 8321 | |
| 8322 | // Bitwise operations are sometimes used as eager logical ops. |
| 8323 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8324 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8325 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8326 | return; |
| 8327 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8328 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8329 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8330 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8331 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8332 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8333 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8334 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8335 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 8336 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8337 | << BinOp::getOpcodeStr(lhsopc), |
| 8338 | lhs->getSourceRange()); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8339 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8340 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8341 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8342 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8343 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8344 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8345 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8346 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 8347 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8348 | << BinOp::getOpcodeStr(rhsopc), |
| 8349 | rhs->getSourceRange()); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8350 | } |
| 8351 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8352 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8353 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8354 | /// in parentheses. |
| 8355 | static void |
| 8356 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| 8357 | Expr *E) { |
| 8358 | assert(isa<BinaryOperator>(E) && |
| 8359 | cast<BinaryOperator>(E)->getOpcode() == BO_LAnd); |
| 8360 | SuggestParentheses(Self, OpLoc, |
| 8361 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| 8362 | << E->getSourceRange(), |
| 8363 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| 8364 | E->getSourceRange(), |
| 8365 | Self.PDiag(0), SourceRange()); |
| 8366 | } |
| 8367 | |
| 8368 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8369 | /// 'true'. |
| 8370 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8371 | bool Res; |
| 8372 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8373 | } |
| 8374 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8375 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8376 | /// 'false'. |
| 8377 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8378 | bool Res; |
| 8379 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8380 | } |
| 8381 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8382 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8383 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8384 | Expr *OrLHS, Expr *OrRHS) { |
| 8385 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8386 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8387 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 8388 | if (EvaluatesAsFalse(S, OrRHS)) |
| 8389 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8390 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8391 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8392 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8393 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8394 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8395 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8396 | // "a || b && 1", but warn now. |
| 8397 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8398 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8399 | } |
| 8400 | } |
| 8401 | } |
| 8402 | } |
| 8403 | |
| 8404 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8405 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8406 | Expr *OrLHS, Expr *OrRHS) { |
| 8407 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8408 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8409 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 8410 | if (EvaluatesAsFalse(S, OrLHS)) |
| 8411 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8412 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8413 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8414 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8415 | } |
| 8416 | } |
| 8417 | } |
| 8418 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8419 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8420 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8421 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8422 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8423 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8424 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8425 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 8426 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8427 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8428 | // 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] | 8429 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8430 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 8431 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8432 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8435 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8436 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8437 | tok::TokenKind Kind, |
| 8438 | Expr *lhs, Expr *rhs) { |
| 8439 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8440 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8441 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8442 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8443 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8444 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8445 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8446 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8447 | } |
| 8448 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8449 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8450 | BinaryOperatorKind Opc, |
| 8451 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8452 | if (getLangOptions().CPlusPlus) { |
| 8453 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8454 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8455 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8456 | UseBuiltinOperator = false; |
| 8457 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8458 | UseBuiltinOperator = true; |
| 8459 | } else { |
| 8460 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 8461 | !rhs->getType()->isOverloadableType(); |
| 8462 | } |
| 8463 | |
| 8464 | if (!UseBuiltinOperator) { |
| 8465 | // Find all of the overloaded operators visible from this |
| 8466 | // point. We perform both an operator-name lookup from the local |
| 8467 | // scope and an argument-dependent lookup based on the types of |
| 8468 | // the arguments. |
| 8469 | UnresolvedSet<16> Functions; |
| 8470 | OverloadedOperatorKind OverOp |
| 8471 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8472 | if (S && OverOp != OO_None) |
| 8473 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 8474 | Functions); |
| 8475 | |
| 8476 | // Build the (potentially-overloaded, potentially-dependent) |
| 8477 | // binary operation. |
| 8478 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 8479 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8480 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8481 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8482 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8483 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8484 | } |
| 8485 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8486 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8487 | UnaryOperatorKind Opc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8488 | Expr *Input) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8489 | ExprValueKind VK = VK_RValue; |
| 8490 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8491 | QualType resultType; |
| 8492 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8493 | case UO_PreInc: |
| 8494 | case UO_PreDec: |
| 8495 | case UO_PostInc: |
| 8496 | case UO_PostDec: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8497 | resultType = CheckIncrementDecrementOperand(*this, Input, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8498 | Opc == UO_PreInc || |
| 8499 | Opc == UO_PostInc, |
| 8500 | Opc == UO_PreInc || |
| 8501 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8502 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8503 | case UO_AddrOf: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8504 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8505 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8506 | case UO_Deref: |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8507 | if (Input->getType() == Context.OverloadTy ) { |
| 8508 | ExprResult er = ResolveAndFixSingleFunctionTemplateSpecialization(Input); |
| 8509 | if (er.isUsable()) |
| 8510 | Input = er.release(); |
| 8511 | } |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8512 | DefaultFunctionArrayLvalueConversion(Input); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8513 | resultType = CheckIndirectionOperand(*this, Input, VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8514 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8515 | case UO_Plus: |
| 8516 | case UO_Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8517 | UsualUnaryConversions(Input); |
| 8518 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8519 | if (resultType->isDependentType()) |
| 8520 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8521 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8522 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8523 | break; |
| 8524 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8525 | resultType->isEnumeralType()) |
| 8526 | break; |
| 8527 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8528 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8529 | resultType->isPointerType()) |
| 8530 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8531 | else if (resultType->isPlaceholderType()) { |
| 8532 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8533 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8534 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8535 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8536 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8537 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8538 | << resultType << Input->getSourceRange()); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8539 | case UO_Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8540 | UsualUnaryConversions(Input); |
| 8541 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8542 | if (resultType->isDependentType()) |
| 8543 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8544 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8545 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8546 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8547 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8548 | << resultType << Input->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8549 | else if (resultType->hasIntegerRepresentation()) |
| 8550 | break; |
| 8551 | else if (resultType->isPlaceholderType()) { |
| 8552 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8553 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8554 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8555 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8556 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8557 | << resultType << Input->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8558 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8559 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8560 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8561 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8562 | DefaultFunctionArrayLvalueConversion(Input); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 8563 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8564 | if (resultType->isDependentType()) |
| 8565 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8566 | if (resultType->isScalarType()) { // C99 6.5.3.3p1 |
| 8567 | // ok, fallthrough |
| 8568 | } else if (resultType->isPlaceholderType()) { |
| 8569 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 8570 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8571 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8572 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8573 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 8574 | << resultType << Input->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8575 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8577 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8578 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8579 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8580 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8581 | case UO_Real: |
| 8582 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8583 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8584 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
| 8585 | if (Input->getValueKind() != VK_RValue && |
| 8586 | Input->getObjectKind() == OK_Ordinary) |
| 8587 | VK = Input->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8588 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8589 | case UO_Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8590 | resultType = Input->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8591 | VK = Input->getValueKind(); |
| 8592 | OK = Input->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8593 | break; |
| 8594 | } |
| 8595 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8596 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8597 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8598 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, |
| 8599 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8600 | } |
| 8601 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8602 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8603 | UnaryOperatorKind Opc, |
| 8604 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8605 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8606 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8607 | // Find all of the overloaded operators visible from this |
| 8608 | // point. We perform both an operator-name lookup from the local |
| 8609 | // scope and an argument-dependent lookup based on the types of |
| 8610 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8611 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8612 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8613 | if (S && OverOp != OO_None) |
| 8614 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8615 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8616 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8617 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8618 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8619 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8620 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8621 | } |
| 8622 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8623 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8624 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8625 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8626 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8627 | } |
| 8628 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8629 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8630 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8631 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8632 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8633 | // 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] | 8634 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8635 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8636 | } |
| 8637 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8638 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8639 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8640 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8641 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8642 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8643 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8644 | bool isFileScope |
| 8645 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8646 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8647 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8648 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8649 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8650 | // example, it is not possible to goto into a stmt expression apparently. |
| 8651 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8652 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8653 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8654 | // as the type of the stmtexpr. |
| 8655 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8656 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8657 | if (!Compound->body_empty()) { |
| 8658 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8659 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8660 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8661 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8662 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8663 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8664 | } |
| 8665 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8666 | // Do function/array conversion on the last expression, but not |
| 8667 | // lvalue-to-rvalue. However, initialize an unqualified type. |
| 8668 | DefaultFunctionArrayConversion(LastExpr); |
| 8669 | Ty = LastExpr->getType().getUnqualifiedType(); |
| 8670 | |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8671 | if (!Ty->isDependentType() && !LastExpr->isTypeDependent()) { |
| 8672 | ExprResult Res = PerformCopyInitialization( |
| 8673 | InitializedEntity::InitializeResult(LPLoc, |
| 8674 | Ty, |
| 8675 | false), |
| 8676 | SourceLocation(), |
| 8677 | Owned(LastExpr)); |
| 8678 | if (Res.isInvalid()) |
| 8679 | return ExprError(); |
| 8680 | if ((LastExpr = Res.takeAs<Expr>())) { |
| 8681 | if (!LastLabelStmt) |
| 8682 | Compound->setLastStmt(LastExpr); |
| 8683 | else |
| 8684 | LastLabelStmt->setSubStmt(LastExpr); |
| 8685 | StmtExprMayBindToTemp = true; |
| 8686 | } |
| 8687 | } |
| 8688 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8689 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8690 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8691 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8692 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8693 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8694 | if (StmtExprMayBindToTemp) |
| 8695 | return MaybeBindToTemporary(ResStmtExpr); |
| 8696 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8697 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8698 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8699 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8700 | TypeSourceInfo *TInfo, |
| 8701 | OffsetOfComponent *CompPtr, |
| 8702 | unsigned NumComponents, |
| 8703 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8704 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8705 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8706 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8707 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8708 | // We must have at least one component that refers to the type, and the first |
| 8709 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8710 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8711 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8712 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8713 | << ArgTy << TypeRange); |
| 8714 | |
| 8715 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8716 | // with an incomplete type would be ill-formed. |
| 8717 | if (!Dependent |
| 8718 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8719 | PDiag(diag::err_offsetof_incomplete_type) |
| 8720 | << TypeRange)) |
| 8721 | return ExprError(); |
| 8722 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8723 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8724 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8725 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8726 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8727 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8728 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8729 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8730 | |
| 8731 | bool DidWarnAboutNonPOD = false; |
| 8732 | QualType CurrentType = ArgTy; |
| 8733 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 8734 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 8735 | llvm::SmallVector<Expr*, 4> Exprs; |
| 8736 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8737 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8738 | if (OC.isBrackets) { |
| 8739 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8740 | if (!CurrentType->isDependentType()) { |
| 8741 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8742 | if(!AT) |
| 8743 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8744 | << CurrentType); |
| 8745 | CurrentType = AT->getElementType(); |
| 8746 | } else |
| 8747 | CurrentType = Context.DependentTy; |
| 8748 | |
| 8749 | // The expression must be an integral expression. |
| 8750 | // FIXME: An integral constant expression? |
| 8751 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8752 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8753 | !Idx->getType()->isIntegerType()) |
| 8754 | return ExprError(Diag(Idx->getLocStart(), |
| 8755 | diag::err_typecheck_subscript_not_integer) |
| 8756 | << Idx->getSourceRange()); |
| 8757 | |
| 8758 | // Record this array index. |
| 8759 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8760 | Exprs.push_back(Idx); |
| 8761 | continue; |
| 8762 | } |
| 8763 | |
| 8764 | // Offset of a field. |
| 8765 | if (CurrentType->isDependentType()) { |
| 8766 | // We have the offset of a field, but we can't look into the dependent |
| 8767 | // type. Just record the identifier of the field. |
| 8768 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8769 | CurrentType = Context.DependentTy; |
| 8770 | continue; |
| 8771 | } |
| 8772 | |
| 8773 | // We need to have a complete type to look into. |
| 8774 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8775 | diag::err_offsetof_incomplete_type)) |
| 8776 | return ExprError(); |
| 8777 | |
| 8778 | // Look for the designated field. |
| 8779 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8780 | if (!RC) |
| 8781 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8782 | << CurrentType); |
| 8783 | RecordDecl *RD = RC->getDecl(); |
| 8784 | |
| 8785 | // C++ [lib.support.types]p5: |
| 8786 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8787 | // International Standard. type shall be a POD structure or a POD union |
| 8788 | // (clause 9). |
| 8789 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8790 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8791 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8792 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8793 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8794 | << CurrentType)) |
| 8795 | DidWarnAboutNonPOD = true; |
| 8796 | } |
| 8797 | |
| 8798 | // Look for the field. |
| 8799 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8800 | LookupQualifiedName(R, RD); |
| 8801 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8802 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8803 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8804 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8805 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8806 | } |
| 8807 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8808 | if (!MemberDecl) |
| 8809 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8810 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8811 | OC.LocEnd)); |
| 8812 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8813 | // C99 7.17p3: |
| 8814 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8815 | // |
| 8816 | // We diagnose this as an error. |
| 8817 | if (MemberDecl->getBitWidth()) { |
| 8818 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8819 | << MemberDecl->getDeclName() |
| 8820 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8821 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8822 | return ExprError(); |
| 8823 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8824 | |
| 8825 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8826 | if (IndirectMemberDecl) |
| 8827 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8828 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8829 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8830 | // the base class indirections. |
| 8831 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8832 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8833 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8834 | CXXBasePath &Path = Paths.front(); |
| 8835 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8836 | B != BEnd; ++B) |
| 8837 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8838 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8839 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8840 | if (IndirectMemberDecl) { |
| 8841 | for (IndirectFieldDecl::chain_iterator FI = |
| 8842 | IndirectMemberDecl->chain_begin(), |
| 8843 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8844 | assert(isa<FieldDecl>(*FI)); |
| 8845 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8846 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8847 | } |
| 8848 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8849 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8850 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8851 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8852 | } |
| 8853 | |
| 8854 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8855 | TInfo, Comps.data(), Comps.size(), |
| 8856 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8857 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8858 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8859 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8860 | SourceLocation BuiltinLoc, |
| 8861 | SourceLocation TypeLoc, |
| 8862 | ParsedType argty, |
| 8863 | OffsetOfComponent *CompPtr, |
| 8864 | unsigned NumComponents, |
| 8865 | SourceLocation RPLoc) { |
| 8866 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8867 | TypeSourceInfo *ArgTInfo; |
| 8868 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8869 | if (ArgTy.isNull()) |
| 8870 | return ExprError(); |
| 8871 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8872 | if (!ArgTInfo) |
| 8873 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8874 | |
| 8875 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8876 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8877 | } |
| 8878 | |
| 8879 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8880 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8881 | Expr *CondExpr, |
| 8882 | Expr *LHSExpr, Expr *RHSExpr, |
| 8883 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8884 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8885 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8886 | ExprValueKind VK = VK_RValue; |
| 8887 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8888 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8889 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8890 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8891 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8892 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8893 | } else { |
| 8894 | // The conditional expression is required to be a constant expression. |
| 8895 | llvm::APSInt condEval(32); |
| 8896 | SourceLocation ExpLoc; |
| 8897 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8898 | return ExprError(Diag(ExpLoc, |
| 8899 | diag::err_typecheck_choose_expr_requires_constant) |
| 8900 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8901 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8902 | // 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] | 8903 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8904 | |
| 8905 | resType = ActiveExpr->getType(); |
| 8906 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8907 | VK = ActiveExpr->getValueKind(); |
| 8908 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8909 | } |
| 8910 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8911 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8912 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8913 | resType->isDependentType(), |
| 8914 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8915 | } |
| 8916 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8917 | //===----------------------------------------------------------------------===// |
| 8918 | // Clang Extensions. |
| 8919 | //===----------------------------------------------------------------------===// |
| 8920 | |
| 8921 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8922 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8923 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8924 | PushBlockScope(BlockScope, Block); |
| 8925 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8926 | if (BlockScope) |
| 8927 | PushDeclContext(BlockScope, Block); |
| 8928 | else |
| 8929 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8930 | } |
| 8931 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8932 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8933 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8934 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8935 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8936 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8937 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8938 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8939 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8940 | // GetTypeForDeclarator always produces a function type for a block |
| 8941 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8942 | // unless the function was written with a typedef. |
| 8943 | assert(T->isFunctionType() && |
| 8944 | "GetTypeForDeclarator made a non-function block signature"); |
| 8945 | |
| 8946 | // Look for an explicit signature in that function type. |
| 8947 | FunctionProtoTypeLoc ExplicitSignature; |
| 8948 | |
| 8949 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8950 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8951 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8952 | |
| 8953 | // Check whether that explicit signature was synthesized by |
| 8954 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8955 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8956 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8957 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8958 | // This would be much cheaper if we stored TypeLocs instead of |
| 8959 | // TypeSourceInfos. |
| 8960 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8961 | unsigned Size = Result.getFullDataSize(); |
| 8962 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8963 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8964 | |
| 8965 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8966 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8967 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8968 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8969 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8970 | CurBlock->FunctionType = T; |
| 8971 | |
| 8972 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8973 | QualType RetTy = Fn->getResultType(); |
| 8974 | bool isVariadic = |
| 8975 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8976 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8977 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8978 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8979 | // Don't allow returning a objc interface by value. |
| 8980 | if (RetTy->isObjCObjectType()) { |
| 8981 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8982 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8983 | return; |
| 8984 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8985 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8986 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8987 | // return type. TODO: what should we do with declarators like: |
| 8988 | // ^ * { ... } |
| 8989 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8990 | if (RetTy != Context.DependentTy) |
| 8991 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8992 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8993 | // Push block parameters from the declarator if we had them. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8994 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8995 | if (ExplicitSignature) { |
| 8996 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8997 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8998 | if (Param->getIdentifier() == 0 && |
| 8999 | !Param->isImplicit() && |
| 9000 | !Param->isInvalidDecl() && |
| 9001 | !getLangOptions().CPlusPlus) |
| 9002 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9003 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9004 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9005 | |
| 9006 | // Fake up parameter variables if we have a typedef, like |
| 9007 | // ^ fntype { ... } |
| 9008 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9009 | for (FunctionProtoType::arg_type_iterator |
| 9010 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9011 | ParmVarDecl *Param = |
| 9012 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9013 | ParamInfo.getSourceRange().getBegin(), |
| 9014 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9015 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9016 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9017 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9018 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9019 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9020 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9021 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9022 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9023 | CurBlock->TheDecl->param_end(), |
| 9024 | /*CheckParameterNames=*/false); |
| 9025 | } |
| 9026 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9027 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9028 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9029 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9030 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9031 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9032 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9033 | // FIXME: remove the attribute. |
| 9034 | } |
| 9035 | |
| 9036 | // Put the parameter variables in scope. We can bail out immediately |
| 9037 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9038 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9039 | return; |
| 9040 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9041 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9042 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9043 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9044 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9045 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9046 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9047 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9048 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9049 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9050 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9051 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9052 | } |
| 9053 | |
| 9054 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9055 | /// is invoked to pop the information about the block from the action impl. |
| 9056 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9057 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9058 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9059 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9060 | } |
| 9061 | |
| 9062 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9063 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9064 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9065 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9066 | // If blocks are disabled, emit an error. |
| 9067 | if (!LangOpts.Blocks) |
| 9068 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9069 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9070 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9071 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9072 | PopDeclContext(); |
| 9073 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9074 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9075 | if (!BSI->ReturnType.isNull()) |
| 9076 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9077 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9078 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9079 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9080 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9081 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9082 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9083 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9084 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9085 | // If the user wrote a function type in some form, try to use that. |
| 9086 | if (!BSI->FunctionType.isNull()) { |
| 9087 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9088 | |
| 9089 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9090 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9091 | |
| 9092 | // Turn protoless block types into nullary block types. |
| 9093 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9094 | FunctionProtoType::ExtProtoInfo EPI; |
| 9095 | EPI.ExtInfo = Ext; |
| 9096 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9097 | |
| 9098 | // Otherwise, if we don't need to change anything about the function type, |
| 9099 | // preserve its sugar structure. |
| 9100 | } else if (FTy->getResultType() == RetTy && |
| 9101 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9102 | BlockTy = BSI->FunctionType; |
| 9103 | |
| 9104 | // Otherwise, make the minimal modifications to the function type. |
| 9105 | } else { |
| 9106 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9107 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9108 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9109 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9110 | BlockTy = Context.getFunctionType(RetTy, |
| 9111 | FPT->arg_type_begin(), |
| 9112 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9113 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9114 | } |
| 9115 | |
| 9116 | // If we don't have a function type, just build one from nothing. |
| 9117 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9118 | FunctionProtoType::ExtProtoInfo EPI; |
| 9119 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, 0, CC_Default); |
| 9120 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9121 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9122 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9123 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9124 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9125 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9126 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9127 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9128 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9129 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9130 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9131 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9132 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9133 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | e0054f6 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9134 | |
Ted Kremenek | 3ed6fc0 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9135 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9136 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9137 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9138 | } |
| 9139 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9140 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9141 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9142 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9143 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9144 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9145 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9146 | } |
| 9147 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9148 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9149 | Expr *E, TypeSourceInfo *TInfo, |
| 9150 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9151 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9152 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9153 | // Get the va_list type |
| 9154 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9155 | if (VaListType->isArrayType()) { |
| 9156 | // Deal with implicit array decay; for example, on x86-64, |
| 9157 | // va_list is an array, but it's supposed to decay to |
| 9158 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9159 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9160 | // Make sure the input expression also decays appropriately. |
| 9161 | UsualUnaryConversions(E); |
| 9162 | } else { |
| 9163 | // Otherwise, the va_list argument must be an l-value because |
| 9164 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9165 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9166 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9167 | return ExprError(); |
| 9168 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9169 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9170 | if (!E->isTypeDependent() && |
| 9171 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9172 | return ExprError(Diag(E->getLocStart(), |
| 9173 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9174 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9175 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9176 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9177 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9178 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9179 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9180 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9181 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9182 | } |
| 9183 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9184 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9185 | // The type of __null will be int or long, depending on the size of |
| 9186 | // pointers on the target. |
| 9187 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9188 | unsigned pw = Context.Target.getPointerWidth(0); |
| 9189 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9190 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9191 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9192 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9193 | else if (pw == Context.Target.getLongLongWidth()) |
| 9194 | Ty = Context.LongLongTy; |
| 9195 | else { |
| 9196 | assert(!"I don't know size of pointer!"); |
| 9197 | Ty = Context.IntTy; |
| 9198 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9199 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9200 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9201 | } |
| 9202 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9203 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9204 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9205 | if (!SemaRef.getLangOptions().ObjC1) |
| 9206 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9207 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9208 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9209 | if (!PT) |
| 9210 | return; |
| 9211 | |
| 9212 | // Check if the destination is of type 'id'. |
| 9213 | if (!PT->isObjCIdType()) { |
| 9214 | // Check if the destination is the 'NSString' interface. |
| 9215 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9216 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9217 | return; |
| 9218 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9219 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9220 | // Strip off any parens and casts. |
| 9221 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 9222 | if (!SL || SL->isWide()) |
| 9223 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9224 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9225 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9226 | } |
| 9227 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9228 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9229 | SourceLocation Loc, |
| 9230 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9231 | Expr *SrcExpr, AssignmentAction Action, |
| 9232 | bool *Complained) { |
| 9233 | if (Complained) |
| 9234 | *Complained = false; |
| 9235 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9236 | // Decode the result (notice that AST's are still created for extensions). |
| 9237 | bool isInvalid = false; |
| 9238 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9239 | FixItHint Hint; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9240 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9241 | switch (ConvTy) { |
| 9242 | default: assert(0 && "Unknown conversion type"); |
| 9243 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9244 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9245 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9246 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9247 | case IntToPointer: |
| 9248 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9249 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9250 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9251 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9252 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 9253 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9254 | case IncompatiblePointerSign: |
| 9255 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9256 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9257 | case FunctionVoidPointer: |
| 9258 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9259 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9260 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9261 | // Perform array-to-pointer decay if necessary. |
| 9262 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9263 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9264 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9265 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9266 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9267 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9268 | break; |
| 9269 | } |
| 9270 | |
| 9271 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9272 | // fallthrough |
| 9273 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9274 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9275 | // If the qualifiers lost were because we were applying the |
| 9276 | // (deprecated) C++ conversion from a string literal to a char* |
| 9277 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9278 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9279 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9280 | // bit of refactoring (so that the second argument is an |
| 9281 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9282 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9283 | // C++ semantics. |
| 9284 | if (getLangOptions().CPlusPlus && |
| 9285 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9286 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9287 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9288 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9289 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9290 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9291 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9292 | case IntToBlockPointer: |
| 9293 | DiagKind = diag::err_int_to_block_pointer; |
| 9294 | break; |
| 9295 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9296 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9297 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9298 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9299 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9300 | // it can give a more specific diagnostic. |
| 9301 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9302 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9303 | case IncompatibleVectors: |
| 9304 | DiagKind = diag::warn_incompatible_vectors; |
| 9305 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9306 | case Incompatible: |
| 9307 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9308 | isInvalid = true; |
| 9309 | break; |
| 9310 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9311 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9312 | QualType FirstType, SecondType; |
| 9313 | switch (Action) { |
| 9314 | case AA_Assigning: |
| 9315 | case AA_Initializing: |
| 9316 | // The destination type comes first. |
| 9317 | FirstType = DstType; |
| 9318 | SecondType = SrcType; |
| 9319 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9320 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9321 | case AA_Returning: |
| 9322 | case AA_Passing: |
| 9323 | case AA_Converting: |
| 9324 | case AA_Sending: |
| 9325 | case AA_Casting: |
| 9326 | // The source type comes first. |
| 9327 | FirstType = SrcType; |
| 9328 | SecondType = DstType; |
| 9329 | break; |
| 9330 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9331 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9332 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9333 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9334 | if (Complained) |
| 9335 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9336 | return isInvalid; |
| 9337 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9338 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9339 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9340 | llvm::APSInt ICEResult; |
| 9341 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9342 | if (Result) |
| 9343 | *Result = ICEResult; |
| 9344 | return false; |
| 9345 | } |
| 9346 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9347 | Expr::EvalResult EvalResult; |
| 9348 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9349 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9350 | EvalResult.HasSideEffects) { |
| 9351 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9352 | |
| 9353 | if (EvalResult.Diag) { |
| 9354 | // We only show the note if it's not the usual "invalid subexpression" |
| 9355 | // or if it's actually in a subexpression. |
| 9356 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9357 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9358 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9359 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9360 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9361 | return true; |
| 9362 | } |
| 9363 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9364 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9365 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9366 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9367 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9368 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9369 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9370 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9371 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9372 | if (Result) |
| 9373 | *Result = EvalResult.Val.getInt(); |
| 9374 | return false; |
| 9375 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9376 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9377 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9378 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9379 | ExprEvalContexts.push_back( |
| 9380 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9381 | } |
| 9382 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9383 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9384 | Sema::PopExpressionEvaluationContext() { |
| 9385 | // Pop the current expression evaluation context off the stack. |
| 9386 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9387 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9388 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9389 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9390 | if (Rec.PotentiallyReferenced) { |
| 9391 | // Mark any remaining declarations in the current position of the stack |
| 9392 | // as "referenced". If they were not meant to be referenced, semantic |
| 9393 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9394 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9395 | I = Rec.PotentiallyReferenced->begin(), |
| 9396 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9397 | I != IEnd; ++I) |
| 9398 | MarkDeclarationReferenced(I->first, I->second); |
| 9399 | } |
| 9400 | |
| 9401 | if (Rec.PotentiallyDiagnosed) { |
| 9402 | // Emit any pending diagnostics. |
| 9403 | for (PotentiallyEmittedDiagnostics::iterator |
| 9404 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9405 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9406 | I != IEnd; ++I) |
| 9407 | Diag(I->first, I->second); |
| 9408 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9409 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9410 | |
| 9411 | // When are coming out of an unevaluated context, clear out any |
| 9412 | // temporaries that we may have created as part of the evaluation of |
| 9413 | // the expression in that context: they aren't relevant because they |
| 9414 | // will never be constructed. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9415 | if (Rec.Context == Unevaluated && |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9416 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 9417 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9418 | ExprTemporaries.end()); |
| 9419 | |
| 9420 | // Destroy the popped expression evaluation record. |
| 9421 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9422 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9423 | |
| 9424 | /// \brief Note that the given declaration was referenced in the source code. |
| 9425 | /// |
| 9426 | /// This routine should be invoke whenever a given declaration is referenced |
| 9427 | /// in the source code, and where that reference occurred. If this declaration |
| 9428 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9429 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9430 | /// |
| 9431 | /// \param Loc the location where the declaration was referenced. |
| 9432 | /// |
| 9433 | /// \param D the declaration that has been referenced by the source code. |
| 9434 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9435 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9436 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9437 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9438 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9439 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 9440 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 9441 | // template or not. The reason for this is that unevaluated expressions |
| 9442 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 9443 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9444 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9445 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9446 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9447 | return; |
| 9448 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9449 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9450 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9451 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9452 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9453 | // Do not mark anything as "used" within a dependent context; wait for |
| 9454 | // an instantiation. |
| 9455 | if (CurContext->isDependentContext()) |
| 9456 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9457 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9458 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9459 | case Unevaluated: |
| 9460 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9461 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9462 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9463 | case PotentiallyEvaluated: |
| 9464 | // We are in a potentially-evaluated expression, so this declaration is |
| 9465 | // "used"; handle this below. |
| 9466 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9467 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9468 | case PotentiallyPotentiallyEvaluated: |
| 9469 | // We are in an expression that may be potentially evaluated; queue this |
| 9470 | // declaration reference until we know whether the expression is |
| 9471 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9472 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9473 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9474 | |
| 9475 | case PotentiallyEvaluatedIfUsed: |
| 9476 | // Referenced declarations will only be used if the construct in the |
| 9477 | // containing expression is used. |
| 9478 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9480 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9481 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9482 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9483 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 9484 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9485 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 9486 | return; |
| 9487 | if (!Constructor->isUsed(false)) |
| 9488 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9489 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 9490 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9491 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9492 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 9493 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9494 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9495 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9496 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9497 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9498 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9499 | if (Destructor->isVirtual()) |
| 9500 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9501 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 9502 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 9503 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9504 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9505 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9506 | } else if (MethodDecl->isVirtual()) |
| 9507 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9508 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9509 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9510 | // Recursive functions should be marked when used from another function. |
| 9511 | if (CurContext == Function) return; |
| 9512 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9513 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9514 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9515 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9516 | bool AlreadyInstantiated = false; |
| 9517 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9518 | = Function->getTemplateSpecializationInfo()) { |
| 9519 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9520 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9521 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9522 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9523 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9524 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9525 | = Function->getMemberSpecializationInfo()) { |
| 9526 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9527 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9528 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9529 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9530 | AlreadyInstantiated = true; |
| 9531 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9532 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9533 | if (!AlreadyInstantiated) { |
| 9534 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9535 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9536 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9537 | Loc)); |
| 9538 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9539 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9540 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9541 | } else { |
| 9542 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9543 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9544 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9545 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9546 | MarkDeclarationReferenced(Loc, *i); |
| 9547 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9548 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9549 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9550 | // Keep track of used but undefined functions. |
| 9551 | if (!Function->isPure() && !Function->hasBody() && |
| 9552 | Function->getLinkage() != ExternalLinkage) { |
| 9553 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9554 | if (old.isInvalid()) old = Loc; |
| 9555 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9556 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9557 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9558 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9559 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9560 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9561 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9562 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9563 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9564 | Var->getInstantiatedFromStaticDataMember()) { |
| 9565 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9566 | assert(MSInfo && "Missing member specialization information?"); |
| 9567 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9568 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9569 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9570 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9571 | } |
| 9572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9573 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9574 | // Keep track of used but undefined variables. We make a hole in |
| 9575 | // the warning for static const data members with in-line |
| 9576 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9577 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9578 | && Var->getLinkage() != ExternalLinkage |
| 9579 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9580 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9581 | if (old.isInvalid()) old = Loc; |
| 9582 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9583 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9584 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9585 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9586 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9587 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9588 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9589 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9590 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9591 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9592 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9593 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9594 | Sema &S; |
| 9595 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9596 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9597 | public: |
| 9598 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9599 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9600 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9601 | |
| 9602 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9603 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9604 | }; |
| 9605 | } |
| 9606 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9607 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9608 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9609 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9610 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9611 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9612 | |
| 9613 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9614 | } |
| 9615 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9616 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9617 | if (ClassTemplateSpecializationDecl *Spec |
| 9618 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9619 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9620 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9621 | } |
| 9622 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9623 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9624 | } |
| 9625 | |
| 9626 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9627 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9628 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9629 | } |
| 9630 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9631 | namespace { |
| 9632 | /// \brief Helper class that marks all of the declarations referenced by |
| 9633 | /// potentially-evaluated subexpressions as "referenced". |
| 9634 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9635 | Sema &S; |
| 9636 | |
| 9637 | public: |
| 9638 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9639 | |
| 9640 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9641 | |
| 9642 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9643 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9644 | } |
| 9645 | |
| 9646 | void VisitMemberExpr(MemberExpr *E) { |
| 9647 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9648 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9649 | } |
| 9650 | |
| 9651 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9652 | if (E->getConstructor()) |
| 9653 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9654 | if (E->getOperatorNew()) |
| 9655 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9656 | if (E->getOperatorDelete()) |
| 9657 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9658 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9659 | } |
| 9660 | |
| 9661 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9662 | if (E->getOperatorDelete()) |
| 9663 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9664 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9665 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9666 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9667 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9668 | S.LookupDestructor(Record)); |
| 9669 | } |
| 9670 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9671 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9672 | } |
| 9673 | |
| 9674 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9675 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9676 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
| 9679 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9680 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9681 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9682 | |
| 9683 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9684 | Visit(E->getExpr()); |
| 9685 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9686 | }; |
| 9687 | } |
| 9688 | |
| 9689 | /// \brief Mark any declarations that appear within this expression or any |
| 9690 | /// potentially-evaluated subexpressions as "referenced". |
| 9691 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9692 | EvaluatedExprMarker(*this).Visit(E); |
| 9693 | } |
| 9694 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9695 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9696 | /// of the program being compiled. |
| 9697 | /// |
| 9698 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9699 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9700 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9701 | /// expressions, code used only during overload resolution, etc., are not |
| 9702 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9703 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9704 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9705 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9706 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9707 | /// This routine should be used for all diagnostics that describe the run-time |
| 9708 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9709 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9710 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9711 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9712 | const PartialDiagnostic &PD) { |
| 9713 | switch (ExprEvalContexts.back().Context ) { |
| 9714 | case Unevaluated: |
| 9715 | // The argument will never be evaluated, so don't complain. |
| 9716 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9717 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9718 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9719 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9720 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9721 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9722 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9723 | } |
| 9724 | else |
| 9725 | Diag(Loc, PD); |
| 9726 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9727 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9728 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9729 | case PotentiallyPotentiallyEvaluated: |
| 9730 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9731 | break; |
| 9732 | } |
| 9733 | |
| 9734 | return false; |
| 9735 | } |
| 9736 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9737 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9738 | CallExpr *CE, FunctionDecl *FD) { |
| 9739 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9740 | return false; |
| 9741 | |
| 9742 | PartialDiagnostic Note = |
| 9743 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9744 | << FD->getDeclName() : PDiag(); |
| 9745 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9746 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9747 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9748 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9749 | PDiag(diag::err_call_function_incomplete_return) |
| 9750 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9751 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9752 | << CE->getSourceRange(), |
| 9753 | std::make_pair(NoteLoc, Note))) |
| 9754 | return true; |
| 9755 | |
| 9756 | return false; |
| 9757 | } |
| 9758 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9759 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9760 | // will prevent this condition from triggering, which is what we want. |
| 9761 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9762 | SourceLocation Loc; |
| 9763 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9764 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9765 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9766 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9767 | if (isa<BinaryOperator>(E)) { |
| 9768 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9769 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9770 | return; |
| 9771 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9772 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9773 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9774 | // Greylist some idioms by putting them into a warning subcategory. |
| 9775 | if (ObjCMessageExpr *ME |
| 9776 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9777 | Selector Sel = ME->getSelector(); |
| 9778 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9779 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9780 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9781 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9782 | |
| 9783 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9784 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9785 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9786 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9787 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9788 | Loc = Op->getOperatorLoc(); |
| 9789 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9790 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9791 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9792 | return; |
| 9793 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9794 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9795 | Loc = Op->getOperatorLoc(); |
| 9796 | } else { |
| 9797 | // Not an assignment. |
| 9798 | return; |
| 9799 | } |
| 9800 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9801 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 9802 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9803 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9804 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9805 | |
| 9806 | if (IsOrAssign) |
| 9807 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9808 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9809 | else |
| 9810 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9811 | << FixItHint::CreateReplacement(Loc, "=="); |
| 9812 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9813 | Diag(Loc, diag::note_condition_assign_silence) |
| 9814 | << FixItHint::CreateInsertion(Open, "(") |
| 9815 | << FixItHint::CreateInsertion(Close, ")"); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9816 | } |
| 9817 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9818 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9819 | /// that the user intended an assignment used as condition. |
| 9820 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9821 | // Don't warn if the parens came from a macro. |
| 9822 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9823 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9824 | return; |
| 9825 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9826 | Expr *E = parenE->IgnoreParens(); |
| 9827 | |
| 9828 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9829 | if (opE->getOpcode() == BO_EQ && |
| 9830 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9831 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9832 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9833 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9834 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
| 9835 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9836 | << FixItHint::CreateReplacement(Loc, "="); |
| 9837 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9838 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9839 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9840 | } |
| 9841 | } |
| 9842 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9843 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 9844 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9845 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9846 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9847 | |
| 9848 | if (!E->isTypeDependent()) { |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9849 | if (E->isBoundMemberFunction(Context)) |
| 9850 | return Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9851 | << E->getSourceRange(); |
| 9852 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9853 | if (getLangOptions().CPlusPlus) |
| 9854 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9855 | |
| 9856 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9857 | |
| 9858 | QualType T = E->getType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9859 | if (!T->isScalarType()) // C99 6.8.4.1p1 |
| 9860 | return Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9861 | << T << E->getSourceRange(); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9862 | } |
| 9863 | |
| 9864 | return false; |
| 9865 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9866 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9867 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9868 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9869 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9870 | return ExprError(); |
| 9871 | |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9872 | if (CheckBooleanCondition(Sub, Loc)) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9873 | return ExprError(); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9874 | |
| 9875 | return Owned(Sub); |
| 9876 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9877 | |
| 9878 | /// Check for operands with placeholder types and complain if found. |
| 9879 | /// Returns true if there was an error and no recovery was possible. |
| 9880 | ExprResult Sema::CheckPlaceholderExpr(Expr *E, SourceLocation Loc) { |
| 9881 | const BuiltinType *BT = E->getType()->getAs<BuiltinType>(); |
| 9882 | if (!BT || !BT->isPlaceholderType()) return Owned(E); |
| 9883 | |
| 9884 | // If this is overload, check for a single overload. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 9885 | assert(BT->getKind() == BuiltinType::Overload); |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9886 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
| 9887 | E->getSourceRange(), |
| 9888 | QualType(), |
| 9889 | diag::err_ovl_unresolvable); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9890 | } |