Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | c3a6ade | 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" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 19 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 22 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 23 | #include "clang/AST/Expr.h" |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 26 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 28 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 29 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 30 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 31 | #include "clang/Lex/LiteralSupport.h" |
| 32 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 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 | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 38 | #include "clang/Sema/Template.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | using namespace sema; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 41 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 42 | |
Douglas Gregor | 171c45a | 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 | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 52 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 53 | /// decls. |
| 54 | /// |
Douglas Gregor | 171c45a | 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 | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 57 | /// |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 58 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 59 | bool UnknownObjCClass) { |
Douglas Gregor | 5bb5e4a | 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 | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 71 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 5bb5e4a | 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 | 30482bc | 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 | b2bc2e6 | 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 | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 85 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 86 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 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 | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 92 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 20b2ebd | 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 | 73067a0 | 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 | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 124 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Douglas Gregor | 20b2ebd | 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 | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 144 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 150 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 152 | return; |
Douglas Gregor | c298ffc | 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 | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 156 | int sentinelPos = attr->getSentinel(); |
| 157 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | |
Mike Stump | 87c57ac | 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 | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 161 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 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 | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 175 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 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 | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 185 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 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 | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 189 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 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 | 0aa5c45 | 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 | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 205 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 206 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 207 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 208 | return; |
| 209 | |
| 210 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 211 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 212 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 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 | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 222 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 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 | 7ddbcf4 | 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 | e981a8c | 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 | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 237 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 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 | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Douglas Gregor | 87f95b0 | 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 | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 254 | //===----------------------------------------------------------------------===// |
| 255 | // Standard Promotions and Conversions |
| 256 | //===----------------------------------------------------------------------===// |
| 257 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 258 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 259 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 260 | QualType Ty = E->getType(); |
| 261 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 262 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 263 | if (Ty->isFunctionType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 264 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 265 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 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 | 9321c74 | 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 | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 278 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 279 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 280 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 281 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 282 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 283 | } |
| 284 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 285 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 286 | // C++ [conv.lval]p1: |
| 287 | // A glvalue of a non-function, non-array type T can be |
| 288 | // converted to a prvalue. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 289 | if (!E->isGLValue()) return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 290 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 291 | QualType T = E->getType(); |
| 292 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 293 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 294 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 295 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 296 | ExprResult Res = ConvertPropertyForRValue(E); |
| 297 | if (Res.isInvalid()) |
| 298 | return Owned(E); |
| 299 | E = Res.take(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 300 | if (!E->isGLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 301 | return Owned(E); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 302 | } |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 303 | |
| 304 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 305 | // expressions of certain types in C++. |
| 306 | if (getLangOptions().CPlusPlus && |
| 307 | (E->getType() == Context.OverloadTy || |
| 308 | T->isDependentType() || |
| 309 | T->isRecordType())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 310 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 311 | |
| 312 | // The C standard is actually really unclear on this point, and |
| 313 | // DR106 tells us what the result should be but not why. It's |
| 314 | // generally best to say that void types just doesn't undergo |
| 315 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 316 | // 'void' type are never l-values, but qualified void can be. |
| 317 | if (T->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 318 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 319 | |
| 320 | // C++ [conv.lval]p1: |
| 321 | // [...] If T is a non-class type, the type of the prvalue is the |
| 322 | // cv-unqualified version of T. Otherwise, the type of the |
| 323 | // rvalue is T. |
| 324 | // |
| 325 | // C99 6.3.2.1p2: |
| 326 | // If the lvalue has qualified type, the value has the unqualified |
| 327 | // version of the type of the lvalue; otherwise, the value has the |
| 328 | // type of the lvalue. |
| 329 | if (T.hasQualifiers()) |
| 330 | T = T.getUnqualifiedType(); |
| 331 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 332 | CheckArrayAccess(E); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 333 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 334 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 335 | E, 0, VK_RValue)); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 336 | } |
| 337 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 338 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 339 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 340 | if (Res.isInvalid()) |
| 341 | return ExprError(); |
| 342 | Res = DefaultLvalueConversion(Res.take()); |
| 343 | if (Res.isInvalid()) |
| 344 | return ExprError(); |
| 345 | return move(Res); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 349 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 350 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 351 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 352 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 353 | /// In these instances, this routine should *not* be called. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 354 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 355 | // First, convert to an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 356 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 357 | if (Res.isInvalid()) |
| 358 | return Owned(E); |
| 359 | E = Res.take(); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 360 | |
| 361 | QualType Ty = E->getType(); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 362 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 363 | |
| 364 | // Try to perform integral promotions if the object has a theoretically |
| 365 | // promotable type. |
| 366 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 367 | // C99 6.3.1.1p2: |
| 368 | // |
| 369 | // The following may be used in an expression wherever an int or |
| 370 | // unsigned int may be used: |
| 371 | // - an object or expression with an integer type whose integer |
| 372 | // conversion rank is less than or equal to the rank of int |
| 373 | // and unsigned int. |
| 374 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 375 | // |
| 376 | // If an int can represent all values of the original type, the |
| 377 | // value is converted to an int; otherwise, it is converted to an |
| 378 | // unsigned int. These are called the integer promotions. All |
| 379 | // other types are unchanged by the integer promotions. |
| 380 | |
| 381 | QualType PTy = Context.isPromotableBitField(E); |
| 382 | if (!PTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 383 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 384 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 385 | } |
| 386 | if (Ty->isPromotableIntegerType()) { |
| 387 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 388 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 389 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 390 | } |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 391 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 392 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 395 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 397 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 398 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 399 | QualType Ty = E->getType(); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 400 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 402 | ExprResult Res = UsualUnaryConversions(E); |
| 403 | if (Res.isInvalid()) |
| 404 | return Owned(E); |
| 405 | E = Res.take(); |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 407 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 408 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 409 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 410 | |
| 411 | return Owned(E); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 414 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 415 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 416 | /// interfaces passed by value. |
| 417 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 418 | FunctionDecl *FDecl) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 419 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
| 420 | if (ExprRes.isInvalid()) |
| 421 | return ExprError(); |
| 422 | E = ExprRes.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 424 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 425 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 426 | // etc. |
| 427 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 428 | return Owned(E); |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 429 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 430 | if (E->getType()->isObjCObjectType() && |
| 431 | DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 432 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 433 | << E->getType() << CT)) |
| 434 | return ExprError(); |
Douglas Gregor | 7ca84af | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 435 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 436 | if (!E->getType()->isPODType() && |
| 437 | DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 438 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 439 | << E->getType() << CT)) |
| 440 | return ExprError(); |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 441 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 442 | return Owned(E); |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 445 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 446 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 448 | /// responsible for emitting appropriate error diagnostics. |
| 449 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 450 | /// GCC. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 451 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 452 | bool isCompAssign) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | if (!isCompAssign) { |
| 454 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 455 | if (lhsExpr.isInvalid()) |
| 456 | return QualType(); |
| 457 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 458 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 459 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 460 | if (rhsExpr.isInvalid()) |
| 461 | return QualType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 462 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 464 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 465 | QualType lhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 466 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | QualType rhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 468 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 469 | |
| 470 | // If both types are identical, no conversion is needed. |
| 471 | if (lhs == rhs) |
| 472 | return lhs; |
| 473 | |
| 474 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 475 | // The caller can deal with this (e.g. pointer + int). |
| 476 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 477 | return lhs; |
| 478 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 479 | // Apply unary and bitfield promotions to the LHS's type. |
| 480 | QualType lhs_unpromoted = lhs; |
| 481 | if (lhs->isPromotableIntegerType()) |
| 482 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 483 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 484 | if (!LHSBitfieldPromoteTy.isNull()) |
| 485 | lhs = LHSBitfieldPromoteTy; |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 486 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 487 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 488 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 489 | // If both types are identical, no conversion is needed. |
| 490 | if (lhs == rhs) |
| 491 | return lhs; |
| 492 | |
| 493 | // At this point, we have two different arithmetic types. |
| 494 | |
| 495 | // Handle complex types first (C99 6.3.1.8p1). |
| 496 | bool LHSComplexFloat = lhs->isComplexType(); |
| 497 | bool RHSComplexFloat = rhs->isComplexType(); |
| 498 | if (LHSComplexFloat || RHSComplexFloat) { |
| 499 | // if we have an integer operand, the result is the complex type. |
| 500 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 501 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 502 | if (rhs->isIntegerType()) { |
| 503 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 504 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 505 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 506 | } else { |
| 507 | assert(rhs->isComplexIntegerType()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 508 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 509 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 510 | return lhs; |
| 511 | } |
| 512 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 513 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 514 | if (!isCompAssign) { |
| 515 | // int -> float -> _Complex float |
| 516 | if (lhs->isIntegerType()) { |
| 517 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 518 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 519 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 520 | } else { |
| 521 | assert(lhs->isComplexIntegerType()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 522 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 525 | return rhs; |
| 526 | } |
| 527 | |
| 528 | // This handles complex/complex, complex/float, or float/complex. |
| 529 | // When both operands are complex, the shorter operand is converted to the |
| 530 | // type of the longer, and that is the type of the result. This corresponds |
| 531 | // to what is done when combining two real floating-point operands. |
| 532 | // The fun begins when size promotion occur across type domains. |
| 533 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 534 | // floating-point type, the less precise type is converted, within it's |
| 535 | // real or complex domain, to the precision of the other type. For example, |
| 536 | // when combining a "long double" with a "double _Complex", the |
| 537 | // "double _Complex" is promoted to "long double _Complex". |
| 538 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 539 | |
| 540 | // If both are complex, just cast to the more precise type. |
| 541 | if (LHSComplexFloat && RHSComplexFloat) { |
| 542 | if (order > 0) { |
| 543 | // _Complex float -> _Complex double |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 544 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 545 | return lhs; |
| 546 | |
| 547 | } else if (order < 0) { |
| 548 | // _Complex float -> _Complex double |
| 549 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 550 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 551 | return rhs; |
| 552 | } |
| 553 | return lhs; |
| 554 | } |
| 555 | |
| 556 | // If just the LHS is complex, the RHS needs to be converted, |
| 557 | // and the LHS might need to be promoted. |
| 558 | if (LHSComplexFloat) { |
| 559 | if (order > 0) { // LHS is wider |
| 560 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 561 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 562 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 563 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 564 | return lhs; |
| 565 | } |
| 566 | |
| 567 | // RHS is at least as wide. Find its corresponding complex type. |
| 568 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 569 | |
| 570 | // double -> _Complex double |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 571 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 572 | |
| 573 | // _Complex float -> _Complex double |
| 574 | if (!isCompAssign && order < 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 575 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 576 | |
| 577 | return result; |
| 578 | } |
| 579 | |
| 580 | // Just the RHS is complex, so the LHS needs to be converted |
| 581 | // and the RHS might need to be promoted. |
| 582 | assert(RHSComplexFloat); |
| 583 | |
| 584 | if (order < 0) { // RHS is wider |
| 585 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 586 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e84389b | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 587 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 588 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 589 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 590 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 591 | return rhs; |
| 592 | } |
| 593 | |
| 594 | // LHS is at least as wide. Find its corresponding complex type. |
| 595 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 596 | |
| 597 | // double -> _Complex double |
| 598 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 599 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 600 | |
| 601 | // _Complex float -> _Complex double |
| 602 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 603 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 604 | |
| 605 | return result; |
| 606 | } |
| 607 | |
| 608 | // Now handle "real" floating types (i.e. float, double, long double). |
| 609 | bool LHSFloat = lhs->isRealFloatingType(); |
| 610 | bool RHSFloat = rhs->isRealFloatingType(); |
| 611 | if (LHSFloat || RHSFloat) { |
| 612 | // If we have two real floating types, convert the smaller operand |
| 613 | // to the bigger result. |
| 614 | if (LHSFloat && RHSFloat) { |
| 615 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 616 | if (order > 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 617 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 618 | return lhs; |
| 619 | } |
| 620 | |
| 621 | assert(order < 0 && "illegal float comparison"); |
| 622 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 623 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 624 | return rhs; |
| 625 | } |
| 626 | |
| 627 | // If we have an integer operand, the result is the real floating type. |
| 628 | if (LHSFloat) { |
| 629 | if (rhs->isIntegerType()) { |
| 630 | // Convert rhs to the lhs floating point type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 631 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 632 | return lhs; |
| 633 | } |
| 634 | |
| 635 | // Convert both sides to the appropriate complex float. |
| 636 | assert(rhs->isComplexIntegerType()); |
| 637 | QualType result = Context.getComplexType(lhs); |
| 638 | |
| 639 | // _Complex int -> _Complex float |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 640 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 641 | |
| 642 | // float -> _Complex float |
| 643 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 644 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 645 | |
| 646 | return result; |
| 647 | } |
| 648 | |
| 649 | assert(RHSFloat); |
| 650 | if (lhs->isIntegerType()) { |
| 651 | // Convert lhs to the rhs floating point type. |
| 652 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 653 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 654 | return rhs; |
| 655 | } |
| 656 | |
| 657 | // Convert both sides to the appropriate complex float. |
| 658 | assert(lhs->isComplexIntegerType()); |
| 659 | QualType result = Context.getComplexType(rhs); |
| 660 | |
| 661 | // _Complex int -> _Complex float |
| 662 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 663 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 664 | |
| 665 | // float -> _Complex float |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 666 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 667 | |
| 668 | return result; |
| 669 | } |
| 670 | |
| 671 | // Handle GCC complex int extension. |
| 672 | // FIXME: if the operands are (int, _Complex long), we currently |
| 673 | // don't promote the complex. Also, signedness? |
| 674 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 675 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 676 | if (lhsComplexInt && rhsComplexInt) { |
| 677 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 678 | rhsComplexInt->getElementType()); |
| 679 | assert(order && "inequal types with equal element ordering"); |
| 680 | if (order > 0) { |
| 681 | // _Complex int -> _Complex long |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 682 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 683 | return lhs; |
| 684 | } |
| 685 | |
| 686 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 687 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 688 | return rhs; |
| 689 | } else if (lhsComplexInt) { |
| 690 | // int -> _Complex int |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 691 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 692 | return lhs; |
| 693 | } else if (rhsComplexInt) { |
| 694 | // int -> _Complex int |
| 695 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 696 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 697 | return rhs; |
| 698 | } |
| 699 | |
| 700 | // Finally, we have two differing integer types. |
| 701 | // The rules for this case are in C99 6.3.1.8 |
| 702 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 703 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 704 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 705 | if (lhsSigned == rhsSigned) { |
| 706 | // Same signedness; use the higher-ranked type |
| 707 | if (compare >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 709 | return lhs; |
| 710 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 711 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 712 | return rhs; |
| 713 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 714 | // The unsigned type has greater than or equal rank to the |
| 715 | // signed type, so use the unsigned type |
| 716 | if (rhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 717 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 718 | return lhs; |
| 719 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 720 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 721 | return rhs; |
| 722 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 723 | // The two types are different widths; if we are here, that |
| 724 | // means the signed type is larger than the unsigned type, so |
| 725 | // use the signed type. |
| 726 | if (lhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 727 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 728 | return lhs; |
| 729 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 730 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 731 | return rhs; |
| 732 | } else { |
| 733 | // The signed type is higher-ranked than the unsigned type, |
| 734 | // but isn't actually any bigger (like unsigned int and long |
| 735 | // on most 32-bit systems). Use the unsigned type corresponding |
| 736 | // to the signed type. |
| 737 | QualType result = |
| 738 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 739 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 740 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 741 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 742 | return result; |
| 743 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 746 | //===----------------------------------------------------------------------===// |
| 747 | // Semantic Analysis for various Expression Types |
| 748 | //===----------------------------------------------------------------------===// |
| 749 | |
| 750 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 751 | ExprResult |
| 752 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 753 | SourceLocation DefaultLoc, |
| 754 | SourceLocation RParenLoc, |
| 755 | Expr *ControllingExpr, |
| 756 | MultiTypeArg types, |
| 757 | MultiExprArg exprs) { |
| 758 | unsigned NumAssocs = types.size(); |
| 759 | assert(NumAssocs == exprs.size()); |
| 760 | |
| 761 | ParsedType *ParsedTypes = types.release(); |
| 762 | Expr **Exprs = exprs.release(); |
| 763 | |
| 764 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 765 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 766 | if (ParsedTypes[i]) |
| 767 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 768 | else |
| 769 | Types[i] = 0; |
| 770 | } |
| 771 | |
| 772 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 773 | ControllingExpr, Types, Exprs, |
| 774 | NumAssocs); |
Benjamin Kramer | 3462376 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 775 | delete [] Types; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 776 | return ER; |
| 777 | } |
| 778 | |
| 779 | ExprResult |
| 780 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 781 | SourceLocation DefaultLoc, |
| 782 | SourceLocation RParenLoc, |
| 783 | Expr *ControllingExpr, |
| 784 | TypeSourceInfo **Types, |
| 785 | Expr **Exprs, |
| 786 | unsigned NumAssocs) { |
| 787 | bool TypeErrorFound = false, |
| 788 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 789 | ContainsUnexpandedParameterPack |
| 790 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 791 | |
| 792 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 793 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 794 | ContainsUnexpandedParameterPack = true; |
| 795 | |
| 796 | if (Types[i]) { |
| 797 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 798 | ContainsUnexpandedParameterPack = true; |
| 799 | |
| 800 | if (Types[i]->getType()->isDependentType()) { |
| 801 | IsResultDependent = true; |
| 802 | } else { |
| 803 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 804 | // complete object type other than a variably modified type." |
| 805 | unsigned D = 0; |
| 806 | if (Types[i]->getType()->isIncompleteType()) |
| 807 | D = diag::err_assoc_type_incomplete; |
| 808 | else if (!Types[i]->getType()->isObjectType()) |
| 809 | D = diag::err_assoc_type_nonobject; |
| 810 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 811 | D = diag::err_assoc_type_variably_modified; |
| 812 | |
| 813 | if (D != 0) { |
| 814 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 815 | << Types[i]->getTypeLoc().getSourceRange() |
| 816 | << Types[i]->getType(); |
| 817 | TypeErrorFound = true; |
| 818 | } |
| 819 | |
| 820 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 821 | // selection shall specify compatible types." |
| 822 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 823 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 824 | Context.typesAreCompatible(Types[i]->getType(), |
| 825 | Types[j]->getType())) { |
| 826 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 827 | diag::err_assoc_compatible_types) |
| 828 | << Types[j]->getTypeLoc().getSourceRange() |
| 829 | << Types[j]->getType() |
| 830 | << Types[i]->getType(); |
| 831 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 832 | diag::note_compat_assoc) |
| 833 | << Types[i]->getTypeLoc().getSourceRange() |
| 834 | << Types[i]->getType(); |
| 835 | TypeErrorFound = true; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | if (TypeErrorFound) |
| 841 | return ExprError(); |
| 842 | |
| 843 | // If we determined that the generic selection is result-dependent, don't |
| 844 | // try to compute the result expression. |
| 845 | if (IsResultDependent) |
| 846 | return Owned(new (Context) GenericSelectionExpr( |
| 847 | Context, KeyLoc, ControllingExpr, |
| 848 | Types, Exprs, NumAssocs, DefaultLoc, |
| 849 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 850 | |
| 851 | llvm::SmallVector<unsigned, 1> CompatIndices; |
| 852 | unsigned DefaultIndex = -1U; |
| 853 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 854 | if (!Types[i]) |
| 855 | DefaultIndex = i; |
| 856 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 857 | Types[i]->getType())) |
| 858 | CompatIndices.push_back(i); |
| 859 | } |
| 860 | |
| 861 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 862 | // type compatible with at most one of the types named in its generic |
| 863 | // association list." |
| 864 | if (CompatIndices.size() > 1) { |
| 865 | // We strip parens here because the controlling expression is typically |
| 866 | // parenthesized in macro definitions. |
| 867 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 868 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 869 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 870 | << (unsigned) CompatIndices.size(); |
| 871 | for (llvm::SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
| 872 | E = CompatIndices.end(); I != E; ++I) { |
| 873 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 874 | diag::note_compat_assoc) |
| 875 | << Types[*I]->getTypeLoc().getSourceRange() |
| 876 | << Types[*I]->getType(); |
| 877 | } |
| 878 | return ExprError(); |
| 879 | } |
| 880 | |
| 881 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 882 | // its controlling expression shall have type compatible with exactly one of |
| 883 | // the types named in its generic association list." |
| 884 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 885 | // We strip parens here because the controlling expression is typically |
| 886 | // parenthesized in macro definitions. |
| 887 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 888 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 889 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 890 | return ExprError(); |
| 891 | } |
| 892 | |
| 893 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 894 | // type name that is compatible with the type of the controlling expression, |
| 895 | // then the result expression of the generic selection is the expression |
| 896 | // in that generic association. Otherwise, the result expression of the |
| 897 | // generic selection is the expression in the default generic association." |
| 898 | unsigned ResultIndex = |
| 899 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 900 | |
| 901 | return Owned(new (Context) GenericSelectionExpr( |
| 902 | Context, KeyLoc, ControllingExpr, |
| 903 | Types, Exprs, NumAssocs, DefaultLoc, |
| 904 | RParenLoc, ContainsUnexpandedParameterPack, |
| 905 | ResultIndex)); |
| 906 | } |
| 907 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 908 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 909 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 910 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 911 | /// multiple tokens. However, the common case is that StringToks points to one |
| 912 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 913 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 914 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 915 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 916 | assert(NumStringToks && "Must have at least one string!"); |
| 917 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 918 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 919 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 920 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 921 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 922 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 923 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 924 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 926 | QualType StrTy = Context.CharTy; |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 927 | if (Literal.AnyWide) |
| 928 | StrTy = Context.getWCharType(); |
| 929 | else if (Literal.Pascal) |
| 930 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 931 | |
| 932 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
Chris Lattner | a8687ae | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 933 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 934 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 935 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 936 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 937 | // the nul terminator character as well as the string length for pascal |
| 938 | // strings. |
| 939 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 940 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 941 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 942 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 943 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 944 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 945 | Literal.GetStringLength(), |
Anders Carlsson | 7524540 | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 946 | Literal.AnyWide, Literal.Pascal, StrTy, |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 947 | &StringTokLocs[0], |
| 948 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 949 | } |
| 950 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 951 | enum CaptureResult { |
| 952 | /// No capture is required. |
| 953 | CR_NoCapture, |
| 954 | |
| 955 | /// A capture is required. |
| 956 | CR_Capture, |
| 957 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 958 | /// A by-ref capture is required. |
| 959 | CR_CaptureByRef, |
| 960 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 961 | /// An error occurred when trying to capture the given variable. |
| 962 | CR_Error |
| 963 | }; |
| 964 | |
| 965 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 966 | /// |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 967 | /// \param var - the variable referenced |
| 968 | /// \param DC - the context which we couldn't capture through |
| 969 | static CaptureResult |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 970 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 971 | VarDecl *var, DeclContext *DC) { |
| 972 | switch (S.ExprEvalContexts.back().Context) { |
| 973 | case Sema::Unevaluated: |
| 974 | // The argument will never be evaluated, so don't complain. |
| 975 | return CR_NoCapture; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 976 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 977 | case Sema::PotentiallyEvaluated: |
| 978 | case Sema::PotentiallyEvaluatedIfUsed: |
| 979 | break; |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 980 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 981 | case Sema::PotentiallyPotentiallyEvaluated: |
| 982 | // FIXME: delay these! |
| 983 | break; |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 984 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 986 | // Don't diagnose about capture if we're not actually in code right |
| 987 | // now; in general, there are more appropriate places that will |
| 988 | // diagnose this. |
| 989 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 990 | |
John McCall | 92d627e | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 991 | // Certain madnesses can happen with parameter declarations, which |
| 992 | // we want to ignore. |
| 993 | if (isa<ParmVarDecl>(var)) { |
| 994 | // - If the parameter still belongs to the translation unit, then |
| 995 | // we're actually just using one parameter in the declaration of |
| 996 | // the next. This is useful in e.g. VLAs. |
| 997 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 998 | return CR_NoCapture; |
| 999 | |
| 1000 | // - This particular madness can happen in ill-formed default |
| 1001 | // arguments; claim it's okay and let downstream code handle it. |
| 1002 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1003 | return CR_NoCapture; |
| 1004 | } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1005 | |
| 1006 | DeclarationName functionName; |
| 1007 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1008 | functionName = fn->getDeclName(); |
| 1009 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1010 | |
| 1011 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1012 | << var->getIdentifier() << functionName; |
| 1013 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1014 | << var->getIdentifier(); |
| 1015 | |
| 1016 | return CR_Error; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1019 | /// There is a well-formed capture at a particular scope level; |
| 1020 | /// propagate it through all the nested blocks. |
| 1021 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1022 | const BlockDecl::Capture &capture) { |
| 1023 | VarDecl *var = capture.getVariable(); |
| 1024 | |
| 1025 | // Update all the inner blocks with the capture information. |
| 1026 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1027 | i != e; ++i) { |
| 1028 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1029 | innerBlock->Captures.push_back( |
| 1030 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1031 | /*nested*/ true, capture.getCopyExpr())); |
| 1032 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1033 | } |
| 1034 | |
| 1035 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1036 | } |
| 1037 | |
| 1038 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1039 | /// given value in the current context requires a variable capture. |
| 1040 | /// |
| 1041 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1042 | /// up-to-date. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1043 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1044 | ValueDecl *value) { |
| 1045 | // Only variables ever require capture. |
| 1046 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1047 | if (!var) return CR_NoCapture; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Fast path: variables from the current context never require capture. |
| 1050 | DeclContext *DC = S.CurContext; |
| 1051 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1052 | |
| 1053 | // Only variables with local storage require capture. |
| 1054 | // FIXME: What about 'const' variables in C++? |
| 1055 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1056 | |
| 1057 | // Otherwise, we need to capture. |
| 1058 | |
| 1059 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1060 | do { |
| 1061 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1062 | // scopes don't work. |
| 1063 | if (!isa<BlockDecl>(DC)) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1064 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1065 | |
| 1066 | BlockScopeInfo *blockScope = |
| 1067 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1068 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1069 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1070 | // Check whether we've already captured it in this block. If so, |
| 1071 | // we're done. |
| 1072 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1073 | return propagateCapture(S, functionScopesIndex, |
| 1074 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1075 | |
| 1076 | functionScopesIndex--; |
| 1077 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1078 | } while (var->getDeclContext() != DC); |
| 1079 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1080 | // Okay, we descended all the way to the block that defines the variable. |
| 1081 | // Actually try to capture it. |
| 1082 | QualType type = var->getType(); |
| 1083 | |
| 1084 | // Prohibit variably-modified types. |
| 1085 | if (type->isVariablyModifiedType()) { |
| 1086 | S.Diag(loc, diag::err_ref_vm_type); |
| 1087 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1088 | return CR_Error; |
| 1089 | } |
| 1090 | |
| 1091 | // Prohibit arrays, even in __block variables, but not references to |
| 1092 | // them. |
| 1093 | if (type->isArrayType()) { |
| 1094 | S.Diag(loc, diag::err_ref_array_type); |
| 1095 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1096 | return CR_Error; |
| 1097 | } |
| 1098 | |
| 1099 | S.MarkDeclarationReferenced(loc, var); |
| 1100 | |
| 1101 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1102 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1103 | |
| 1104 | // Build a copy expression. |
| 1105 | Expr *copyExpr = 0; |
| 1106 | if (!byRef && S.getLangOptions().CPlusPlus && |
| 1107 | !type->isDependentType() && type->isStructureOrClassType()) { |
| 1108 | // According to the blocks spec, the capture of a variable from |
| 1109 | // the stack requires a const copy constructor. This is not true |
| 1110 | // of the copy/move done to move a __block variable to the heap. |
| 1111 | type.addConst(); |
| 1112 | |
| 1113 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1114 | ExprResult result = |
| 1115 | S.PerformCopyInitialization( |
| 1116 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1117 | type, false), |
| 1118 | loc, S.Owned(declRef)); |
| 1119 | |
| 1120 | // Build a full-expression copy expression if initialization |
| 1121 | // succeeded and used a non-trivial constructor. Recover from |
| 1122 | // errors by pretending that the copy isn't necessary. |
| 1123 | if (!result.isInvalid() && |
| 1124 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1125 | result = S.MaybeCreateExprWithCleanups(result); |
| 1126 | copyExpr = result.take(); |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | // We're currently at the declarer; go back to the closure. |
| 1131 | functionScopesIndex++; |
| 1132 | BlockScopeInfo *blockScope = |
| 1133 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1134 | |
| 1135 | // Build a valid capture in this scope. |
| 1136 | blockScope->Captures.push_back( |
| 1137 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1138 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1139 | |
| 1140 | // Propagate that to inner captures if necessary. |
| 1141 | return propagateCapture(S, functionScopesIndex, |
| 1142 | blockScope->Captures.back()); |
| 1143 | } |
| 1144 | |
| 1145 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1146 | const DeclarationNameInfo &NameInfo, |
| 1147 | bool byRef) { |
| 1148 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1149 | |
| 1150 | VarDecl *var = cast<VarDecl>(vd); |
| 1151 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1152 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1153 | |
| 1154 | QualType exprType = var->getType().getNonReferenceType(); |
| 1155 | |
| 1156 | BlockDeclRefExpr *BDRE; |
| 1157 | if (!byRef) { |
| 1158 | // The variable will be bound by copy; make it const within the |
| 1159 | // closure, but record that this was done in the expression. |
| 1160 | bool constAdded = !exprType.isConstQualified(); |
| 1161 | exprType.addConst(); |
| 1162 | |
| 1163 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1164 | NameInfo.getLoc(), false, |
| 1165 | constAdded); |
| 1166 | } else { |
| 1167 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1168 | NameInfo.getLoc(), true); |
| 1169 | } |
| 1170 | |
| 1171 | return S.Owned(BDRE); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1172 | } |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1173 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1174 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1175 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1176 | SourceLocation Loc, |
| 1177 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1178 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1179 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1182 | /// BuildDeclRefExpr - Build an expression that references a |
| 1183 | /// declaration that does not require a closure capture. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1184 | ExprResult |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1185 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1186 | const DeclarationNameInfo &NameInfo, |
| 1187 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1188 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1190 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1191 | SS? SS->getWithLocInContext(Context) |
| 1192 | : NestedNameSpecifierLoc(), |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1193 | D, NameInfo, Ty, VK); |
| 1194 | |
| 1195 | // Just in case we're building an illegal pointer-to-member. |
| 1196 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1197 | E->setObjectKind(OK_BitField); |
| 1198 | |
| 1199 | return Owned(E); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1202 | static ExprResult |
| 1203 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1204 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1205 | DeclAccessPair FoundDecl, |
| 1206 | const DeclarationNameInfo &MemberNameInfo); |
| 1207 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1208 | ExprResult |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1209 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1210 | SourceLocation loc, |
| 1211 | IndirectFieldDecl *indirectField, |
| 1212 | Expr *baseObjectExpr, |
| 1213 | SourceLocation opLoc) { |
| 1214 | // First, build the expression that refers to the base object. |
| 1215 | |
| 1216 | bool baseObjectIsPointer = false; |
| 1217 | Qualifiers baseQuals; |
| 1218 | |
| 1219 | // Case 1: the base of the indirect field is not a field. |
| 1220 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1221 | CXXScopeSpec EmptySS; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1222 | if (baseVariable) { |
| 1223 | assert(baseVariable->getType()->isRecordType()); |
| 1224 | |
| 1225 | // In principle we could have a member access expression that |
| 1226 | // accesses an anonymous struct/union that's a static member of |
| 1227 | // the base object's class. However, under the current standard, |
| 1228 | // static data members cannot be anonymous structs or unions. |
| 1229 | // Supporting this is as easy as building a MemberExpr here. |
| 1230 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1231 | |
| 1232 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1233 | |
| 1234 | ExprResult result = |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1235 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1236 | if (result.isInvalid()) return ExprError(); |
| 1237 | |
| 1238 | baseObjectExpr = result.take(); |
| 1239 | baseObjectIsPointer = false; |
| 1240 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1241 | |
| 1242 | // Case 2: the base of the indirect field is a field and the user |
| 1243 | // wrote a member expression. |
| 1244 | } else if (baseObjectExpr) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1245 | // The caller provided the base object expression. Determine |
| 1246 | // whether its a pointer and whether it adds any qualifiers to the |
| 1247 | // anonymous struct/union fields we're looking into. |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1248 | QualType objectType = baseObjectExpr->getType(); |
| 1249 | |
| 1250 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1251 | baseObjectIsPointer = true; |
| 1252 | objectType = ptr->getPointeeType(); |
| 1253 | } else { |
| 1254 | baseObjectIsPointer = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1255 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1256 | baseQuals = objectType.getQualifiers(); |
| 1257 | |
| 1258 | // Case 3: the base of the indirect field is a field and we should |
| 1259 | // build an implicit member access. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | // We've found a member of an anonymous struct/union that is |
| 1262 | // inside a non-anonymous struct/union, so in a well-formed |
| 1263 | // program our base object expression is "this". |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1264 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 1265 | if (!method) { |
| 1266 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1267 | << indirectField->getDeclName(); |
| 1268 | return ExprError(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1271 | // Our base object expression is "this". |
| 1272 | baseObjectExpr = |
| 1273 | new (Context) CXXThisExpr(loc, method->getThisType(Context), |
| 1274 | /*isImplicit=*/ true); |
| 1275 | baseObjectIsPointer = true; |
| 1276 | baseQuals = Qualifiers::fromCVRMask(method->getTypeQualifiers()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // Build the implicit member references to the field of the |
| 1280 | // anonymous struct/union. |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1281 | Expr *result = baseObjectExpr; |
| 1282 | IndirectFieldDecl::chain_iterator |
| 1283 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1284 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1285 | // Build the first member access in the chain with full information. |
| 1286 | if (!baseVariable) { |
| 1287 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1288 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1289 | // FIXME: use the real found-decl info! |
| 1290 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1291 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1292 | // Make a nameInfo that properly uses the anonymous name. |
| 1293 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1294 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1295 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1296 | EmptySS, field, foundDecl, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1297 | memberNameInfo).take(); |
| 1298 | baseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1299 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1300 | // FIXME: check qualified member access |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1303 | // In all cases, we should now skip the first declaration in the chain. |
| 1304 | ++FI; |
| 1305 | |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1306 | while (FI != FEnd) { |
| 1307 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1308 | |
| 1309 | // FIXME: these are somewhat meaningless |
| 1310 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1311 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1312 | |
| 1313 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1314 | (FI == FEnd? SS : EmptySS), field, |
| 1315 | foundDecl, memberNameInfo) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1316 | .take(); |
| 1317 | } |
| 1318 | |
| 1319 | return Owned(result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1320 | } |
| 1321 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1322 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1323 | /// possibly a list of template arguments. |
| 1324 | /// |
| 1325 | /// If this produces template arguments, it is permitted to call |
| 1326 | /// DecomposeTemplateName. |
| 1327 | /// |
| 1328 | /// This actually loses a lot of source location information for |
| 1329 | /// non-standard name kinds; we should consider preserving that in |
| 1330 | /// some way. |
| 1331 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1332 | const UnqualifiedId &Id, |
| 1333 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1334 | DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1335 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1336 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1337 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1338 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1339 | |
| 1340 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1341 | Id.TemplateId->getTemplateArgs(), |
| 1342 | Id.TemplateId->NumArgs); |
| 1343 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1344 | TemplateArgsPtr.release(); |
| 1345 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1346 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1347 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1348 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1349 | TemplateArgs = &Buffer; |
| 1350 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1351 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1352 | TemplateArgs = 0; |
| 1353 | } |
| 1354 | } |
| 1355 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1356 | /// Determines if the given class is provably not derived from all of |
| 1357 | /// the prospective base classes. |
| 1358 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1359 | CXXRecordDecl *Record, |
| 1360 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1361 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1362 | return false; |
| 1363 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1364 | RecordDecl *RD = Record->getDefinition(); |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1365 | if (!RD) return false; |
| 1366 | Record = cast<CXXRecordDecl>(RD); |
| 1367 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1368 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1369 | E = Record->bases_end(); I != E; ++I) { |
| 1370 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1371 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1372 | if (!BaseRT) return false; |
| 1373 | |
| 1374 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1375 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
| 1379 | return true; |
| 1380 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1381 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1382 | enum IMAKind { |
| 1383 | /// The reference is definitely not an instance member access. |
| 1384 | IMA_Static, |
| 1385 | |
| 1386 | /// The reference may be an implicit instance member access. |
| 1387 | IMA_Mixed, |
| 1388 | |
| 1389 | /// The reference may be to an instance member, but it is invalid if |
| 1390 | /// so, because the context is not an instance method. |
| 1391 | IMA_Mixed_StaticContext, |
| 1392 | |
| 1393 | /// The reference may be to an instance member, but it is invalid if |
| 1394 | /// so, because the context is from an unrelated class. |
| 1395 | IMA_Mixed_Unrelated, |
| 1396 | |
| 1397 | /// The reference is definitely an implicit instance member access. |
| 1398 | IMA_Instance, |
| 1399 | |
| 1400 | /// The reference may be to an unresolved using declaration. |
| 1401 | IMA_Unresolved, |
| 1402 | |
| 1403 | /// The reference may be to an unresolved using declaration and the |
| 1404 | /// context is not an instance method. |
| 1405 | IMA_Unresolved_StaticContext, |
| 1406 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1407 | /// All possible referrents are instance members and the current |
| 1408 | /// context is not an instance method. |
| 1409 | IMA_Error_StaticContext, |
| 1410 | |
| 1411 | /// All possible referrents are instance members of an unrelated |
| 1412 | /// class. |
| 1413 | IMA_Error_Unrelated |
| 1414 | }; |
| 1415 | |
| 1416 | /// The given lookup names class member(s) and is not being used for |
| 1417 | /// an address-of-member expression. Classify the type of access |
| 1418 | /// according to whether it's possible that this reference names an |
| 1419 | /// instance member. This is best-effort; it is okay to |
| 1420 | /// conservatively answer "yes", in which case some errors will simply |
| 1421 | /// not be caught until template-instantiation. |
| 1422 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1423 | const LookupResult &R) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1424 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1425 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1426 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1427 | bool isStaticContext = |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1428 | (!isa<CXXMethodDecl>(DC) || |
| 1429 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1430 | |
| 1431 | if (R.isUnresolvableResult()) |
| 1432 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1433 | |
| 1434 | // Collect all the declaring classes of instance members we find. |
| 1435 | bool hasNonInstance = false; |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1436 | bool hasField = false; |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1437 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1438 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1439 | NamedDecl *D = *I; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1440 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1441 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1442 | if (dyn_cast<FieldDecl>(D)) |
| 1443 | hasField = true; |
| 1444 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1445 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1446 | Classes.insert(R->getCanonicalDecl()); |
| 1447 | } |
| 1448 | else |
| 1449 | hasNonInstance = true; |
| 1450 | } |
| 1451 | |
| 1452 | // If we didn't find any instance members, it can't be an implicit |
| 1453 | // member reference. |
| 1454 | if (Classes.empty()) |
| 1455 | return IMA_Static; |
| 1456 | |
| 1457 | // If the current context is not an instance method, it can't be |
| 1458 | // an implicit member reference. |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1459 | if (isStaticContext) { |
| 1460 | if (hasNonInstance) |
| 1461 | return IMA_Mixed_StaticContext; |
| 1462 | |
| 1463 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1464 | // C++0x [expr.prim.general]p10: |
| 1465 | // An id-expression that denotes a non-static data member or non-static |
| 1466 | // member function of a class can only be used: |
| 1467 | // (...) |
| 1468 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1469 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1470 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1471 | if (isUnevaluatedExpression) |
| 1472 | return IMA_Mixed_StaticContext; |
| 1473 | } |
| 1474 | |
| 1475 | return IMA_Error_StaticContext; |
| 1476 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1477 | |
Argyrios Kyrtzidis | 36e4ae3 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1478 | CXXRecordDecl * |
| 1479 | contextClass = cast<CXXMethodDecl>(DC)->getParent()->getCanonicalDecl(); |
| 1480 | |
| 1481 | // [class.mfct.non-static]p3: |
| 1482 | // ...is used in the body of a non-static member function of class X, |
| 1483 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 1484 | // non-static non-type member of some class C [...] |
| 1485 | // ...if C is not X or a base class of X, the class member access expression |
| 1486 | // is ill-formed. |
| 1487 | if (R.getNamingClass() && |
| 1488 | contextClass != R.getNamingClass()->getCanonicalDecl() && |
| 1489 | contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) |
| 1490 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1491 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1492 | // If we can prove that the current context is unrelated to all the |
| 1493 | // declaring classes, it can't be an implicit member reference (in |
| 1494 | // which case it's an error if any of those members are selected). |
Argyrios Kyrtzidis | 36e4ae3 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1495 | if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1496 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1497 | |
| 1498 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1499 | } |
| 1500 | |
| 1501 | /// Diagnose a reference to a field with no object available. |
| 1502 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1503 | const CXXScopeSpec &SS, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1504 | NamedDecl *rep, |
| 1505 | const DeclarationNameInfo &nameInfo) { |
| 1506 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1507 | SourceRange Range(Loc); |
| 1508 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1509 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1510 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1511 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1512 | if (MD->isStatic()) { |
| 1513 | // "invalid use of member 'x' in static member function" |
| 1514 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1515 | << Range << nameInfo.getName(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1521 | << nameInfo.getName() << Range; |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1528 | /// Diagnose an empty lookup. |
| 1529 | /// |
| 1530 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1531 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1532 | CorrectTypoContext CTC) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1533 | DeclarationName Name = R.getLookupName(); |
| 1534 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1535 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1536 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1537 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1538 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1539 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1540 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1541 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1542 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1543 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1544 | // If the original lookup was an unqualified lookup, fake an |
| 1545 | // unqualified lookup. This is useful when (for example) the |
| 1546 | // original lookup would not have found something because it was a |
| 1547 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1548 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1549 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1550 | if (isa<CXXRecordDecl>(DC)) { |
| 1551 | LookupQualifiedName(R, DC); |
| 1552 | |
| 1553 | if (!R.empty()) { |
| 1554 | // Don't give errors about ambiguities in this lookup. |
| 1555 | R.suppressDiagnostics(); |
| 1556 | |
| 1557 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1558 | bool isInstance = CurMethod && |
| 1559 | CurMethod->isInstance() && |
| 1560 | DC == CurMethod->getParent(); |
| 1561 | |
| 1562 | // Give a code modification hint to insert 'this->'. |
| 1563 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1564 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1565 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1566 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1567 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1568 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1569 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1570 | if (DepMethod) { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1571 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1572 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1573 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1574 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1575 | R.getNameLoc(), DepThisType, false); |
| 1576 | TemplateArgumentListInfo TList; |
| 1577 | if (ULE->hasExplicitTemplateArgs()) |
| 1578 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1580 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1581 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1582 | CXXDependentScopeMemberExpr *DepExpr = |
| 1583 | CXXDependentScopeMemberExpr::Create( |
| 1584 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1585 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1586 | R.getLookupNameInfo(), &TList); |
| 1587 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1588 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1589 | // FIXME: we should be able to handle this case too. It is correct |
| 1590 | // to add this-> here. This is a workaround for PR7947. |
| 1591 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1592 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1593 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1594 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1595 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1596 | |
| 1597 | // Do we really want to note all of these? |
| 1598 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1599 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1600 | |
| 1601 | // Tell the callee to try to recover. |
| 1602 | return false; |
| 1603 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1604 | |
| 1605 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1609 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1610 | DeclarationName Corrected; |
Daniel Dunbar | f7ced25 | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1611 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1612 | if (!R.empty()) { |
| 1613 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1614 | if (SS.isEmpty()) |
| 1615 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1616 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1617 | R.getLookupName().getAsString()); |
| 1618 | else |
| 1619 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1620 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1621 | << SS.getRange() |
| 1622 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1623 | R.getLookupName().getAsString()); |
| 1624 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1625 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1626 | << ND->getDeclName(); |
| 1627 | |
| 1628 | // Tell the callee to try to recover. |
| 1629 | return false; |
| 1630 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1632 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1633 | // FIXME: If we ended up with a typo for a type name or |
| 1634 | // Objective-C class name, we're in trouble because the parser |
| 1635 | // is in the wrong place to recover. Suggest the typo |
| 1636 | // correction, but don't make it a fix-it since we're not going |
| 1637 | // to recover well anyway. |
| 1638 | if (SS.isEmpty()) |
| 1639 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1640 | else |
| 1641 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1642 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1643 | << SS.getRange(); |
| 1644 | |
| 1645 | // Don't try to recover; it won't work. |
| 1646 | return true; |
| 1647 | } |
| 1648 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1649 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1650 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1651 | if (SS.isEmpty()) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1652 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1653 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1654 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1655 | << Name << computeDeclContext(SS, false) << Corrected |
| 1656 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1657 | return true; |
| 1658 | } |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1659 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | // Emit a special diagnostic for failed member lookups. |
| 1663 | // FIXME: computing the declaration context might fail here (?) |
| 1664 | if (!SS.isEmpty()) { |
| 1665 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1666 | << Name << computeDeclContext(SS, false) |
| 1667 | << SS.getRange(); |
| 1668 | return true; |
| 1669 | } |
| 1670 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1671 | // Give up, we can't recover. |
| 1672 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1673 | return true; |
| 1674 | } |
| 1675 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1676 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1677 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1678 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1679 | if (!IDecl) |
| 1680 | return 0; |
| 1681 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1682 | if (!ClassImpDecl) |
| 1683 | return 0; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1684 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1685 | if (!property) |
| 1686 | return 0; |
| 1687 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1688 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1689 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1690 | return 0; |
| 1691 | return property; |
| 1692 | } |
| 1693 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1694 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1695 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1696 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1697 | if (!IDecl) |
| 1698 | return false; |
| 1699 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1700 | if (!ClassImpDecl) |
| 1701 | return false; |
| 1702 | if (ObjCPropertyImplDecl *PIDecl |
| 1703 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1704 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1705 | PIDecl->getPropertyIvarDecl()) |
| 1706 | return false; |
| 1707 | |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1711 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1712 | LookupResult &Lookup, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1713 | IdentifierInfo *II, |
| 1714 | SourceLocation NameLoc) { |
| 1715 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1716 | bool LookForIvars; |
| 1717 | if (Lookup.empty()) |
| 1718 | LookForIvars = true; |
| 1719 | else if (CurMeth->isClassMethod()) |
| 1720 | LookForIvars = false; |
| 1721 | else |
| 1722 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | 9312fcc | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1723 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1724 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1725 | if (!LookForIvars) |
| 1726 | return 0; |
| 1727 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1728 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1729 | if (!IDecl) |
| 1730 | return 0; |
| 1731 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1732 | if (!ClassImpDecl) |
| 1733 | return 0; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1734 | bool DynamicImplSeen = false; |
| 1735 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1736 | if (!property) |
| 1737 | return 0; |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1738 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1739 | DynamicImplSeen = |
| 1740 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1741 | // property implementation has a designated ivar. No need to assume a new |
| 1742 | // one. |
| 1743 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1744 | return 0; |
| 1745 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1746 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1747 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1748 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1749 | NameLoc, NameLoc, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1750 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1751 | ObjCIvarDecl::Private, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1752 | (Expr *)0, true); |
| 1753 | ClassImpDecl->addDecl(Ivar); |
| 1754 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1755 | property->setPropertyIvarDecl(Ivar); |
| 1756 | return Ivar; |
| 1757 | } |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1761 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1762 | CXXScopeSpec &SS, |
| 1763 | UnqualifiedId &Id, |
| 1764 | bool HasTrailingLParen, |
| 1765 | bool isAddressOfOperand) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1766 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1767 | "cannot be direct & operand and have a trailing lparen"); |
| 1768 | |
| 1769 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1770 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1771 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1772 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1773 | |
| 1774 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1775 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1776 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1777 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1778 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1779 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1780 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1781 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1782 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1783 | // C++ [temp.dep.expr]p3: |
| 1784 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1785 | // -- an identifier that was declared with a dependent type, |
| 1786 | // (note: handled after lookup) |
| 1787 | // -- a template-id that is dependent, |
| 1788 | // (note: handled in BuildTemplateIdExpr) |
| 1789 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1790 | // -- a nested-name-specifier that contains a class-name that |
| 1791 | // names a dependent type. |
| 1792 | // Determine whether this is a member of an unknown specialization; |
| 1793 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1794 | bool DependentID = false; |
| 1795 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1796 | Name.getCXXNameType()->isDependentType()) { |
| 1797 | DependentID = true; |
| 1798 | } else if (SS.isSet()) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1799 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1800 | if (RequireCompleteDeclContext(SS, DC)) |
| 1801 | return ExprError(); |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1802 | } else { |
| 1803 | DependentID = true; |
| 1804 | } |
| 1805 | } |
| 1806 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1807 | if (DependentID) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1808 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1809 | TemplateArgs); |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1810 | |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1811 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1812 | // Perform the required lookup. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1813 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1814 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1815 | // Lookup the template name again to correctly establish the context in |
| 1816 | // which it was found. This is really unfortunate as we already did the |
| 1817 | // lookup to determine that it was a template name in the first place. If |
| 1818 | // this becomes a performance hit, we can work harder to preserve those |
| 1819 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1820 | bool MemberOfUnknownSpecialization; |
| 1821 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1822 | MemberOfUnknownSpecialization); |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1823 | |
| 1824 | if (MemberOfUnknownSpecialization || |
| 1825 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1826 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1827 | TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1828 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1829 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1830 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1832 | // If the result might be in a dependent base class, this is a dependent |
| 1833 | // id-expression. |
| 1834 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1835 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1836 | TemplateArgs); |
| 1837 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1838 | // If this reference is in an Objective-C method, then we need to do |
| 1839 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1840 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1841 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1842 | if (E.isInvalid()) |
| 1843 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1845 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1846 | return Owned(Ex); |
| 1847 | |
| 1848 | // Synthesize ivars lazily. |
Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1849 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1850 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1851 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) { |
| 1852 | if (const ObjCPropertyDecl *Property = |
| 1853 | canSynthesizeProvisionalIvar(II)) { |
| 1854 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1855 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1856 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1857 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1858 | isAddressOfOperand); |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1859 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1860 | } |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1861 | // for further use, this must be set to false if in class method. |
| 1862 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1863 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1864 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1865 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1866 | if (R.isAmbiguous()) |
| 1867 | return ExprError(); |
| 1868 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1869 | // Determine whether this name might be a candidate for |
| 1870 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1871 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1872 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1873 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1874 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1875 | // in C90, extension in C99, forbidden in C++). |
| 1876 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1877 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1878 | if (D) R.addDecl(D); |
| 1879 | } |
| 1880 | |
| 1881 | // If this name wasn't predeclared and if this is not a function |
| 1882 | // call, diagnose the problem. |
| 1883 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1884 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1885 | return ExprError(); |
| 1886 | |
| 1887 | assert(!R.empty() && |
| 1888 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1889 | |
| 1890 | // If we found an Objective-C instance variable, let |
| 1891 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1892 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1893 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1894 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1895 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1896 | assert(E.isInvalid() || E.get()); |
| 1897 | return move(E); |
| 1898 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1899 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1900 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1902 | // This is guaranteed from this point on. |
| 1903 | assert(!R.empty() || ADL); |
| 1904 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1905 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1906 | // C++ [class.mfct.non-static]p3: |
| 1907 | // When an id-expression that is not part of a class member access |
| 1908 | // syntax and not used to form a pointer to member is used in the |
| 1909 | // body of a non-static member function of class X, if name lookup |
| 1910 | // resolves the name in the id-expression to a non-static non-type |
| 1911 | // member of some class C, the id-expression is transformed into a |
| 1912 | // class member access expression using (*this) as the |
| 1913 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1914 | // |
| 1915 | // But we don't actually need to do this for '&' operands if R |
| 1916 | // resolved to a function or overloaded function set, because the |
| 1917 | // expression is ill-formed if it actually works out to be a |
| 1918 | // non-static member function: |
| 1919 | // |
| 1920 | // C++ [expr.ref]p4: |
| 1921 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1922 | // [t]he expression can be used only as the left-hand operand of a |
| 1923 | // member function call. |
| 1924 | // |
| 1925 | // There are other safeguards against such uses, but it's important |
| 1926 | // to get this right here so that we don't end up making a |
| 1927 | // spuriously dependent expression if we're inside a dependent |
| 1928 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1929 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1930 | bool MightBeImplicitMember; |
| 1931 | if (!isAddressOfOperand) |
| 1932 | MightBeImplicitMember = true; |
| 1933 | else if (!SS.isEmpty()) |
| 1934 | MightBeImplicitMember = false; |
| 1935 | else if (R.isOverloadedResult()) |
| 1936 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1937 | else if (R.isUnresolvableResult()) |
| 1938 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1939 | else |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1940 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1941 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1942 | |
| 1943 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1944 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1947 | if (TemplateArgs) |
| 1948 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1949 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1950 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1951 | } |
| 1952 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1953 | /// Builds an expression which might be an implicit member expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1954 | ExprResult |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1955 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1956 | LookupResult &R, |
| 1957 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1958 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1959 | case IMA_Instance: |
| 1960 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1961 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1962 | case IMA_Mixed: |
| 1963 | case IMA_Mixed_Unrelated: |
| 1964 | case IMA_Unresolved: |
| 1965 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1966 | |
| 1967 | case IMA_Static: |
| 1968 | case IMA_Mixed_StaticContext: |
| 1969 | case IMA_Unresolved_StaticContext: |
| 1970 | if (TemplateArgs) |
| 1971 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1972 | return BuildDeclarationNameExpr(SS, R, false); |
| 1973 | |
| 1974 | case IMA_Error_StaticContext: |
| 1975 | case IMA_Error_Unrelated: |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1976 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 1977 | R.getLookupNameInfo()); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1978 | return ExprError(); |
| 1979 | } |
| 1980 | |
| 1981 | llvm_unreachable("unexpected instance member access kind"); |
| 1982 | return ExprError(); |
| 1983 | } |
| 1984 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1985 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1986 | /// declaration name, generally during template instantiation. |
| 1987 | /// There's a large number of things which don't need to be done along |
| 1988 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1989 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1990 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1991 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1992 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1993 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1994 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1995 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1996 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1997 | return ExprError(); |
| 1998 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1999 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2000 | LookupQualifiedName(R, DC); |
| 2001 | |
| 2002 | if (R.isAmbiguous()) |
| 2003 | return ExprError(); |
| 2004 | |
| 2005 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2006 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 2007 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2008 | return ExprError(); |
| 2009 | } |
| 2010 | |
| 2011 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 2012 | } |
| 2013 | |
| 2014 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 2015 | /// detected that we're currently inside an ObjC method. Perform some |
| 2016 | /// additional lookup. |
| 2017 | /// |
| 2018 | /// Ideally, most of this would be done by lookup, but there's |
| 2019 | /// actually quite a lot of extra work involved. |
| 2020 | /// |
| 2021 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2022 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2023 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2024 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2025 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2026 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2027 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2028 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 2029 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 2030 | // found a decl, but that decl is outside the current instance method (i.e. |
| 2031 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 2032 | // this name, if the lookup sucedes, we replace it our current decl. |
| 2033 | |
| 2034 | // If we're in a class method, we don't normally want to look for |
| 2035 | // ivars. But if we don't find anything else, and there's an |
| 2036 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2037 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2038 | |
| 2039 | bool LookForIvars; |
| 2040 | if (Lookup.empty()) |
| 2041 | LookForIvars = true; |
| 2042 | else if (IsClassMethod) |
| 2043 | LookForIvars = false; |
| 2044 | else |
| 2045 | LookForIvars = (Lookup.isSingleResult() && |
| 2046 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2047 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2048 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2049 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2050 | ObjCInterfaceDecl *ClassDeclared; |
| 2051 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2052 | // Diagnose using an ivar in a class method. |
| 2053 | if (IsClassMethod) |
| 2054 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2055 | << IV->getDeclName()); |
| 2056 | |
| 2057 | // If we're referencing an invalid decl, just return this as a silent |
| 2058 | // error node. The error diagnostic was already emitted on the decl. |
| 2059 | if (IV->isInvalidDecl()) |
| 2060 | return ExprError(); |
| 2061 | |
| 2062 | // Check if referencing a field with __attribute__((deprecated)). |
| 2063 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2064 | return ExprError(); |
| 2065 | |
| 2066 | // Diagnose the use of an ivar outside of the declaring class. |
| 2067 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 2068 | ClassDeclared != IFace) |
| 2069 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2070 | |
| 2071 | // FIXME: This should use a new expr for a direct reference, don't |
| 2072 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2073 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2074 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2075 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2076 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2077 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2078 | SelfName, false, false); |
| 2079 | if (SelfExpr.isInvalid()) |
| 2080 | return ExprError(); |
| 2081 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2082 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2083 | if (SelfExpr.isInvalid()) |
| 2084 | return ExprError(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2085 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2086 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2087 | Expr *base = SelfExpr.take(); |
| 2088 | base = base->IgnoreParenImpCasts(); |
| 2089 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 2090 | const NamedDecl *ND = DE->getDecl(); |
| 2091 | if (!isa<ImplicitParamDecl>(ND)) { |
Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2092 | // relax the rule such that it is allowed to have a shadow 'self' |
| 2093 | // where stand-alone ivar can be found in this 'self' object. |
| 2094 | // This is to match gcc's behavior. |
| 2095 | ObjCInterfaceDecl *selfIFace = 0; |
| 2096 | if (const ObjCObjectPointerType *OPT = |
| 2097 | base->getType()->getAsObjCInterfacePointerType()) |
| 2098 | selfIFace = OPT->getInterfaceDecl(); |
| 2099 | if (!selfIFace || |
| 2100 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2101 | Diag(Loc, diag::error_implicit_ivar_access) |
| 2102 | << IV->getDeclName(); |
| 2103 | Diag(ND->getLocation(), diag::note_declared_at); |
| 2104 | return ExprError(); |
| 2105 | } |
Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2106 | } |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2107 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2108 | return Owned(new (Context) |
| 2109 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2110 | SelfExpr.take(), true, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2111 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2112 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2113 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2114 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2115 | ObjCInterfaceDecl *ClassDeclared; |
| 2116 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2117 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 2118 | IFace == ClassDeclared) |
| 2119 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2120 | } |
| 2121 | } |
| 2122 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2123 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2124 | // FIXME. Consolidate this with similar code in LookupName. |
| 2125 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 2126 | if (!(getLangOptions().CPlusPlus && |
| 2127 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2128 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2129 | S, Lookup.isForRedeclaration(), |
| 2130 | Lookup.getNameLoc()); |
| 2131 | if (D) Lookup.addDecl(D); |
| 2132 | } |
| 2133 | } |
| 2134 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2135 | // Sentinel value saying that we didn't do anything special. |
| 2136 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2137 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2138 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2139 | /// \brief Cast a base object to a member's actual type. |
| 2140 | /// |
| 2141 | /// Logically this happens in three phases: |
| 2142 | /// |
| 2143 | /// * First we cast from the base type to the naming class. |
| 2144 | /// The naming class is the class into which we were looking |
| 2145 | /// when we found the member; it's the qualifier type if a |
| 2146 | /// qualifier was provided, and otherwise it's the base type. |
| 2147 | /// |
| 2148 | /// * Next we cast from the naming class to the declaring class. |
| 2149 | /// If the member we found was brought into a class's scope by |
| 2150 | /// a using declaration, this is that class; otherwise it's |
| 2151 | /// the class declaring the member. |
| 2152 | /// |
| 2153 | /// * Finally we cast from the declaring class to the "true" |
| 2154 | /// declaring class of the member. This conversion does not |
| 2155 | /// obey access control. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2156 | ExprResult |
| 2157 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2158 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2159 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2160 | NamedDecl *Member) { |
| 2161 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2162 | if (!RD) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2163 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2164 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2165 | QualType DestRecordType; |
| 2166 | QualType DestType; |
| 2167 | QualType FromRecordType; |
| 2168 | QualType FromType = From->getType(); |
| 2169 | bool PointerConversions = false; |
| 2170 | if (isa<FieldDecl>(Member)) { |
| 2171 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2173 | if (FromType->getAs<PointerType>()) { |
| 2174 | DestType = Context.getPointerType(DestRecordType); |
| 2175 | FromRecordType = FromType->getPointeeType(); |
| 2176 | PointerConversions = true; |
| 2177 | } else { |
| 2178 | DestType = DestRecordType; |
| 2179 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2180 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2181 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2182 | if (Method->isStatic()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2183 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2185 | DestType = Method->getThisType(Context); |
| 2186 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2188 | if (FromType->getAs<PointerType>()) { |
| 2189 | FromRecordType = FromType->getPointeeType(); |
| 2190 | PointerConversions = true; |
| 2191 | } else { |
| 2192 | FromRecordType = FromType; |
| 2193 | DestType = DestRecordType; |
| 2194 | } |
| 2195 | } else { |
| 2196 | // No conversion necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2197 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2198 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2200 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2201 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2203 | // If the unqualified types are the same, no conversion is necessary. |
| 2204 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2205 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2206 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2207 | SourceRange FromRange = From->getSourceRange(); |
| 2208 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2209 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2210 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2211 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2212 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2213 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2214 | // class name. |
| 2215 | // |
| 2216 | // If the member was a qualified name and the qualified referred to a |
| 2217 | // specific base subobject type, we'll cast to that intermediate type |
| 2218 | // first and then to the object in which the member is declared. That allows |
| 2219 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2220 | // |
| 2221 | // class Base { public: int x; }; |
| 2222 | // class Derived1 : public Base { }; |
| 2223 | // class Derived2 : public Base { }; |
| 2224 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2225 | // |
| 2226 | // void VeryDerived::f() { |
| 2227 | // x = 17; // error: ambiguous base subobjects |
| 2228 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2229 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2230 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2231 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2232 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2233 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2234 | |
| 2235 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2236 | |
| 2237 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2238 | // type of the object type, in which case we just ignore it. |
| 2239 | // Otherwise build the appropriate casts. |
| 2240 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2241 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2242 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2243 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2244 | return ExprError(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2245 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2246 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2247 | QType = Context.getPointerType(QType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2248 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2249 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2250 | |
| 2251 | FromType = QType; |
| 2252 | FromRecordType = QRecordType; |
| 2253 | |
| 2254 | // If the qualifier type was the same as the destination type, |
| 2255 | // we're done. |
| 2256 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2257 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2258 | } |
| 2259 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2260 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2261 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2262 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2263 | // If we actually found the member through a using declaration, cast |
| 2264 | // down to the using declaration's type. |
| 2265 | // |
| 2266 | // Pointer equality is fine here because only one declaration of a |
| 2267 | // class ever has member declarations. |
| 2268 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2269 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2270 | QualType URecordType = Context.getTypeDeclType( |
| 2271 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2272 | |
| 2273 | // We only need to do this if the naming-class to declaring-class |
| 2274 | // conversion is non-trivial. |
| 2275 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2276 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2277 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2278 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2279 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2280 | return ExprError(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2281 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2282 | QualType UType = URecordType; |
| 2283 | if (PointerConversions) |
| 2284 | UType = Context.getPointerType(UType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2285 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2286 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2287 | FromType = UType; |
| 2288 | FromRecordType = URecordType; |
| 2289 | } |
| 2290 | |
| 2291 | // We don't do access control for the conversion from the |
| 2292 | // declaring class to the true declaring class. |
| 2293 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2294 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2295 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2296 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2297 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2298 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2299 | IgnoreAccess)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2300 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2301 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2302 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2303 | VK, &BasePath); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2304 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2305 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2306 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2307 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | 2cfcef6 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2308 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2309 | DeclAccessPair FoundDecl, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2310 | const DeclarationNameInfo &MemberNameInfo, |
| 2311 | QualType Ty, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2312 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2313 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2314 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2315 | Member, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2316 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2317 | } |
| 2318 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2319 | static ExprResult |
| 2320 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2321 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2322 | DeclAccessPair FoundDecl, |
| 2323 | const DeclarationNameInfo &MemberNameInfo) { |
| 2324 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2325 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2326 | // that *x is always an l-value), except that if the base isn't |
| 2327 | // an ordinary object then we must have an rvalue. |
| 2328 | ExprValueKind VK = VK_LValue; |
| 2329 | ExprObjectKind OK = OK_Ordinary; |
| 2330 | if (!IsArrow) { |
| 2331 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2332 | VK = BaseExpr->getValueKind(); |
| 2333 | else |
| 2334 | VK = VK_RValue; |
| 2335 | } |
| 2336 | if (VK != VK_RValue && Field->isBitField()) |
| 2337 | OK = OK_BitField; |
| 2338 | |
| 2339 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2340 | QualType MemberType = Field->getType(); |
| 2341 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2342 | MemberType = Ref->getPointeeType(); |
| 2343 | VK = VK_LValue; |
| 2344 | } else { |
| 2345 | QualType BaseType = BaseExpr->getType(); |
| 2346 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2347 | |
| 2348 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2349 | |
| 2350 | // GC attributes are never picked up by members. |
| 2351 | BaseQuals.removeObjCGCAttr(); |
| 2352 | |
| 2353 | // CVR attributes from the base are picked up by members, |
| 2354 | // except that 'mutable' members don't pick up 'const'. |
| 2355 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2356 | |
| 2357 | Qualifiers MemberQuals |
| 2358 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2359 | |
| 2360 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2361 | assert(!MemberQuals.hasAddressSpace()); |
| 2362 | |
| 2363 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2364 | if (Combined != MemberQuals) |
| 2365 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2366 | } |
| 2367 | |
| 2368 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2369 | ExprResult Base = |
| 2370 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2371 | FoundDecl, Field); |
| 2372 | if (Base.isInvalid()) |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2373 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2374 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2375 | Field, FoundDecl, MemberNameInfo, |
| 2376 | MemberType, VK, OK)); |
| 2377 | } |
| 2378 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2379 | /// Builds an implicit member access expression. The current context |
| 2380 | /// is known to be an instance method, and the given unqualified lookup |
| 2381 | /// set is known to contain only instance members, at least one of which |
| 2382 | /// is from an appropriate type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2383 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2384 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2385 | LookupResult &R, |
| 2386 | const TemplateArgumentListInfo *TemplateArgs, |
| 2387 | bool IsKnownInstance) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2388 | assert(!R.empty() && !R.isAmbiguous()); |
| 2389 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2390 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2392 | // We may have found a field within an anonymous union or struct |
| 2393 | // (C++ [class.union]). |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2394 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2395 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2396 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2397 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2398 | // If this is known to be an instance access, go ahead and build an |
| 2399 | // implicit 'this' expression now. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2400 | // 'this' expression now. |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2401 | CXXMethodDecl *method = tryCaptureCXXThis(); |
| 2402 | assert(method && "didn't correctly pre-flight capture of 'this'"); |
| 2403 | |
| 2404 | QualType thisType = method->getThisType(Context); |
| 2405 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2406 | if (IsKnownInstance) { |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2407 | SourceLocation Loc = R.getNameLoc(); |
| 2408 | if (SS.getRange().isValid()) |
| 2409 | Loc = SS.getRange().getBegin(); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2410 | baseExpr = new (Context) CXXThisExpr(loc, thisType, /*isImplicit=*/true); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2413 | return BuildMemberReferenceExpr(baseExpr, thisType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2414 | /*OpLoc*/ SourceLocation(), |
| 2415 | /*IsArrow*/ true, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2416 | SS, |
| 2417 | /*FirstQualifierInScope*/ 0, |
| 2418 | R, TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2421 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2422 | const LookupResult &R, |
| 2423 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2424 | // Only when used directly as the postfix-expression of a call. |
| 2425 | if (!HasTrailingLParen) |
| 2426 | return false; |
| 2427 | |
| 2428 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2429 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2430 | return false; |
| 2431 | |
| 2432 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2433 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2434 | return false; |
| 2435 | |
| 2436 | // Turn off ADL when we find certain kinds of declarations during |
| 2437 | // normal lookup: |
| 2438 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2439 | NamedDecl *D = *I; |
| 2440 | |
| 2441 | // C++0x [basic.lookup.argdep]p3: |
| 2442 | // -- a declaration of a class member |
| 2443 | // Since using decls preserve this property, we check this on the |
| 2444 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2445 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2446 | return false; |
| 2447 | |
| 2448 | // C++0x [basic.lookup.argdep]p3: |
| 2449 | // -- a block-scope function declaration that is not a |
| 2450 | // using-declaration |
| 2451 | // NOTE: we also trigger this for function templates (in fact, we |
| 2452 | // don't check the decl type at all, since all other decl types |
| 2453 | // turn off ADL anyway). |
| 2454 | if (isa<UsingShadowDecl>(D)) |
| 2455 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2456 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2457 | return false; |
| 2458 | |
| 2459 | // C++0x [basic.lookup.argdep]p3: |
| 2460 | // -- a declaration that is neither a function or a function |
| 2461 | // template |
| 2462 | // And also for builtin functions. |
| 2463 | if (isa<FunctionDecl>(D)) { |
| 2464 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2465 | |
| 2466 | // But also builtin functions. |
| 2467 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2468 | return false; |
| 2469 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2470 | return false; |
| 2471 | } |
| 2472 | |
| 2473 | return true; |
| 2474 | } |
| 2475 | |
| 2476 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2477 | /// Diagnoses obvious problems with the use of the given declaration |
| 2478 | /// as an expression. This is only actually called for lookups that |
| 2479 | /// were not overloaded, and it doesn't promise that the declaration |
| 2480 | /// will in fact be used. |
| 2481 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2482 | if (isa<TypedefNameDecl>(D)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2483 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2484 | return true; |
| 2485 | } |
| 2486 | |
| 2487 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2488 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2489 | return true; |
| 2490 | } |
| 2491 | |
| 2492 | if (isa<NamespaceDecl>(D)) { |
| 2493 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2494 | return true; |
| 2495 | } |
| 2496 | |
| 2497 | return false; |
| 2498 | } |
| 2499 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2500 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2501 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2502 | LookupResult &R, |
| 2503 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2504 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2505 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2506 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2507 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2508 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2509 | |
| 2510 | // We only need to check the declaration if there's exactly one |
| 2511 | // result, because in the overloaded case the results can only be |
| 2512 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2513 | if (R.isSingleResult() && |
| 2514 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2515 | return ExprError(); |
| 2516 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2517 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2518 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2519 | // we've picked a target. |
| 2520 | R.suppressDiagnostics(); |
| 2521 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2522 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2523 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2524 | SS.getWithLocInContext(Context), |
| 2525 | R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2526 | NeedsADL, R.isOverloadedResult(), |
| 2527 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2528 | |
| 2529 | return Owned(ULE); |
| 2530 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2531 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2532 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2533 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2534 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2535 | const DeclarationNameInfo &NameInfo, |
| 2536 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2537 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2538 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2539 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2540 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2541 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2542 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2543 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2544 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2545 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2546 | // Specifically diagnose references to class templates that are missing |
| 2547 | // a template argument list. |
| 2548 | Diag(Loc, diag::err_template_decl_ref) |
| 2549 | << Template << SS.getRange(); |
| 2550 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2551 | return ExprError(); |
| 2552 | } |
| 2553 | |
| 2554 | // Make sure that we're referring to a value. |
| 2555 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2556 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2557 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2558 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2559 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2560 | return ExprError(); |
| 2561 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2563 | // Check whether this declaration can be used. Note that we suppress |
| 2564 | // this check when we're going to perform argument-dependent lookup |
| 2565 | // on this function name, because this might not be the function |
| 2566 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2567 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2568 | return ExprError(); |
| 2569 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2570 | // Only create DeclRefExpr's for valid Decl's. |
| 2571 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2572 | return ExprError(); |
| 2573 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2574 | // Handle members of anonymous structs and unions. If we got here, |
| 2575 | // and the reference is to a class member indirect field, then this |
| 2576 | // must be the subject of a pointer-to-member expression. |
| 2577 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2578 | if (!indirectField->isCXXClassMember()) |
| 2579 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2580 | indirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2582 | // If the identifier reference is inside a block, and it refers to a value |
| 2583 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2584 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2585 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2586 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2587 | // We do not do this for things like enum constants, global variables, etc, |
| 2588 | // as they do not get snapshotted. |
| 2589 | // |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2590 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2591 | case CR_Error: |
| 2592 | return ExprError(); |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2593 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2594 | case CR_Capture: |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2595 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2596 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2597 | |
| 2598 | case CR_CaptureByRef: |
| 2599 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2600 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2601 | |
| 2602 | case CR_NoCapture: { |
| 2603 | // If this reference is not in a block or if the referenced |
| 2604 | // variable is within the block, create a normal DeclRefExpr. |
| 2605 | |
| 2606 | QualType type = VD->getType(); |
Daniel Dunbar | 7c2dc36 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2607 | ExprValueKind valueKind = VK_RValue; |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2608 | |
| 2609 | switch (D->getKind()) { |
| 2610 | // Ignore all the non-ValueDecl kinds. |
| 2611 | #define ABSTRACT_DECL(kind) |
| 2612 | #define VALUE(type, base) |
| 2613 | #define DECL(type, base) \ |
| 2614 | case Decl::type: |
| 2615 | #include "clang/AST/DeclNodes.inc" |
| 2616 | llvm_unreachable("invalid value decl kind"); |
| 2617 | return ExprError(); |
| 2618 | |
| 2619 | // These shouldn't make it here. |
| 2620 | case Decl::ObjCAtDefsField: |
| 2621 | case Decl::ObjCIvar: |
| 2622 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2623 | return ExprError(); |
| 2624 | |
| 2625 | // Enum constants are always r-values and never references. |
| 2626 | // Unresolved using declarations are dependent. |
| 2627 | case Decl::EnumConstant: |
| 2628 | case Decl::UnresolvedUsingValue: |
| 2629 | valueKind = VK_RValue; |
| 2630 | break; |
| 2631 | |
| 2632 | // Fields and indirect fields that got here must be for |
| 2633 | // pointer-to-member expressions; we just call them l-values for |
| 2634 | // internal consistency, because this subexpression doesn't really |
| 2635 | // exist in the high-level semantics. |
| 2636 | case Decl::Field: |
| 2637 | case Decl::IndirectField: |
| 2638 | assert(getLangOptions().CPlusPlus && |
| 2639 | "building reference to field in C?"); |
| 2640 | |
| 2641 | // These can't have reference type in well-formed programs, but |
| 2642 | // for internal consistency we do this anyway. |
| 2643 | type = type.getNonReferenceType(); |
| 2644 | valueKind = VK_LValue; |
| 2645 | break; |
| 2646 | |
| 2647 | // Non-type template parameters are either l-values or r-values |
| 2648 | // depending on the type. |
| 2649 | case Decl::NonTypeTemplateParm: { |
| 2650 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2651 | type = reftype->getPointeeType(); |
| 2652 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2653 | break; |
| 2654 | } |
| 2655 | |
| 2656 | // For non-references, we need to strip qualifiers just in case |
| 2657 | // the template parameter was declared as 'const int' or whatever. |
| 2658 | valueKind = VK_RValue; |
| 2659 | type = type.getUnqualifiedType(); |
| 2660 | break; |
| 2661 | } |
| 2662 | |
| 2663 | case Decl::Var: |
| 2664 | // In C, "extern void blah;" is valid and is an r-value. |
| 2665 | if (!getLangOptions().CPlusPlus && |
| 2666 | !type.hasQualifiers() && |
| 2667 | type->isVoidType()) { |
| 2668 | valueKind = VK_RValue; |
| 2669 | break; |
| 2670 | } |
| 2671 | // fallthrough |
| 2672 | |
| 2673 | case Decl::ImplicitParam: |
| 2674 | case Decl::ParmVar: |
| 2675 | // These are always l-values. |
| 2676 | valueKind = VK_LValue; |
| 2677 | type = type.getNonReferenceType(); |
| 2678 | break; |
| 2679 | |
| 2680 | case Decl::Function: { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2681 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2682 | |
| 2683 | // If we're referring to a function with an __unknown_anytype |
| 2684 | // result type, make the entire expression __unknown_anytype. |
| 2685 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2686 | type = Context.UnknownAnyTy; |
| 2687 | valueKind = VK_RValue; |
| 2688 | break; |
| 2689 | } |
| 2690 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2691 | // Functions are l-values in C++. |
| 2692 | if (getLangOptions().CPlusPlus) { |
| 2693 | valueKind = VK_LValue; |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | // C99 DR 316 says that, if a function type comes from a |
| 2698 | // function definition (without a prototype), that type is only |
| 2699 | // used for checking compatibility. Therefore, when referencing |
| 2700 | // the function, we pretend that we don't have the full function |
| 2701 | // type. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2702 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2703 | isa<FunctionProtoType>(fty)) |
| 2704 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2705 | fty->getExtInfo()); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2706 | |
| 2707 | // Functions are r-values in C. |
| 2708 | valueKind = VK_RValue; |
| 2709 | break; |
| 2710 | } |
| 2711 | |
| 2712 | case Decl::CXXMethod: |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2713 | // If we're referring to a method with an __unknown_anytype |
| 2714 | // result type, make the entire expression __unknown_anytype. |
| 2715 | // This should only be possible with a type written directly. |
| 2716 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2717 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2718 | type = Context.UnknownAnyTy; |
| 2719 | valueKind = VK_RValue; |
| 2720 | break; |
| 2721 | } |
| 2722 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2723 | // C++ methods are l-values if static, r-values if non-static. |
| 2724 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2725 | valueKind = VK_LValue; |
| 2726 | break; |
| 2727 | } |
| 2728 | // fallthrough |
| 2729 | |
| 2730 | case Decl::CXXConversion: |
| 2731 | case Decl::CXXDestructor: |
| 2732 | case Decl::CXXConstructor: |
| 2733 | valueKind = VK_RValue; |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2738 | } |
| 2739 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2740 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2741 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2742 | llvm_unreachable("unknown capture result"); |
| 2743 | return ExprError(); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2744 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2745 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2746 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2747 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2748 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2749 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2750 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2751 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2752 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2753 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2754 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2755 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2756 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2757 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2759 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2760 | if (!currentDecl && getCurBlock()) |
| 2761 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2762 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2763 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2764 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2765 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2766 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2767 | QualType ResTy; |
| 2768 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2769 | ResTy = Context.DependentTy; |
| 2770 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2771 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2772 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2773 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2774 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2775 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2776 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2777 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2780 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2781 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2782 | bool Invalid = false; |
| 2783 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2784 | if (Invalid) |
| 2785 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2786 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2787 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2788 | PP); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2789 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2790 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2791 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2792 | QualType Ty; |
| 2793 | if (!getLangOptions().CPlusPlus) |
| 2794 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2795 | else if (Literal.isWide()) |
| 2796 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2797 | else if (Literal.isMultiChar()) |
| 2798 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2799 | else |
| 2800 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2801 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2802 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2803 | Literal.isWide(), |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2804 | Ty, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2805 | } |
| 2806 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2807 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2808 | // Fast path for a single digit (which is quite common). A single digit |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2809 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2810 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2811 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2812 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2813 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2814 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2815 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2816 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2817 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2818 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2819 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2820 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2821 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2822 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2823 | bool Invalid = false; |
| 2824 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2825 | if (Invalid) |
| 2826 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2827 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2829 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2830 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2831 | return ExprError(); |
| 2832 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2833 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2834 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2835 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2836 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2837 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2838 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2839 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2840 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2841 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2842 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2843 | |
| 2844 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2845 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2846 | using llvm::APFloat; |
| 2847 | APFloat Val(Format); |
| 2848 | |
| 2849 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Overflow is always an error, but underflow is only an error if |
| 2852 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2853 | if ((result & APFloat::opOverflow) || |
| 2854 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2855 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2856 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2857 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2858 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2859 | APFloat::getLargest(Format).toString(buffer); |
| 2860 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2861 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2862 | APFloat::getSmallest(Format).toString(buffer); |
| 2863 | } |
| 2864 | |
| 2865 | Diag(Tok.getLocation(), diagnostic) |
| 2866 | << Ty |
| 2867 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2868 | } |
| 2869 | |
| 2870 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2871 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2872 | |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2873 | if (Ty == Context.DoubleTy) { |
| 2874 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2875 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2876 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2877 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2878 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2879 | } |
| 2880 | } |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2881 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2882 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2883 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2884 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2885 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2886 | // long long is a C99 feature. |
| 2887 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2888 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2889 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2890 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2891 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2892 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2893 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2894 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2895 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2896 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2897 | Ty = Context.UnsignedLongLongTy; |
| 2898 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2899 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2900 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2901 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2902 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2903 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2904 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2905 | // be an unsigned int. |
| 2906 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2907 | |
| 2908 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2909 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2910 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2911 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2912 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2913 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2914 | // Does it fit in a unsigned int? |
| 2915 | if (ResultVal.isIntN(IntSize)) { |
| 2916 | // Does it fit in a signed int? |
| 2917 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2918 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2919 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2920 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2921 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2922 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2923 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2924 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2925 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2926 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2927 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2928 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2929 | // Does it fit in a unsigned long? |
| 2930 | if (ResultVal.isIntN(LongSize)) { |
| 2931 | // Does it fit in a signed long? |
| 2932 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2933 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2934 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2935 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2936 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2937 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2940 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2941 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2942 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2943 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2944 | // Does it fit in a unsigned long long? |
| 2945 | if (ResultVal.isIntN(LongLongSize)) { |
| 2946 | // Does it fit in a signed long long? |
Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2947 | // To be compatible with MSVC, hex integer literals ending with the |
| 2948 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2949 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2950 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2951 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2952 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2953 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2954 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2955 | } |
| 2956 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2957 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2958 | // If we still couldn't decide a type, we probably have something that |
| 2959 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2960 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2961 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2962 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2963 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2964 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2965 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2966 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2967 | ResultVal = ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2968 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2969 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2970 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2971 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2972 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2973 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2975 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2976 | |
| 2977 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2980 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2981 | SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2982 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2983 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 2986 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 2987 | /// See C99 6.3.2.1p[2-4] for more details. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2988 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2989 | SourceLocation OpLoc, |
| 2990 | SourceRange ExprRange, |
| 2991 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2992 | if (exprType->isDependentType()) |
| 2993 | return false; |
| 2994 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2995 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2996 | // the result is the size of the referenced type." |
| 2997 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2998 | // result shall be the alignment of the referenced type." |
| 2999 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 3000 | exprType = Ref->getPointeeType(); |
| 3001 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3002 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 3003 | // scalar or vector data type argument..." |
| 3004 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 3005 | // type (C99 6.2.5p18) or void. |
| 3006 | if (ExprKind == UETT_VecStep) { |
| 3007 | if (!(exprType->isArithmeticType() || exprType->isVoidType() || |
| 3008 | exprType->isVectorType())) { |
| 3009 | Diag(OpLoc, diag::err_vecstep_non_scalar_vector_type) |
| 3010 | << exprType << ExprRange; |
| 3011 | return true; |
| 3012 | } |
| 3013 | } |
| 3014 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3015 | // C99 6.5.3.4p1: |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3016 | if (exprType->isFunctionType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3017 | // alignof(function) is allowed as an extension. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3018 | if (ExprKind == UETT_SizeOf) |
| 3019 | Diag(OpLoc, diag::ext_sizeof_function_type) |
| 3020 | << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3021 | return false; |
| 3022 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3024 | // Allow sizeof(void)/alignof(void) as an extension. vec_step(void) is not |
| 3025 | // an extension, as void is a built-in scalar type (OpenCL 1.1 6.1.1). |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3026 | if (exprType->isVoidType()) { |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3027 | if (ExprKind != UETT_VecStep) |
| 3028 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 3029 | << ExprKind << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3030 | return false; |
| 3031 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3033 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 3034 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3035 | << ExprKind << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3036 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3037 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3038 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3039 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3040 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3041 | << exprType << (ExprKind == UETT_SizeOf) |
| 3042 | << ExprRange; |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3043 | return true; |
Chris Lattner | 37920f5 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 3044 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3045 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3046 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 3049 | static bool CheckAlignOfExpr(Sema &S, Expr *E, SourceLocation OpLoc, |
| 3050 | SourceRange ExprRange) { |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3051 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3052 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3054 | if (isa<DeclRefExpr>(E)) |
| 3055 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3056 | |
| 3057 | // Cannot know anything else if the expression is dependent. |
| 3058 | if (E->isTypeDependent()) |
| 3059 | return false; |
| 3060 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3061 | if (E->getBitField()) { |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 3062 | S. Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3063 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3064 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3065 | |
| 3066 | // Alignment of a field access is always okay, so long as it isn't a |
| 3067 | // bit-field. |
| 3068 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3069 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3070 | return false; |
| 3071 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3072 | return S.CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 3073 | UETT_AlignOf); |
| 3074 | } |
| 3075 | |
| 3076 | bool Sema::CheckVecStepExpr(Expr *E, SourceLocation OpLoc, |
| 3077 | SourceRange ExprRange) { |
| 3078 | E = E->IgnoreParens(); |
| 3079 | |
| 3080 | // Cannot know anything else if the expression is dependent. |
| 3081 | if (E->isTypeDependent()) |
| 3082 | return false; |
| 3083 | |
| 3084 | return CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, ExprRange, |
| 3085 | UETT_VecStep); |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3086 | } |
| 3087 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3088 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3089 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3090 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3091 | SourceLocation OpLoc, |
| 3092 | UnaryExprOrTypeTrait ExprKind, |
| 3093 | SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3094 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3095 | return ExprError(); |
| 3096 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3097 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3098 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3099 | if (!T->isDependentType() && |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3100 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3101 | return ExprError(); |
| 3102 | |
| 3103 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3104 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3105 | Context.getSizeType(), |
| 3106 | OpLoc, R.getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | /// \brief Build a sizeof or alignof expression given an expression |
| 3110 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3111 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3112 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3113 | UnaryExprOrTypeTrait ExprKind, |
| 3114 | SourceRange R) { |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3115 | // Verify that the operand is valid. |
| 3116 | bool isInvalid = false; |
| 3117 | if (E->isTypeDependent()) { |
| 3118 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3119 | } else if (ExprKind == UETT_AlignOf) { |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 3120 | isInvalid = CheckAlignOfExpr(*this, E, OpLoc, R); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3121 | } else if (ExprKind == UETT_VecStep) { |
| 3122 | isInvalid = CheckVecStepExpr(E, OpLoc, R); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3123 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3124 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 3125 | isInvalid = true; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3126 | } else if (E->getType()->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3127 | ExprResult PE = CheckPlaceholderExpr(E); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3128 | if (PE.isInvalid()) return ExprError(); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3129 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind, R); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3130 | } else { |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3131 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E->getType(), OpLoc, R, |
| 3132 | UETT_SizeOf); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | if (isInvalid) |
| 3136 | return ExprError(); |
| 3137 | |
| 3138 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3139 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, E, |
| 3140 | Context.getSizeType(), |
| 3141 | OpLoc, R.getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3144 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3145 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3146 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3147 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3148 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 3149 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 3150 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 3151 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3152 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3153 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3154 | if (isType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3155 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3156 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3157 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3159 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3160 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3161 | ExprResult Result |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3162 | = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind, |
| 3163 | ArgEx->getSourceRange()); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3164 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3165 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3166 | } |
| 3167 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3168 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3169 | bool isReal) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3170 | if (V.get()->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3171 | return S.Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3172 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3173 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3174 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3175 | V = S.DefaultLvalueConversion(V.take()); |
| 3176 | if (V.isInvalid()) |
| 3177 | return QualType(); |
| 3178 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3179 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3180 | // These operators return the element type of a complex type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3181 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3182 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3184 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3185 | if (V.get()->getType()->isArithmeticType()) |
| 3186 | return V.get()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3188 | // Test for placeholders. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3189 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3190 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3191 | if (PR.get() != V.get()) { |
| 3192 | V = move(PR); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3193 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3196 | // Reject anything else. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3197 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3198 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3199 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3203 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3204 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3205 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3206 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3207 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3208 | switch (Kind) { |
| 3209 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3210 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3211 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3212 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3213 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3214 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3217 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3218 | /// having l-value type. These are: |
| 3219 | /// - 'void', but not qualified void |
| 3220 | /// - function types |
| 3221 | /// |
| 3222 | /// The exact rule here is C99 6.3.2.1: |
| 3223 | /// An lvalue is an expression with an object type or an incomplete |
| 3224 | /// type other than void. |
| 3225 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3226 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3227 | T->isFunctionType()); |
| 3228 | } |
| 3229 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3230 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3231 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3232 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3233 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3234 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3235 | if (Result.isInvalid()) return ExprError(); |
| 3236 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3237 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3238 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3240 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3241 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3242 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3243 | Context.DependentTy, |
| 3244 | VK_LValue, OK_Ordinary, |
| 3245 | RLoc)); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3249 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3250 | LHSExp->getType()->isEnumeralType() || |
| 3251 | RHSExp->getType()->isRecordType() || |
| 3252 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3253 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3256 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3260 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3261 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3262 | Expr *Idx, SourceLocation RLoc) { |
| 3263 | Expr *LHSExp = Base; |
| 3264 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3265 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3266 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3267 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3268 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3269 | if (Result.isInvalid()) |
| 3270 | return ExprError(); |
| 3271 | LHSExp = Result.take(); |
| 3272 | } |
| 3273 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3274 | if (Result.isInvalid()) |
| 3275 | return ExprError(); |
| 3276 | RHSExp = Result.take(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3277 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3278 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3279 | ExprValueKind VK = VK_LValue; |
| 3280 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3281 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3282 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3283 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3284 | // in the subscript position. As a result, we need to derive the array base |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3285 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3286 | Expr *BaseExpr, *IndexExpr; |
| 3287 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3288 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3289 | BaseExpr = LHSExp; |
| 3290 | IndexExpr = RHSExp; |
| 3291 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3292 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3293 | BaseExpr = LHSExp; |
| 3294 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3295 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3296 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3297 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3298 | BaseExpr = RHSExp; |
| 3299 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3300 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3302 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3303 | BaseExpr = LHSExp; |
| 3304 | IndexExpr = RHSExp; |
| 3305 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3307 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3308 | // Handle the uncommon case of "123[Ptr]". |
| 3309 | BaseExpr = RHSExp; |
| 3310 | IndexExpr = LHSExp; |
| 3311 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3312 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3313 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3314 | IndexExpr = RHSExp; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3315 | VK = LHSExp->getValueKind(); |
| 3316 | if (VK != VK_RValue) |
| 3317 | OK = OK_VectorComponent; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3318 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3319 | // FIXME: need to deal with const... |
| 3320 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3321 | } else if (LHSTy->isArrayType()) { |
| 3322 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3323 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3324 | // wasn't promoted because of the C90 rule that doesn't |
| 3325 | // allow promoting non-lvalue arrays. Warn, then |
| 3326 | // force the promotion here. |
| 3327 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3328 | LHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3329 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3330 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3331 | LHSTy = LHSExp->getType(); |
| 3332 | |
| 3333 | BaseExpr = LHSExp; |
| 3334 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3335 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3336 | } else if (RHSTy->isArrayType()) { |
| 3337 | // Same as previous, except for 123[f().a] case |
| 3338 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3339 | RHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3340 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3341 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3342 | RHSTy = RHSExp->getType(); |
| 3343 | |
| 3344 | BaseExpr = RHSExp; |
| 3345 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3346 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 3347 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3348 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3349 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3350 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3351 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3352 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3353 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3354 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 3355 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3356 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3357 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3358 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3359 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3360 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3361 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3363 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3364 | // incomplete types are not object types. |
| 3365 | if (ResultType->isFunctionType()) { |
| 3366 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3367 | << ResultType << BaseExpr->getSourceRange(); |
| 3368 | return ExprError(); |
| 3369 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3371 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3372 | // GNU extension: subscripting on pointer to void |
| 3373 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3374 | << BaseExpr->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3375 | |
| 3376 | // C forbids expressions of unqualified void type from being l-values. |
| 3377 | // See IsCForbiddenLValueType. |
| 3378 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3379 | } else if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3381 | PDiag(diag::err_subscript_incomplete_type) |
| 3382 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3383 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3385 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3386 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3387 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3388 | << ResultType << BaseExpr->getSourceRange(); |
| 3389 | return ExprError(); |
| 3390 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3391 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3392 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3393 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3394 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3395 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3396 | ResultType, VK, OK, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3399 | /// Check an ext-vector component access expression. |
| 3400 | /// |
| 3401 | /// VK should be set in advance to the value kind of the base |
| 3402 | /// expression. |
| 3403 | static QualType |
| 3404 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3405 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3406 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3407 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3408 | // see FIXME there. |
| 3409 | // |
| 3410 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3411 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3412 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3413 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3414 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3415 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3416 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3417 | // This flag determines whether or not the component is one of the four |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3418 | // special names that indicate a subset of exactly half the elements are |
| 3419 | // to be selected. |
| 3420 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3421 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3422 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3423 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 0359e12 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 3424 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3425 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3426 | bool HasRepeated = false; |
| 3427 | bool HasIndex[16] = {}; |
| 3428 | |
| 3429 | int Idx; |
| 3430 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3431 | // Check that we've found one of the special components, or that the component |
| 3432 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3433 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3434 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3435 | HalvingSwizzle = true; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3436 | } else if (!HexSwizzle && |
| 3437 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3438 | do { |
| 3439 | if (HasIndex[Idx]) HasRepeated = true; |
| 3440 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3441 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3442 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3443 | } else { |
| 3444 | if (HexSwizzle) compStr++; |
| 3445 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3446 | if (HasIndex[Idx]) HasRepeated = true; |
| 3447 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3448 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3449 | } |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3450 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3451 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3452 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3453 | // We didn't get to the end of the string. This means the component names |
| 3454 | // didn't come from the same set *or* we encountered an illegal name. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3455 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3456 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3457 | return QualType(); |
| 3458 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3459 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3460 | // Ensure no component accessor exceeds the width of the vector type it |
| 3461 | // operates on. |
| 3462 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3463 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3464 | |
| 3465 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3466 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3467 | |
| 3468 | while (*compStr) { |
| 3469 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3470 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3471 | << baseType << SourceRange(CompLoc); |
| 3472 | return QualType(); |
| 3473 | } |
| 3474 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3475 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3476 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3477 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3478 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3479 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3480 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3481 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | ac8183a | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3482 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3483 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3484 | if (HexSwizzle) |
| 3485 | CompSize--; |
| 3486 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3487 | if (CompSize == 1) |
| 3488 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3489 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3490 | if (HasRepeated) VK = VK_RValue; |
| 3491 | |
| 3492 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3493 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3494 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3495 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3496 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3497 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3498 | } |
| 3499 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3500 | } |
| 3501 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3502 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3503 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3504 | const Selector &Sel, |
| 3505 | ASTContext &Context) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3506 | if (Member) |
| 3507 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3508 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3509 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3510 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3511 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3512 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3513 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3514 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3515 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3516 | return D; |
| 3517 | } |
| 3518 | return 0; |
| 3519 | } |
| 3520 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3521 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3522 | IdentifierInfo *Member, |
| 3523 | const Selector &Sel, |
| 3524 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3525 | // Check protocols on qualified interfaces. |
| 3526 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3527 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3528 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3529 | if (Member) |
| 3530 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3531 | GDecl = PD; |
| 3532 | break; |
| 3533 | } |
| 3534 | // Also must look for a getter or setter name which uses property syntax. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3535 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3536 | GDecl = OMD; |
| 3537 | break; |
| 3538 | } |
| 3539 | } |
| 3540 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3541 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3542 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3543 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3544 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3545 | Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3546 | if (GDecl) |
| 3547 | return GDecl; |
| 3548 | } |
| 3549 | } |
| 3550 | return GDecl; |
| 3551 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3552 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3553 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3554 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3555 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3556 | const CXXScopeSpec &SS, |
| 3557 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3558 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3559 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3560 | // Even in dependent contexts, try to diagnose base expressions with |
| 3561 | // obviously wrong types, e.g.: |
| 3562 | // |
| 3563 | // T* t; |
| 3564 | // t.f; |
| 3565 | // |
| 3566 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3567 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3568 | // allows this, while still reporting an error if T is a struct pointer. |
| 3569 | if (!IsArrow) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3570 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3571 | if (PT && (!getLangOptions().ObjC1 || |
| 3572 | PT->getPointeeType()->isRecordType())) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3573 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3574 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3575 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3576 | return ExprError(); |
| 3577 | } |
| 3578 | } |
| 3579 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3580 | assert(BaseType->isDependentType() || |
| 3581 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3582 | isDependentScopeSpecifier(SS)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3583 | |
| 3584 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3585 | // must have pointer type, and the accessed type is the pointee. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3586 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3587 | IsArrow, OpLoc, |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3588 | SS.getWithLocInContext(Context), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3589 | FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3590 | NameInfo, TemplateArgs)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | /// We know that the given qualified member reference points only to |
| 3594 | /// declarations which do not belong to the static type of the base |
| 3595 | /// expression. Diagnose the problem. |
| 3596 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3597 | Expr *BaseExpr, |
| 3598 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3599 | const CXXScopeSpec &SS, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3600 | NamedDecl *rep, |
| 3601 | const DeclarationNameInfo &nameInfo) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3602 | // If this is an implicit member access, use a different set of |
| 3603 | // diagnostics. |
| 3604 | if (!BaseExpr) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3605 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3606 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3607 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3608 | << SS.getRange() << rep << BaseType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | // Check whether the declarations we found through a nested-name |
| 3612 | // specifier in a member expression are actually members of the base |
| 3613 | // type. The restriction here is: |
| 3614 | // |
| 3615 | // C++ [expr.ref]p2: |
| 3616 | // ... In these cases, the id-expression shall name a |
| 3617 | // member of the class or of one of its base classes. |
| 3618 | // |
| 3619 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3620 | // an unrelated class, and for us to find an overload set including |
| 3621 | // decls from classes which are not superclasses, as long as the decl |
| 3622 | // we actually pick through overload resolution is from a superclass. |
| 3623 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3624 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3625 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3626 | const LookupResult &R) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3627 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3628 | if (!BaseRT) { |
| 3629 | // We can't check this yet because the base type is still |
| 3630 | // dependent. |
| 3631 | assert(BaseType->isDependentType()); |
| 3632 | return false; |
| 3633 | } |
| 3634 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3635 | |
| 3636 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3637 | // If this is an implicit member reference and we find a |
| 3638 | // non-instance member, it's not an error. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3639 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3640 | return false; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3641 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3642 | // Note that we use the DC of the decl, not the underlying decl. |
Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3643 | DeclContext *DC = (*I)->getDeclContext(); |
| 3644 | while (DC->isTransparentContext()) |
| 3645 | DC = DC->getParent(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3647 | if (!DC->isRecord()) |
| 3648 | continue; |
| 3649 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3650 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3651 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3652 | |
| 3653 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3654 | return false; |
| 3655 | } |
| 3656 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3657 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3658 | R.getRepresentativeDecl(), |
| 3659 | R.getLookupNameInfo()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3660 | return true; |
| 3661 | } |
| 3662 | |
| 3663 | static bool |
| 3664 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3665 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3666 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3667 | bool HasTemplateArgs) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3668 | RecordDecl *RDecl = RTy->getDecl(); |
| 3669 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3670 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3671 | << BaseRange)) |
| 3672 | return true; |
| 3673 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3674 | if (HasTemplateArgs) { |
| 3675 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3676 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3677 | |
| 3678 | bool MOUS; |
| 3679 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3680 | return false; |
| 3681 | } |
| 3682 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3683 | DeclContext *DC = RDecl; |
| 3684 | if (SS.isSet()) { |
| 3685 | // If the member name was a qualified-id, look into the |
| 3686 | // nested-name-specifier. |
| 3687 | DC = SemaRef.computeDeclContext(SS, false); |
| 3688 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3689 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3690 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3691 | << SS.getRange() << DC; |
| 3692 | return true; |
| 3693 | } |
| 3694 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3695 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3696 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3697 | if (!isa<TypeDecl>(DC)) { |
| 3698 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3699 | << DC << SS.getRange(); |
| 3700 | return true; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3701 | } |
| 3702 | } |
| 3703 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3704 | // The record definition is complete, now look up the member. |
| 3705 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3706 | |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3707 | if (!R.empty()) |
| 3708 | return false; |
| 3709 | |
| 3710 | // We didn't find anything with the given name, so try to correct |
| 3711 | // for typos. |
| 3712 | DeclarationName Name = R.getLookupName(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3713 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3714 | !R.empty() && |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3715 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3716 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3717 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3718 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3719 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3720 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3721 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3722 | << ND->getDeclName(); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3723 | return false; |
| 3724 | } else { |
| 3725 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3726 | R.setLookupName(Name); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3729 | return false; |
| 3730 | } |
| 3731 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3732 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3733 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3734 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3735 | CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3736 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3737 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3738 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3739 | if (BaseType->isDependentType() || |
| 3740 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3741 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3742 | IsArrow, OpLoc, |
| 3743 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3744 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3745 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3746 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3747 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3748 | // Implicit member accesses. |
| 3749 | if (!Base) { |
| 3750 | QualType RecordTy = BaseType; |
| 3751 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3752 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3753 | RecordTy->getAs<RecordType>(), |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3754 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3755 | return ExprError(); |
| 3756 | |
| 3757 | // Explicit member accesses. |
| 3758 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3759 | ExprResult BaseResult = Owned(Base); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3760 | ExprResult Result = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3761 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3762 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3763 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3764 | if (BaseResult.isInvalid()) |
| 3765 | return ExprError(); |
| 3766 | Base = BaseResult.take(); |
| 3767 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3768 | if (Result.isInvalid()) { |
| 3769 | Owned(Base); |
| 3770 | return ExprError(); |
| 3771 | } |
| 3772 | |
| 3773 | if (Result.get()) |
| 3774 | return move(Result); |
Sebastian Redl | fa1f70f | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3775 | |
| 3776 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3777 | BaseType = Base->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3780 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3781 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3782 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3783 | } |
| 3784 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3785 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3786 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3787 | SourceLocation OpLoc, bool IsArrow, |
| 3788 | const CXXScopeSpec &SS, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3789 | NamedDecl *FirstQualifierInScope, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3790 | LookupResult &R, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3791 | const TemplateArgumentListInfo *TemplateArgs, |
| 3792 | bool SuppressQualifierCheck) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3793 | QualType BaseType = BaseExprType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3794 | if (IsArrow) { |
| 3795 | assert(BaseType->isPointerType()); |
| 3796 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3797 | } |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3798 | R.setBaseObjectType(BaseType); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3799 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3800 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3801 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3802 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3803 | |
| 3804 | if (R.isAmbiguous()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3805 | return ExprError(); |
| 3806 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3807 | if (R.empty()) { |
| 3808 | // Rederive where we looked up. |
| 3809 | DeclContext *DC = (SS.isSet() |
| 3810 | ? computeDeclContext(SS, false) |
| 3811 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3812 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3813 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3814 | << MemberName << DC |
| 3815 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3816 | return ExprError(); |
| 3817 | } |
| 3818 | |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3819 | // Diagnose lookups that find only declarations from a non-base |
| 3820 | // type. This is possible for either qualified lookups (which may |
| 3821 | // have been qualified with an unrelated type) or implicit member |
| 3822 | // expressions (which were found with unqualified lookup and thus |
| 3823 | // may have come from an enclosing scope). Note that it's okay for |
| 3824 | // lookup to find declarations from a non-base type as long as those |
| 3825 | // aren't the ones picked by overload resolution. |
| 3826 | if ((SS.isSet() || !BaseExpr || |
| 3827 | (isa<CXXThisExpr>(BaseExpr) && |
| 3828 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3829 | !SuppressQualifierCheck && |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3830 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3831 | return ExprError(); |
| 3832 | |
| 3833 | // Construct an unresolved result if we in fact got an unresolved |
| 3834 | // result. |
| 3835 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3836 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3837 | // pick a member. |
| 3838 | R.suppressDiagnostics(); |
| 3839 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3840 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3841 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3842 | BaseExpr, BaseExprType, |
| 3843 | IsArrow, OpLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 3844 | SS.getWithLocInContext(Context), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3845 | MemberNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3846 | TemplateArgs, R.begin(), R.end()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3847 | |
| 3848 | return Owned(MemExpr); |
| 3849 | } |
| 3850 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3851 | assert(R.isSingleResult()); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3852 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3853 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 3854 | |
| 3855 | // FIXME: diagnose the presence of template arguments now. |
| 3856 | |
| 3857 | // If the decl being referenced had an error, return an error for this |
| 3858 | // sub-expr without emitting another error, in order to avoid cascading |
| 3859 | // error cases. |
| 3860 | if (MemberDecl->isInvalidDecl()) |
| 3861 | return ExprError(); |
| 3862 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3863 | // Handle the implicit-member-access case. |
| 3864 | if (!BaseExpr) { |
| 3865 | // If this is not an instance member, convert to a non-member access. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3866 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3867 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3868 | |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 3869 | SourceLocation Loc = R.getNameLoc(); |
| 3870 | if (SS.getRange().isValid()) |
| 3871 | Loc = SS.getRange().getBegin(); |
| 3872 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3875 | bool ShouldCheckUse = true; |
| 3876 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 3877 | // Don't diagnose the use of a virtual member function unless it's |
| 3878 | // explicitly qualified. |
| 3879 | if (MD->isVirtual() && !SS.isSet()) |
| 3880 | ShouldCheckUse = false; |
| 3881 | } |
| 3882 | |
| 3883 | // Check the use of this member. |
| 3884 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 3885 | Owned(BaseExpr); |
| 3886 | return ExprError(); |
| 3887 | } |
| 3888 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3889 | // Perform a property load on the base regardless of whether we |
| 3890 | // actually need it for the declaration. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3891 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 3892 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 3893 | if (Result.isInvalid()) |
| 3894 | return ExprError(); |
| 3895 | BaseExpr = Result.take(); |
| 3896 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3897 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3898 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 3899 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 3900 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3901 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3902 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 3903 | // We may have found a field within an anonymous union or struct |
| 3904 | // (C++ [class.union]). |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3905 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3906 | BaseExpr, OpLoc); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3907 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3908 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 3909 | MarkDeclarationReferenced(MemberLoc, Var); |
| 3910 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3911 | Var, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3912 | Var->getType().getNonReferenceType(), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3913 | VK_LValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3916 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3917 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3918 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3919 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3920 | MemberFn->getType(), |
| 3921 | MemberFn->isInstance() ? VK_RValue : VK_LValue, |
| 3922 | OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3923 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3924 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3925 | |
| 3926 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 3927 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3928 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3929 | Enum, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3930 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | Owned(BaseExpr); |
| 3934 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3935 | // We found something that we didn't expect. Complain. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3936 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3937 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3938 | << MemberName << BaseType << int(IsArrow); |
| 3939 | else |
| 3940 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 3941 | << MemberName << BaseType << int(IsArrow); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3943 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 3944 | << MemberName; |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3945 | R.suppressDiagnostics(); |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3946 | return ExprError(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3947 | } |
| 3948 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3949 | /// Given that normal member access failed on the given expression, |
| 3950 | /// and given that the expression's type involves builtin-id or |
| 3951 | /// builtin-Class, decide whether substituting in the redefinition |
| 3952 | /// types would be profitable. The redefinition type is whatever |
| 3953 | /// this translation unit tried to typedef to id/Class; we store |
| 3954 | /// it to the side and then re-use it in places like this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3955 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3956 | const ObjCObjectPointerType *opty |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3957 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3958 | if (!opty) return false; |
| 3959 | |
| 3960 | const ObjCObjectType *ty = opty->getObjectType(); |
| 3961 | |
| 3962 | QualType redef; |
| 3963 | if (ty->isObjCId()) { |
| 3964 | redef = S.Context.ObjCIdRedefinitionType; |
| 3965 | } else if (ty->isObjCClass()) { |
| 3966 | redef = S.Context.ObjCClassRedefinitionType; |
| 3967 | } else { |
| 3968 | return false; |
| 3969 | } |
| 3970 | |
| 3971 | // Do the substitution as long as the redefinition type isn't just a |
| 3972 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 3973 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 3974 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 3975 | return false; |
| 3976 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3977 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3978 | return true; |
| 3979 | } |
| 3980 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3981 | /// Look up the given member of the given non-type-dependent |
| 3982 | /// expression. This can return in one of two ways: |
| 3983 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 3984 | /// assume that lookup was performed and the results written into |
| 3985 | /// the provided structure. It will take over from there. |
| 3986 | /// * Otherwise, the returned expression will be produced in place of |
| 3987 | /// an ordinary member expression. |
| 3988 | /// |
| 3989 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 3990 | /// fixed for ObjC++. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3991 | ExprResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3992 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3993 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3994 | CXXScopeSpec &SS, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3995 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3996 | assert(BaseExpr.get() && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 3998 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3999 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4000 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4001 | if (IsArrow) { |
| 4002 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4003 | if (BaseExpr.isInvalid()) |
| 4004 | return ExprError(); |
| 4005 | } |
| 4006 | |
| 4007 | QualType BaseType = BaseExpr.get()->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4008 | assert(!BaseType->isDependentType()); |
| 4009 | |
| 4010 | DeclarationName MemberName = R.getLookupName(); |
| 4011 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4012 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4013 | // For later type-checking purposes, turn arrow accesses into dot |
| 4014 | // accesses. The only access type we support that doesn't follow |
| 4015 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 4016 | // and those never use arrows, so this is unaffected. |
| 4017 | if (IsArrow) { |
| 4018 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4019 | BaseType = Ptr->getPointeeType(); |
| 4020 | else if (const ObjCObjectPointerType *Ptr |
| 4021 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 4022 | BaseType = Ptr->getPointeeType(); |
| 4023 | else if (BaseType->isRecordType()) { |
| 4024 | // Recover from arrow accesses to records, e.g.: |
| 4025 | // struct MyRecord foo; |
| 4026 | // foo->bar |
| 4027 | // This is actually well-formed in C++ if MyRecord has an |
| 4028 | // overloaded operator->, but that should have been dealt with |
| 4029 | // by now. |
| 4030 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4031 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4032 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4033 | IsArrow = false; |
| 4034 | } else { |
| 4035 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4036 | << BaseType << BaseExpr.get()->getSourceRange(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4037 | return ExprError(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4041 | // Handle field access to simple records. |
| 4042 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4043 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4044 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 4045 | return ExprError(); |
| 4046 | |
| 4047 | // Returning valid-but-null is how we indicate to the caller that |
| 4048 | // the lookup result was filled in. |
| 4049 | return Owned((Expr*) 0); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4050 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4051 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4052 | // Handle ivar access to Objective-C objects. |
| 4053 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4054 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4055 | |
| 4056 | // There are three cases for the base type: |
| 4057 | // - builtin id (qualified or unqualified) |
| 4058 | // - builtin Class (qualified or unqualified) |
| 4059 | // - an interface |
| 4060 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 4061 | if (!IDecl) { |
| 4062 | // There's an implicit 'isa' ivar on all objects. |
| 4063 | // But we only actually find it this way on objects of type 'id', |
| 4064 | // apparently. |
| 4065 | if (OTy->isObjCId() && Member->isStr("isa")) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4066 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4067 | Context.getObjCClassType())); |
| 4068 | |
| 4069 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4070 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4071 | ObjCImpDecl, HasTemplateArgs); |
| 4072 | goto fail; |
| 4073 | } |
| 4074 | |
| 4075 | ObjCInterfaceDecl *ClassDeclared; |
| 4076 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 4077 | |
| 4078 | if (!IV) { |
| 4079 | // Attempt to correct for typos in ivar names. |
| 4080 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 4081 | LookupMemberName); |
| 4082 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 4083 | IsArrow ? CTC_ObjCIvarLookup |
| 4084 | : CTC_ObjCPropertyLookup) && |
| 4085 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 4086 | Diag(R.getNameLoc(), |
| 4087 | diag::err_typecheck_member_reference_ivar_suggest) |
| 4088 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 4089 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 4090 | IV->getNameAsString()); |
| 4091 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 4092 | << IV->getDeclName(); |
| 4093 | } else { |
| 4094 | Res.clear(); |
| 4095 | Res.setLookupName(Member); |
| 4096 | |
| 4097 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 4098 | << IDecl->getDeclName() << MemberName |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4099 | << BaseExpr.get()->getSourceRange(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4100 | return ExprError(); |
| 4101 | } |
| 4102 | } |
| 4103 | |
| 4104 | // If the decl being referenced had an error, return an error for this |
| 4105 | // sub-expr without emitting another error, in order to avoid cascading |
| 4106 | // error cases. |
| 4107 | if (IV->isInvalidDecl()) |
| 4108 | return ExprError(); |
| 4109 | |
| 4110 | // Check whether we can reference this field. |
| 4111 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 4112 | return ExprError(); |
| 4113 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 4114 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 4115 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 4116 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 4117 | ClassOfMethodDecl = MD->getClassInterface(); |
| 4118 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 4119 | // Case of a c-function declared inside an objc implementation. |
| 4120 | // FIXME: For a c-style function nested inside an objc implementation |
| 4121 | // class, there is no implementation context available, so we pass |
| 4122 | // down the context as argument to this routine. Ideally, this context |
| 4123 | // need be passed down in the AST node and somehow calculated from the |
| 4124 | // AST for a function decl. |
| 4125 | if (ObjCImplementationDecl *IMPD = |
| 4126 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 4127 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 4128 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 4129 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 4130 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 4131 | } |
| 4132 | |
| 4133 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 4134 | if (ClassDeclared != IDecl || |
| 4135 | ClassOfMethodDecl != ClassDeclared) |
| 4136 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 4137 | << IV->getDeclName(); |
| 4138 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 4139 | // @protected |
| 4140 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 4141 | << IV->getDeclName(); |
| 4142 | } |
| 4143 | |
| 4144 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4145 | MemberLoc, BaseExpr.take(), |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4146 | IsArrow)); |
| 4147 | } |
| 4148 | |
| 4149 | // Objective-C property access. |
| 4150 | const ObjCObjectPointerType *OPT; |
| 4151 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 4152 | // This actually uses the base as an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4153 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4154 | if (BaseExpr.isInvalid()) |
| 4155 | return ExprError(); |
| 4156 | |
| 4157 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4158 | |
| 4159 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 4160 | |
| 4161 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 4162 | |
| 4163 | // id, with and without qualifiers. |
| 4164 | if (OT->isObjCId()) { |
| 4165 | // Check protocols on qualified interfaces. |
| 4166 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 4167 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 4168 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 4169 | // Check the use of this declaration |
| 4170 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 4171 | return ExprError(); |
| 4172 | |
| 4173 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 4174 | VK_LValue, |
| 4175 | OK_ObjCProperty, |
| 4176 | MemberLoc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4177 | BaseExpr.take())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
| 4180 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4181 | // Check the use of this method. |
| 4182 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4183 | return ExprError(); |
| 4184 | Selector SetterSel = |
| 4185 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4186 | PP.getSelectorTable(), Member); |
| 4187 | ObjCMethodDecl *SMD = 0; |
| 4188 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4189 | SetterSel, Context)) |
| 4190 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 4191 | QualType PType = OMD->getSendResultType(); |
| 4192 | |
| 4193 | ExprValueKind VK = VK_LValue; |
| 4194 | if (!getLangOptions().CPlusPlus && |
| 4195 | IsCForbiddenLValueType(Context, PType)) |
| 4196 | VK = VK_RValue; |
| 4197 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4198 | |
| 4199 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4200 | VK, OK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4201 | MemberLoc, BaseExpr.take())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4202 | } |
| 4203 | } |
Fariborz Jahanian | b03a4c2 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4204 | // Use of id.member can only be for a property reference. Do not |
| 4205 | // use the 'id' redefinition in this case. |
| 4206 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4207 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4208 | ObjCImpDecl, HasTemplateArgs); |
| 4209 | |
| 4210 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4211 | << MemberName << BaseType); |
| 4212 | } |
| 4213 | |
| 4214 | // 'Class', unqualified only. |
| 4215 | if (OT->isObjCClass()) { |
| 4216 | // Only works in a method declaration (??!). |
| 4217 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4218 | if (!MD) { |
| 4219 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4220 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4221 | ObjCImpDecl, HasTemplateArgs); |
| 4222 | |
| 4223 | goto fail; |
| 4224 | } |
| 4225 | |
| 4226 | // Also must look for a getter name which uses property syntax. |
| 4227 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4228 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4229 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4230 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4231 | // Check the use of this method. |
| 4232 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4233 | return ExprError(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4234 | } else |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4235 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4236 | // If we found a getter then this may be a valid dot-reference, we |
| 4237 | // will look for the matching setter, in case it is needed. |
| 4238 | Selector SetterSel = |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4239 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4240 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4241 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4242 | if (!Setter) { |
| 4243 | // If this reference is in an @implementation, also check for 'private' |
| 4244 | // methods. |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4245 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4246 | } |
| 4247 | // Look through local category implementations associated with the class. |
| 4248 | if (!Setter) |
| 4249 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4250 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4251 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4252 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4253 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4254 | if (Getter || Setter) { |
| 4255 | QualType PType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4256 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4257 | ExprValueKind VK = VK_LValue; |
| 4258 | if (Getter) { |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4259 | PType = Getter->getSendResultType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4260 | if (!getLangOptions().CPlusPlus && |
| 4261 | IsCForbiddenLValueType(Context, PType)) |
| 4262 | VK = VK_RValue; |
| 4263 | } else { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4264 | // Get the expression type from Setter's incoming parameter. |
| 4265 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4266 | } |
| 4267 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4268 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4269 | // FIXME: we must check that the setter has property type. |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4270 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4271 | PType, VK, OK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4272 | MemberLoc, BaseExpr.take())); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4273 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4274 | |
| 4275 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4276 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4277 | ObjCImpDecl, HasTemplateArgs); |
| 4278 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4279 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4280 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4281 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4282 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4283 | // Normal property access. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4284 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4285 | SourceLocation(), QualType(), false); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4286 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4287 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4288 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4289 | if (BaseType->isExtVectorType()) { |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4290 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4291 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4292 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4293 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4294 | Member, MemberLoc); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4295 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4296 | return ExprError(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4297 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4298 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4299 | *Member, MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4300 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4301 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4302 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4303 | // not just a pointer to builtin-sel again. |
| 4304 | if (IsArrow && |
| 4305 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4306 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4307 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4308 | CK_BitCast); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4309 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4310 | ObjCImpDecl, HasTemplateArgs); |
| 4311 | } |
| 4312 | |
| 4313 | // Failure cases. |
| 4314 | fail: |
| 4315 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4316 | // Recover from dot accesses to pointers, e.g.: |
| 4317 | // type *foo; |
| 4318 | // foo.bar |
| 4319 | // This is actually well-formed in two cases: |
| 4320 | // - 'type' is an Objective C type |
| 4321 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4322 | // the appropriate pointer type |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4323 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4324 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4325 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4326 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4327 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4328 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4329 | |
| 4330 | // Recurse as an -> access. |
| 4331 | IsArrow = true; |
| 4332 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4333 | ObjCImpDecl, HasTemplateArgs); |
| 4334 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4335 | } |
| 4336 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4337 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4338 | // because they forgot parentheses to call that function. |
| 4339 | bool TryCall = false; |
| 4340 | bool Overloaded = false; |
| 4341 | UnresolvedSet<8> AllOverloads; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4342 | if (const OverloadExpr *Overloads = dyn_cast<OverloadExpr>(BaseExpr.get())) { |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4343 | AllOverloads.append(Overloads->decls_begin(), Overloads->decls_end()); |
| 4344 | TryCall = true; |
| 4345 | Overloaded = true; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4346 | } else if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(BaseExpr.get())) { |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4347 | if (FunctionDecl* Fun = dyn_cast<FunctionDecl>(DeclRef->getDecl())) { |
| 4348 | AllOverloads.addDecl(Fun); |
| 4349 | TryCall = true; |
| 4350 | } |
| 4351 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4352 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4353 | if (TryCall) { |
| 4354 | // Plunder the overload set for something that would make the member |
| 4355 | // expression valid. |
| 4356 | UnresolvedSet<4> ViableOverloads; |
| 4357 | bool HasViableZeroArgOverload = false; |
| 4358 | for (OverloadExpr::decls_iterator it = AllOverloads.begin(), |
| 4359 | DeclsEnd = AllOverloads.end(); it != DeclsEnd; ++it) { |
Matt Beaumont-Gay | f8bb45f | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4360 | // Our overload set may include TemplateDecls, which we'll ignore for the |
| 4361 | // purposes of determining whether we can issue a '()' fixit. |
| 4362 | if (const FunctionDecl *OverloadDecl = dyn_cast<FunctionDecl>(*it)) { |
| 4363 | QualType ResultTy = OverloadDecl->getResultType(); |
| 4364 | if ((!IsArrow && ResultTy->isRecordType()) || |
| 4365 | (IsArrow && ResultTy->isPointerType() && |
| 4366 | ResultTy->getPointeeType()->isRecordType())) { |
| 4367 | ViableOverloads.addDecl(*it); |
| 4368 | if (OverloadDecl->getMinRequiredArguments() == 0) { |
| 4369 | HasViableZeroArgOverload = true; |
| 4370 | } |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4371 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4375 | if (!HasViableZeroArgOverload || ViableOverloads.size() != 1) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4376 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | f8bb45f | 2011-03-05 02:42:30 +0000 | [diff] [blame] | 4377 | << (AllOverloads.size() > 1) << 0 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4378 | << BaseExpr.get()->getSourceRange(); |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4379 | int ViableOverloadCount = ViableOverloads.size(); |
| 4380 | int I; |
| 4381 | for (I = 0; I < ViableOverloadCount; ++I) { |
| 4382 | // FIXME: Magic number for max shown overloads stolen from |
| 4383 | // OverloadCandidateSet::NoteCandidates. |
| 4384 | if (I >= 4 && Diags.getShowOverloads() == Diagnostic::Ovl_Best) { |
| 4385 | break; |
| 4386 | } |
| 4387 | Diag(ViableOverloads[I].getDecl()->getSourceRange().getBegin(), |
| 4388 | diag::note_member_ref_possible_intended_overload); |
| 4389 | } |
| 4390 | if (I != ViableOverloadCount) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4391 | Diag(BaseExpr.get()->getExprLoc(), diag::note_ovl_too_many_candidates) |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4392 | << int(ViableOverloadCount - I); |
| 4393 | } |
| 4394 | return ExprError(); |
| 4395 | } |
| 4396 | } else { |
| 4397 | // We don't have an expression that's convenient to get a Decl from, but we |
| 4398 | // can at least check if the type is "function of 0 arguments which returns |
| 4399 | // an acceptable type". |
| 4400 | const FunctionType *Fun = NULL; |
| 4401 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 4402 | if ((Fun = Ptr->getPointeeType()->getAs<FunctionType>())) { |
| 4403 | TryCall = true; |
| 4404 | } |
| 4405 | } else if ((Fun = BaseType->getAs<FunctionType>())) { |
| 4406 | TryCall = true; |
| 4407 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4408 | |
| 4409 | if (TryCall) { |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4410 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Fun)) { |
| 4411 | if (FPT->getNumArgs() == 0) { |
| 4412 | QualType ResultTy = Fun->getResultType(); |
| 4413 | TryCall = (!IsArrow && ResultTy->isRecordType()) || |
| 4414 | (IsArrow && ResultTy->isPointerType() && |
| 4415 | ResultTy->getPointeeType()->isRecordType()); |
| 4416 | } |
Matt Beaumont-Gay | 956fc1c | 2011-02-17 02:54:17 +0000 | [diff] [blame] | 4417 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4418 | } |
| 4419 | } |
| 4420 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4421 | if (TryCall) { |
| 4422 | // At this point, we know BaseExpr looks like it's potentially callable with |
| 4423 | // 0 arguments, and that it returns something of a reasonable type, so we |
| 4424 | // can emit a fixit and carry on pretending that BaseExpr was actually a |
| 4425 | // CallExpr. |
| 4426 | SourceLocation ParenInsertionLoc = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4427 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4428 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4429 | << int(Overloaded) << 1 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4430 | << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4431 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4432 | ExprResult NewBase = ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4433 | MultiExprArg(*this, 0, 0), |
| 4434 | ParenInsertionLoc); |
| 4435 | if (NewBase.isInvalid()) |
| 4436 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4437 | BaseExpr = NewBase; |
| 4438 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4439 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4440 | ObjCImpDecl, HasTemplateArgs); |
| 4441 | } |
| 4442 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4443 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4444 | << BaseType << BaseExpr.get()->getSourceRange(); |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4446 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4449 | /// The main callback when the parser finds something like |
| 4450 | /// expression . [nested-name-specifier] identifier |
| 4451 | /// expression -> [nested-name-specifier] identifier |
| 4452 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4453 | /// possibilities, including destructor and operator references. |
| 4454 | /// |
| 4455 | /// \param OpKind either tok::arrow or tok::period |
| 4456 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4457 | /// is used to diagnose mis-uses of special members that can |
| 4458 | /// only be called |
| 4459 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4460 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4461 | /// aren't properly put in the context chain |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4462 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4463 | SourceLocation OpLoc, |
| 4464 | tok::TokenKind OpKind, |
| 4465 | CXXScopeSpec &SS, |
| 4466 | UnqualifiedId &Id, |
| 4467 | Decl *ObjCImpDecl, |
| 4468 | bool HasTrailingLParen) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4469 | if (SS.isSet() && SS.isInvalid()) |
| 4470 | return ExprError(); |
| 4471 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4472 | // Warn about the explicit constructor calls Microsoft extension. |
| 4473 | if (getLangOptions().Microsoft && |
| 4474 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4475 | Diag(Id.getSourceRange().getBegin(), |
| 4476 | diag::ext_ms_explicit_constructor_call); |
| 4477 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4478 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4479 | |
| 4480 | // Decompose the name into its component parts. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4481 | DeclarationNameInfo NameInfo; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4482 | const TemplateArgumentListInfo *TemplateArgs; |
| 4483 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4484 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4485 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4486 | DeclarationName Name = NameInfo.getName(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4487 | bool IsArrow = (OpKind == tok::arrow); |
| 4488 | |
| 4489 | NamedDecl *FirstQualifierInScope |
| 4490 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4491 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4492 | |
| 4493 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4494 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4495 | if (Result.isInvalid()) return ExprError(); |
| 4496 | Base = Result.take(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4498 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4499 | isDependentScopeSpecifier(SS)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4500 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4501 | IsArrow, OpLoc, |
| 4502 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4503 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4504 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4505 | LookupResult R(*this, NameInfo, LookupMemberName); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4506 | ExprResult BaseResult = Owned(Base); |
| 4507 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4508 | SS, ObjCImpDecl, TemplateArgs != 0); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4509 | if (BaseResult.isInvalid()) |
| 4510 | return ExprError(); |
| 4511 | Base = BaseResult.take(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4512 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4513 | if (Result.isInvalid()) { |
| 4514 | Owned(Base); |
| 4515 | return ExprError(); |
| 4516 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4517 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4518 | if (Result.get()) { |
| 4519 | // The only way a reference to a destructor can be used is to |
| 4520 | // immediately call it, which falls into this case. If the |
| 4521 | // next token is not a '(', produce a diagnostic and build the |
| 4522 | // call now. |
| 4523 | if (!HasTrailingLParen && |
| 4524 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4525 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4526 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4527 | return move(Result); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4530 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4531 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4532 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | return move(Result); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4538 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4539 | FunctionDecl *FD, |
| 4540 | ParmVarDecl *Param) { |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4541 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4542 | Diag(CallLoc, |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4543 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4544 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4545 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4546 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4547 | return ExprError(); |
| 4548 | } |
| 4549 | |
| 4550 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4551 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4552 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4553 | // Instantiate the expression. |
| 4554 | MultiLevelTemplateArgumentList ArgList |
| 4555 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4556 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4557 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4558 | = ArgList.getInnermost(); |
| 4559 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4560 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4561 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4562 | ExprResult Result; |
| 4563 | { |
| 4564 | // C++ [dcl.fct.default]p5: |
| 4565 | // The names in the [default argument] expression are bound, and |
| 4566 | // the semantic constraints are checked, at the point where the |
| 4567 | // default argument expression appears. |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4568 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4569 | Result = SubstExpr(UninstExpr, ArgList); |
| 4570 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4571 | if (Result.isInvalid()) |
| 4572 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4574 | // Check the expression as an initializer for the parameter. |
| 4575 | InitializedEntity Entity |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4576 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4577 | InitializationKind Kind |
| 4578 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4579 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4580 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4581 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4582 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4583 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4584 | MultiExprArg(*this, &ResultE, 1)); |
| 4585 | if (Result.isInvalid()) |
| 4586 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4587 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4588 | // Build the default argument expression. |
| 4589 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4590 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4591 | } |
| 4592 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4593 | // If the default expression creates temporaries, we need to |
| 4594 | // push them to the current stack of expression temporaries so they'll |
| 4595 | // be properly destroyed. |
| 4596 | // FIXME: We should really be rebuilding the default argument with new |
| 4597 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4598 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4599 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4600 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4601 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4602 | ExprTemporaries.push_back(Temporary); |
| 4603 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4604 | |
| 4605 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4606 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4607 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4608 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4609 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4610 | } |
| 4611 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4612 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4613 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4614 | /// function prototype Proto. Call is the call expression itself, and |
| 4615 | /// Fn is the function expression. For a C++ member function, this |
| 4616 | /// routine does not attempt to convert the object argument. Returns |
| 4617 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4618 | bool |
| 4619 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4620 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4621 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4622 | Expr **Args, unsigned NumArgs, |
| 4623 | SourceLocation RParenLoc) { |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4624 | // Bail out early if calling a builtin with custom typechecking. |
| 4625 | // We don't need to do this in the |
| 4626 | if (FDecl) |
| 4627 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4628 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4629 | return false; |
| 4630 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4631 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4632 | // assignment, to the types of the corresponding parameter, ... |
| 4633 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4634 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4635 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4636 | // If too few arguments are available (and we don't have default |
| 4637 | // arguments for the remaining parameters), don't make the call. |
| 4638 | if (NumArgs < NumArgsInProto) { |
| 4639 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4640 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4641 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4642 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4643 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | // If too many are passed and not variadic, error on the extras and drop |
| 4647 | // them. |
| 4648 | if (NumArgs > NumArgsInProto) { |
| 4649 | if (!Proto->isVariadic()) { |
| 4650 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4651 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4652 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4653 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4654 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4655 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4656 | |
| 4657 | // Emit the location of the prototype. |
| 4658 | if (FDecl && !FDecl->getBuiltinID()) |
| 4659 | Diag(FDecl->getLocStart(), |
| 4660 | diag::note_typecheck_call_too_many_args) |
| 4661 | << FDecl; |
| 4662 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4663 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4664 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4665 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4666 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4667 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4668 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4669 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4670 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4671 | if (Fn->getType()->isBlockPointerType()) |
| 4672 | CallType = VariadicBlock; // Block |
| 4673 | else if (isa<MemberExpr>(Fn)) |
| 4674 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4675 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4676 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4677 | if (Invalid) |
| 4678 | return true; |
| 4679 | unsigned TotalNumArgs = AllArgs.size(); |
| 4680 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4681 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4682 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4683 | return false; |
| 4684 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4685 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4686 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4687 | FunctionDecl *FDecl, |
| 4688 | const FunctionProtoType *Proto, |
| 4689 | unsigned FirstProtoArg, |
| 4690 | Expr **Args, unsigned NumArgs, |
| 4691 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4692 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4693 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4694 | unsigned NumArgsToCheck = NumArgs; |
| 4695 | bool Invalid = false; |
| 4696 | if (NumArgs != NumArgsInProto) |
| 4697 | // Use default arguments for missing arguments |
| 4698 | NumArgsToCheck = NumArgsInProto; |
| 4699 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4700 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4701 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4702 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4704 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4705 | if (ArgIx < NumArgs) { |
| 4706 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4707 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4708 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4709 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4710 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4711 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4712 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4714 | // Pass the argument |
| 4715 | ParmVarDecl *Param = 0; |
| 4716 | if (FDecl && i < FDecl->getNumParams()) |
| 4717 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4718 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4719 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4720 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4721 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4722 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4723 | SourceLocation(), |
| 4724 | Owned(Arg)); |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4725 | if (ArgE.isInvalid()) |
| 4726 | return true; |
| 4727 | |
| 4728 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4729 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4730 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4731 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4732 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4733 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4734 | if (ArgExpr.isInvalid()) |
| 4735 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4736 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4737 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4738 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4739 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4740 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4742 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4743 | if (CallType != VariadicDoesNotApply) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4744 | |
| 4745 | // Assume that extern "C" functions with variadic arguments that |
| 4746 | // return __unknown_anytype aren't *really* variadic. |
| 4747 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4748 | FDecl && FDecl->isExternC()) { |
| 4749 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4750 | ExprResult arg; |
| 4751 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4752 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4753 | else |
| 4754 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4755 | Invalid |= arg.isInvalid(); |
| 4756 | AllArgs.push_back(arg.take()); |
| 4757 | } |
| 4758 | |
| 4759 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4760 | } else { |
| 4761 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4762 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4763 | Invalid |= Arg.isInvalid(); |
| 4764 | AllArgs.push_back(Arg.take()); |
| 4765 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4766 | } |
| 4767 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4768 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4771 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4772 | /// to have a function type. |
| 4773 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4774 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4775 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4776 | /// This provides the location of the left/right parens and a list of comma |
| 4777 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4778 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4779 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4780 | MultiExprArg args, SourceLocation RParenLoc, |
| 4781 | Expr *ExecConfig) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4782 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4783 | |
| 4784 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4785 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4786 | if (Result.isInvalid()) return ExprError(); |
| 4787 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4788 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4789 | Expr **Args = args.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4791 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4792 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4793 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4794 | if (NumArgs > 0) { |
| 4795 | // Pseudo-destructor calls should not have any arguments. |
| 4796 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4797 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4798 | SourceRange(Args[0]->getLocStart(), |
| 4799 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4800 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4801 | NumArgs = 0; |
| 4802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4804 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4805 | VK_RValue, RParenLoc)); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4806 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4808 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4809 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4810 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4811 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4812 | bool Dependent = false; |
| 4813 | if (Fn->isTypeDependent()) |
| 4814 | Dependent = true; |
| 4815 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4816 | Dependent = true; |
| 4817 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4818 | if (Dependent) { |
| 4819 | if (ExecConfig) { |
| 4820 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4821 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4822 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4823 | } else { |
| 4824 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4825 | Context.DependentTy, VK_RValue, |
| 4826 | RParenLoc)); |
| 4827 | } |
| 4828 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4829 | |
| 4830 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4831 | if (Fn->getType()->isRecordType()) |
| 4832 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4833 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4834 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4835 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4836 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4837 | if (result.isInvalid()) return ExprError(); |
| 4838 | Fn = result.take(); |
| 4839 | } |
| 4840 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4841 | Expr *NakedFn = Fn->IgnoreParens(); |
| 4842 | |
| 4843 | // Determine whether this is a call to an unresolved member function. |
| 4844 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4845 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 4846 | // an unresolved using declaration), it has to be an overloaded |
| 4847 | // function set, which means it must contain either multiple |
| 4848 | // declarations (all methods or method templates) or a single |
| 4849 | // method template. |
| 4850 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4851 | isa<FunctionTemplateDecl>( |
| 4852 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 8f184a3 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 4853 | (void)MemE; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4854 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4855 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4856 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4859 | // Determine whether this is a call to a member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4860 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4861 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4862 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4863 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4864 | RParenLoc); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4865 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4866 | |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4867 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4868 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4869 | if (BO->getOpcode() == BO_PtrMemD || |
| 4870 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | c8be952 | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 4871 | if (const FunctionProtoType *FPT |
| 4872 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4873 | QualType ResultTy = FPT->getCallResultType(Context); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4874 | ExprValueKind VK = Expr::getValueKindForType(FPT->getResultType()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4875 | |
Douglas Gregor | 125fa40 | 2011-02-04 12:57:49 +0000 | [diff] [blame] | 4876 | // Check that the object type isn't more qualified than the |
| 4877 | // member function we're calling. |
| 4878 | Qualifiers FuncQuals = Qualifiers::fromCVRMask(FPT->getTypeQuals()); |
| 4879 | Qualifiers ObjectQuals |
| 4880 | = BO->getOpcode() == BO_PtrMemD |
| 4881 | ? BO->getLHS()->getType().getQualifiers() |
| 4882 | : BO->getLHS()->getType()->getAs<PointerType>() |
| 4883 | ->getPointeeType().getQualifiers(); |
| 4884 | |
| 4885 | Qualifiers Difference = ObjectQuals - FuncQuals; |
| 4886 | Difference.removeObjCGCAttr(); |
| 4887 | Difference.removeAddressSpace(); |
| 4888 | if (Difference) { |
| 4889 | std::string QualsString = Difference.getAsString(); |
| 4890 | Diag(LParenLoc, diag::err_pointer_to_member_call_drops_quals) |
| 4891 | << BO->getType().getUnqualifiedType() |
| 4892 | << QualsString |
| 4893 | << (QualsString.find(' ') == std::string::npos? 1 : 2); |
| 4894 | } |
| 4895 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4896 | CXXMemberCallExpr *TheCall |
Abramo Bagnara | 21e9d86 | 2010-12-03 21:39:42 +0000 | [diff] [blame] | 4897 | = new (Context) CXXMemberCallExpr(Context, Fn, Args, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4898 | NumArgs, ResultTy, VK, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4899 | RParenLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4900 | |
| 4901 | if (CheckCallReturnType(FPT->getResultType(), |
| 4902 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4903 | TheCall, 0)) |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4904 | return ExprError(); |
Anders Carlsson | 63dce02 | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 4905 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4906 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4907 | RParenLoc)) |
| 4908 | return ExprError(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4909 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4910 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4911 | } |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4912 | } |
| 4913 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4916 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4917 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4918 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4919 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4920 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4921 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 4922 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 4923 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4924 | RParenLoc, ExecConfig); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4927 | NamedDecl *NDecl = 0; |
Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4928 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4929 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4930 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4931 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4932 | if (isa<DeclRefExpr>(NakedFn)) |
| 4933 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 4934 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4935 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 4936 | ExecConfig); |
| 4937 | } |
| 4938 | |
| 4939 | ExprResult |
| 4940 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 4941 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 4942 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 4943 | if (!ConfigDecl) |
| 4944 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 4945 | << "cudaConfigureCall"); |
| 4946 | QualType ConfigQTy = ConfigDecl->getType(); |
| 4947 | |
| 4948 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 4949 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 4950 | |
| 4951 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4954 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4955 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4956 | /// unary-convert to an expression of function-pointer or |
| 4957 | /// block-pointer type. |
| 4958 | /// |
| 4959 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4960 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4961 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4962 | SourceLocation LParenLoc, |
| 4963 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4964 | SourceLocation RParenLoc, |
| 4965 | Expr *Config) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4966 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 4967 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4968 | // Promote the function operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4969 | ExprResult Result = UsualUnaryConversions(Fn); |
| 4970 | if (Result.isInvalid()) |
| 4971 | return ExprError(); |
| 4972 | Fn = Result.take(); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4973 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4974 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4975 | // of arguments and function on error. |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4976 | CallExpr *TheCall; |
| 4977 | if (Config) { |
| 4978 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 4979 | cast<CallExpr>(Config), |
| 4980 | Args, NumArgs, |
| 4981 | Context.BoolTy, |
| 4982 | VK_RValue, |
| 4983 | RParenLoc); |
| 4984 | } else { |
| 4985 | TheCall = new (Context) CallExpr(Context, Fn, |
| 4986 | Args, NumArgs, |
| 4987 | Context.BoolTy, |
| 4988 | VK_RValue, |
| 4989 | RParenLoc); |
| 4990 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4991 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4992 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 4993 | |
| 4994 | // Bail out early if calling a builtin with custom typechecking. |
| 4995 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 4996 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 4997 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4998 | retry: |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4999 | const FunctionType *FuncT; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5000 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5001 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 5002 | // have type pointer to function". |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5003 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5004 | if (FuncT == 0) |
| 5005 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5006 | << Fn->getType() << Fn->getSourceRange()); |
| 5007 | } else if (const BlockPointerType *BPT = |
| 5008 | Fn->getType()->getAs<BlockPointerType>()) { |
| 5009 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 5010 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5011 | // Handle calls to expressions of unknown-any type. |
| 5012 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 5013 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5014 | if (rewrite.isInvalid()) return ExprError(); |
| 5015 | Fn = rewrite.take(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 5016 | TheCall->setCallee(Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5017 | goto retry; |
| 5018 | } |
| 5019 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5020 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5021 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5022 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5023 | |
Peter Collingbourne | 4b66c47 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 5024 | if (getLangOptions().CUDA) { |
| 5025 | if (Config) { |
| 5026 | // CUDA: Kernel calls must be to global functions |
| 5027 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 5028 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 5029 | << FDecl->getName() << Fn->getSourceRange()); |
| 5030 | |
| 5031 | // CUDA: Kernel function must have 'void' return type |
| 5032 | if (!FuncT->getResultType()->isVoidType()) |
| 5033 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 5034 | << Fn->getType() << Fn->getSourceRange()); |
| 5035 | } |
| 5036 | } |
| 5037 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5038 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5039 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5040 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5041 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5042 | return ExprError(); |
| 5043 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5044 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5045 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5046 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5047 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5048 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5049 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5050 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5051 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5052 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5053 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5054 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5055 | if (FDecl) { |
| 5056 | // Check if we have too few/too many template arguments, based |
| 5057 | // on our knowledge of the function definition. |
| 5058 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 5059 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5060 | const FunctionProtoType *Proto |
| 5061 | = Def->getType()->getAs<FunctionProtoType>(); |
| 5062 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5063 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 5064 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5065 | } |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5066 | |
| 5067 | // If the function we're calling isn't a function prototype, but we have |
| 5068 | // a function prototype from a prior declaratiom, use that prototype. |
| 5069 | if (!FDecl->hasPrototype()) |
| 5070 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5073 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5074 | for (unsigned i = 0; i != NumArgs; i++) { |
| 5075 | Expr *Arg = Args[i]; |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5076 | |
| 5077 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5078 | InitializedEntity Entity |
| 5079 | = InitializedEntity::InitializeParameter(Context, |
| 5080 | Proto->getArgType(i)); |
| 5081 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 5082 | SourceLocation(), |
| 5083 | Owned(Arg)); |
| 5084 | if (ArgE.isInvalid()) |
| 5085 | return true; |
| 5086 | |
| 5087 | Arg = ArgE.takeAs<Expr>(); |
| 5088 | |
| 5089 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5090 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 5091 | |
| 5092 | if (ArgE.isInvalid()) |
| 5093 | return true; |
| 5094 | |
| 5095 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5096 | } |
| 5097 | |
Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 5098 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 5099 | Arg->getType(), |
| 5100 | PDiag(diag::err_call_incomplete_argument) |
| 5101 | << Arg->getSourceRange())) |
| 5102 | return ExprError(); |
| 5103 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5104 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5105 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5106 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5107 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5108 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 5109 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5110 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 5111 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5112 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 5113 | // Check for sentinels |
| 5114 | if (NDecl) |
| 5115 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5117 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5118 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5119 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5120 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5121 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5122 | if (BuiltinID) |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 5123 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5124 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5125 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5126 | return ExprError(); |
| 5127 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5128 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5129 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5132 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5133 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5134 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5135 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 5136 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5137 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5138 | |
| 5139 | TypeSourceInfo *TInfo; |
| 5140 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 5141 | if (!TInfo) |
| 5142 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 5143 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5144 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5147 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5148 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5149 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5150 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 5151 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5152 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 5153 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 5154 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 5155 | << SourceRange(LParenLoc, |
| 5156 | literalExpr->getSourceRange().getEnd()))) |
| 5157 | return ExprError(); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5158 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5159 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 5160 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 5161 | } else if (!literalType->isDependentType() && |
| 5162 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5163 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5165 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5166 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5167 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5168 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5169 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5170 | InitializationKind Kind |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5171 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5172 | /*IsCStyleCast=*/true); |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5173 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5174 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5175 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5176 | &literalType); |
| 5177 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5178 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5179 | literalExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5180 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5181 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5182 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5183 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5184 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5185 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5186 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5187 | // In C, compound literals are l-values for some reason. |
| 5188 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5189 | |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5190 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5191 | VK, literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5192 | } |
| 5193 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5194 | ExprResult |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5195 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5196 | SourceLocation RBraceLoc) { |
| 5197 | unsigned NumInit = initlist.size(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5198 | Expr **InitList = initlist.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5199 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5200 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5201 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5202 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5203 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5204 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5205 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5206 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5209 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5210 | /// except the final cast and returning the kind required. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5211 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5212 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5213 | // Also, callers should have filtered out the invalid cases with |
| 5214 | // pointers. Everything else should be possible. |
| 5215 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5216 | QualType SrcTy = Src.get()->getType(); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5217 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5218 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5219 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5220 | switch (SrcTy->getScalarTypeKind()) { |
| 5221 | case Type::STK_MemberPointer: |
| 5222 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5223 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5224 | case Type::STK_Pointer: |
| 5225 | switch (DestTy->getScalarTypeKind()) { |
| 5226 | case Type::STK_Pointer: |
| 5227 | return DestTy->isObjCObjectPointerType() ? |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5228 | CK_AnyPointerToObjCPointerCast : |
| 5229 | CK_BitCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5230 | case Type::STK_Bool: |
| 5231 | return CK_PointerToBoolean; |
| 5232 | case Type::STK_Integral: |
| 5233 | return CK_PointerToIntegral; |
| 5234 | case Type::STK_Floating: |
| 5235 | case Type::STK_FloatingComplex: |
| 5236 | case Type::STK_IntegralComplex: |
| 5237 | case Type::STK_MemberPointer: |
| 5238 | llvm_unreachable("illegal cast from pointer"); |
| 5239 | } |
| 5240 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5241 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5242 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5243 | case Type::STK_Integral: |
| 5244 | switch (DestTy->getScalarTypeKind()) { |
| 5245 | case Type::STK_Pointer: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5246 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5247 | return CK_NullToPointer; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5248 | return CK_IntegralToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5249 | case Type::STK_Bool: |
| 5250 | return CK_IntegralToBoolean; |
| 5251 | case Type::STK_Integral: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5252 | return CK_IntegralCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5253 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5254 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5255 | case Type::STK_IntegralComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5256 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5257 | CK_IntegralCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5258 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5259 | case Type::STK_FloatingComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5260 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5261 | CK_IntegralToFloating); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5262 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5263 | case Type::STK_MemberPointer: |
| 5264 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5265 | } |
| 5266 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5267 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5268 | case Type::STK_Floating: |
| 5269 | switch (DestTy->getScalarTypeKind()) { |
| 5270 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5271 | return CK_FloatingCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5272 | case Type::STK_Bool: |
| 5273 | return CK_FloatingToBoolean; |
| 5274 | case Type::STK_Integral: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5275 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5276 | case Type::STK_FloatingComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5277 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5278 | CK_FloatingCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5279 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5280 | case Type::STK_IntegralComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5281 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5282 | CK_FloatingToIntegral); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5283 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5284 | case Type::STK_Pointer: |
| 5285 | llvm_unreachable("valid float->pointer cast?"); |
| 5286 | case Type::STK_MemberPointer: |
| 5287 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5288 | } |
| 5289 | break; |
| 5290 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5291 | case Type::STK_FloatingComplex: |
| 5292 | switch (DestTy->getScalarTypeKind()) { |
| 5293 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5294 | return CK_FloatingComplexCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5295 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5296 | return CK_FloatingComplexToIntegralComplex; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5297 | case Type::STK_Floating: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5298 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5299 | if (S.Context.hasSameType(ET, DestTy)) |
| 5300 | return CK_FloatingComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5301 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5302 | return CK_FloatingCast; |
| 5303 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5304 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5305 | return CK_FloatingComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5306 | case Type::STK_Integral: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5307 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5308 | CK_FloatingComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5309 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5310 | case Type::STK_Pointer: |
| 5311 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5312 | case Type::STK_MemberPointer: |
| 5313 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5314 | } |
| 5315 | break; |
| 5316 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5317 | case Type::STK_IntegralComplex: |
| 5318 | switch (DestTy->getScalarTypeKind()) { |
| 5319 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5320 | return CK_IntegralComplexToFloatingComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5321 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5322 | return CK_IntegralComplexCast; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5323 | case Type::STK_Integral: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5324 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5325 | if (S.Context.hasSameType(ET, DestTy)) |
| 5326 | return CK_IntegralComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5327 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5328 | return CK_IntegralCast; |
| 5329 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5330 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5331 | return CK_IntegralComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5332 | case Type::STK_Floating: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5333 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5334 | CK_IntegralComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5335 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5336 | case Type::STK_Pointer: |
| 5337 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5338 | case Type::STK_MemberPointer: |
| 5339 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5340 | } |
| 5341 | break; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5342 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5343 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5344 | llvm_unreachable("Unhandled scalar cast"); |
| 5345 | return CK_BitCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5346 | } |
| 5347 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5348 | /// CheckCastTypes - Check type constraints for casting between types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5349 | ExprResult Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 5350 | Expr *castExpr, CastKind& Kind, ExprValueKind &VK, |
| 5351 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5352 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5353 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5354 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5355 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 15417cf | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5356 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 5357 | castExpr->getLocEnd()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5358 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5359 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5360 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5361 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5362 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5363 | // We only support r-value casts in C. |
| 5364 | VK = VK_RValue; |
| 5365 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5366 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5367 | // type needs to be scalar. |
| 5368 | if (castType->isVoidType()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5369 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5370 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5371 | if (castExprRes.isInvalid()) |
| 5372 | return ExprError(); |
| 5373 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5374 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5375 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5376 | Kind = CK_ToVoid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5377 | return Owned(castExpr); |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5378 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5379 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5380 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5381 | if (castExprRes.isInvalid()) |
| 5382 | return ExprError(); |
| 5383 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5384 | |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5385 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5386 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5387 | return ExprError(); |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5388 | |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5389 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5390 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5391 | (castType->isStructureType() || castType->isUnionType())) { |
| 5392 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5393 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5394 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5395 | << castType << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5396 | Kind = CK_NoOp; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5397 | return Owned(castExpr); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5398 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5399 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5400 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5401 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5402 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5403 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5404 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5405 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5406 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5407 | castExpr->getType()) && |
| 5408 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5409 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5410 | << castExpr->getSourceRange(); |
| 5411 | break; |
| 5412 | } |
| 5413 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5414 | if (Field == FieldEnd) { |
| 5415 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5416 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5417 | return ExprError(); |
| 5418 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5419 | Kind = CK_ToUnion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5420 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5421 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5422 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5423 | // Reject any other conversions to non-scalar types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5424 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5425 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5426 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5427 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5428 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5429 | // The type we're casting to is known to be a scalar or vector. |
| 5430 | |
| 5431 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5432 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5433 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5434 | Diag(castExpr->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5435 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5436 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5437 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5438 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5439 | |
| 5440 | if (castType->isExtVectorType()) |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5441 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5442 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5443 | if (castType->isVectorType()) { |
| 5444 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5445 | VectorType::AltiVecVector && |
| 5446 | (castExpr->getType()->isIntegerType() || |
| 5447 | castExpr->getType()->isFloatingType())) { |
| 5448 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5449 | return Owned(castExpr); |
| 5450 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5451 | return ExprError(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5452 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5453 | return Owned(castExpr); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5454 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5455 | if (castExpr->getType()->isVectorType()) { |
| 5456 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5457 | return ExprError(); |
| 5458 | else |
| 5459 | return Owned(castExpr); |
| 5460 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5461 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5462 | // The source and target types are both scalars, i.e. |
| 5463 | // - arithmetic types (fundamental, enum, and complex) |
| 5464 | // - all kinds of pointers |
| 5465 | // Note that member pointers were filtered out with C++, above. |
| 5466 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5467 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5468 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5469 | return ExprError(); |
| 5470 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5471 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5472 | // If either type is a pointer, the other type has to be either an |
| 5473 | // integer or a pointer. |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5474 | if (!castType->isArithmeticType()) { |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5475 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5476 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5477 | castExprType->isArithmeticType()) { |
| 5478 | Diag(castExpr->getLocStart(), |
| 5479 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5480 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5481 | return ExprError(); |
| 5482 | } |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5483 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5484 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5485 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5486 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5487 | return ExprError(); |
| 5488 | } |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5489 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5490 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5491 | castExprRes = Owned(castExpr); |
| 5492 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5493 | if (castExprRes.isInvalid()) |
| 5494 | return ExprError(); |
| 5495 | castExpr = castExprRes.take(); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5496 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5497 | if (Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5498 | CheckCastAlign(castExpr, castType, TyR); |
| 5499 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5500 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5501 | } |
| 5502 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5503 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5504 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5505 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5506 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5507 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5508 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5509 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5510 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5511 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5512 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5513 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5514 | } else |
| 5515 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5516 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5517 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5518 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5519 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5520 | return false; |
| 5521 | } |
| 5522 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5523 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5524 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5525 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5526 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5527 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5528 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5529 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5530 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5531 | if (SrcTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5532 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5533 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5534 | << DestTy << SrcTy << R; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5535 | return ExprError(); |
| 5536 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5537 | Kind = CK_BitCast; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5538 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5541 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5542 | // conversion will take place first from scalar to elt type, and then |
| 5543 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5544 | if (SrcTy->isPointerType()) |
| 5545 | return Diag(R.getBegin(), |
| 5546 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5547 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5548 | |
| 5549 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5550 | ExprResult CastExprRes = Owned(CastExpr); |
| 5551 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5552 | if (CastExprRes.isInvalid()) |
| 5553 | return ExprError(); |
| 5554 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5555 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5556 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5557 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5558 | } |
| 5559 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5560 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5561 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5562 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5563 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5564 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5565 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5566 | TypeSourceInfo *castTInfo; |
| 5567 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5568 | if (!castTInfo) |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5569 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5570 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5571 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5572 | if (isa<ParenListExpr>(castExpr)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5573 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5574 | castTInfo); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5575 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5576 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5579 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5580 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5581 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5582 | CastKind Kind = CK_Invalid; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5583 | ExprValueKind VK = VK_RValue; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5584 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5585 | ExprResult CastResult = |
| 5586 | CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
| 5587 | Kind, VK, BasePath); |
| 5588 | if (CastResult.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5589 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5590 | castExpr = CastResult.take(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5591 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5592 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5593 | Ty->getType().getNonLValueExprType(Context), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5594 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5595 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5596 | } |
| 5597 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5598 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5599 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5600 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5601 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5602 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5603 | if (!E) |
| 5604 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5605 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5606 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5607 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5608 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5609 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5610 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5611 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5612 | if (Result.isInvalid()) return ExprError(); |
| 5613 | |
| 5614 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5615 | } |
| 5616 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5617 | ExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5618 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5619 | SourceLocation RParenLoc, Expr *Op, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5620 | TypeSourceInfo *TInfo) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5621 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5622 | QualType Ty = TInfo->getType(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5623 | bool isVectorLiteral = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5624 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5625 | // Check for an altivec or OpenCL literal, |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5626 | // i.e. all the elements are integer constants. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5627 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5628 | if (PE->getNumExprs() == 0) { |
| 5629 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5630 | return ExprError(); |
| 5631 | } |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5632 | if (PE->getNumExprs() == 1) { |
| 5633 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5634 | isVectorLiteral = true; |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5635 | } |
| 5636 | else |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5637 | isVectorLiteral = true; |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5638 | } |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5639 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5640 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5641 | // then handle it as such. |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5642 | if (isVectorLiteral) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5643 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5644 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5645 | // initializers must be one or must match the size of the vector. |
| 5646 | // If a single value is specified in the initializer then it will be |
| 5647 | // replicated to all the components of the vector |
| 5648 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5649 | VectorType::AltiVecVector) { |
| 5650 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5651 | // The number of initializers must be one or must match the size of the |
| 5652 | // vector. If a single value is specified in the initializer then it will |
| 5653 | // be replicated to all the components of the vector |
| 5654 | if (PE->getNumExprs() == 1) { |
| 5655 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5656 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5657 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5658 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5659 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5660 | } |
| 5661 | else if (PE->getNumExprs() < numElems) { |
| 5662 | Diag(PE->getExprLoc(), |
| 5663 | diag::err_incorrect_number_of_vector_initializers); |
| 5664 | return ExprError(); |
| 5665 | } |
| 5666 | else |
| 5667 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5668 | initExprs.push_back(PE->getExpr(i)); |
| 5669 | } |
| 5670 | else |
| 5671 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5672 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5673 | |
| 5674 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5675 | // braces instead of the original commas. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5676 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5677 | &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5678 | initExprs.size(), RParenLoc); |
| 5679 | E->setType(Ty); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5680 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5681 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5682 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5683 | // sequence of BinOp comma operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5684 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5685 | if (Result.isInvalid()) return ExprError(); |
| 5686 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5687 | } |
| 5688 | } |
| 5689 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5690 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5691 | SourceLocation R, |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5692 | MultiExprArg Val, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5693 | ParsedType TypeOfCast) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5694 | unsigned nexprs = Val.size(); |
| 5695 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5696 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5697 | Expr *expr; |
| 5698 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5699 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5700 | else |
| 5701 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5702 | return Owned(expr); |
| 5703 | } |
| 5704 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5705 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5706 | /// constant and the other is not a pointer. |
| 5707 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5708 | SourceLocation QuestionLoc) { |
| 5709 | Expr *NullExpr = LHS; |
| 5710 | Expr *NonPointerExpr = RHS; |
| 5711 | Expr::NullPointerConstantKind NullKind = |
| 5712 | NullExpr->isNullPointerConstant(Context, |
| 5713 | Expr::NPC_ValueDependentIsNotNull); |
| 5714 | |
| 5715 | if (NullKind == Expr::NPCK_NotNull) { |
| 5716 | NullExpr = RHS; |
| 5717 | NonPointerExpr = LHS; |
| 5718 | NullKind = |
| 5719 | NullExpr->isNullPointerConstant(Context, |
| 5720 | Expr::NPC_ValueDependentIsNotNull); |
| 5721 | } |
| 5722 | |
| 5723 | if (NullKind == Expr::NPCK_NotNull) |
| 5724 | return false; |
| 5725 | |
| 5726 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5727 | // In this case, check to make sure that we got here from a "NULL" |
| 5728 | // string in the source code. |
| 5729 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5730 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5731 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5732 | return false; |
| 5733 | } |
| 5734 | |
| 5735 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5736 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5737 | << NonPointerExpr->getType() << DiagType |
| 5738 | << NonPointerExpr->getSourceRange(); |
| 5739 | return true; |
| 5740 | } |
| 5741 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5742 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5743 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5744 | /// C99 6.5.15 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5745 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5746 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5747 | SourceLocation QuestionLoc) { |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5748 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5749 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5750 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5751 | LHS = move(lhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5752 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5753 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5754 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5755 | RHS = move(rhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5756 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5757 | // C++ is sufficiently different to merit its own checker. |
| 5758 | if (getLangOptions().CPlusPlus) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5759 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5760 | |
| 5761 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5762 | OK = OK_Ordinary; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5763 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5764 | Cond = UsualUnaryConversions(Cond.take()); |
| 5765 | if (Cond.isInvalid()) |
| 5766 | return QualType(); |
| 5767 | LHS = UsualUnaryConversions(LHS.take()); |
| 5768 | if (LHS.isInvalid()) |
| 5769 | return QualType(); |
| 5770 | RHS = UsualUnaryConversions(RHS.take()); |
| 5771 | if (RHS.isInvalid()) |
| 5772 | return QualType(); |
| 5773 | |
| 5774 | QualType CondTy = Cond.get()->getType(); |
| 5775 | QualType LHSTy = LHS.get()->getType(); |
| 5776 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5777 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5778 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5779 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5780 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5781 | // Throw an error if its not either. |
| 5782 | if (getLangOptions().OpenCL) { |
| 5783 | if (!CondTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5784 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5785 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5786 | << CondTy; |
| 5787 | return QualType(); |
| 5788 | } |
| 5789 | } |
| 5790 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5791 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5792 | << CondTy; |
| 5793 | return QualType(); |
| 5794 | } |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5795 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5796 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5797 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5798 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5799 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5800 | |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5801 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5802 | // attempt to implicity convert them to the vector type to act like the |
| 5803 | // built in select. |
| 5804 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5805 | // Both operands should be of scalar type. |
| 5806 | if (!LHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5807 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5808 | << CondTy; |
| 5809 | return QualType(); |
| 5810 | } |
| 5811 | if (!RHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5812 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5813 | << CondTy; |
| 5814 | return QualType(); |
| 5815 | } |
| 5816 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5817 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5818 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5819 | } |
| 5820 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5821 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5822 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5823 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5824 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5825 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5826 | return QualType(); |
| 5827 | return LHS.get()->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5828 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5829 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5830 | // If both operands are the same structure or union type, the result is that |
| 5831 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5832 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5833 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5834 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5835 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5836 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5837 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5838 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5839 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5840 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5841 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5842 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5843 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5844 | if (!LHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5845 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5846 | << RHS.get()->getSourceRange(); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5847 | if (!RHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5848 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5849 | << LHS.get()->getSourceRange(); |
| 5850 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 5851 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5852 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5853 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5854 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5855 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5856 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5857 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5858 | // promote the null to a pointer. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5859 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5860 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5861 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5862 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5863 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 5864 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5865 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5866 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5867 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5868 | // All objective-c pointer type analysis is done here. |
| 5869 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5870 | QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5871 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5872 | return QualType(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5873 | if (!compositeType.isNull()) |
| 5874 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5875 | |
| 5876 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5877 | // Handle block pointer types. |
| 5878 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5879 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5880 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5881 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5882 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 5883 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5884 | return destType; |
| 5885 | } |
| 5886 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5887 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5888 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5889 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5890 | // We have 2 block pointer types. |
| 5891 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5892 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5893 | return LHSTy; |
| 5894 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5895 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5896 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5897 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5898 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5899 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5900 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5901 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5902 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5903 | // In this situation, we assume void* type. No especially good |
| 5904 | // reason, but this is what gcc does, and we do have to pick |
| 5905 | // to get a consistent AST. |
| 5906 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5907 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5908 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5909 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5910 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5911 | // The block pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5912 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 5913 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5914 | return LHSTy; |
| 5915 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5916 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5917 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5918 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5919 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5920 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5921 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5922 | |
| 5923 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5924 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5925 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5926 | QualType destPointee |
| 5927 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5928 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5929 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5930 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5931 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5932 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5933 | return destType; |
| 5934 | } |
| 5935 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5936 | QualType destPointee |
| 5937 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5938 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5939 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5940 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5941 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5942 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5943 | return destType; |
| 5944 | } |
| 5945 | |
| 5946 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5947 | // Two identical pointer types are always compatible. |
| 5948 | return LHSTy; |
| 5949 | } |
| 5950 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5951 | rhptee.getUnqualifiedType())) { |
| 5952 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5953 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5954 | // In this situation, we assume void* type. No especially good |
| 5955 | // reason, but this is what gcc does, and we do have to pick |
| 5956 | // to get a consistent AST. |
| 5957 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5958 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5959 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5960 | return incompatTy; |
| 5961 | } |
| 5962 | // The pointer types are compatible. |
| 5963 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 5964 | // differently qualified versions of compatible types, the result type is |
| 5965 | // a pointer to an appropriately qualified version of the *composite* |
| 5966 | // type. |
| 5967 | // FIXME: Need to calculate the composite type. |
| 5968 | // FIXME: Need to add qualifiers |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5969 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 5970 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5971 | return LHSTy; |
| 5972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5973 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5974 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5975 | // null pointers have been filtered out by this point. |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5976 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 5977 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5978 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5979 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5980 | return RHSTy; |
| 5981 | } |
| 5982 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 5983 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5984 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5985 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5986 | return LHSTy; |
| 5987 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5988 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5989 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 5990 | // constant and the other is not a pointer type. In this case, the user most |
| 5991 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5992 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5993 | return QualType(); |
| 5994 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5995 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5996 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5997 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5998 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 5999 | } |
| 6000 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6001 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 6002 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6003 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6004 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6005 | QualType LHSTy = LHS.get()->getType(); |
| 6006 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6007 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6008 | // Handle things like Class and struct objc_class*. Here we case the result |
| 6009 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 6010 | // redefinition type if an attempt is made to access its fields. |
| 6011 | if (LHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6012 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6013 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6014 | return LHSTy; |
| 6015 | } |
| 6016 | if (RHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6017 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6018 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6019 | return RHSTy; |
| 6020 | } |
| 6021 | // And the same for struct objc_object* / id |
| 6022 | if (LHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6023 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6024 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6025 | return LHSTy; |
| 6026 | } |
| 6027 | if (RHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6028 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6029 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6030 | return RHSTy; |
| 6031 | } |
| 6032 | // And the same for struct objc_selector* / SEL |
| 6033 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6034 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6035 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6036 | return LHSTy; |
| 6037 | } |
| 6038 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6039 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6040 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6041 | return RHSTy; |
| 6042 | } |
| 6043 | // Check constraints for Objective-C object pointers types. |
| 6044 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6045 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6046 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6047 | // Two identical object pointer types are always compatible. |
| 6048 | return LHSTy; |
| 6049 | } |
| 6050 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 6051 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 6052 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6053 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6054 | // If both operands are interfaces and either operand can be |
| 6055 | // assigned to the other, use that type as the composite |
| 6056 | // type. This allows |
| 6057 | // xxx ? (A*) a : (B*) b |
| 6058 | // where B is a subclass of A. |
| 6059 | // |
| 6060 | // Additionally, as for assignment, if either type is 'id' |
| 6061 | // allow silent coercion. Finally, if the types are |
| 6062 | // incompatible then make sure to use 'id' as the composite |
| 6063 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6064 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6065 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 6066 | // It could return the composite type. |
| 6067 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 6068 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 6069 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 6070 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 6071 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 6072 | RHSTy->isObjCQualifiedIdType()) && |
| 6073 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 6074 | // Need to handle "id<xx>" explicitly. |
| 6075 | // GCC allows qualified id and any Objective-C type to devolve to |
| 6076 | // id. Currently localizing to here until clear this should be |
| 6077 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 6078 | compositeType = Context.getObjCIdType(); |
| 6079 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 6080 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6081 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6082 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 6083 | ; |
| 6084 | else { |
| 6085 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 6086 | << LHSTy << RHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6087 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6088 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6089 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6090 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6091 | return incompatTy; |
| 6092 | } |
| 6093 | // The object pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6094 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 6095 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6096 | return compositeType; |
| 6097 | } |
| 6098 | // Check Objective-C object pointer types and 'void *' |
| 6099 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 6100 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6101 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6102 | QualType destPointee |
| 6103 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 6104 | QualType destType = Context.getPointerType(destPointee); |
| 6105 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6106 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6107 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6108 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6109 | return destType; |
| 6110 | } |
| 6111 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 6112 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6113 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 6114 | QualType destPointee |
| 6115 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 6116 | QualType destType = Context.getPointerType(destPointee); |
| 6117 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6118 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6119 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6120 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6121 | return destType; |
| 6122 | } |
| 6123 | return QualType(); |
| 6124 | } |
| 6125 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6126 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6127 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6128 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6129 | SourceLocation ColonLoc, |
| 6130 | Expr *CondExpr, Expr *LHSExpr, |
| 6131 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 6132 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 6133 | // was the condition. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6134 | OpaqueValueExpr *opaqueValue = 0; |
| 6135 | Expr *commonExpr = 0; |
| 6136 | if (LHSExpr == 0) { |
| 6137 | commonExpr = CondExpr; |
| 6138 | |
| 6139 | // We usually want to apply unary conversions *before* saving, except |
| 6140 | // in the special case of a C++ l-value conditional. |
| 6141 | if (!(getLangOptions().CPlusPlus |
| 6142 | && !commonExpr->isTypeDependent() |
| 6143 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 6144 | && commonExpr->isGLValue() |
| 6145 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 6146 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 6147 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6148 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 6149 | if (commonRes.isInvalid()) |
| 6150 | return ExprError(); |
| 6151 | commonExpr = commonRes.take(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 6155 | commonExpr->getType(), |
| 6156 | commonExpr->getValueKind(), |
| 6157 | commonExpr->getObjectKind()); |
| 6158 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 6159 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6160 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6161 | ExprValueKind VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6162 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6163 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 6164 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6165 | VK, OK, QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6166 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 6167 | RHS.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6168 | return ExprError(); |
| 6169 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6170 | if (!commonExpr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6171 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 6172 | LHS.take(), ColonLoc, |
| 6173 | RHS.take(), result, VK, OK)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6174 | |
| 6175 | return Owned(new (Context) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6176 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 6177 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6178 | } |
| 6179 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6180 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6181 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6182 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6183 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6184 | // FIXME: add a couple examples in this comment. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6185 | static Sema::AssignConvertType |
| 6186 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6187 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6188 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6189 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6190 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6191 | const Type *lhptee, *rhptee; |
| 6192 | Qualifiers lhq, rhq; |
| 6193 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6194 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6195 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6196 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6197 | |
| 6198 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6199 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6200 | // qualifiers of the type *pointed to* by the right; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6201 | Qualifiers lq; |
| 6202 | |
| 6203 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6204 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6205 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6206 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6207 | |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6208 | // It's okay to add or remove GC qualifiers when converting to |
| 6209 | // and from void*. |
| 6210 | else if (lhq.withoutObjCGCAttr().compatiblyIncludes(rhq.withoutObjCGCAttr()) |
| 6211 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6212 | ; // keep old |
| 6213 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6214 | // For GCC compatibility, other qualifier mismatches are treated |
| 6215 | // as still compatible in C. |
| 6216 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6217 | } |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6218 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6219 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6220 | // incomplete type and the other is a pointer to a qualified or unqualified |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6221 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6222 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6223 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6224 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6225 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6226 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6227 | assert(rhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6228 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6229 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6230 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6231 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6232 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6233 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6234 | |
| 6235 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6236 | assert(lhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6237 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6238 | } |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6239 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6240 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6241 | // unqualified versions of compatible types, ... |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6242 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6243 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6244 | // Check if the pointee types are compatible ignoring the sign. |
| 6245 | // We explicitly check for char so that we catch "char" vs |
| 6246 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6247 | if (lhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6248 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6249 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6250 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6251 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6252 | if (rhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6253 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6254 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6255 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6256 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6257 | if (ltrans == rtrans) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6258 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6259 | // takes priority over sign incompatibility because the sign |
| 6260 | // warning can be disabled. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6261 | if (ConvTy != Sema::Compatible) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6262 | return ConvTy; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6263 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6264 | return Sema::IncompatiblePointerSign; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6265 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6266 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6267 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6268 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6269 | // the eventual target type is the same and the pointers have the same |
| 6270 | // level of indirection, this must be the issue. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6271 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6272 | do { |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6273 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6274 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6275 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6276 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6277 | if (lhptee == rhptee) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6278 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6279 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6280 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6281 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6282 | return Sema::IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6283 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6284 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6285 | } |
| 6286 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6287 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6288 | /// block pointer types are compatible or whether a block and normal pointer |
| 6289 | /// are compatible. It is more restrict than comparing two function pointer |
| 6290 | // types. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6291 | static Sema::AssignConvertType |
| 6292 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6293 | QualType rhsType) { |
| 6294 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6295 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6296 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6297 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6298 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6299 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6300 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6301 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6302 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6303 | // In C++, the types have to match exactly. |
| 6304 | if (S.getLangOptions().CPlusPlus) |
| 6305 | return Sema::IncompatibleBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6306 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6307 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6308 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6309 | // For blocks we enforce that qualifiers are identical. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6310 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6311 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6312 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6313 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6314 | return Sema::IncompatibleBlockPointer; |
| 6315 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6316 | return ConvTy; |
| 6317 | } |
| 6318 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6319 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6320 | /// for assignment compatibility. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6321 | static Sema::AssignConvertType |
| 6322 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6323 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6324 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6325 | |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6326 | if (lhsType->isObjCBuiltinType()) { |
| 6327 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6328 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6329 | !rhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6330 | return Sema::IncompatiblePointer; |
| 6331 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6332 | } |
| 6333 | if (rhsType->isObjCBuiltinType()) { |
| 6334 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6335 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6336 | !lhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6337 | return Sema::IncompatiblePointer; |
| 6338 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6339 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6340 | QualType lhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6341 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6342 | QualType rhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6343 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6344 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6345 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6346 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6347 | |
| 6348 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6349 | return Sema::Compatible; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6350 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6351 | return Sema::IncompatibleObjCQualifiedId; |
| 6352 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6353 | } |
| 6354 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6355 | Sema::AssignConvertType |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6356 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6357 | QualType lhsType, QualType rhsType) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6358 | // Fake up an opaque expression. We don't actually care about what |
| 6359 | // cast operations are required, so if CheckAssignmentConstraints |
| 6360 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6361 | // usually happen on valid code. |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6362 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6363 | ExprResult rhsPtr = &rhs; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6364 | CastKind K = CK_Invalid; |
| 6365 | |
| 6366 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6367 | } |
| 6368 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6369 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6370 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6371 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6372 | /// |
| 6373 | /// int a, *pint; |
| 6374 | /// short *pshort; |
| 6375 | /// struct foo *pfoo; |
| 6376 | /// |
| 6377 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6378 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6379 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6380 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6381 | /// |
| 6382 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6383 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6384 | /// |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6385 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6386 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6387 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6388 | CastKind &Kind) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6389 | QualType rhsType = rhs.get()->getType(); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6390 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6391 | // Get canonical types. We're not formatting these types, just comparing |
| 6392 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6393 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6394 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6395 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6396 | // Common case: no conversion required. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6397 | if (lhsType == rhsType) { |
| 6398 | Kind = CK_NoOp; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6399 | return Compatible; |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6400 | } |
| 6401 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6402 | // If the left-hand side is a reference type, then we are in a |
| 6403 | // (rare!) case where we've allowed the use of references in C, |
| 6404 | // e.g., as a parameter type in a built-in function. In this case, |
| 6405 | // just make sure that the type referenced is compatible with the |
| 6406 | // right-hand side type. The caller is responsible for adjusting |
| 6407 | // lhsType so that the resulting expression does not have reference |
| 6408 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6409 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6410 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6411 | Kind = CK_LValueBitCast; |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6412 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6413 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6414 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6415 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6416 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6417 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6418 | // to the same ExtVector type. |
| 6419 | if (lhsType->isExtVectorType()) { |
| 6420 | if (rhsType->isExtVectorType()) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6421 | return Incompatible; |
| 6422 | if (rhsType->isArithmeticType()) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6423 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6424 | // element type. |
| 6425 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6426 | if (elType != rhsType) { |
| 6427 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6428 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6429 | } |
| 6430 | Kind = CK_VectorSplat; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6431 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6432 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6434 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6435 | // Conversions to or from vector type. |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6436 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6437 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6438 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6439 | // vector type and vice versa |
| 6440 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6441 | Kind = CK_BitCast; |
| 6442 | return Compatible; |
| 6443 | } |
| 6444 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6445 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6446 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6447 | // no bits are changed but the result type is different. |
| 6448 | if (getLangOptions().LaxVectorConversions && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6449 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6450 | Kind = CK_BitCast; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6451 | return IncompatibleVectors; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6452 | } |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6453 | } |
| 6454 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6455 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6456 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6457 | // Arithmetic conversions. |
Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6458 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6459 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6460 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6461 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6462 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6463 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6464 | // Conversions to normal pointers. |
| 6465 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6466 | // U* -> T* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6467 | if (isa<PointerType>(rhsType)) { |
| 6468 | Kind = CK_BitCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6469 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6470 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6471 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6472 | // int -> T* |
| 6473 | if (rhsType->isIntegerType()) { |
| 6474 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6475 | return IntToPointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6476 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6477 | |
| 6478 | // C pointers are not compatible with ObjC object pointers, |
| 6479 | // with two exceptions: |
| 6480 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6481 | // - conversions to void* |
| 6482 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6483 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6484 | return Compatible; |
| 6485 | } |
| 6486 | |
| 6487 | // - conversions from 'Class' to the redefinition type |
| 6488 | if (rhsType->isObjCClassType() && |
| 6489 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6490 | Kind = CK_BitCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6491 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6492 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6493 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6494 | Kind = CK_BitCast; |
| 6495 | return IncompatiblePointer; |
| 6496 | } |
| 6497 | |
| 6498 | // U^ -> void* |
| 6499 | if (rhsType->getAs<BlockPointerType>()) { |
| 6500 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6501 | Kind = CK_BitCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6502 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6503 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6504 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6505 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6506 | return Incompatible; |
| 6507 | } |
| 6508 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6509 | // Conversions to block pointers. |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6510 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6511 | // U^ -> T^ |
| 6512 | if (rhsType->isBlockPointerType()) { |
| 6513 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6514 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | // int or null -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6518 | if (rhsType->isIntegerType()) { |
| 6519 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6520 | return IntToBlockPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6521 | } |
| 6522 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6523 | // id -> T^ |
| 6524 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6525 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6526 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6527 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6528 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6529 | // void* -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6530 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6531 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6532 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6533 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6534 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6535 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6536 | return Incompatible; |
| 6537 | } |
| 6538 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6539 | // Conversions to Objective-C pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6540 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6541 | // A* -> B* |
| 6542 | if (rhsType->isObjCObjectPointerType()) { |
| 6543 | Kind = CK_BitCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6544 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6545 | } |
| 6546 | |
| 6547 | // int or null -> A* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6548 | if (rhsType->isIntegerType()) { |
| 6549 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6550 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6553 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6554 | // with two exceptions: |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6555 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6556 | // - conversions from 'void*' |
| 6557 | if (rhsType->isVoidPointerType()) { |
| 6558 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6559 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | // - conversions to 'Class' from its redefinition type |
| 6563 | if (lhsType->isObjCClassType() && |
| 6564 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6565 | Kind = CK_BitCast; |
| 6566 | return Compatible; |
| 6567 | } |
| 6568 | |
| 6569 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6570 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6571 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6572 | |
| 6573 | // T^ -> A* |
| 6574 | if (rhsType->isBlockPointerType()) { |
| 6575 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6576 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6579 | return Incompatible; |
| 6580 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6581 | |
| 6582 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6583 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6584 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6585 | if (lhsType == Context.BoolTy) { |
| 6586 | Kind = CK_PointerToBoolean; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6587 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6588 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6589 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6590 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6591 | if (lhsType->isIntegerType()) { |
| 6592 | Kind = CK_PointerToIntegral; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6593 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6594 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6595 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6596 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6597 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6598 | |
| 6599 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6600 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6601 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6602 | if (lhsType == Context.BoolTy) { |
| 6603 | Kind = CK_PointerToBoolean; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6604 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6605 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6606 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6607 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6608 | if (lhsType->isIntegerType()) { |
| 6609 | Kind = CK_PointerToIntegral; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6610 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6611 | } |
| 6612 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6613 | return Incompatible; |
| 6614 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6615 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6616 | // struct A -> struct B |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6617 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6618 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6619 | Kind = CK_NoOp; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6620 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6621 | } |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 6622 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6623 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6624 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 6625 | } |
| 6626 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6627 | /// \brief Constructs a transparent union from an expression that is |
| 6628 | /// used to initialize the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6629 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6630 | QualType UnionType, FieldDecl *Field) { |
| 6631 | // Build an initializer list that designates the appropriate member |
| 6632 | // of the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6633 | Expr *E = EResult.take(); |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6634 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6635 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6636 | SourceLocation()); |
| 6637 | Initializer->setType(UnionType); |
| 6638 | Initializer->setInitializedFieldInUnion(Field); |
| 6639 | |
| 6640 | // Build a compound literal constructing a value of the transparent |
| 6641 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6642 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6643 | EResult = S.Owned( |
| 6644 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6645 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
| 6648 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6649 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6650 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6651 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6652 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6653 | // transparent_union GCC extension. |
| 6654 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6655 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6656 | return Incompatible; |
| 6657 | |
| 6658 | // The field to initialize within the transparent union. |
| 6659 | RecordDecl *UD = UT->getDecl(); |
| 6660 | FieldDecl *InitField = 0; |
| 6661 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6662 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6663 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6664 | it != itend; ++it) { |
| 6665 | if (it->getType()->isPointerType()) { |
| 6666 | // If the transparent union contains a pointer type, we allow: |
| 6667 | // 1) void pointer |
| 6668 | // 2) null pointer constant |
| 6669 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6670 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6671 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6672 | InitField = *it; |
| 6673 | break; |
| 6674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6675 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6676 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6677 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6678 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6679 | InitField = *it; |
| 6680 | break; |
| 6681 | } |
| 6682 | } |
| 6683 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6684 | CastKind Kind = CK_Invalid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6685 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6686 | == Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6687 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6688 | InitField = *it; |
| 6689 | break; |
| 6690 | } |
| 6691 | } |
| 6692 | |
| 6693 | if (!InitField) |
| 6694 | return Incompatible; |
| 6695 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6696 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6697 | return Compatible; |
| 6698 | } |
| 6699 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6700 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6701 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6702 | if (getLangOptions().CPlusPlus) { |
| 6703 | if (!lhsType->isRecordType()) { |
| 6704 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6705 | // expression is implicitly converted (C++ 4) to the |
| 6706 | // cv-unqualified type of the left operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6707 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6708 | lhsType.getUnqualifiedType(), |
| 6709 | AA_Assigning); |
| 6710 | if (Res.isInvalid()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6711 | return Incompatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6712 | rExpr = move(Res); |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6713 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6717 | // structures. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6718 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6719 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6720 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6721 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6722 | if ((lhsType->isPointerType() || |
| 6723 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6724 | lhsType->isBlockPointerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6725 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6726 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6727 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6728 | return Compatible; |
| 6729 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6730 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6731 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6732 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6733 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6734 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6735 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6736 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6737 | if (!lhsType->isReferenceType()) { |
| 6738 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 6739 | if (rExpr.isInvalid()) |
| 6740 | return Incompatible; |
| 6741 | } |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6742 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6743 | CastKind Kind = CK_Invalid; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6744 | Sema::AssignConvertType result = |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6745 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6746 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6747 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6748 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6749 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6750 | // so that we can use references in built-in functions even in C. |
| 6751 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6752 | // does not have reference type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6753 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 6754 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6755 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6758 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6759 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6760 | << lex.get()->getType() << rex.get()->getType() |
| 6761 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6762 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6765 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6766 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6767 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6768 | QualType lhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6769 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6770 | QualType rhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6771 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6772 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6773 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 6774 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 6775 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6776 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6777 | // Handle the case of a vector & extvector type of the same size and element |
| 6778 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6779 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6780 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6781 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6782 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6783 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6784 | if (lhsType->isExtVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6785 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6786 | return lhsType; |
| 6787 | } |
| 6788 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6789 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 6790 | return rhsType; |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6791 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 6792 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6793 | // vectors, the total size only needs to be the same. This is a |
| 6794 | // bitcast; no bits are changed but the result type is different. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6795 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6796 | return lhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6797 | } |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 6798 | } |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 6799 | } |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6800 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6801 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6802 | // Handle the case of equivalent AltiVec and GCC vector types |
| 6803 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 6804 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6805 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6806 | return rhsType; |
| 6807 | } |
| 6808 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6809 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6810 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6811 | bool swapped = false; |
| 6812 | if (rhsType->isExtVectorType()) { |
| 6813 | swapped = true; |
| 6814 | std::swap(rex, lex); |
| 6815 | std::swap(rhsType, lhsType); |
| 6816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6817 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6818 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6819 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6820 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 6821 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6822 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 6823 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6824 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6825 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6826 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6827 | if (swapped) std::swap(rex, lex); |
| 6828 | return lhsType; |
| 6829 | } |
| 6830 | } |
| 6831 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 6832 | rhsType->isRealFloatingType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6833 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 6834 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6835 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6836 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6837 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6838 | if (swapped) std::swap(rex, lex); |
| 6839 | return lhsType; |
| 6840 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6841 | } |
| 6842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6843 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6844 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6845 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6846 | << lex.get()->getType() << rex.get()->getType() |
| 6847 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 6848 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6851 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6852 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 6853 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6854 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6855 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6856 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6857 | if (lex.isInvalid() || rex.isInvalid()) |
| 6858 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6859 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6860 | if (!lex.get()->getType()->isArithmeticType() || |
| 6861 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6862 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6863 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6864 | // Check for division by zero. |
| 6865 | if (isDiv && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6866 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 6867 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 6868 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6869 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6870 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6871 | } |
| 6872 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6873 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6874 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 6875 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 6876 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6877 | rex.get()->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6878 | return CheckVectorOperands(Loc, lex, rex); |
| 6879 | return InvalidOperands(Loc, lex, rex); |
| 6880 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6881 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6882 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6883 | if (lex.isInvalid() || rex.isInvalid()) |
| 6884 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6885 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6886 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6887 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6888 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6889 | // Check for remainder by zero. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6890 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 6891 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 6892 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6893 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6894 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6895 | } |
| 6896 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6897 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6898 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 6899 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6900 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6901 | if (CompLHSTy) *CompLHSTy = compType; |
| 6902 | return compType; |
| 6903 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6904 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6905 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6906 | if (lex.isInvalid() || rex.isInvalid()) |
| 6907 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6908 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 6909 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6910 | if (lex.get()->getType()->isArithmeticType() && |
| 6911 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6912 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6913 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6914 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6915 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6916 | // Put any potential pointer into PExp |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6917 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6918 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6919 | std::swap(PExp, IExp); |
| 6920 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6921 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6922 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6923 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6924 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6925 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6926 | // Check for arithmetic on pointers to incomplete types. |
| 6927 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6928 | if (getLangOptions().CPlusPlus) { |
| 6929 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6930 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6931 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6932 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6933 | |
| 6934 | // GNU extension: arithmetic on pointer to void |
| 6935 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6936 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6937 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6938 | if (getLangOptions().CPlusPlus) { |
| 6939 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6940 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6941 | return QualType(); |
| 6942 | } |
| 6943 | |
| 6944 | // GNU extension: arithmetic on pointer to function |
| 6945 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6946 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6947 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6948 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6949 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6950 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6951 | PExp->getType()->isObjCObjectPointerType()) && |
| 6952 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6953 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 6954 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6955 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6956 | return QualType(); |
| 6957 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6958 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6959 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6960 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6961 | << PointeeTy << PExp->getSourceRange(); |
| 6962 | return QualType(); |
| 6963 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6964 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6965 | if (CompLHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6966 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6967 | if (LHSTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6968 | LHSTy = lex.get()->getType(); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6969 | if (LHSTy->isPromotableIntegerType()) |
| 6970 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6971 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6972 | *CompLHSTy = LHSTy; |
| 6973 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6974 | return PExp->getType(); |
| 6975 | } |
| 6976 | } |
| 6977 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6978 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6979 | } |
| 6980 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6981 | // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6982 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6983 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6984 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6985 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6986 | if (CompLHSTy) *CompLHSTy = compType; |
| 6987 | return compType; |
| 6988 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6989 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6990 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6991 | if (lex.isInvalid() || rex.isInvalid()) |
| 6992 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6993 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6994 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6995 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6996 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6997 | if (lex.get()->getType()->isArithmeticType() && |
| 6998 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6999 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7000 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7001 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7002 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7003 | // Either ptr - int or ptr - ptr. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7004 | if (lex.get()->getType()->isAnyPointerType()) { |
| 7005 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7006 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7007 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7008 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7009 | bool ComplainAboutVoid = false; |
| 7010 | Expr *ComplainAboutFunc = 0; |
| 7011 | if (lpointee->isVoidType()) { |
| 7012 | if (getLangOptions().CPlusPlus) { |
| 7013 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7014 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7015 | return QualType(); |
| 7016 | } |
| 7017 | |
| 7018 | // GNU C extension: arithmetic on pointer to void |
| 7019 | ComplainAboutVoid = true; |
| 7020 | } else if (lpointee->isFunctionType()) { |
| 7021 | if (getLangOptions().CPlusPlus) { |
| 7022 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7023 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7024 | return QualType(); |
| 7025 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7026 | |
| 7027 | // GNU C extension: arithmetic on pointer to function |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7028 | ComplainAboutFunc = lex.get(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7029 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7030 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7031 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7032 | << lex.get()->getSourceRange() |
| 7033 | << lex.get()->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7034 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7035 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7036 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7037 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7038 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7039 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7040 | return QualType(); |
| 7041 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7042 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7043 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7044 | if (rex.get()->getType()->isIntegerType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7045 | if (ComplainAboutVoid) |
| 7046 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7047 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7048 | if (ComplainAboutFunc) |
| 7049 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7050 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7051 | << ComplainAboutFunc->getSourceRange(); |
| 7052 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7053 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 7054 | return lex.get()->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7055 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7056 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7057 | // Handle pointer-pointer subtractions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7058 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 7059 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7060 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7061 | // RHS must be a completely-type object type. |
| 7062 | // Handle the GNU void* extension. |
| 7063 | if (rpointee->isVoidType()) { |
| 7064 | if (getLangOptions().CPlusPlus) { |
| 7065 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7066 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7067 | return QualType(); |
| 7068 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7069 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7070 | ComplainAboutVoid = true; |
| 7071 | } else if (rpointee->isFunctionType()) { |
| 7072 | if (getLangOptions().CPlusPlus) { |
| 7073 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7074 | << rex.get()->getType() << rex.get()->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7075 | return QualType(); |
| 7076 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7077 | |
| 7078 | // GNU extension: arithmetic on pointer to function |
| 7079 | if (!ComplainAboutFunc) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7080 | ComplainAboutFunc = rex.get(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7081 | } else if (!rpointee->isDependentType() && |
| 7082 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7083 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7084 | << rex.get()->getSourceRange() |
| 7085 | << rex.get()->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7086 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7087 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7088 | if (getLangOptions().CPlusPlus) { |
| 7089 | // Pointee types must be the same: C++ [expr.add] |
| 7090 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 7091 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7092 | << lex.get()->getType() << rex.get()->getType() |
| 7093 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7094 | return QualType(); |
| 7095 | } |
| 7096 | } else { |
| 7097 | // Pointee types must be compatible C99 6.5.6p3 |
| 7098 | if (!Context.typesAreCompatible( |
| 7099 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 7100 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 7101 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7102 | << lex.get()->getType() << rex.get()->getType() |
| 7103 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7104 | return QualType(); |
| 7105 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7106 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7107 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7108 | if (ComplainAboutVoid) |
| 7109 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7110 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7111 | if (ComplainAboutFunc) |
| 7112 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7113 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7114 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7115 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7116 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7117 | return Context.getPointerDiffType(); |
| 7118 | } |
| 7119 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7120 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7121 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7122 | } |
| 7123 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7124 | static bool isScopedEnumerationType(QualType T) { |
| 7125 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 7126 | return ET->getDecl()->isScoped(); |
| 7127 | return false; |
| 7128 | } |
| 7129 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7130 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7131 | SourceLocation Loc, unsigned Opc, |
| 7132 | QualType LHSTy) { |
| 7133 | llvm::APSInt Right; |
| 7134 | // Check right/shifter operand |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7135 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7136 | return; |
| 7137 | |
| 7138 | if (Right.isNegative()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7139 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 63657fe | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 7140 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7141 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7142 | return; |
| 7143 | } |
| 7144 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7145 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7146 | if (Right.uge(LeftBits)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7147 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 26bbc3d | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 7148 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7149 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7150 | return; |
| 7151 | } |
| 7152 | if (Opc != BO_Shl) |
| 7153 | return; |
| 7154 | |
| 7155 | // When left shifting an ICE which is signed, we can check for overflow which |
| 7156 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 7157 | // integers have defined behavior modulo one more than the maximum value |
| 7158 | // representable in the result type, so never warn for those. |
| 7159 | llvm::APSInt Left; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7160 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7161 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 7162 | return; |
| 7163 | llvm::APInt ResultBits = |
| 7164 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 7165 | if (LeftBits.uge(ResultBits)) |
| 7166 | return; |
| 7167 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 7168 | Result = Result.shl(Right); |
| 7169 | |
| 7170 | // If we are only missing a sign bit, this is less likely to result in actual |
| 7171 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 7172 | // expected value. Thus we place this behind a different warning that can be |
| 7173 | // turned off separately if needed. |
| 7174 | if (LeftBits == ResultBits - 1) { |
| 7175 | S.Diag(Loc, diag::warn_shift_result_overrides_sign_bit) |
| 7176 | << Result.toString(10) << LHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7177 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7178 | return; |
| 7179 | } |
| 7180 | |
| 7181 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
| 7182 | << Result.toString(10) << Result.getMinSignedBits() << LHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7183 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7184 | } |
| 7185 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7186 | // C99 6.5.7 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7187 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7188 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7189 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7190 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7191 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7192 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7193 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7194 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7195 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7196 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7197 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7198 | return InvalidOperands(Loc, lex, rex); |
| 7199 | } |
| 7200 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7201 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7202 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7203 | return CheckVectorOperands(Loc, lex, rex); |
| 7204 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7205 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7206 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7207 | |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7208 | // For the LHS, do usual unary conversions, but then reset them away |
| 7209 | // if this is a compound assignment. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7210 | ExprResult old_lex = lex; |
| 7211 | lex = UsualUnaryConversions(lex.take()); |
| 7212 | if (lex.isInvalid()) |
| 7213 | return QualType(); |
| 7214 | QualType LHSTy = lex.get()->getType(); |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7215 | if (isCompAssign) lex = old_lex; |
| 7216 | |
| 7217 | // The RHS is simpler. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7218 | rex = UsualUnaryConversions(rex.take()); |
| 7219 | if (rex.isInvalid()) |
| 7220 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7221 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7222 | // Sanity-check shift operands |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7223 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7224 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7225 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7226 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7227 | } |
| 7228 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7229 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7230 | if (DeclContext *DC = D->getDeclContext()) { |
| 7231 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7232 | return true; |
| 7233 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7234 | return FD->isFunctionTemplateSpecialization(); |
| 7235 | } |
| 7236 | return false; |
| 7237 | } |
| 7238 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7239 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7240 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7241 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7242 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7243 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7244 | // Handle vector comparisons separately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7245 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7246 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7247 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7248 | QualType lType = lex.get()->getType(); |
| 7249 | QualType rType = rex.get()->getType(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7250 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7251 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7252 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7253 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7254 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7255 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7256 | |
| 7257 | |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7258 | // Two different enums will raise a warning when compared. |
| 7259 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7260 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7261 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7262 | RHSEnumType->getDecl()->getIdentifier() && |
| 7263 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7264 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7265 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7266 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7267 | } |
| 7268 | } |
| 7269 | } |
| 7270 | |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7271 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7272 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7273 | !lex.get()->getLocStart().isMacroID() && |
| 7274 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7275 | // For non-floating point types, check for self-comparisons of the form |
| 7276 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7277 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7278 | // |
| 7279 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7280 | // expansion. Also don't warn about comparisons which are only self |
| 7281 | // comparisons within a template specialization. The warnings should catch |
| 7282 | // obvious cases in the definition of the template anyways. The idea is to |
| 7283 | // warn when the typed comparison operator will always evaluate to the same |
| 7284 | // result. |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7285 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7286 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7287 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7288 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7289 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7290 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7291 | << (Opc == BO_EQ |
| 7292 | || Opc == BO_LE |
| 7293 | || Opc == BO_GE)); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7294 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7295 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7296 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7297 | // what is it always going to eval to? |
| 7298 | char always_evals_to; |
| 7299 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7300 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7301 | always_evals_to = 0; // false |
| 7302 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7303 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7304 | always_evals_to = 1; // true |
| 7305 | break; |
| 7306 | default: |
| 7307 | // best we can say is 'a constant' |
| 7308 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7309 | break; |
| 7310 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7311 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7312 | << 1 // array |
| 7313 | << always_evals_to); |
| 7314 | } |
| 7315 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7317 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7318 | if (isa<CastExpr>(LHSStripped)) |
| 7319 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7320 | if (isa<CastExpr>(RHSStripped)) |
| 7321 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7322 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7323 | // Warn about comparisons against a string constant (unless the other |
| 7324 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7325 | Expr *literalString = 0; |
| 7326 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7327 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7328 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7329 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7330 | literalString = lex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7331 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7332 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7333 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7334 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7335 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7336 | literalString = rex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7337 | literalStringStripped = RHSStripped; |
| 7338 | } |
| 7339 | |
| 7340 | if (literalString) { |
| 7341 | std::string resultComparison; |
| 7342 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7343 | case BO_LT: resultComparison = ") < 0"; break; |
| 7344 | case BO_GT: resultComparison = ") > 0"; break; |
| 7345 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7346 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7347 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7348 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7349 | default: assert(false && "Invalid comparison operator"); |
| 7350 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7351 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7352 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7353 | PDiag(diag::warn_stringcompare) |
| 7354 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7355 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7356 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7357 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7358 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7359 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7360 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7361 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7362 | if (lex.isInvalid() || rex.isInvalid()) |
| 7363 | return QualType(); |
| 7364 | } |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7365 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7366 | lex = UsualUnaryConversions(lex.take()); |
| 7367 | if (lex.isInvalid()) |
| 7368 | return QualType(); |
| 7369 | |
| 7370 | rex = UsualUnaryConversions(rex.take()); |
| 7371 | if (rex.isInvalid()) |
| 7372 | return QualType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7373 | } |
| 7374 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7375 | lType = lex.get()->getType(); |
| 7376 | rType = rex.get()->getType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7377 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7378 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7379 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7380 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7381 | if (isRelational) { |
| 7382 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7383 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7384 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7385 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7386 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7387 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7388 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7389 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7390 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7391 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7392 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7393 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7394 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7395 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7396 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7397 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7398 | // All of the following pointer-related warnings are GCC extensions, except |
| 7399 | // when handling null pointer constants. |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7400 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7401 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7402 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7403 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7404 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7405 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7406 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7407 | if (LCanPointeeTy == RCanPointeeTy) |
| 7408 | return ResultTy; |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7409 | if (!isRelational && |
| 7410 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7411 | // Valid unless comparison between non-null pointer and function pointer |
| 7412 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7413 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7414 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7415 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7416 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7417 | Diag(Loc, |
| 7418 | isSFINAEContext()? |
| 7419 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7420 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7421 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7422 | |
| 7423 | if (isSFINAEContext()) |
| 7424 | return QualType(); |
| 7425 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7426 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7427 | return ResultTy; |
| 7428 | } |
| 7429 | } |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7430 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7431 | // C++ [expr.rel]p2: |
| 7432 | // [...] Pointer conversions (4.10) and qualification |
| 7433 | // conversions (4.4) are performed on pointer operands (or on |
| 7434 | // a pointer operand and a null pointer constant) to bring |
| 7435 | // them to their composite pointer type. [...] |
| 7436 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7437 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7438 | // comparisons of pointers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7439 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7440 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7441 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7442 | if (T.isNull()) { |
| 7443 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7444 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7445 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7446 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7447 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7448 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7449 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7450 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7451 | } |
| 7452 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7453 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7454 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7455 | return ResultTy; |
| 7456 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7457 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7458 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7459 | RCanPointeeTy.getUnqualifiedType())) { |
| 7460 | // Valid unless a relational comparison of function pointers |
| 7461 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7462 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7463 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7464 | } |
| 7465 | } else if (!isRelational && |
| 7466 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7467 | // Valid unless comparison between non-null pointer and function pointer |
| 7468 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7469 | && !LHSIsNull && !RHSIsNull) { |
| 7470 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7471 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7472 | } |
| 7473 | } else { |
| 7474 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7475 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7476 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 7477 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7478 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7479 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7480 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7481 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7482 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7483 | } |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7484 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7485 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7486 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7487 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7488 | // Comparison of nullptr_t with itself. |
| 7489 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7490 | return ResultTy; |
| 7491 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7492 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7493 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7494 | if (RHSIsNull && |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7495 | ((lType->isPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7496 | (!isRelational && lType->isMemberPointerType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7497 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7498 | lType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7499 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7500 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7501 | return ResultTy; |
| 7502 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7503 | if (LHSIsNull && |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7504 | ((rType->isPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7505 | (!isRelational && rType->isMemberPointerType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7506 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7507 | rType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7508 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7509 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7510 | return ResultTy; |
| 7511 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7512 | |
| 7513 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7514 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7515 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7516 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7517 | // In addition, pointers to members can be compared, or a pointer to |
| 7518 | // member and a null pointer constant. Pointer to member conversions |
| 7519 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7520 | // them to a common type. If one operand is a null pointer constant, |
| 7521 | // the common type is the type of the other operand. Otherwise, the |
| 7522 | // common type is a pointer to member type similar (4.4) to the type |
| 7523 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7524 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7525 | // types. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7526 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7527 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7528 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7529 | if (T.isNull()) { |
| 7530 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7531 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7532 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7533 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7534 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7535 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7536 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7537 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7539 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7540 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7541 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7542 | return ResultTy; |
| 7543 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7544 | |
| 7545 | // Handle scoped enumeration types specifically, since they don't promote |
| 7546 | // to integers. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7547 | if (lex.get()->getType()->isEnumeralType() && |
| 7548 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7549 | return ResultTy; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7551 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7552 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7553 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7554 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7555 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7556 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7557 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7558 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7559 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7560 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7561 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7562 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7563 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7564 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7565 | |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7566 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7567 | if (!isRelational |
| 7568 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7569 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7570 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7571 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7572 | ->getPointeeType()->isVoidType()) |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7573 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7574 | ->getPointeeType()->isVoidType()))) |
| 7575 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7576 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7577 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7578 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7579 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7580 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7581 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7582 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7583 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7584 | |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7585 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7586 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7587 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7588 | if (LPT || RPT) { |
| 7589 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7590 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7591 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7592 | if (!LPtrToVoid && !RPtrToVoid && |
| 7593 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7594 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7595 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7596 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7597 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7598 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7599 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7600 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7601 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7602 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7603 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7604 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7605 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7606 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7607 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7608 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7609 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7610 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7611 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7612 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7613 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7614 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7615 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7616 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7617 | bool isError = false; |
| 7618 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7619 | (RHSIsNull && rType->isIntegerType())) { |
| 7620 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7621 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7622 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7623 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7624 | else if (getLangOptions().CPlusPlus) { |
| 7625 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7626 | isError = true; |
| 7627 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7628 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7629 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7630 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7631 | Diag(Loc, DiagID) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7632 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7633 | if (isError) |
| 7634 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7635 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7636 | |
| 7637 | if (lType->isIntegerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7638 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7639 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7640 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7641 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7642 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7643 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 7644 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7645 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7646 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7647 | if (!isRelational && RHSIsNull |
| 7648 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7649 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7650 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7651 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7652 | if (!isRelational && LHSIsNull |
| 7653 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7654 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7655 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7656 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7657 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7658 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7659 | } |
| 7660 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7661 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7662 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7663 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7664 | /// types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7665 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7666 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7667 | bool isRelational) { |
| 7668 | // Check to make sure we're operating on vectors of the same type and width, |
| 7669 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7670 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7671 | if (vType.isNull()) |
| 7672 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7673 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7674 | QualType lType = lex.get()->getType(); |
| 7675 | QualType rType = rex.get()->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7676 | |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7677 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7678 | // bool for C++, int for C |
Anton Yartsev | 93900c7 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7679 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7680 | return Context.getLogicalOperationType(); |
| 7681 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7682 | // For non-floating point types, check for self-comparisons of the form |
| 7683 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7684 | // often indicate logic errors in the program. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7685 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7686 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7687 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7688 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7689 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7690 | PDiag(diag::warn_comparison_always) |
| 7691 | << 0 // self- |
| 7692 | << 2 // "a constant" |
| 7693 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7694 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7695 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7696 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7697 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7698 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7699 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7700 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7701 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7702 | // Return the type for the comparison, which is the same as vector type for |
| 7703 | // integer vectors, or an integer type of identical size and number of |
| 7704 | // elements for floating point vectors. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7705 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7706 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7707 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7708 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7709 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7710 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7711 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7712 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7713 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7714 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7715 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7716 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7717 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7718 | } |
| 7719 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7720 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7721 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7722 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7723 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7724 | rex.get()->getType()->hasIntegerRepresentation()) |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7725 | return CheckVectorOperands(Loc, lex, rex); |
| 7726 | |
| 7727 | return InvalidOperands(Loc, lex, rex); |
| 7728 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7729 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7730 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 7731 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 7732 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 7733 | return QualType(); |
| 7734 | lex = lexResult.take(); |
| 7735 | rex = rexResult.take(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7736 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7737 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7738 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7739 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7740 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7741 | } |
| 7742 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7743 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7744 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7745 | |
| 7746 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7747 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7748 | // is a constant. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7749 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 7750 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | deee7a3 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7751 | // Don't warn in macros. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7752 | !Loc.isMacroID()) { |
| 7753 | // If the RHS can be constant folded, and if it constant folds to something |
| 7754 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7755 | // happened to fold to true/false) then warn. |
| 7756 | Expr::EvalResult Result; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7757 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects && |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7758 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 7759 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7760 | << rex.get()->getSourceRange() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7761 | << (Opc == BO_LAnd ? "&&" : "||") |
| 7762 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7763 | } |
| 7764 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7765 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7766 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7767 | lex = UsualUnaryConversions(lex.take()); |
| 7768 | if (lex.isInvalid()) |
| 7769 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7770 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7771 | rex = UsualUnaryConversions(rex.take()); |
| 7772 | if (rex.isInvalid()) |
| 7773 | return QualType(); |
| 7774 | |
| 7775 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7776 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7777 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7778 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7779 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7780 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7781 | // The following is safe because we only use this method for |
| 7782 | // non-overloadable operands. |
| 7783 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7784 | // C++ [expr.log.and]p1 |
| 7785 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7786 | // The operands are both contextually converted to type bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7787 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 7788 | if (lexRes.isInvalid()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7789 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7790 | lex = move(lexRes); |
| 7791 | |
| 7792 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 7793 | if (rexRes.isInvalid()) |
| 7794 | return InvalidOperands(Loc, lex, rex); |
| 7795 | rex = move(rexRes); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7796 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7797 | // C++ [expr.log.and]p2 |
| 7798 | // C++ [expr.log.or]p2 |
| 7799 | // The result is a bool. |
| 7800 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 7801 | } |
| 7802 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7803 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7804 | /// is a read-only property; return true if so. A readonly property expression |
| 7805 | /// depends on various declarations and thus must be treated specially. |
| 7806 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7807 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7808 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7809 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7810 | if (PropExpr->isImplicitProperty()) return false; |
| 7811 | |
| 7812 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7813 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 7814 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7815 | PropExpr->getBase()->getType(); |
| 7816 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7817 | if (const ObjCObjectPointerType *OPT = |
| 7818 | BaseType->getAsObjCInterfacePointerType()) |
| 7819 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7820 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7821 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7822 | } |
| 7823 | return false; |
| 7824 | } |
| 7825 | |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7826 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 7827 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 7828 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 7829 | if (PropExpr->isImplicitProperty()) return false; |
| 7830 | |
| 7831 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7832 | QualType T = PDecl->getType(); |
| 7833 | if (T->isReferenceType()) |
Fariborz Jahanian | 20688cc | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 7834 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7835 | CanQualType CT = S.Context.getCanonicalType(T); |
| 7836 | return CT.isConstQualified(); |
| 7837 | } |
| 7838 | return false; |
| 7839 | } |
| 7840 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7841 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 7842 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 7843 | return false; |
| 7844 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 7845 | NamedDecl *Member = ME->getMemberDecl(); |
| 7846 | if (isa<FieldDecl>(Member)) { |
| 7847 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 7848 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 7849 | return false; |
| 7850 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 7851 | } |
| 7852 | return false; |
| 7853 | } |
| 7854 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7855 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7856 | /// emit an error and return true. If so, return false. |
| 7857 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7858 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7859 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7860 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7861 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7862 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 7863 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 7864 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7865 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7866 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7867 | if (IsLV == Expr::MLV_Valid) |
| 7868 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7869 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7870 | unsigned Diag = 0; |
| 7871 | bool NeedType = false; |
| 7872 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7873 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7874 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7875 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7876 | NeedType = true; |
| 7877 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7878 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7879 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7880 | NeedType = true; |
| 7881 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7882 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7883 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7884 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7885 | case Expr::MLV_Valid: |
| 7886 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7887 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7888 | case Expr::MLV_MemberFunction: |
| 7889 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7890 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7891 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7892 | case Expr::MLV_IncompleteType: |
| 7893 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7894 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7895 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 7896 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7897 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7898 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7899 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 7900 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7901 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7902 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7903 | case Expr::MLV_ReadonlyProperty: |
| 7904 | Diag = diag::error_readonly_property_assignment; |
| 7905 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7906 | case Expr::MLV_NoSetterProperty: |
| 7907 | Diag = diag::error_nosetter_property_assignment; |
| 7908 | break; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7909 | case Expr::MLV_InvalidMessageExpression: |
| 7910 | Diag = diag::error_readonly_message_assignment; |
| 7911 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7912 | case Expr::MLV_SubObjCPropertySetting: |
| 7913 | Diag = diag::error_no_subobject_property_setting; |
| 7914 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7915 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7916 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7917 | SourceRange Assign; |
| 7918 | if (Loc != OrigLoc) |
| 7919 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7920 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7921 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7922 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7923 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7924 | return true; |
| 7925 | } |
| 7926 | |
| 7927 | |
| 7928 | |
| 7929 | // C99 6.5.16.1 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7930 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7931 | SourceLocation Loc, |
| 7932 | QualType CompoundType) { |
| 7933 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 7934 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7935 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7936 | |
| 7937 | QualType LHSType = LHS->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7938 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7939 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7940 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7941 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7942 | // Simple assignment "x = y". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7943 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 7944 | ExprResult LHSResult = Owned(LHS); |
| 7945 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7946 | if (LHSResult.isInvalid()) |
| 7947 | return QualType(); |
| 7948 | LHS = LHSResult.take(); |
| 7949 | } |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7950 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7951 | if (RHS.isInvalid()) |
| 7952 | return QualType(); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7953 | // Special case of NSObject attributes on c-style pointer types. |
| 7954 | if (ConvTy == IncompatiblePointer && |
| 7955 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7956 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7957 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7958 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7959 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7960 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7961 | if (ConvTy == Compatible && |
| 7962 | getLangOptions().ObjCNonFragileABI && |
| 7963 | LHSType->isObjCObjectType()) |
| 7964 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7965 | << LHSType; |
| 7966 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7967 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7968 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7969 | // instead of "x += 4". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7970 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7971 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7972 | RHSCheck = ICE->getSubExpr(); |
| 7973 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7974 | if ((UO->getOpcode() == UO_Plus || |
| 7975 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7976 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7977 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7978 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7979 | // And there is a space or other character before the subexpr of the |
| 7980 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7981 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7982 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7983 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7984 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7985 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7986 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7987 | } |
| 7988 | } else { |
| 7989 | // Compound assignment "x += y" |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7990 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7991 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7992 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7993 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7994 | RHS.get(), AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7995 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7996 | |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 7997 | |
| 7998 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 7999 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 8000 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 8001 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 8002 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 8003 | // check. |
| 8004 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8005 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 8006 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 8007 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 8008 | !UO->getType().isVolatileQualified()) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 8009 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 8010 | PDiag(diag::warn_indirection_through_null) |
| 8011 | << UO->getSubExpr()->getSourceRange()); |
| 8012 | DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 8013 | PDiag(diag::note_indirection_through_null)); |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 8014 | } |
| 8015 | |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8016 | // Check for trivial buffer overflows. |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8017 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8018 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8019 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 8020 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8021 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8022 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 8023 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8024 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 8025 | // operand. |
John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 8026 | return (getLangOptions().CPlusPlus |
| 8027 | ? LHSType : LHSType.getUnqualifiedType()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 8028 | } |
| 8029 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8030 | // C99 6.5.17 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8031 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8032 | SourceLocation Loc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8033 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 8034 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8035 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 8036 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8037 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 8038 | return QualType(); |
| 8039 | |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8040 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 8041 | // operands, but not unary promotions. |
| 8042 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8043 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8044 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 8045 | // containing site to determine what should be done with the RHS. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8046 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 8047 | if (LHS.isInvalid()) |
| 8048 | return QualType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8049 | |
| 8050 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8051 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 8052 | if (RHS.isInvalid()) |
| 8053 | return QualType(); |
| 8054 | if (!RHS.get()->getType()->isVoidType()) |
| 8055 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8056 | } |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8057 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8058 | return RHS.get()->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 8059 | } |
| 8060 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 8061 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 8062 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8063 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 8064 | ExprValueKind &VK, |
| 8065 | SourceLocation OpLoc, |
| 8066 | bool isInc, bool isPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8067 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8068 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8069 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8070 | QualType ResType = Op->getType(); |
| 8071 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 8072 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8073 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8074 | // Decrement of bool is not allowed. |
| 8075 | if (!isInc) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8076 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8077 | return QualType(); |
| 8078 | } |
| 8079 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8080 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8081 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8082 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 8083 | } else if (ResType->isAnyPointerType()) { |
| 8084 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8085 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8086 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8087 | if (PointeeTy->isVoidType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8088 | if (S.getLangOptions().CPlusPlus) { |
| 8089 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8090 | << Op->getSourceRange(); |
| 8091 | return QualType(); |
| 8092 | } |
| 8093 | |
| 8094 | // Pointer to void is a GNU extension in C. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8095 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8096 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8097 | if (S.getLangOptions().CPlusPlus) { |
| 8098 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8099 | << Op->getType() << Op->getSourceRange(); |
| 8100 | return QualType(); |
| 8101 | } |
| 8102 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8103 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8104 | << ResType << Op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8105 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 8106 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8107 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 8108 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 8109 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8110 | // Diagnose bad cases where we step over interface counts. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8111 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 8112 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8113 | << PointeeTy << Op->getSourceRange(); |
| 8114 | return QualType(); |
| 8115 | } |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 8116 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8117 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8118 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8119 | << ResType << Op->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8120 | } else if (ResType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8121 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8122 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8123 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 8124 | isInc, isPrefix); |
Anton Yartsev | 85129b8 | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 8125 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 8126 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8127 | } else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8128 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 8129 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8130 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 8131 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8132 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 9e1e551 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 8133 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8134 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8135 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8136 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 8137 | // (in C or with postfix), the increment is the unqualified type of the |
| 8138 | // operand. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8139 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 8140 | VK = VK_LValue; |
| 8141 | return ResType; |
| 8142 | } else { |
| 8143 | VK = VK_RValue; |
| 8144 | return ResType.getUnqualifiedType(); |
| 8145 | } |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 8146 | } |
| 8147 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8148 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8149 | assert(E->getValueKind() == VK_LValue && |
| 8150 | E->getObjectKind() == OK_ObjCProperty); |
| 8151 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 8152 | |
| 8153 | ExprValueKind VK = VK_RValue; |
| 8154 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8155 | if (const ObjCMethodDecl *GetterMethod = |
| 8156 | PRE->getImplicitPropertyGetter()) { |
| 8157 | QualType Result = GetterMethod->getResultType(); |
| 8158 | VK = Expr::getValueKindForType(Result); |
| 8159 | } |
| 8160 | else { |
| 8161 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 8162 | << PRE->getBase()->getType(); |
| 8163 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8164 | } |
| 8165 | |
| 8166 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 8167 | E, 0, VK); |
John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 8168 | |
| 8169 | ExprResult Result = MaybeBindToTemporary(E); |
| 8170 | if (!Result.isInvalid()) |
| 8171 | E = Result.take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8172 | |
| 8173 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8174 | } |
| 8175 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8176 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 8177 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 8178 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8179 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8180 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8181 | if (PropRef->isImplicitProperty()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8182 | // If using property-dot syntax notation for assignment, and there is a |
| 8183 | // setter, RHS expression is being passed to the setter argument. So, |
| 8184 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8185 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8186 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8187 | LHSTy = (*P)->getType(); |
| 8188 | |
| 8189 | // Otherwise, if the getter returns an l-value, just call that. |
| 8190 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8191 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8192 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8193 | if (VK == VK_LValue) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8194 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8195 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8196 | return; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8197 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8198 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8199 | } |
| 8200 | |
| 8201 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8202 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8203 | InitializedEntity::InitializeParameter(Context, LHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8204 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8205 | if (!ArgE.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8206 | RHS = ArgE; |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8207 | } |
| 8208 | } |
| 8209 | |
| 8210 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8211 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8212 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8213 | /// where the declaration is needed for type checking. We only need to |
| 8214 | /// handle cases when the expression references a function designator |
| 8215 | /// or is an lvalue. Here are some examples: |
| 8216 | /// - &(x) => x |
| 8217 | /// - &*****f => f for f a function designator. |
| 8218 | /// - &s.xx => s |
| 8219 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8220 | /// - *(x + 1) -> x, if x is an array |
| 8221 | /// - &"123"[2] -> 0 |
| 8222 | /// - & __real__ x -> x |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8223 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8224 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8225 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8226 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8227 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8228 | // If this is an arrow operator, the address is an offset from |
| 8229 | // the base's value, so the object the base refers to is |
| 8230 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8231 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8232 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8233 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8234 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8235 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8236 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8237 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8238 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8239 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8240 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8241 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8242 | } |
| 8243 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8244 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8245 | case Stmt::UnaryOperatorClass: { |
| 8246 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8247 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8248 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8249 | case UO_Real: |
| 8250 | case UO_Imag: |
| 8251 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8252 | return getPrimaryDecl(UO->getSubExpr()); |
| 8253 | default: |
| 8254 | return 0; |
| 8255 | } |
| 8256 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8257 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8258 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8259 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8260 | // If the result of an implicit cast is an l-value, we care about |
| 8261 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8262 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8263 | default: |
| 8264 | return 0; |
| 8265 | } |
| 8266 | } |
| 8267 | |
| 8268 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8269 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8270 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8271 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 8272 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8273 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8274 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8275 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8276 | SourceLocation OpLoc) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8277 | if (OrigOp->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8278 | return S.Context.DependentTy; |
| 8279 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8280 | return S.Context.OverloadTy; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8281 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8282 | return S.Context.UnknownAnyTy; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8283 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8284 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8285 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8286 | // Make sure to ignore parentheses in subsequent checks |
| 8287 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8288 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8289 | if (S.getLangOptions().C99) { |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8290 | // Implement C99-only parts of addressof rules. |
| 8291 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8292 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8293 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8294 | // (assuming the deref expression is valid). |
| 8295 | return uOp->getSubExpr()->getType(); |
| 8296 | } |
| 8297 | // Technically, there should be a check for array subscript |
| 8298 | // expressions here, but the result of one is always an lvalue anyway. |
| 8299 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8300 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8301 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8302 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8303 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8304 | bool sfinae = S.isSFINAEContext(); |
| 8305 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8306 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8307 | << op->getType() << op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8308 | if (sfinae) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8309 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8310 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8311 | return S.Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8312 | } else if (lval == Expr::LV_MemberFunction) { |
| 8313 | // If it's an instance method, make a member pointer. |
| 8314 | // The expression must have exactly the form &A::foo. |
| 8315 | |
| 8316 | // If the underlying expression isn't a decl ref, give up. |
| 8317 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8318 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8319 | << OrigOp->getSourceRange(); |
| 8320 | return QualType(); |
| 8321 | } |
| 8322 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8323 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8324 | |
| 8325 | // The id-expression was parenthesized. |
| 8326 | if (OrigOp != DRE) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8327 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8328 | << OrigOp->getSourceRange(); |
| 8329 | |
| 8330 | // The method was named without a qualifier. |
| 8331 | } else if (!DRE->getQualifier()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8332 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8333 | << op->getSourceRange(); |
| 8334 | } |
| 8335 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8336 | return S.Context.getMemberPointerType(op->getType(), |
| 8337 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8338 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8339 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8340 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8341 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8342 | // FIXME: emit more specific diag... |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8343 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8344 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8345 | return QualType(); |
| 8346 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8347 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8348 | // The operand cannot be a bit-field |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8349 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8350 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8351 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8352 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8353 | // The operand cannot be an element of a vector |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8354 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8355 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8356 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8357 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8358 | // cannot take address of a property expression. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8359 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8360 | << "property expression" << op->getSourceRange(); |
| 8361 | return QualType(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8362 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8363 | // We have an lvalue with a decl. Make sure the decl is not declared |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8364 | // with the register storage-class specifier. |
| 8365 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8366 | // in C++ it is not error to take address of a register |
| 8367 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8368 | if (vd->getStorageClass() == SC_Register && |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8369 | !S.getLangOptions().CPlusPlus) { |
| 8370 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8371 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8372 | return QualType(); |
| 8373 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8374 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8375 | return S.Context.OverloadTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8376 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8377 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8378 | // Could be a pointer to member, though, if there is an explicit |
| 8379 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8380 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8381 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8382 | if (Ctx && Ctx->isRecord()) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8383 | if (dcl->getType()->isReferenceType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8384 | S.Diag(OpLoc, |
| 8385 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8386 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8387 | return QualType(); |
| 8388 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8389 | |
Argyrios Kyrtzidis | 8322b42 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8390 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8391 | Ctx = Ctx->getParent(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8392 | return S.Context.getMemberPointerType(op->getType(), |
| 8393 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8394 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8395 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8396 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 8397 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8398 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8399 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8400 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8401 | // Taking the address of a void variable is technically illegal, but we |
| 8402 | // allow it in cases which are otherwise valid. |
| 8403 | // Example: "extern void x; void* y = &x;". |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8404 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8405 | } |
| 8406 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8407 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 0bdcb8a | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 8408 | if (op->getType()->isObjCObjectType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8409 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8410 | return S.Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8411 | } |
| 8412 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8413 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8414 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8415 | SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8416 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8417 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8418 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8419 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8420 | if (ConvResult.isInvalid()) |
| 8421 | return QualType(); |
| 8422 | Op = ConvResult.take(); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8423 | QualType OpTy = Op->getType(); |
| 8424 | QualType Result; |
| 8425 | |
| 8426 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8427 | // is an incomplete type or void. It would be possible to warn about |
| 8428 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8429 | // warning is unlikely to catch any mistakes. |
| 8430 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8431 | Result = PT->getPointeeType(); |
| 8432 | else if (const ObjCObjectPointerType *OPT = |
| 8433 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8434 | Result = OPT->getPointeeType(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8435 | else { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8436 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8437 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8438 | if (PR.take() != Op) |
| 8439 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8440 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8441 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8442 | if (Result.isNull()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8443 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8444 | << OpTy << Op->getSourceRange(); |
| 8445 | return QualType(); |
| 8446 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8447 | |
| 8448 | // Dereferences are usually l-values... |
| 8449 | VK = VK_LValue; |
| 8450 | |
| 8451 | // ...except that certain expressions are never l-values in C. |
| 8452 | if (!S.getLangOptions().CPlusPlus && |
| 8453 | IsCForbiddenLValueType(S.Context, Result)) |
| 8454 | VK = VK_RValue; |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8455 | |
| 8456 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 8457 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8458 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8459 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8460 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8461 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8462 | switch (Kind) { |
| 8463 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8464 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8465 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8466 | case tok::star: Opc = BO_Mul; break; |
| 8467 | case tok::slash: Opc = BO_Div; break; |
| 8468 | case tok::percent: Opc = BO_Rem; break; |
| 8469 | case tok::plus: Opc = BO_Add; break; |
| 8470 | case tok::minus: Opc = BO_Sub; break; |
| 8471 | case tok::lessless: Opc = BO_Shl; break; |
| 8472 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8473 | case tok::lessequal: Opc = BO_LE; break; |
| 8474 | case tok::less: Opc = BO_LT; break; |
| 8475 | case tok::greaterequal: Opc = BO_GE; break; |
| 8476 | case tok::greater: Opc = BO_GT; break; |
| 8477 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8478 | case tok::equalequal: Opc = BO_EQ; break; |
| 8479 | case tok::amp: Opc = BO_And; break; |
| 8480 | case tok::caret: Opc = BO_Xor; break; |
| 8481 | case tok::pipe: Opc = BO_Or; break; |
| 8482 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8483 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8484 | case tok::equal: Opc = BO_Assign; break; |
| 8485 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8486 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8487 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8488 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8489 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8490 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8491 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8492 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8493 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8494 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8495 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8496 | } |
| 8497 | return Opc; |
| 8498 | } |
| 8499 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8500 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8501 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8502 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8503 | switch (Kind) { |
| 8504 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8505 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8506 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8507 | case tok::amp: Opc = UO_AddrOf; break; |
| 8508 | case tok::star: Opc = UO_Deref; break; |
| 8509 | case tok::plus: Opc = UO_Plus; break; |
| 8510 | case tok::minus: Opc = UO_Minus; break; |
| 8511 | case tok::tilde: Opc = UO_Not; break; |
| 8512 | case tok::exclaim: Opc = UO_LNot; break; |
| 8513 | case tok::kw___real: Opc = UO_Real; break; |
| 8514 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8515 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8516 | } |
| 8517 | return Opc; |
| 8518 | } |
| 8519 | |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8520 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8521 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8522 | /// suppressed in the event of macro expansions. |
| 8523 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8524 | SourceLocation OpLoc) { |
| 8525 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8526 | return; |
| 8527 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8528 | return; |
| 8529 | lhs = lhs->IgnoreParenImpCasts(); |
| 8530 | rhs = rhs->IgnoreParenImpCasts(); |
| 8531 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8532 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8533 | if (!LeftDeclRef || !RightDeclRef || |
| 8534 | LeftDeclRef->getLocation().isMacroID() || |
| 8535 | RightDeclRef->getLocation().isMacroID()) |
| 8536 | return; |
| 8537 | const ValueDecl *LeftDecl = |
| 8538 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8539 | const ValueDecl *RightDecl = |
| 8540 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8541 | if (LeftDecl != RightDecl) |
| 8542 | return; |
| 8543 | if (LeftDecl->getType().isVolatileQualified()) |
| 8544 | return; |
| 8545 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8546 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8547 | return; |
| 8548 | |
| 8549 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8550 | << LeftDeclRef->getType() |
| 8551 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8552 | } |
| 8553 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8554 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8555 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8556 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8557 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8558 | BinaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8559 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8560 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8561 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8562 | // The following two variables are used for compound assignment operators |
| 8563 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8564 | QualType CompResultTy; // Type of computation result |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8565 | ExprValueKind VK = VK_RValue; |
| 8566 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8567 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8568 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8569 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8570 | // But since an assignment can trigger target based overload, exclude it in |
| 8571 | // our blind search. i.e: |
| 8572 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8573 | // f<int> == 0; // resolve f<int> blindly |
| 8574 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8575 | if (Opc != BO_Assign) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8576 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8577 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8578 | lhs = move(resolvedLHS); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8579 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8580 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8581 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8582 | rhs = move(resolvedRHS); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8583 | } |
| 8584 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8585 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8586 | case BO_Assign: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8587 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8588 | if (getLangOptions().CPlusPlus && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8589 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8590 | VK = lhs.get()->getValueKind(); |
| 8591 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8592 | } |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8593 | if (!ResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8594 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8595 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8596 | case BO_PtrMemD: |
| 8597 | case BO_PtrMemI: |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8598 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8599 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8600 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8601 | case BO_Mul: |
| 8602 | case BO_Div: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8603 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8604 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8605 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8606 | case BO_Rem: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8607 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 8608 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8609 | case BO_Add: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8610 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 8611 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8612 | case BO_Sub: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8613 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 8614 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8615 | case BO_Shl: |
| 8616 | case BO_Shr: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8617 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8618 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8619 | case BO_LE: |
| 8620 | case BO_LT: |
| 8621 | case BO_GE: |
| 8622 | case BO_GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8623 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8624 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8625 | case BO_EQ: |
| 8626 | case BO_NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8627 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8628 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8629 | case BO_And: |
| 8630 | case BO_Xor: |
| 8631 | case BO_Or: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8632 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 8633 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8634 | case BO_LAnd: |
| 8635 | case BO_LOr: |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8636 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8637 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8638 | case BO_MulAssign: |
| 8639 | case BO_DivAssign: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8640 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8641 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8642 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8643 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8644 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8645 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8646 | case BO_RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8647 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 8648 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8649 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8650 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8651 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8652 | case BO_AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8653 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8654 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8655 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8656 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8657 | case BO_SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8658 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8659 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8660 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8661 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8662 | case BO_ShlAssign: |
| 8663 | case BO_ShrAssign: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8664 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8665 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8666 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8667 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8668 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8669 | case BO_AndAssign: |
| 8670 | case BO_XorAssign: |
| 8671 | case BO_OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8672 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8673 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8674 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8675 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8676 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8677 | case BO_Comma: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8678 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8679 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 8680 | VK = rhs.get()->getValueKind(); |
| 8681 | OK = rhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8682 | } |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8683 | break; |
| 8684 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8685 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8686 | return ExprError(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8687 | if (CompResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8688 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 8689 | ResultTy, VK, OK, OpLoc)); |
| 8690 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8691 | VK = VK_LValue; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8692 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8693 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8694 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 8695 | ResultTy, VK, OK, CompLHSTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8696 | CompResultTy, OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8697 | } |
| 8698 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8699 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 8700 | /// ParenRange in parentheses. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8701 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 8702 | const PartialDiagnostic &PD, |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8703 | const PartialDiagnostic &FirstNote, |
| 8704 | SourceRange FirstParenRange, |
| 8705 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8706 | SourceRange SecondParenRange) { |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8707 | Self.Diag(Loc, PD); |
| 8708 | |
| 8709 | if (!FirstNote.getDiagID()) |
| 8710 | return; |
| 8711 | |
| 8712 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 8713 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8714 | // We can't display the parentheses, so just return. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8715 | return; |
| 8716 | } |
| 8717 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8718 | Self.Diag(Loc, FirstNote) |
| 8719 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8720 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8721 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8722 | if (!SecondNote.getDiagID()) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8723 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8724 | |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8725 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 8726 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 8727 | // We can't display the parentheses, so just dig the |
| 8728 | // warning/error and return. |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8729 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8730 | return; |
| 8731 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8732 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8733 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8734 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 8735 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8736 | } |
| 8737 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8738 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8739 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8740 | /// comparison operators have higher precedence. The most typical example of |
| 8741 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8742 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8743 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8744 | typedef BinaryOperator BinOp; |
| 8745 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8746 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8747 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8748 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8749 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8750 | rhsopc = BO->getOpcode(); |
| 8751 | |
| 8752 | // Subs are not binary operators. |
| 8753 | if (lhsopc == -1 && rhsopc == -1) |
| 8754 | return; |
| 8755 | |
| 8756 | // Bitwise operations are sometimes used as eager logical ops. |
| 8757 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8758 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8759 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8760 | return; |
| 8761 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8762 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8763 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8764 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8765 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8766 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8767 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8768 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8769 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 8770 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8771 | << BinOp::getOpcodeStr(lhsopc), |
| 8772 | lhs->getSourceRange()); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8773 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8774 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8775 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8776 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8777 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8778 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8779 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8780 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 8781 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8782 | << BinOp::getOpcodeStr(rhsopc), |
| 8783 | rhs->getSourceRange()); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8784 | } |
| 8785 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8786 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8787 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8788 | /// in parentheses. |
| 8789 | static void |
| 8790 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| 8791 | Expr *E) { |
| 8792 | assert(isa<BinaryOperator>(E) && |
| 8793 | cast<BinaryOperator>(E)->getOpcode() == BO_LAnd); |
| 8794 | SuggestParentheses(Self, OpLoc, |
| 8795 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| 8796 | << E->getSourceRange(), |
| 8797 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| 8798 | E->getSourceRange(), |
| 8799 | Self.PDiag(0), SourceRange()); |
| 8800 | } |
| 8801 | |
| 8802 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8803 | /// 'true'. |
| 8804 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8805 | bool Res; |
| 8806 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8807 | } |
| 8808 | |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8809 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8810 | /// 'false'. |
| 8811 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8812 | bool Res; |
| 8813 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8814 | } |
| 8815 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8816 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8817 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8818 | Expr *OrLHS, Expr *OrRHS) { |
| 8819 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8820 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8821 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 8822 | if (EvaluatesAsFalse(S, OrRHS)) |
| 8823 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8824 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8825 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8826 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8827 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8828 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8829 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8830 | // "a || b && 1", but warn now. |
| 8831 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8832 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8833 | } |
| 8834 | } |
| 8835 | } |
| 8836 | } |
| 8837 | |
| 8838 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8839 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8840 | Expr *OrLHS, Expr *OrRHS) { |
| 8841 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8842 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8843 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 8844 | if (EvaluatesAsFalse(S, OrLHS)) |
| 8845 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8846 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8847 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8848 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8849 | } |
| 8850 | } |
| 8851 | } |
| 8852 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8853 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8854 | /// precedence. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8855 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8856 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8857 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8858 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8859 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 8860 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8861 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8862 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | b94e5a3 | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 8863 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8864 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 8865 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8866 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8867 | } |
| 8868 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8869 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8870 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8871 | tok::TokenKind Kind, |
| 8872 | Expr *lhs, Expr *rhs) { |
| 8873 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8874 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8875 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8876 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8877 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8878 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8879 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8880 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8881 | } |
| 8882 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8883 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8884 | BinaryOperatorKind Opc, |
| 8885 | Expr *lhs, Expr *rhs) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8886 | if (getLangOptions().CPlusPlus) { |
| 8887 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8888 | |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8889 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8890 | UseBuiltinOperator = false; |
| 8891 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8892 | UseBuiltinOperator = true; |
| 8893 | } else { |
| 8894 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 8895 | !rhs->getType()->isOverloadableType(); |
| 8896 | } |
| 8897 | |
| 8898 | if (!UseBuiltinOperator) { |
| 8899 | // Find all of the overloaded operators visible from this |
| 8900 | // point. We perform both an operator-name lookup from the local |
| 8901 | // scope and an argument-dependent lookup based on the types of |
| 8902 | // the arguments. |
| 8903 | UnresolvedSet<16> Functions; |
| 8904 | OverloadedOperatorKind OverOp |
| 8905 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8906 | if (S && OverOp != OO_None) |
| 8907 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 8908 | Functions); |
| 8909 | |
| 8910 | // Build the (potentially-overloaded, potentially-dependent) |
| 8911 | // binary operation. |
| 8912 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 8913 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8914 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8915 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8916 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8917 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8918 | } |
| 8919 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8920 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8921 | UnaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8922 | Expr *InputExpr) { |
| 8923 | ExprResult Input = Owned(InputExpr); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8924 | ExprValueKind VK = VK_RValue; |
| 8925 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8926 | QualType resultType; |
| 8927 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8928 | case UO_PreInc: |
| 8929 | case UO_PreDec: |
| 8930 | case UO_PostInc: |
| 8931 | case UO_PostDec: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8932 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8933 | Opc == UO_PreInc || |
| 8934 | Opc == UO_PostInc, |
| 8935 | Opc == UO_PreInc || |
| 8936 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8937 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8938 | case UO_AddrOf: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8939 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8940 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8941 | case UO_Deref: { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8942 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8943 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8944 | Input = move(resolved); |
| 8945 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8946 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8947 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8948 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8949 | case UO_Plus: |
| 8950 | case UO_Minus: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8951 | Input = UsualUnaryConversions(Input.take()); |
| 8952 | if (Input.isInvalid()) return ExprError(); |
| 8953 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8954 | if (resultType->isDependentType()) |
| 8955 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8956 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8957 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8958 | break; |
| 8959 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8960 | resultType->isEnumeralType()) |
| 8961 | break; |
| 8962 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8963 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8964 | resultType->isPointerType()) |
| 8965 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8966 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8967 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8968 | if (Input.isInvalid()) return ExprError(); |
| 8969 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8970 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8971 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8972 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8973 | << resultType << Input.get()->getSourceRange()); |
| 8974 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8975 | case UO_Not: // bitwise complement |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8976 | Input = UsualUnaryConversions(Input.take()); |
| 8977 | if (Input.isInvalid()) return ExprError(); |
| 8978 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8979 | if (resultType->isDependentType()) |
| 8980 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8981 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8982 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8983 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8984 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8985 | << resultType << Input.get()->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8986 | else if (resultType->hasIntegerRepresentation()) |
| 8987 | break; |
| 8988 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8989 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8990 | if (Input.isInvalid()) return ExprError(); |
| 8991 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8992 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8993 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8994 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8995 | } |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8996 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8997 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8998 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 8999 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9000 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9001 | if (Input.isInvalid()) return ExprError(); |
| 9002 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9003 | if (resultType->isDependentType()) |
| 9004 | break; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9005 | if (resultType->isScalarType()) { |
| 9006 | // C99 6.5.3.3p1: ok, fallthrough; |
| 9007 | if (Context.getLangOptions().CPlusPlus) { |
| 9008 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 9009 | // operand contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9010 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 9011 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9012 | } |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9013 | } else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9014 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9015 | if (Input.isInvalid()) return ExprError(); |
| 9016 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9017 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9018 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9019 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9020 | } |
Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 9021 | |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 9022 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9023 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 9024 | resultType = Context.getLogicalOperationType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9025 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9026 | case UO_Real: |
| 9027 | case UO_Imag: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9028 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9029 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9030 | if (Input.isInvalid()) return ExprError(); |
| 9031 | if (Input.get()->getValueKind() != VK_RValue && |
| 9032 | Input.get()->getObjectKind() == OK_Ordinary) |
| 9033 | VK = Input.get()->getValueKind(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 9034 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9035 | case UO_Extension: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9036 | resultType = Input.get()->getType(); |
| 9037 | VK = Input.get()->getValueKind(); |
| 9038 | OK = Input.get()->getObjectKind(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 9039 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9040 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9041 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9042 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9043 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9044 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9045 | VK, OK, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9046 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9047 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9048 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9049 | UnaryOperatorKind Opc, |
| 9050 | Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 9051 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 9052 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9053 | // Find all of the overloaded operators visible from this |
| 9054 | // point. We perform both an operator-name lookup from the local |
| 9055 | // scope and an argument-dependent lookup based on the types of |
| 9056 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9057 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9058 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9059 | if (S && OverOp != OO_None) |
| 9060 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9061 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9062 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9063 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9064 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9065 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9066 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9067 | } |
| 9068 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9069 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9070 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9071 | tok::TokenKind Op, Expr *Input) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9072 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9073 | } |
| 9074 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9075 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9076 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9077 | LabelDecl *TheDecl) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9078 | TheDecl->setUsed(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9079 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9080 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9081 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9082 | } |
| 9083 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9084 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9085 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9086 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9087 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9088 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9089 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9090 | bool isFileScope |
| 9091 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9092 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9093 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9094 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9095 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9096 | // example, it is not possible to goto into a stmt expression apparently. |
| 9097 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9098 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9099 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9100 | // as the type of the stmtexpr. |
| 9101 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9102 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9103 | if (!Compound->body_empty()) { |
| 9104 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9105 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9106 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9107 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9108 | LastLabelStmt = Label; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9109 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9110 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9111 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9112 | // Do function/array conversion on the last expression, but not |
| 9113 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9114 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9115 | if (LastExpr.isInvalid()) |
| 9116 | return ExprError(); |
| 9117 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9118 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9119 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
| 9120 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9121 | InitializedEntity::InitializeResult(LPLoc, |
| 9122 | Ty, |
| 9123 | false), |
| 9124 | SourceLocation(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9125 | LastExpr); |
| 9126 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9127 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9128 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9129 | if (!LastLabelStmt) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9130 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9131 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9132 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9133 | StmtExprMayBindToTemp = true; |
| 9134 | } |
| 9135 | } |
| 9136 | } |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9137 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9138 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9139 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9140 | // expressions are not lvalues. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9141 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9142 | if (StmtExprMayBindToTemp) |
| 9143 | return MaybeBindToTemporary(ResStmtExpr); |
| 9144 | return Owned(ResStmtExpr); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9145 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9146 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9147 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9148 | TypeSourceInfo *TInfo, |
| 9149 | OffsetOfComponent *CompPtr, |
| 9150 | unsigned NumComponents, |
| 9151 | SourceLocation RParenLoc) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9152 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9153 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9154 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9155 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9156 | // We must have at least one component that refers to the type, and the first |
| 9157 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9158 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9159 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9160 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9161 | << ArgTy << TypeRange); |
| 9162 | |
| 9163 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9164 | // with an incomplete type would be ill-formed. |
| 9165 | if (!Dependent |
| 9166 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 9167 | PDiag(diag::err_offsetof_incomplete_type) |
| 9168 | << TypeRange)) |
| 9169 | return ExprError(); |
| 9170 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9171 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9172 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9173 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9174 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9175 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9176 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9177 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9178 | |
| 9179 | bool DidWarnAboutNonPOD = false; |
| 9180 | QualType CurrentType = ArgTy; |
| 9181 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9182 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9183 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9184 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9185 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9186 | if (OC.isBrackets) { |
| 9187 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9188 | if (!CurrentType->isDependentType()) { |
| 9189 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9190 | if(!AT) |
| 9191 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9192 | << CurrentType); |
| 9193 | CurrentType = AT->getElementType(); |
| 9194 | } else |
| 9195 | CurrentType = Context.DependentTy; |
| 9196 | |
| 9197 | // The expression must be an integral expression. |
| 9198 | // FIXME: An integral constant expression? |
| 9199 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9200 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9201 | !Idx->getType()->isIntegerType()) |
| 9202 | return ExprError(Diag(Idx->getLocStart(), |
| 9203 | diag::err_typecheck_subscript_not_integer) |
| 9204 | << Idx->getSourceRange()); |
| 9205 | |
| 9206 | // Record this array index. |
| 9207 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9208 | Exprs.push_back(Idx); |
| 9209 | continue; |
| 9210 | } |
| 9211 | |
| 9212 | // Offset of a field. |
| 9213 | if (CurrentType->isDependentType()) { |
| 9214 | // We have the offset of a field, but we can't look into the dependent |
| 9215 | // type. Just record the identifier of the field. |
| 9216 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9217 | CurrentType = Context.DependentTy; |
| 9218 | continue; |
| 9219 | } |
| 9220 | |
| 9221 | // We need to have a complete type to look into. |
| 9222 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9223 | diag::err_offsetof_incomplete_type)) |
| 9224 | return ExprError(); |
| 9225 | |
| 9226 | // Look for the designated field. |
| 9227 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9228 | if (!RC) |
| 9229 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9230 | << CurrentType); |
| 9231 | RecordDecl *RD = RC->getDecl(); |
| 9232 | |
| 9233 | // C++ [lib.support.types]p5: |
| 9234 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9235 | // International Standard. type shall be a POD structure or a POD union |
| 9236 | // (clause 9). |
| 9237 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9238 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9239 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9240 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9241 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9242 | << CurrentType)) |
| 9243 | DidWarnAboutNonPOD = true; |
| 9244 | } |
| 9245 | |
| 9246 | // Look for the field. |
| 9247 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9248 | LookupQualifiedName(R, RD); |
| 9249 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9250 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9251 | if (!MemberDecl) { |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9252 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9253 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9254 | } |
| 9255 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9256 | if (!MemberDecl) |
| 9257 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9258 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9259 | OC.LocEnd)); |
| 9260 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9261 | // C99 7.17p3: |
| 9262 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9263 | // |
| 9264 | // We diagnose this as an error. |
| 9265 | if (MemberDecl->getBitWidth()) { |
| 9266 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9267 | << MemberDecl->getDeclName() |
| 9268 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9269 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9270 | return ExprError(); |
| 9271 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9272 | |
| 9273 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9274 | if (IndirectMemberDecl) |
| 9275 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9276 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9277 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9278 | // the base class indirections. |
| 9279 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9280 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9281 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9282 | CXXBasePath &Path = Paths.front(); |
| 9283 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9284 | B != BEnd; ++B) |
| 9285 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9286 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9287 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9288 | if (IndirectMemberDecl) { |
| 9289 | for (IndirectFieldDecl::chain_iterator FI = |
| 9290 | IndirectMemberDecl->chain_begin(), |
| 9291 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9292 | assert(isa<FieldDecl>(*FI)); |
| 9293 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9294 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9295 | } |
| 9296 | } else |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9297 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9298 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9299 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9300 | } |
| 9301 | |
| 9302 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9303 | TInfo, Comps.data(), Comps.size(), |
| 9304 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9305 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9306 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9307 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9308 | SourceLocation BuiltinLoc, |
| 9309 | SourceLocation TypeLoc, |
| 9310 | ParsedType argty, |
| 9311 | OffsetOfComponent *CompPtr, |
| 9312 | unsigned NumComponents, |
| 9313 | SourceLocation RPLoc) { |
| 9314 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9315 | TypeSourceInfo *ArgTInfo; |
| 9316 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9317 | if (ArgTy.isNull()) |
| 9318 | return ExprError(); |
| 9319 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9320 | if (!ArgTInfo) |
| 9321 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9322 | |
| 9323 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9324 | RPLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9325 | } |
| 9326 | |
| 9327 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9328 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9329 | Expr *CondExpr, |
| 9330 | Expr *LHSExpr, Expr *RHSExpr, |
| 9331 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9332 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9333 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9334 | ExprValueKind VK = VK_RValue; |
| 9335 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9336 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9337 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9338 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9339 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9340 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9341 | } else { |
| 9342 | // The conditional expression is required to be a constant expression. |
| 9343 | llvm::APSInt condEval(32); |
| 9344 | SourceLocation ExpLoc; |
| 9345 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9346 | return ExprError(Diag(ExpLoc, |
| 9347 | diag::err_typecheck_choose_expr_requires_constant) |
| 9348 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9349 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9350 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9351 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9352 | |
| 9353 | resType = ActiveExpr->getType(); |
| 9354 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9355 | VK = ActiveExpr->getValueKind(); |
| 9356 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9357 | } |
| 9358 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9359 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9360 | resType, VK, OK, RPLoc, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9361 | resType->isDependentType(), |
| 9362 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9363 | } |
| 9364 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9365 | //===----------------------------------------------------------------------===// |
| 9366 | // Clang Extensions. |
| 9367 | //===----------------------------------------------------------------------===// |
| 9368 | |
| 9369 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9370 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9371 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9372 | PushBlockScope(BlockScope, Block); |
| 9373 | CurContext->addDecl(Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9374 | if (BlockScope) |
| 9375 | PushDeclContext(BlockScope, Block); |
| 9376 | else |
| 9377 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9378 | } |
| 9379 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9380 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9381 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9382 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9383 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9384 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9385 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9386 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9387 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9388 | // GetTypeForDeclarator always produces a function type for a block |
| 9389 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9390 | // unless the function was written with a typedef. |
| 9391 | assert(T->isFunctionType() && |
| 9392 | "GetTypeForDeclarator made a non-function block signature"); |
| 9393 | |
| 9394 | // Look for an explicit signature in that function type. |
| 9395 | FunctionProtoTypeLoc ExplicitSignature; |
| 9396 | |
| 9397 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9398 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9399 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9400 | |
| 9401 | // Check whether that explicit signature was synthesized by |
| 9402 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9403 | // written signature. |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9404 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9405 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9406 | // This would be much cheaper if we stored TypeLocs instead of |
| 9407 | // TypeSourceInfos. |
| 9408 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9409 | unsigned Size = Result.getFullDataSize(); |
| 9410 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9411 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9412 | |
| 9413 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9414 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9415 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9416 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9417 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9418 | CurBlock->FunctionType = T; |
| 9419 | |
| 9420 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9421 | QualType RetTy = Fn->getResultType(); |
| 9422 | bool isVariadic = |
| 9423 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9424 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9425 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9426 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9427 | // Don't allow returning a objc interface by value. |
| 9428 | if (RetTy->isObjCObjectType()) { |
| 9429 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9430 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9431 | return; |
| 9432 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9433 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9434 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9435 | // return type. TODO: what should we do with declarators like: |
| 9436 | // ^ * { ... } |
| 9437 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9438 | if (RetTy != Context.DependentTy) |
| 9439 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9440 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9441 | // Push block parameters from the declarator if we had them. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9442 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9443 | if (ExplicitSignature) { |
| 9444 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9445 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9446 | if (Param->getIdentifier() == 0 && |
| 9447 | !Param->isImplicit() && |
| 9448 | !Param->isInvalidDecl() && |
| 9449 | !getLangOptions().CPlusPlus) |
| 9450 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9451 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9452 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9453 | |
| 9454 | // Fake up parameter variables if we have a typedef, like |
| 9455 | // ^ fntype { ... } |
| 9456 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9457 | for (FunctionProtoType::arg_type_iterator |
| 9458 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9459 | ParmVarDecl *Param = |
| 9460 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9461 | ParamInfo.getSourceRange().getBegin(), |
| 9462 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9463 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9464 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9465 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9466 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9467 | // Set the parameters on the block decl. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9468 | if (!Params.empty()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9469 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9470 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9471 | CurBlock->TheDecl->param_end(), |
| 9472 | /*CheckParameterNames=*/false); |
| 9473 | } |
| 9474 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9475 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9476 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9477 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9478 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9479 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9480 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9481 | // FIXME: remove the attribute. |
| 9482 | } |
| 9483 | |
| 9484 | // Put the parameter variables in scope. We can bail out immediately |
| 9485 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9486 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9487 | return; |
| 9488 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9489 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9490 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9491 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9492 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9493 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9494 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9495 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9496 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9497 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9498 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9499 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9500 | } |
| 9501 | |
| 9502 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9503 | /// is invoked to pop the information about the block from the action impl. |
| 9504 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9505 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9506 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9507 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9508 | } |
| 9509 | |
| 9510 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9511 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9512 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9513 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9514 | // If blocks are disabled, emit an error. |
| 9515 | if (!LangOpts.Blocks) |
| 9516 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9517 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9518 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9519 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9520 | PopDeclContext(); |
| 9521 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9522 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9523 | if (!BSI->ReturnType.isNull()) |
| 9524 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9525 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9526 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9527 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9528 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9529 | // Set the captured variables on the block. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9530 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9531 | BSI->CapturesCXXThis); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9532 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9533 | // If the user wrote a function type in some form, try to use that. |
| 9534 | if (!BSI->FunctionType.isNull()) { |
| 9535 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9536 | |
| 9537 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9538 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9539 | |
| 9540 | // Turn protoless block types into nullary block types. |
| 9541 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9542 | FunctionProtoType::ExtProtoInfo EPI; |
| 9543 | EPI.ExtInfo = Ext; |
| 9544 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9545 | |
| 9546 | // Otherwise, if we don't need to change anything about the function type, |
| 9547 | // preserve its sugar structure. |
| 9548 | } else if (FTy->getResultType() == RetTy && |
| 9549 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9550 | BlockTy = BSI->FunctionType; |
| 9551 | |
| 9552 | // Otherwise, make the minimal modifications to the function type. |
| 9553 | } else { |
| 9554 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9555 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9556 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9557 | EPI.ExtInfo = Ext; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9558 | BlockTy = Context.getFunctionType(RetTy, |
| 9559 | FPT->arg_type_begin(), |
| 9560 | FPT->getNumArgs(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9561 | EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9562 | } |
| 9563 | |
| 9564 | // If we don't have a function type, just build one from nothing. |
| 9565 | } else { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9566 | FunctionProtoType::ExtProtoInfo EPI; |
Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 9567 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, false, 0, CC_Default); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9568 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9569 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9570 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9571 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9572 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9573 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9574 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9575 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9576 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9577 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9578 | |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9579 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9580 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9581 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9582 | |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9583 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9584 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9585 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9586 | } |
| 9587 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9588 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9589 | Expr *expr, ParsedType type, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9590 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9591 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9592 | GetTypeFromParser(type, &TInfo); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9593 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9594 | } |
| 9595 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9596 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9597 | Expr *E, TypeSourceInfo *TInfo, |
| 9598 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9599 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9600 | |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9601 | // Get the va_list type |
| 9602 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9603 | if (VaListType->isArrayType()) { |
| 9604 | // Deal with implicit array decay; for example, on x86-64, |
| 9605 | // va_list is an array, but it's supposed to decay to |
| 9606 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9607 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9608 | // Make sure the input expression also decays appropriately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9609 | ExprResult Result = UsualUnaryConversions(E); |
| 9610 | if (Result.isInvalid()) |
| 9611 | return ExprError(); |
| 9612 | E = Result.take(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9613 | } else { |
| 9614 | // Otherwise, the va_list argument must be an l-value because |
| 9615 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9616 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9617 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9618 | return ExprError(); |
| 9619 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9620 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9621 | if (!E->isTypeDependent() && |
| 9622 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9623 | return ExprError(Diag(E->getLocStart(), |
| 9624 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9625 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9626 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9627 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9628 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9629 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9630 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9631 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9632 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9633 | } |
| 9634 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9635 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9636 | // The type of __null will be int or long, depending on the size of |
| 9637 | // pointers on the target. |
| 9638 | QualType Ty; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9639 | unsigned pw = Context.Target.getPointerWidth(0); |
| 9640 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9641 | Ty = Context.IntTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9642 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9643 | Ty = Context.LongTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9644 | else if (pw == Context.Target.getLongLongWidth()) |
| 9645 | Ty = Context.LongLongTy; |
| 9646 | else { |
| 9647 | assert(!"I don't know size of pointer!"); |
| 9648 | Ty = Context.IntTy; |
| 9649 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9650 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9651 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9652 | } |
| 9653 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9654 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9655 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9656 | if (!SemaRef.getLangOptions().ObjC1) |
| 9657 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9658 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9659 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9660 | if (!PT) |
| 9661 | return; |
| 9662 | |
| 9663 | // Check if the destination is of type 'id'. |
| 9664 | if (!PT->isObjCIdType()) { |
| 9665 | // Check if the destination is the 'NSString' interface. |
| 9666 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9667 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9668 | return; |
| 9669 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9670 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9671 | // Strip off any parens and casts. |
| 9672 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 9673 | if (!SL || SL->isWide()) |
| 9674 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9675 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9676 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9677 | } |
| 9678 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9679 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9680 | SourceLocation Loc, |
| 9681 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9682 | Expr *SrcExpr, AssignmentAction Action, |
| 9683 | bool *Complained) { |
| 9684 | if (Complained) |
| 9685 | *Complained = false; |
| 9686 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9687 | // Decode the result (notice that AST's are still created for extensions). |
| 9688 | bool isInvalid = false; |
| 9689 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9690 | FixItHint Hint; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9691 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9692 | switch (ConvTy) { |
| 9693 | default: assert(0 && "Unknown conversion type"); |
| 9694 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9695 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9696 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9697 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9698 | case IntToPointer: |
| 9699 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9700 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9701 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9702 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9703 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 9704 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9705 | case IncompatiblePointerSign: |
| 9706 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9707 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9708 | case FunctionVoidPointer: |
| 9709 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9710 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9711 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 71de91c | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9712 | // Perform array-to-pointer decay if necessary. |
| 9713 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9714 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9715 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9716 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9717 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9718 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9719 | break; |
| 9720 | } |
| 9721 | |
| 9722 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9723 | // fallthrough |
| 9724 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9725 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9726 | // If the qualifiers lost were because we were applying the |
| 9727 | // (deprecated) C++ conversion from a string literal to a char* |
| 9728 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9729 | // Ideally, this check would be performed in |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9730 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9731 | // bit of refactoring (so that the second argument is an |
| 9732 | // expression, rather than a type), which should be done as part |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9733 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9734 | // C++ semantics. |
| 9735 | if (getLangOptions().CPlusPlus && |
| 9736 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9737 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9738 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9739 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9740 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9741 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9742 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9743 | case IntToBlockPointer: |
| 9744 | DiagKind = diag::err_int_to_block_pointer; |
| 9745 | break; |
| 9746 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9747 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9748 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9749 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9750 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9751 | // it can give a more specific diagnostic. |
| 9752 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9753 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9754 | case IncompatibleVectors: |
| 9755 | DiagKind = diag::warn_incompatible_vectors; |
| 9756 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9757 | case Incompatible: |
| 9758 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9759 | isInvalid = true; |
| 9760 | break; |
| 9761 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9762 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9763 | QualType FirstType, SecondType; |
| 9764 | switch (Action) { |
| 9765 | case AA_Assigning: |
| 9766 | case AA_Initializing: |
| 9767 | // The destination type comes first. |
| 9768 | FirstType = DstType; |
| 9769 | SecondType = SrcType; |
| 9770 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9771 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9772 | case AA_Returning: |
| 9773 | case AA_Passing: |
| 9774 | case AA_Converting: |
| 9775 | case AA_Sending: |
| 9776 | case AA_Casting: |
| 9777 | // The source type comes first. |
| 9778 | FirstType = SrcType; |
| 9779 | SecondType = DstType; |
| 9780 | break; |
| 9781 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9782 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9783 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9784 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9785 | if (Complained) |
| 9786 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9787 | return isInvalid; |
| 9788 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9789 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9790 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9791 | llvm::APSInt ICEResult; |
| 9792 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9793 | if (Result) |
| 9794 | *Result = ICEResult; |
| 9795 | return false; |
| 9796 | } |
| 9797 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9798 | Expr::EvalResult EvalResult; |
| 9799 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9800 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9801 | EvalResult.HasSideEffects) { |
| 9802 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9803 | |
| 9804 | if (EvalResult.Diag) { |
| 9805 | // We only show the note if it's not the usual "invalid subexpression" |
| 9806 | // or if it's actually in a subexpression. |
| 9807 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9808 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9809 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9810 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9811 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9812 | return true; |
| 9813 | } |
| 9814 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9815 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9816 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9817 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9818 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9819 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9820 | != Diagnostic::Ignored) |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9821 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9822 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9823 | if (Result) |
| 9824 | *Result = EvalResult.Val.getInt(); |
| 9825 | return false; |
| 9826 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9827 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9828 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9829 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9830 | ExprEvalContexts.push_back( |
| 9831 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9832 | } |
| 9833 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9834 | void |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9835 | Sema::PopExpressionEvaluationContext() { |
| 9836 | // Pop the current expression evaluation context off the stack. |
| 9837 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9838 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9839 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9840 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9841 | if (Rec.PotentiallyReferenced) { |
| 9842 | // Mark any remaining declarations in the current position of the stack |
| 9843 | // as "referenced". If they were not meant to be referenced, semantic |
| 9844 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9845 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9846 | I = Rec.PotentiallyReferenced->begin(), |
| 9847 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9848 | I != IEnd; ++I) |
| 9849 | MarkDeclarationReferenced(I->first, I->second); |
| 9850 | } |
| 9851 | |
| 9852 | if (Rec.PotentiallyDiagnosed) { |
| 9853 | // Emit any pending diagnostics. |
| 9854 | for (PotentiallyEmittedDiagnostics::iterator |
| 9855 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9856 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9857 | I != IEnd; ++I) |
| 9858 | Diag(I->first, I->second); |
| 9859 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9860 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9861 | |
| 9862 | // When are coming out of an unevaluated context, clear out any |
| 9863 | // temporaries that we may have created as part of the evaluation of |
| 9864 | // the expression in that context: they aren't relevant because they |
| 9865 | // will never be constructed. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9866 | if (Rec.Context == Unevaluated && |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9867 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 9868 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9869 | ExprTemporaries.end()); |
| 9870 | |
| 9871 | // Destroy the popped expression evaluation record. |
| 9872 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9873 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9874 | |
| 9875 | /// \brief Note that the given declaration was referenced in the source code. |
| 9876 | /// |
| 9877 | /// This routine should be invoke whenever a given declaration is referenced |
| 9878 | /// in the source code, and where that reference occurred. If this declaration |
| 9879 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9880 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9881 | /// |
| 9882 | /// \param Loc the location where the declaration was referenced. |
| 9883 | /// |
| 9884 | /// \param D the declaration that has been referenced by the source code. |
| 9885 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9886 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9887 | |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9888 | D->setReferenced(); |
| 9889 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9890 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9891 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9892 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 9893 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 9894 | // template or not. The reason for this is that unevaluated expressions |
| 9895 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 9896 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9897 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9898 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9899 | D->setUsed(); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9900 | return; |
| 9901 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9902 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9903 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9904 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9905 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9906 | // Do not mark anything as "used" within a dependent context; wait for |
| 9907 | // an instantiation. |
| 9908 | if (CurContext->isDependentContext()) |
| 9909 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9910 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9911 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9912 | case Unevaluated: |
| 9913 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9914 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9915 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9916 | case PotentiallyEvaluated: |
| 9917 | // We are in a potentially-evaluated expression, so this declaration is |
| 9918 | // "used"; handle this below. |
| 9919 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9920 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9921 | case PotentiallyPotentiallyEvaluated: |
| 9922 | // We are in an expression that may be potentially evaluated; queue this |
| 9923 | // declaration reference until we know whether the expression is |
| 9924 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9925 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9926 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9927 | |
| 9928 | case PotentiallyEvaluatedIfUsed: |
| 9929 | // Referenced declarations will only be used if the construct in the |
| 9930 | // containing expression is used. |
| 9931 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9932 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9933 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9934 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9935 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9936 | unsigned TypeQuals; |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 9937 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9938 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 9939 | return; |
| 9940 | if (!Constructor->isUsed(false)) |
| 9941 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9942 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 9943 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9944 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9945 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 9946 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9947 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9948 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9949 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9950 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9951 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9952 | if (Destructor->isVirtual()) |
| 9953 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9954 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 9955 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 9956 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9957 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9958 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9959 | } else if (MethodDecl->isVirtual()) |
| 9960 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9961 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9962 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9963 | // Recursive functions should be marked when used from another function. |
| 9964 | if (CurContext == Function) return; |
| 9965 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9966 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9967 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9968 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9969 | bool AlreadyInstantiated = false; |
| 9970 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9971 | = Function->getTemplateSpecializationInfo()) { |
| 9972 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9973 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9974 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9975 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9976 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9977 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9978 | = Function->getMemberSpecializationInfo()) { |
| 9979 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9980 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9981 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9982 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9983 | AlreadyInstantiated = true; |
| 9984 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9985 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9986 | if (!AlreadyInstantiated) { |
| 9987 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9988 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9989 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9990 | Loc)); |
| 9991 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9992 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9993 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9994 | } else { |
| 9995 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9996 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9997 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9998 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9999 | MarkDeclarationReferenced(Loc, *i); |
| 10000 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10001 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10002 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10003 | // Keep track of used but undefined functions. |
| 10004 | if (!Function->isPure() && !Function->hasBody() && |
| 10005 | Function->getLinkage() != ExternalLinkage) { |
| 10006 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 10007 | if (old.isInvalid()) old = Loc; |
| 10008 | } |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 10009 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10010 | Function->setUsed(true); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10011 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10012 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10013 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10014 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10015 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10016 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10017 | Var->getInstantiatedFromStaticDataMember()) { |
| 10018 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10019 | assert(MSInfo && "Missing member specialization information?"); |
| 10020 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 10021 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 10022 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10023 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10024 | } |
| 10025 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10026 | |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10027 | // Keep track of used but undefined variables. We make a hole in |
| 10028 | // the warning for static const data members with in-line |
| 10029 | // initializers. |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10030 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10031 | && Var->getLinkage() != ExternalLinkage |
| 10032 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10033 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 10034 | if (old.isInvalid()) old = Loc; |
| 10035 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10036 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10037 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10038 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10039 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10040 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10041 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10042 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10043 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10044 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10045 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10046 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10047 | Sema &S; |
| 10048 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10049 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10050 | public: |
| 10051 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10052 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10053 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10054 | |
| 10055 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10056 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10057 | }; |
| 10058 | } |
| 10059 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10060 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10061 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10062 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 10063 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 10064 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10065 | |
| 10066 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10067 | } |
| 10068 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10069 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10070 | if (ClassTemplateSpecializationDecl *Spec |
| 10071 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10072 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10073 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10074 | } |
| 10075 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10076 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10077 | } |
| 10078 | |
| 10079 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10080 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10081 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10082 | } |
| 10083 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10084 | namespace { |
| 10085 | /// \brief Helper class that marks all of the declarations referenced by |
| 10086 | /// potentially-evaluated subexpressions as "referenced". |
| 10087 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10088 | Sema &S; |
| 10089 | |
| 10090 | public: |
| 10091 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10092 | |
| 10093 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 10094 | |
| 10095 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 10096 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10097 | } |
| 10098 | |
| 10099 | void VisitMemberExpr(MemberExpr *E) { |
| 10100 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10101 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10102 | } |
| 10103 | |
| 10104 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 10105 | if (E->getConstructor()) |
| 10106 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 10107 | if (E->getOperatorNew()) |
| 10108 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 10109 | if (E->getOperatorDelete()) |
| 10110 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10111 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10112 | } |
| 10113 | |
| 10114 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10115 | if (E->getOperatorDelete()) |
| 10116 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10117 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10118 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10119 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 10120 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 10121 | S.LookupDestructor(Record)); |
| 10122 | } |
| 10123 | |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10124 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10125 | } |
| 10126 | |
| 10127 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 10128 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10129 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10130 | } |
| 10131 | |
| 10132 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 10133 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10134 | } |
Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10135 | |
| 10136 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10137 | Visit(E->getExpr()); |
| 10138 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10139 | }; |
| 10140 | } |
| 10141 | |
| 10142 | /// \brief Mark any declarations that appear within this expression or any |
| 10143 | /// potentially-evaluated subexpressions as "referenced". |
| 10144 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 10145 | EvaluatedExprMarker(*this).Visit(E); |
| 10146 | } |
| 10147 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10148 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10149 | /// of the program being compiled. |
| 10150 | /// |
| 10151 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10152 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10153 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10154 | /// expressions, code used only during overload resolution, etc., are not |
| 10155 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10156 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10157 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10158 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10159 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10160 | /// This routine should be used for all diagnostics that describe the run-time |
| 10161 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10162 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10163 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 10164 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10165 | const PartialDiagnostic &PD) { |
| 10166 | switch (ExprEvalContexts.back().Context ) { |
| 10167 | case Unevaluated: |
| 10168 | // The argument will never be evaluated, so don't complain. |
| 10169 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10170 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10171 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10172 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10173 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 10174 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 10175 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 10176 | } |
| 10177 | else |
| 10178 | Diag(Loc, PD); |
| 10179 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10180 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10181 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10182 | case PotentiallyPotentiallyEvaluated: |
| 10183 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10184 | break; |
| 10185 | } |
| 10186 | |
| 10187 | return false; |
| 10188 | } |
| 10189 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10190 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10191 | CallExpr *CE, FunctionDecl *FD) { |
| 10192 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10193 | return false; |
| 10194 | |
| 10195 | PartialDiagnostic Note = |
| 10196 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10197 | << FD->getDeclName() : PDiag(); |
| 10198 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10199 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10200 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10201 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10202 | PDiag(diag::err_call_function_incomplete_return) |
| 10203 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10204 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10205 | << CE->getSourceRange(), |
| 10206 | std::make_pair(NoteLoc, Note))) |
| 10207 | return true; |
| 10208 | |
| 10209 | return false; |
| 10210 | } |
| 10211 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10212 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10213 | // will prevent this condition from triggering, which is what we want. |
| 10214 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10215 | SourceLocation Loc; |
| 10216 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10217 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10218 | bool IsOrAssign = false; |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10219 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10220 | if (isa<BinaryOperator>(E)) { |
| 10221 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10222 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10223 | return; |
| 10224 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10225 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10226 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10227 | // Greylist some idioms by putting them into a warning subcategory. |
| 10228 | if (ObjCMessageExpr *ME |
| 10229 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10230 | Selector Sel = ME->getSelector(); |
| 10231 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10232 | // self = [<foo> init...] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10233 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10234 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10235 | |
| 10236 | // <foo> = [<bar> nextObject] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10237 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10238 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10239 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10240 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10241 | Loc = Op->getOperatorLoc(); |
| 10242 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10243 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10244 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10245 | return; |
| 10246 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10247 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10248 | Loc = Op->getOperatorLoc(); |
| 10249 | } else { |
| 10250 | // Not an assignment. |
| 10251 | return; |
| 10252 | } |
| 10253 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10254 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10255 | |
| 10256 | if (IsOrAssign) |
| 10257 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10258 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10259 | else |
| 10260 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10261 | << FixItHint::CreateReplacement(Loc, "=="); |
| 10262 | |
Fariborz Jahanian | e72509c | 2011-04-13 20:31:26 +0000 | [diff] [blame] | 10263 | SourceLocation Open = E->getSourceRange().getBegin(); |
Eli Friedman | 6620dde | 2011-04-14 00:51:41 +0000 | [diff] [blame] | 10264 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
Fariborz Jahanian | 3867554 | 2011-04-13 22:18:37 +0000 | [diff] [blame] | 10265 | Diag(Loc, diag::note_condition_assign_silence) |
| 10266 | << FixItHint::CreateInsertion(Open, "(") |
Eli Friedman | 6620dde | 2011-04-14 00:51:41 +0000 | [diff] [blame] | 10267 | << FixItHint::CreateInsertion(Close, ")"); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10268 | } |
| 10269 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10270 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10271 | /// that the user intended an assignment used as condition. |
| 10272 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10273 | // Don't warn if the parens came from a macro. |
| 10274 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10275 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10276 | return; |
Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10277 | // Don't warn for dependent expressions. |
| 10278 | if (parenE->isTypeDependent()) |
| 10279 | return; |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10280 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10281 | Expr *E = parenE->IgnoreParens(); |
| 10282 | |
| 10283 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 582dd68 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10284 | if (opE->getOpcode() == BO_EQ && |
| 10285 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10286 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10287 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | c358d9f | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10288 | |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10289 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
| 10290 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10291 | << FixItHint::CreateReplacement(Loc, "="); |
| 10292 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10293 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10294 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10295 | } |
| 10296 | } |
| 10297 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10298 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10299 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10300 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10301 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10302 | |
| 10303 | if (!E->isTypeDependent()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10304 | if (E->isBoundMemberFunction(Context)) { |
| 10305 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10306 | << E->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10307 | return ExprError(); |
| 10308 | } |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10309 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10310 | if (getLangOptions().CPlusPlus) |
| 10311 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10312 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10313 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10314 | if (ERes.isInvalid()) |
| 10315 | return ExprError(); |
| 10316 | E = ERes.take(); |
John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10317 | |
| 10318 | QualType T = E->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10319 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10320 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10321 | << T << E->getSourceRange(); |
| 10322 | return ExprError(); |
| 10323 | } |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10324 | } |
| 10325 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10326 | return Owned(E); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10327 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10328 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10329 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10330 | Expr *Sub) { |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10331 | if (!Sub) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10332 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10333 | |
| 10334 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10335 | } |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10336 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10337 | namespace { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10338 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10339 | /// to have an appropriate type. |
| 10340 | struct RebuildUnknownAnyFunction |
| 10341 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10342 | |
| 10343 | Sema &S; |
| 10344 | |
| 10345 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10346 | |
| 10347 | ExprResult VisitStmt(Stmt *S) { |
| 10348 | llvm_unreachable("unexpected statement!"); |
| 10349 | return ExprError(); |
| 10350 | } |
| 10351 | |
| 10352 | ExprResult VisitExpr(Expr *expr) { |
| 10353 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10354 | << expr->getSourceRange(); |
| 10355 | return ExprError(); |
| 10356 | } |
| 10357 | |
| 10358 | /// Rebuild an expression which simply semantically wraps another |
| 10359 | /// expression which it shares the type and value kind of. |
| 10360 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10361 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10362 | if (subResult.isInvalid()) return ExprError(); |
| 10363 | |
| 10364 | Expr *subExpr = subResult.take(); |
| 10365 | expr->setSubExpr(subExpr); |
| 10366 | expr->setType(subExpr->getType()); |
| 10367 | expr->setValueKind(subExpr->getValueKind()); |
| 10368 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10369 | return expr; |
| 10370 | } |
| 10371 | |
| 10372 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10373 | return rebuildSugarExpr(paren); |
| 10374 | } |
| 10375 | |
| 10376 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10377 | return rebuildSugarExpr(op); |
| 10378 | } |
| 10379 | |
| 10380 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10381 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10382 | if (subResult.isInvalid()) return ExprError(); |
| 10383 | |
| 10384 | Expr *subExpr = subResult.take(); |
| 10385 | op->setSubExpr(subExpr); |
| 10386 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10387 | assert(op->getValueKind() == VK_RValue); |
| 10388 | assert(op->getObjectKind() == OK_Ordinary); |
| 10389 | return op; |
| 10390 | } |
| 10391 | |
| 10392 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10393 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10394 | |
| 10395 | expr->setType(decl->getType()); |
| 10396 | |
| 10397 | assert(expr->getValueKind() == VK_RValue); |
| 10398 | if (S.getLangOptions().CPlusPlus && |
| 10399 | !(isa<CXXMethodDecl>(decl) && |
| 10400 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10401 | expr->setValueKind(VK_LValue); |
| 10402 | |
| 10403 | return expr; |
| 10404 | } |
| 10405 | |
| 10406 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10407 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10408 | } |
| 10409 | |
| 10410 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10411 | return resolveDecl(ref, ref->getDecl()); |
| 10412 | } |
| 10413 | }; |
| 10414 | } |
| 10415 | |
| 10416 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10417 | /// to have a function type. |
| 10418 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10419 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10420 | if (result.isInvalid()) return ExprError(); |
| 10421 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10422 | } |
| 10423 | |
| 10424 | namespace { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10425 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10426 | /// into one which resolves the type directly on the referring |
| 10427 | /// expression. Strict preservation of the original source |
| 10428 | /// structure is not a goal. |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10429 | struct RebuildUnknownAnyExpr |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10430 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10431 | |
| 10432 | Sema &S; |
| 10433 | |
| 10434 | /// The current destination type. |
| 10435 | QualType DestType; |
| 10436 | |
| 10437 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10438 | : S(S), DestType(castType) {} |
| 10439 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10440 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10441 | llvm_unreachable("unexpected statement!"); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10442 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10443 | } |
| 10444 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10445 | ExprResult VisitExpr(Expr *expr) { |
| 10446 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10447 | << expr->getSourceRange(); |
| 10448 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10449 | } |
| 10450 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10451 | ExprResult VisitCallExpr(CallExpr *call); |
| 10452 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10453 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10454 | /// Rebuild an expression which simply semantically wraps another |
| 10455 | /// expression which it shares the type and value kind of. |
| 10456 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10457 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10458 | if (subResult.isInvalid()) return ExprError(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10459 | Expr *subExpr = subResult.take(); |
| 10460 | expr->setSubExpr(subExpr); |
| 10461 | expr->setType(subExpr->getType()); |
| 10462 | expr->setValueKind(subExpr->getValueKind()); |
| 10463 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10464 | return expr; |
| 10465 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10466 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10467 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10468 | return rebuildSugarExpr(paren); |
| 10469 | } |
| 10470 | |
| 10471 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10472 | return rebuildSugarExpr(op); |
| 10473 | } |
| 10474 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10475 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10476 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10477 | if (!ptr) { |
| 10478 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10479 | << op->getSourceRange(); |
| 10480 | return ExprError(); |
| 10481 | } |
| 10482 | assert(op->getValueKind() == VK_RValue); |
| 10483 | assert(op->getObjectKind() == OK_Ordinary); |
| 10484 | op->setType(DestType); |
| 10485 | |
| 10486 | // Build the sub-expression as if it were an object of the pointee type. |
| 10487 | DestType = ptr->getPointeeType(); |
| 10488 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10489 | if (subResult.isInvalid()) return ExprError(); |
| 10490 | op->setSubExpr(subResult.take()); |
| 10491 | return op; |
| 10492 | } |
| 10493 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10494 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10495 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10496 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10497 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10498 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10499 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10500 | } |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10501 | |
| 10502 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10503 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10504 | } |
| 10505 | }; |
| 10506 | } |
| 10507 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10508 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10509 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10510 | Expr *callee = call->getCallee(); |
| 10511 | |
| 10512 | enum FnKind { |
| 10513 | FK_Function, |
| 10514 | FK_FunctionPointer, |
| 10515 | FK_BlockPointer |
| 10516 | }; |
| 10517 | |
| 10518 | FnKind kind; |
| 10519 | QualType type = callee->getType(); |
| 10520 | if (type->isFunctionType()) { |
| 10521 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10522 | kind = FK_Function; |
| 10523 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10524 | type = ptr->getPointeeType(); |
| 10525 | kind = FK_FunctionPointer; |
| 10526 | } else { |
| 10527 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 10528 | kind = FK_BlockPointer; |
| 10529 | } |
| 10530 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 10531 | |
| 10532 | // Verify that this is a legal result type of a function. |
| 10533 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10534 | unsigned diagID = diag::err_func_returning_array_function; |
| 10535 | if (kind == FK_BlockPointer) |
| 10536 | diagID = diag::err_block_returning_array_function; |
| 10537 | |
| 10538 | S.Diag(call->getExprLoc(), diagID) |
| 10539 | << DestType->isFunctionType() << DestType; |
| 10540 | return ExprError(); |
| 10541 | } |
| 10542 | |
| 10543 | // Otherwise, go ahead and set DestType as the call's result. |
| 10544 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 10545 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 10546 | assert(call->getObjectKind() == OK_Ordinary); |
| 10547 | |
| 10548 | // Rebuild the function type, replacing the result type with DestType. |
| 10549 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 10550 | DestType = S.Context.getFunctionType(DestType, |
| 10551 | proto->arg_type_begin(), |
| 10552 | proto->getNumArgs(), |
| 10553 | proto->getExtProtoInfo()); |
| 10554 | else |
| 10555 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 10556 | fnType->getExtInfo()); |
| 10557 | |
| 10558 | // Rebuild the appropriate pointer-to-function type. |
| 10559 | switch (kind) { |
| 10560 | case FK_Function: |
| 10561 | // Nothing to do. |
| 10562 | break; |
| 10563 | |
| 10564 | case FK_FunctionPointer: |
| 10565 | DestType = S.Context.getPointerType(DestType); |
| 10566 | break; |
| 10567 | |
| 10568 | case FK_BlockPointer: |
| 10569 | DestType = S.Context.getBlockPointerType(DestType); |
| 10570 | break; |
| 10571 | } |
| 10572 | |
| 10573 | // Finally, we can recurse. |
| 10574 | ExprResult calleeResult = Visit(callee); |
| 10575 | if (!calleeResult.isUsable()) return ExprError(); |
| 10576 | call->setCallee(calleeResult.take()); |
| 10577 | |
| 10578 | // Bind a temporary if necessary. |
| 10579 | return S.MaybeBindToTemporary(call); |
| 10580 | } |
| 10581 | |
| 10582 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10583 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 10584 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10585 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10586 | // Verify that this is a legal result type of a call. |
| 10587 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10588 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 10589 | << DestType->isFunctionType() << DestType; |
| 10590 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10591 | } |
| 10592 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10593 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 10594 | method->setResultType(DestType); |
| 10595 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10596 | // Change the type of the message. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10597 | msg->setType(DestType.getNonReferenceType()); |
| 10598 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10599 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10600 | return S.MaybeBindToTemporary(msg); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10601 | } |
| 10602 | |
| 10603 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10604 | // The only case we should ever see here is a function-to-pointer decay. |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10605 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10606 | assert(ice->getValueKind() == VK_RValue); |
| 10607 | assert(ice->getObjectKind() == OK_Ordinary); |
| 10608 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10609 | ice->setType(DestType); |
| 10610 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10611 | // Rebuild the sub-expression as the pointee (function) type. |
| 10612 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 10613 | |
| 10614 | ExprResult result = Visit(ice->getSubExpr()); |
| 10615 | if (!result.isUsable()) return ExprError(); |
| 10616 | |
| 10617 | ice->setSubExpr(result.take()); |
| 10618 | return S.Owned(ice); |
| 10619 | } |
| 10620 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10621 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10622 | ExprValueKind valueKind = VK_LValue; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10623 | QualType type = DestType; |
| 10624 | |
| 10625 | // We know how to make this work for certain kinds of decls: |
| 10626 | |
| 10627 | // - functions |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10628 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
| 10629 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10630 | if (method->isInstance()) valueKind = VK_RValue; |
| 10631 | |
| 10632 | // This is true because FunctionDecls must always have function |
| 10633 | // type, so we can't be resolving the entire thing at once. |
| 10634 | assert(type->isFunctionType()); |
| 10635 | |
| 10636 | // Function references aren't l-values in C. |
| 10637 | if (!S.getLangOptions().CPlusPlus) |
| 10638 | valueKind = VK_RValue; |
| 10639 | |
| 10640 | // - variables |
| 10641 | } else if (isa<VarDecl>(decl)) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10642 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 10643 | type = refTy->getPointeeType(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10644 | } else if (type->isFunctionType()) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10645 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10646 | << decl << expr->getSourceRange(); |
| 10647 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10648 | } |
| 10649 | |
| 10650 | // - nothing else |
| 10651 | } else { |
| 10652 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10653 | << decl << expr->getSourceRange(); |
| 10654 | return ExprError(); |
| 10655 | } |
| 10656 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10657 | decl->setType(DestType); |
| 10658 | expr->setType(type); |
| 10659 | expr->setValueKind(valueKind); |
| 10660 | return S.Owned(expr); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10661 | } |
| 10662 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10663 | /// Check a cast of an unknown-any type. We intentionally only |
| 10664 | /// trigger this for C-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10665 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 10666 | Expr *castExpr, CastKind &castKind, |
| 10667 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10668 | // Rewrite the casted expression from scratch. |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10669 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 10670 | if (!result.isUsable()) return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10671 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10672 | castExpr = result.take(); |
| 10673 | VK = castExpr->getValueKind(); |
| 10674 | castKind = CK_NoOp; |
| 10675 | |
| 10676 | return castExpr; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10677 | } |
| 10678 | |
| 10679 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 10680 | Expr *orig = e; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10681 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10682 | while (true) { |
| 10683 | e = e->IgnoreParenImpCasts(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10684 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10685 | e = call->getCallee(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10686 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10687 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10688 | break; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10689 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10690 | } |
| 10691 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10692 | SourceLocation loc; |
| 10693 | NamedDecl *d; |
| 10694 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 10695 | loc = ref->getLocation(); |
| 10696 | d = ref->getDecl(); |
| 10697 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 10698 | loc = mem->getMemberLoc(); |
| 10699 | d = mem->getMemberDecl(); |
| 10700 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 10701 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10702 | loc = msg->getSelectorLoc(); |
| 10703 | d = msg->getMethodDecl(); |
| 10704 | assert(d && "unknown method returning __unknown_any?"); |
| 10705 | } else { |
| 10706 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10707 | << e->getSourceRange(); |
| 10708 | return ExprError(); |
| 10709 | } |
| 10710 | |
| 10711 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10712 | |
| 10713 | // Never recoverable. |
| 10714 | return ExprError(); |
| 10715 | } |
| 10716 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10717 | /// Check for operands with placeholder types and complain if found. |
| 10718 | /// Returns true if there was an error and no recovery was possible. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10719 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10720 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10721 | QualType type = E->getType(); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10722 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10723 | // Overloaded expressions. |
| 10724 | if (type == Context.OverloadTy) |
| 10725 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10726 | E->getSourceRange(), |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10727 | QualType(), |
| 10728 | diag::err_ovl_unresolvable); |
| 10729 | |
| 10730 | // Expressions of unknown type. |
| 10731 | if (type == Context.UnknownAnyTy) |
| 10732 | return diagnoseUnknownAnyExpr(*this, E); |
| 10733 | |
| 10734 | assert(!type->isPlaceholderType()); |
| 10735 | return Owned(E); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10736 | } |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame^] | 10737 | |
| 10738 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 10739 | if (expr->isTypeDependent()) |
| 10740 | return true; |
| 10741 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 10742 | return expr->getType()->isIntegralOrEnumerationType(); |
| 10743 | return false; |
| 10744 | } |