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 |
| 71 | // them again for this specialization. However, we don't remove this |
| 72 | // entry from the table, because we want to avoid ever emitting these |
| 73 | // diagnostics again. |
| 74 | Suppressed.clear(); |
| 75 | } |
| 76 | } |
| 77 | |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 78 | // See if the decl is deprecated. |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 79 | if (const DeprecatedAttr *DA = D->getAttr<DeprecatedAttr>()) |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 80 | EmitDeprecationWarning(D, DA->getMessage(), Loc, UnknownObjCClass); |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 81 | |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 82 | // See if the decl is unavailable |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 83 | if (const UnavailableAttr *UA = D->getAttr<UnavailableAttr>()) { |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 84 | if (UA->getMessage().empty()) { |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 85 | if (!UnknownObjCClass) |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 86 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 87 | else |
| 88 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 89 | << D->getDeclName(); |
| 90 | } |
| 91 | else |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 92 | Diag(Loc, diag::err_unavailable_message) |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 93 | << D->getDeclName() << UA->getMessage(); |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 94 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 95 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 97 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 98 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 99 | if (FD->isDeleted()) { |
| 100 | Diag(Loc, diag::err_deleted_function_use); |
| 101 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 102 | return true; |
| 103 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 104 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 105 | |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 106 | // Warn if this is used but marked unused. |
| 107 | if (D->hasAttr<UnusedAttr>()) |
| 108 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 109 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 110 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 113 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 115 | /// attribute. It warns if call does not have the sentinel argument. |
| 116 | /// |
| 117 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 119 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 121 | return; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 122 | |
| 123 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 124 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 125 | int sentinelPos = attr->getSentinel(); |
| 126 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 128 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 129 | // 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] | 130 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 131 | bool warnNotEnoughArgs = false; |
| 132 | int isMethod = 0; |
| 133 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 134 | // skip over named parameters. |
| 135 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 136 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 137 | if (nullPos) |
| 138 | --nullPos; |
| 139 | else |
| 140 | ++i; |
| 141 | } |
| 142 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 143 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 144 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 145 | // skip over named parameters. |
| 146 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 147 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 148 | if (nullPos) |
| 149 | --nullPos; |
| 150 | else |
| 151 | ++i; |
| 152 | } |
| 153 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 154 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 155 | // block or function pointer call. |
| 156 | QualType Ty = V->getType(); |
| 157 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 158 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 159 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 160 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 161 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 162 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 163 | unsigned k; |
| 164 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 165 | if (nullPos) |
| 166 | --nullPos; |
| 167 | else |
| 168 | ++i; |
| 169 | } |
| 170 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 171 | } |
| 172 | if (Ty->isBlockPointerType()) |
| 173 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 174 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 175 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 176 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 177 | return; |
| 178 | |
| 179 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 180 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 181 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | int sentinel = i; |
| 185 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 186 | --sentinelPos; |
| 187 | ++i; |
| 188 | } |
| 189 | if (sentinelPos > 0) { |
| 190 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 191 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | while (i < NumArgs-1) { |
| 195 | ++i; |
| 196 | ++sentinel; |
| 197 | } |
| 198 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 199 | if (!sentinelExpr) return; |
| 200 | if (sentinelExpr->isTypeDependent()) return; |
| 201 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | e981a8c | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 202 | |
| 203 | // nullptr_t is always treated as null. |
| 204 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 205 | |
Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 206 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 207 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 208 | Expr::NPC_ValueDependentIsNull)) |
| 209 | return; |
| 210 | |
| 211 | // Unfortunately, __null has type 'int'. |
| 212 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 213 | |
| 214 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 215 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 218 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 219 | Expr *Ex = (Expr *)E; |
| 220 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 221 | } |
| 222 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 223 | //===----------------------------------------------------------------------===// |
| 224 | // Standard Promotions and Conversions |
| 225 | //===----------------------------------------------------------------------===// |
| 226 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 227 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 228 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 229 | QualType Ty = E->getType(); |
| 230 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 231 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 232 | if (Ty->isFunctionType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | ImpCastExprToType(E, Context.getPointerType(Ty), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 234 | CK_FunctionToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 235 | else if (Ty->isArrayType()) { |
| 236 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 237 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 238 | // type 'array of type' is converted to an expression that has type 'pointer |
| 239 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 240 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 241 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 242 | // |
| 243 | // C++ 4.2p1: |
| 244 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 245 | // T" can be converted to an rvalue of type "pointer to T". |
| 246 | // |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 247 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
Anders Carlsson | 8fc489d | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 248 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 249 | CK_ArrayToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 250 | } |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 251 | } |
| 252 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 253 | void Sema::DefaultLvalueConversion(Expr *&E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 254 | // C++ [conv.lval]p1: |
| 255 | // A glvalue of a non-function, non-array type T can be |
| 256 | // converted to a prvalue. |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 257 | if (!E->isGLValue()) return; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 258 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 259 | QualType T = E->getType(); |
| 260 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 261 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 262 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 263 | if (E->getObjectKind() == OK_ObjCProperty) { |
| 264 | ConvertPropertyForRValue(E); |
| 265 | if (!E->isGLValue()) |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 266 | return; |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 267 | } |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 268 | |
| 269 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 270 | // expressions of certain types in C++. |
| 271 | if (getLangOptions().CPlusPlus && |
| 272 | (E->getType() == Context.OverloadTy || |
| 273 | T->isDependentType() || |
| 274 | T->isRecordType())) |
| 275 | return; |
| 276 | |
| 277 | // The C standard is actually really unclear on this point, and |
| 278 | // DR106 tells us what the result should be but not why. It's |
| 279 | // generally best to say that void types just doesn't undergo |
| 280 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 281 | // 'void' type are never l-values, but qualified void can be. |
| 282 | if (T->isVoidType()) |
| 283 | return; |
| 284 | |
| 285 | // C++ [conv.lval]p1: |
| 286 | // [...] If T is a non-class type, the type of the prvalue is the |
| 287 | // cv-unqualified version of T. Otherwise, the type of the |
| 288 | // rvalue is T. |
| 289 | // |
| 290 | // C99 6.3.2.1p2: |
| 291 | // If the lvalue has qualified type, the value has the unqualified |
| 292 | // version of the type of the lvalue; otherwise, the value has the |
| 293 | // type of the lvalue. |
| 294 | if (T.hasQualifiers()) |
| 295 | T = T.getUnqualifiedType(); |
| 296 | |
| 297 | E = ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 298 | E, 0, VK_RValue); |
| 299 | } |
| 300 | |
| 301 | void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) { |
| 302 | DefaultFunctionArrayConversion(E); |
| 303 | DefaultLvalueConversion(E); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 307 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 309 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 310 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 311 | /// In these instances, this routine should *not* be called. |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 312 | Expr *Sema::UsualUnaryConversions(Expr *&E) { |
| 313 | // First, convert to an r-value. |
| 314 | DefaultFunctionArrayLvalueConversion(E); |
| 315 | |
| 316 | QualType Ty = E->getType(); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 317 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 318 | |
| 319 | // Try to perform integral promotions if the object has a theoretically |
| 320 | // promotable type. |
| 321 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 322 | // C99 6.3.1.1p2: |
| 323 | // |
| 324 | // The following may be used in an expression wherever an int or |
| 325 | // unsigned int may be used: |
| 326 | // - an object or expression with an integer type whose integer |
| 327 | // conversion rank is less than or equal to the rank of int |
| 328 | // and unsigned int. |
| 329 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 330 | // |
| 331 | // If an int can represent all values of the original type, the |
| 332 | // value is converted to an int; otherwise, it is converted to an |
| 333 | // unsigned int. These are called the integer promotions. All |
| 334 | // other types are unchanged by the integer promotions. |
| 335 | |
| 336 | QualType PTy = Context.isPromotableBitField(E); |
| 337 | if (!PTy.isNull()) { |
| 338 | ImpCastExprToType(E, PTy, CK_IntegralCast); |
| 339 | return E; |
| 340 | } |
| 341 | if (Ty->isPromotableIntegerType()) { |
| 342 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 343 | ImpCastExprToType(E, PT, CK_IntegralCast); |
| 344 | return E; |
| 345 | } |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 346 | } |
| 347 | |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 348 | return E; |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 351 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | /// 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] | 353 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 354 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 355 | QualType Ty = Expr->getType(); |
| 356 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 358 | UsualUnaryConversions(Expr); |
| 359 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 360 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 361 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 362 | return ImpCastExprToType(Expr, Context.DoubleTy, CK_FloatingCast); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 365 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 366 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 367 | /// interfaces passed by value. This returns true if the argument type is |
| 368 | /// completely illegal. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 369 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT, |
| 370 | FunctionDecl *FDecl) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 371 | DefaultArgumentPromotion(Expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 373 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 374 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 375 | // etc. |
| 376 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
| 377 | return false; |
| 378 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 379 | if (Expr->getType()->isObjCObjectType() && |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 380 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 381 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 382 | << Expr->getType() << CT)) |
| 383 | return true; |
Douglas Gregor | 7ca84af | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 385 | if (!Expr->getType()->isPODType() && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 386 | DiagRuntimeBehavior(Expr->getLocStart(), |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 387 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 388 | << Expr->getType() << CT)) |
| 389 | return true; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 390 | |
| 391 | return false; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 394 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 395 | /// 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] | 396 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 397 | /// responsible for emitting appropriate error diagnostics. |
| 398 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 399 | /// GCC. |
| 400 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 401 | bool isCompAssign) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 402 | if (!isCompAssign) |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 403 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 404 | |
| 405 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 406 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 407 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 408 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 409 | QualType lhs = |
| 410 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 411 | QualType rhs = |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 412 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 413 | |
| 414 | // If both types are identical, no conversion is needed. |
| 415 | if (lhs == rhs) |
| 416 | return lhs; |
| 417 | |
| 418 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 419 | // The caller can deal with this (e.g. pointer + int). |
| 420 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 421 | return lhs; |
| 422 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 423 | // Apply unary and bitfield promotions to the LHS's type. |
| 424 | QualType lhs_unpromoted = lhs; |
| 425 | if (lhs->isPromotableIntegerType()) |
| 426 | lhs = Context.getPromotedIntegerType(lhs); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 427 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 428 | if (!LHSBitfieldPromoteTy.isNull()) |
| 429 | lhs = LHSBitfieldPromoteTy; |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 430 | if (lhs != lhs_unpromoted && !isCompAssign) |
| 431 | ImpCastExprToType(lhsExpr, lhs, CK_IntegralCast); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 432 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 433 | // If both types are identical, no conversion is needed. |
| 434 | if (lhs == rhs) |
| 435 | return lhs; |
| 436 | |
| 437 | // At this point, we have two different arithmetic types. |
| 438 | |
| 439 | // Handle complex types first (C99 6.3.1.8p1). |
| 440 | bool LHSComplexFloat = lhs->isComplexType(); |
| 441 | bool RHSComplexFloat = rhs->isComplexType(); |
| 442 | if (LHSComplexFloat || RHSComplexFloat) { |
| 443 | // if we have an integer operand, the result is the complex type. |
| 444 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 445 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 446 | if (rhs->isIntegerType()) { |
| 447 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 448 | ImpCastExprToType(rhsExpr, fp, CK_IntegralToFloating); |
| 449 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
| 450 | } else { |
| 451 | assert(rhs->isComplexIntegerType()); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 452 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 453 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 454 | return lhs; |
| 455 | } |
| 456 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 457 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 458 | if (!isCompAssign) { |
| 459 | // int -> float -> _Complex float |
| 460 | if (lhs->isIntegerType()) { |
| 461 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 462 | ImpCastExprToType(lhsExpr, fp, CK_IntegralToFloating); |
| 463 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 464 | } else { |
| 465 | assert(lhs->isComplexIntegerType()); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 466 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 467 | } |
| 468 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 469 | return rhs; |
| 470 | } |
| 471 | |
| 472 | // This handles complex/complex, complex/float, or float/complex. |
| 473 | // When both operands are complex, the shorter operand is converted to the |
| 474 | // type of the longer, and that is the type of the result. This corresponds |
| 475 | // to what is done when combining two real floating-point operands. |
| 476 | // The fun begins when size promotion occur across type domains. |
| 477 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 478 | // floating-point type, the less precise type is converted, within it's |
| 479 | // real or complex domain, to the precision of the other type. For example, |
| 480 | // when combining a "long double" with a "double _Complex", the |
| 481 | // "double _Complex" is promoted to "long double _Complex". |
| 482 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 483 | |
| 484 | // If both are complex, just cast to the more precise type. |
| 485 | if (LHSComplexFloat && RHSComplexFloat) { |
| 486 | if (order > 0) { |
| 487 | // _Complex float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 488 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 489 | return lhs; |
| 490 | |
| 491 | } else if (order < 0) { |
| 492 | // _Complex float -> _Complex double |
| 493 | if (!isCompAssign) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 494 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 495 | return rhs; |
| 496 | } |
| 497 | return lhs; |
| 498 | } |
| 499 | |
| 500 | // If just the LHS is complex, the RHS needs to be converted, |
| 501 | // and the LHS might need to be promoted. |
| 502 | if (LHSComplexFloat) { |
| 503 | if (order > 0) { // LHS is wider |
| 504 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 505 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| 506 | ImpCastExprToType(rhsExpr, fp, CK_FloatingCast); |
| 507 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 508 | return lhs; |
| 509 | } |
| 510 | |
| 511 | // RHS is at least as wide. Find its corresponding complex type. |
| 512 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 513 | |
| 514 | // double -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 515 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 516 | |
| 517 | // _Complex float -> _Complex double |
| 518 | if (!isCompAssign && order < 0) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 519 | ImpCastExprToType(lhsExpr, result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 520 | |
| 521 | return result; |
| 522 | } |
| 523 | |
| 524 | // Just the RHS is complex, so the LHS needs to be converted |
| 525 | // and the RHS might need to be promoted. |
| 526 | assert(RHSComplexFloat); |
| 527 | |
| 528 | if (order < 0) { // RHS is wider |
| 529 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 530 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e84389b | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 531 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| 532 | ImpCastExprToType(lhsExpr, fp, CK_FloatingCast); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 533 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingRealToComplex); |
| 534 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 535 | return rhs; |
| 536 | } |
| 537 | |
| 538 | // LHS is at least as wide. Find its corresponding complex type. |
| 539 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 540 | |
| 541 | // double -> _Complex double |
| 542 | if (!isCompAssign) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 543 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 544 | |
| 545 | // _Complex float -> _Complex double |
| 546 | if (order > 0) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 547 | ImpCastExprToType(rhsExpr, result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 548 | |
| 549 | return result; |
| 550 | } |
| 551 | |
| 552 | // Now handle "real" floating types (i.e. float, double, long double). |
| 553 | bool LHSFloat = lhs->isRealFloatingType(); |
| 554 | bool RHSFloat = rhs->isRealFloatingType(); |
| 555 | if (LHSFloat || RHSFloat) { |
| 556 | // If we have two real floating types, convert the smaller operand |
| 557 | // to the bigger result. |
| 558 | if (LHSFloat && RHSFloat) { |
| 559 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 560 | if (order > 0) { |
| 561 | ImpCastExprToType(rhsExpr, lhs, CK_FloatingCast); |
| 562 | return lhs; |
| 563 | } |
| 564 | |
| 565 | assert(order < 0 && "illegal float comparison"); |
| 566 | if (!isCompAssign) |
| 567 | ImpCastExprToType(lhsExpr, rhs, CK_FloatingCast); |
| 568 | return rhs; |
| 569 | } |
| 570 | |
| 571 | // If we have an integer operand, the result is the real floating type. |
| 572 | if (LHSFloat) { |
| 573 | if (rhs->isIntegerType()) { |
| 574 | // Convert rhs to the lhs floating point type. |
| 575 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralToFloating); |
| 576 | return lhs; |
| 577 | } |
| 578 | |
| 579 | // Convert both sides to the appropriate complex float. |
| 580 | assert(rhs->isComplexIntegerType()); |
| 581 | QualType result = Context.getComplexType(lhs); |
| 582 | |
| 583 | // _Complex int -> _Complex float |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 584 | ImpCastExprToType(rhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 585 | |
| 586 | // float -> _Complex float |
| 587 | if (!isCompAssign) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 588 | ImpCastExprToType(lhsExpr, result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 589 | |
| 590 | return result; |
| 591 | } |
| 592 | |
| 593 | assert(RHSFloat); |
| 594 | if (lhs->isIntegerType()) { |
| 595 | // Convert lhs to the rhs floating point type. |
| 596 | if (!isCompAssign) |
| 597 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralToFloating); |
| 598 | return rhs; |
| 599 | } |
| 600 | |
| 601 | // Convert both sides to the appropriate complex float. |
| 602 | assert(lhs->isComplexIntegerType()); |
| 603 | QualType result = Context.getComplexType(rhs); |
| 604 | |
| 605 | // _Complex int -> _Complex float |
| 606 | if (!isCompAssign) |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 607 | ImpCastExprToType(lhsExpr, result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 608 | |
| 609 | // float -> _Complex float |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 610 | ImpCastExprToType(rhsExpr, result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 611 | |
| 612 | return result; |
| 613 | } |
| 614 | |
| 615 | // Handle GCC complex int extension. |
| 616 | // FIXME: if the operands are (int, _Complex long), we currently |
| 617 | // don't promote the complex. Also, signedness? |
| 618 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 619 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 620 | if (lhsComplexInt && rhsComplexInt) { |
| 621 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 622 | rhsComplexInt->getElementType()); |
| 623 | assert(order && "inequal types with equal element ordering"); |
| 624 | if (order > 0) { |
| 625 | // _Complex int -> _Complex long |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 626 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 627 | return lhs; |
| 628 | } |
| 629 | |
| 630 | if (!isCompAssign) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 631 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 632 | return rhs; |
| 633 | } else if (lhsComplexInt) { |
| 634 | // int -> _Complex int |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 635 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 636 | return lhs; |
| 637 | } else if (rhsComplexInt) { |
| 638 | // int -> _Complex int |
| 639 | if (!isCompAssign) |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 640 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 641 | return rhs; |
| 642 | } |
| 643 | |
| 644 | // Finally, we have two differing integer types. |
| 645 | // The rules for this case are in C99 6.3.1.8 |
| 646 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 647 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 648 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 649 | if (lhsSigned == rhsSigned) { |
| 650 | // Same signedness; use the higher-ranked type |
| 651 | if (compare >= 0) { |
| 652 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 653 | return lhs; |
| 654 | } else if (!isCompAssign) |
| 655 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 656 | return rhs; |
| 657 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 658 | // The unsigned type has greater than or equal rank to the |
| 659 | // signed type, so use the unsigned type |
| 660 | if (rhsSigned) { |
| 661 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 662 | return lhs; |
| 663 | } else if (!isCompAssign) |
| 664 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 665 | return rhs; |
| 666 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 667 | // The two types are different widths; if we are here, that |
| 668 | // means the signed type is larger than the unsigned type, so |
| 669 | // use the signed type. |
| 670 | if (lhsSigned) { |
| 671 | ImpCastExprToType(rhsExpr, lhs, CK_IntegralCast); |
| 672 | return lhs; |
| 673 | } else if (!isCompAssign) |
| 674 | ImpCastExprToType(lhsExpr, rhs, CK_IntegralCast); |
| 675 | return rhs; |
| 676 | } else { |
| 677 | // The signed type is higher-ranked than the unsigned type, |
| 678 | // but isn't actually any bigger (like unsigned int and long |
| 679 | // on most 32-bit systems). Use the unsigned type corresponding |
| 680 | // to the signed type. |
| 681 | QualType result = |
| 682 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 683 | ImpCastExprToType(rhsExpr, result, CK_IntegralCast); |
| 684 | if (!isCompAssign) |
| 685 | ImpCastExprToType(lhsExpr, result, CK_IntegralCast); |
| 686 | return result; |
| 687 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 690 | //===----------------------------------------------------------------------===// |
| 691 | // Semantic Analysis for various Expression Types |
| 692 | //===----------------------------------------------------------------------===// |
| 693 | |
| 694 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 695 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 696 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 697 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 698 | /// multiple tokens. However, the common case is that StringToks points to one |
| 699 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 700 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 701 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 702 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 703 | assert(NumStringToks && "Must have at least one string!"); |
| 704 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 705 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 706 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 707 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 709 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 710 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 711 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 712 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 713 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | cbad725 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 714 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 715 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 716 | |
| 717 | // 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] | 718 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 719 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 721 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 722 | // the nul terminator character as well as the string length for pascal |
| 723 | // strings. |
| 724 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 725 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 726 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 727 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 728 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 729 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 730 | Literal.GetStringLength(), |
| 731 | Literal.AnyWide, StrTy, |
| 732 | &StringTokLocs[0], |
| 733 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 736 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 737 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 738 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 739 | /// for values inside the block or for globals). |
| 740 | /// |
Douglas Gregor | 4f13beb | 2010-03-01 20:44:28 +0000 | [diff] [blame] | 741 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockScopeInfo records |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 742 | /// up-to-date. |
| 743 | /// |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 744 | static bool ShouldSnapshotBlockValueReference(Sema &S, BlockScopeInfo *CurBlock, |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 745 | ValueDecl *VD) { |
| 746 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 747 | // we wanted to. |
| 748 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 749 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 750 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 751 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 752 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 753 | return false; |
| 754 | |
| 755 | // If this is a reference to an extern, static, or global variable, no need to |
| 756 | // snapshot it. |
| 757 | // FIXME: What about 'const' variables in C++? |
| 758 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 759 | if (!Var->hasLocalStorage()) |
| 760 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 762 | // Blocks that have these can't be constant. |
| 763 | CurBlock->hasBlockDeclRefExprs = true; |
| 764 | |
| 765 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 766 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 767 | // be defined outside all of the current blocks (in which case the blocks do |
| 768 | // all get the bit). Walk the nesting chain. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 769 | for (unsigned I = S.FunctionScopes.size() - 1; I; --I) { |
| 770 | BlockScopeInfo *NextBlock = dyn_cast<BlockScopeInfo>(S.FunctionScopes[I]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 771 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 772 | if (!NextBlock) |
| 773 | continue; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 774 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 775 | // If we found the defining block for the variable, don't mark the block as |
| 776 | // having a reference outside it. |
| 777 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 778 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 779 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 780 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 781 | // a snapshot as well. |
| 782 | NextBlock->hasBlockDeclRefExprs = true; |
| 783 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 785 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 788 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 789 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 790 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
| 791 | SourceLocation Loc, const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 792 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 793 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 797 | ExprResult |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 798 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 799 | ExprValueKind VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 800 | const DeclarationNameInfo &NameInfo, |
| 801 | const CXXScopeSpec *SS) { |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 802 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 803 | Diag(NameInfo.getLoc(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 805 | << D->getDeclName(); |
| 806 | return ExprError(); |
| 807 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 809 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 1524333 | 2010-04-27 21:10:04 +0000 | [diff] [blame] | 810 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 811 | // Non-type template parameters can be referenced anywhere they are |
| 812 | // visible. |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 813 | Ty = Ty.getNonLValueExprType(Context); |
Douglas Gregor | 1524333 | 2010-04-27 21:10:04 +0000 | [diff] [blame] | 814 | } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 815 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 816 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 817 | Diag(NameInfo.getLoc(), |
| 818 | diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 819 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 821 | << D->getIdentifier(); |
| 822 | return ExprError(); |
| 823 | } |
| 824 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 825 | |
| 826 | // This ridiculousness brought to you by 'extern void x;' and the |
| 827 | // GNU compiler collection. |
| 828 | } else if (!getLangOptions().CPlusPlus && !Ty.hasQualifiers() && |
| 829 | Ty->isVoidType()) { |
| 830 | VK = VK_RValue; |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 833 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 834 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 835 | |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 836 | Expr *E = DeclRefExpr::Create(Context, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 837 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 838 | SS? SS->getRange() : SourceRange(), |
| 839 | D, NameInfo, Ty, VK); |
| 840 | |
| 841 | // Just in case we're building an illegal pointer-to-member. |
| 842 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 843 | E->setObjectKind(OK_BitField); |
| 844 | |
| 845 | return Owned(E); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 846 | } |
| 847 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 848 | static ExprResult |
| 849 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 850 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 851 | DeclAccessPair FoundDecl, |
| 852 | const DeclarationNameInfo &MemberNameInfo); |
| 853 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 854 | ExprResult |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 855 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 856 | IndirectFieldDecl *IndirectField, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 857 | Expr *BaseObjectExpr, |
| 858 | SourceLocation OpLoc) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 859 | // Build the expression that refers to the base object, from |
| 860 | // which we will build a sequence of member references to each |
| 861 | // of the anonymous union objects and, eventually, the field we |
| 862 | // found via name lookup. |
| 863 | bool BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 864 | Qualifiers BaseQuals; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 865 | VarDecl *BaseObject = IndirectField->getVarDecl(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 866 | if (BaseObject) { |
| 867 | // BaseObject is an anonymous struct/union variable (and is, |
| 868 | // therefore, not part of another non-anonymous record). |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 869 | MarkDeclarationReferenced(Loc, BaseObject); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 870 | BaseObjectExpr = |
| 871 | new (Context) DeclRefExpr(BaseObject, BaseObject->getType(), |
| 872 | VK_LValue, Loc); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 873 | BaseQuals |
| 874 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 875 | } else if (BaseObjectExpr) { |
| 876 | // The caller provided the base object expression. Determine |
| 877 | // whether its a pointer and whether it adds any qualifiers to the |
| 878 | // anonymous struct/union fields we're looking into. |
| 879 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 880 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 881 | BaseObjectIsPointer = true; |
| 882 | ObjectType = ObjectPtr->getPointeeType(); |
| 883 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 884 | BaseQuals |
| 885 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 886 | } else { |
| 887 | // We've found a member of an anonymous struct/union that is |
| 888 | // inside a non-anonymous struct/union, so in a well-formed |
| 889 | // program our base object expression is "this". |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 890 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 891 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 892 | if (!MD->isStatic()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | QualType AnonFieldType |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 894 | = Context.getTagDeclType( |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 895 | cast<RecordDecl>( |
| 896 | (*IndirectField->chain_begin())->getDeclContext())); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 897 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 898 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 899 | == Context.getCanonicalType(ThisType)) || |
| 900 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 901 | // Our base object expression is "this". |
Douglas Gregor | 4b65441 | 2009-12-24 20:23:34 +0000 | [diff] [blame] | 902 | BaseObjectExpr = new (Context) CXXThisExpr(Loc, |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 903 | MD->getThisType(Context), |
| 904 | /*isImplicit=*/true); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 905 | BaseObjectIsPointer = true; |
| 906 | } |
| 907 | } else { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 908 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 909 | << IndirectField->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 910 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 911 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | if (!BaseObjectExpr) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 915 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 916 | << IndirectField->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | // Build the implicit member references to the field of the |
| 920 | // anonymous struct/union. |
| 921 | Expr *Result = BaseObjectExpr; |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 922 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 923 | IndirectFieldDecl::chain_iterator FI = IndirectField->chain_begin(), |
| 924 | FEnd = IndirectField->chain_end(); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 925 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 926 | // Skip the first VarDecl if present. |
| 927 | if (BaseObject) |
| 928 | FI++; |
| 929 | for (; FI != FEnd; FI++) { |
| 930 | FieldDecl *Field = cast<FieldDecl>(*FI); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 931 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 932 | // FIXME: the first access can be qualified |
| 933 | CXXScopeSpec SS; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 934 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 935 | // FIXME: these are somewhat meaningless |
| 936 | DeclarationNameInfo MemberNameInfo(Field->getDeclName(), Loc); |
| 937 | DeclAccessPair FoundDecl = DeclAccessPair::make(Field, Field->getAccess()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 938 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 939 | Result = BuildFieldReferenceExpr(*this, Result, BaseObjectIsPointer, |
| 940 | SS, Field, FoundDecl, MemberNameInfo) |
| 941 | .take(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 942 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 943 | // All the implicit accesses are dot-accesses. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 944 | BaseObjectIsPointer = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 947 | return Owned(Result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 950 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 951 | /// possibly a list of template arguments. |
| 952 | /// |
| 953 | /// If this produces template arguments, it is permitted to call |
| 954 | /// DecomposeTemplateName. |
| 955 | /// |
| 956 | /// This actually loses a lot of source location information for |
| 957 | /// non-standard name kinds; we should consider preserving that in |
| 958 | /// some way. |
| 959 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 960 | const UnqualifiedId &Id, |
| 961 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 962 | DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 963 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 964 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 965 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 966 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 967 | |
| 968 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 969 | Id.TemplateId->getTemplateArgs(), |
| 970 | Id.TemplateId->NumArgs); |
| 971 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 972 | TemplateArgsPtr.release(); |
| 973 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 974 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 975 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 976 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 977 | TemplateArgs = &Buffer; |
| 978 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 979 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 980 | TemplateArgs = 0; |
| 981 | } |
| 982 | } |
| 983 | |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 984 | /// Determines whether the given record is "fully-formed" at the given |
| 985 | /// location, i.e. whether a qualified lookup into it is assured of |
| 986 | /// getting consistent results already. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 987 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 988 | if (!Record->hasDefinition()) |
| 989 | return false; |
| 990 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 991 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 992 | E = Record->bases_end(); I != E; ++I) { |
| 993 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 994 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 995 | if (!BaseRT) return false; |
| 996 | |
| 997 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 998 | if (!BaseRecord->hasDefinition() || |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 999 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 1000 | return false; |
| 1001 | } |
| 1002 | |
| 1003 | return true; |
| 1004 | } |
| 1005 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1006 | /// Determines if the given class is provably not derived from all of |
| 1007 | /// the prospective base classes. |
| 1008 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1009 | CXXRecordDecl *Record, |
| 1010 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1011 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1012 | return false; |
| 1013 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1014 | RecordDecl *RD = Record->getDefinition(); |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1015 | if (!RD) return false; |
| 1016 | Record = cast<CXXRecordDecl>(RD); |
| 1017 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1018 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1019 | E = Record->bases_end(); I != E; ++I) { |
| 1020 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1021 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1022 | if (!BaseRT) return false; |
| 1023 | |
| 1024 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1025 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1026 | return false; |
| 1027 | } |
| 1028 | |
| 1029 | return true; |
| 1030 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1031 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1032 | enum IMAKind { |
| 1033 | /// The reference is definitely not an instance member access. |
| 1034 | IMA_Static, |
| 1035 | |
| 1036 | /// The reference may be an implicit instance member access. |
| 1037 | IMA_Mixed, |
| 1038 | |
| 1039 | /// The reference may be to an instance member, but it is invalid if |
| 1040 | /// so, because the context is not an instance method. |
| 1041 | IMA_Mixed_StaticContext, |
| 1042 | |
| 1043 | /// The reference may be to an instance member, but it is invalid if |
| 1044 | /// so, because the context is from an unrelated class. |
| 1045 | IMA_Mixed_Unrelated, |
| 1046 | |
| 1047 | /// The reference is definitely an implicit instance member access. |
| 1048 | IMA_Instance, |
| 1049 | |
| 1050 | /// The reference may be to an unresolved using declaration. |
| 1051 | IMA_Unresolved, |
| 1052 | |
| 1053 | /// The reference may be to an unresolved using declaration and the |
| 1054 | /// context is not an instance method. |
| 1055 | IMA_Unresolved_StaticContext, |
| 1056 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1057 | /// All possible referrents are instance members and the current |
| 1058 | /// context is not an instance method. |
| 1059 | IMA_Error_StaticContext, |
| 1060 | |
| 1061 | /// All possible referrents are instance members of an unrelated |
| 1062 | /// class. |
| 1063 | IMA_Error_Unrelated |
| 1064 | }; |
| 1065 | |
| 1066 | /// The given lookup names class member(s) and is not being used for |
| 1067 | /// an address-of-member expression. Classify the type of access |
| 1068 | /// according to whether it's possible that this reference names an |
| 1069 | /// instance member. This is best-effort; it is okay to |
| 1070 | /// conservatively answer "yes", in which case some errors will simply |
| 1071 | /// not be caught until template-instantiation. |
| 1072 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 1073 | const LookupResult &R) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1074 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1075 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1076 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1077 | bool isStaticContext = |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1078 | (!isa<CXXMethodDecl>(DC) || |
| 1079 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1080 | |
| 1081 | if (R.isUnresolvableResult()) |
| 1082 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1083 | |
| 1084 | // Collect all the declaring classes of instance members we find. |
| 1085 | bool hasNonInstance = false; |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1086 | bool hasField = false; |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1087 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1088 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1089 | NamedDecl *D = *I; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1090 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1091 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1092 | if (dyn_cast<FieldDecl>(D)) |
| 1093 | hasField = true; |
| 1094 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1095 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1096 | Classes.insert(R->getCanonicalDecl()); |
| 1097 | } |
| 1098 | else |
| 1099 | hasNonInstance = true; |
| 1100 | } |
| 1101 | |
| 1102 | // If we didn't find any instance members, it can't be an implicit |
| 1103 | // member reference. |
| 1104 | if (Classes.empty()) |
| 1105 | return IMA_Static; |
| 1106 | |
| 1107 | // If the current context is not an instance method, it can't be |
| 1108 | // an implicit member reference. |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1109 | if (isStaticContext) { |
| 1110 | if (hasNonInstance) |
| 1111 | return IMA_Mixed_StaticContext; |
| 1112 | |
| 1113 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1114 | // C++0x [expr.prim.general]p10: |
| 1115 | // An id-expression that denotes a non-static data member or non-static |
| 1116 | // member function of a class can only be used: |
| 1117 | // (...) |
| 1118 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1119 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1120 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1121 | if (isUnevaluatedExpression) |
| 1122 | return IMA_Mixed_StaticContext; |
| 1123 | } |
| 1124 | |
| 1125 | return IMA_Error_StaticContext; |
| 1126 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1127 | |
| 1128 | // If we can prove that the current context is unrelated to all the |
| 1129 | // declaring classes, it can't be an implicit member reference (in |
| 1130 | // which case it's an error if any of those members are selected). |
| 1131 | if (IsProvablyNotDerivedFrom(SemaRef, |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1132 | cast<CXXMethodDecl>(DC)->getParent(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1133 | Classes)) |
| 1134 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1135 | |
| 1136 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1137 | } |
| 1138 | |
| 1139 | /// Diagnose a reference to a field with no object available. |
| 1140 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1141 | const CXXScopeSpec &SS, |
| 1142 | const LookupResult &R) { |
| 1143 | SourceLocation Loc = R.getNameLoc(); |
| 1144 | SourceRange Range(Loc); |
| 1145 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1146 | |
| 1147 | if (R.getAsSingle<FieldDecl>()) { |
| 1148 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1149 | if (MD->isStatic()) { |
| 1150 | // "invalid use of member 'x' in static member function" |
| 1151 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 1152 | << Range << R.getLookupName(); |
| 1153 | return; |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1158 | << R.getLookupName() << Range; |
| 1159 | return; |
| 1160 | } |
| 1161 | |
| 1162 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1165 | /// Diagnose an empty lookup. |
| 1166 | /// |
| 1167 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1168 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1169 | CorrectTypoContext CTC) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1170 | DeclarationName Name = R.getLookupName(); |
| 1171 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1172 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1173 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1174 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1175 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1176 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1177 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1178 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1179 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1180 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1181 | // If the original lookup was an unqualified lookup, fake an |
| 1182 | // unqualified lookup. This is useful when (for example) the |
| 1183 | // original lookup would not have found something because it was a |
| 1184 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1185 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1186 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1187 | if (isa<CXXRecordDecl>(DC)) { |
| 1188 | LookupQualifiedName(R, DC); |
| 1189 | |
| 1190 | if (!R.empty()) { |
| 1191 | // Don't give errors about ambiguities in this lookup. |
| 1192 | R.suppressDiagnostics(); |
| 1193 | |
| 1194 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1195 | bool isInstance = CurMethod && |
| 1196 | CurMethod->isInstance() && |
| 1197 | DC == CurMethod->getParent(); |
| 1198 | |
| 1199 | // Give a code modification hint to insert 'this->'. |
| 1200 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1201 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1202 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1203 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1204 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1205 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1206 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1207 | if (DepMethod) { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1208 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1209 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1210 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1211 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1212 | R.getNameLoc(), DepThisType, false); |
| 1213 | TemplateArgumentListInfo TList; |
| 1214 | if (ULE->hasExplicitTemplateArgs()) |
| 1215 | ULE->copyTemplateArgumentsInto(TList); |
| 1216 | CXXDependentScopeMemberExpr *DepExpr = |
| 1217 | CXXDependentScopeMemberExpr::Create( |
| 1218 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 1219 | ULE->getQualifier(), ULE->getQualifierRange(), NULL, |
| 1220 | R.getLookupNameInfo(), &TList); |
| 1221 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1222 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1223 | // FIXME: we should be able to handle this case too. It is correct |
| 1224 | // to add this-> here. This is a workaround for PR7947. |
| 1225 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1226 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1227 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1228 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1229 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1230 | |
| 1231 | // Do we really want to note all of these? |
| 1232 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1233 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1234 | |
| 1235 | // Tell the callee to try to recover. |
| 1236 | return false; |
| 1237 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1238 | |
| 1239 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
| 1242 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1243 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1244 | DeclarationName Corrected; |
Daniel Dunbar | f7ced25 | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1245 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1246 | if (!R.empty()) { |
| 1247 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1248 | if (SS.isEmpty()) |
| 1249 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1250 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1251 | R.getLookupName().getAsString()); |
| 1252 | else |
| 1253 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1254 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1255 | << SS.getRange() |
| 1256 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1257 | R.getLookupName().getAsString()); |
| 1258 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1259 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1260 | << ND->getDeclName(); |
| 1261 | |
| 1262 | // Tell the callee to try to recover. |
| 1263 | return false; |
| 1264 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1266 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1267 | // FIXME: If we ended up with a typo for a type name or |
| 1268 | // Objective-C class name, we're in trouble because the parser |
| 1269 | // is in the wrong place to recover. Suggest the typo |
| 1270 | // correction, but don't make it a fix-it since we're not going |
| 1271 | // to recover well anyway. |
| 1272 | if (SS.isEmpty()) |
| 1273 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1274 | else |
| 1275 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1276 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1277 | << SS.getRange(); |
| 1278 | |
| 1279 | // Don't try to recover; it won't work. |
| 1280 | return true; |
| 1281 | } |
| 1282 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1283 | // 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] | 1284 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1285 | if (SS.isEmpty()) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1286 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1287 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1288 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1289 | << Name << computeDeclContext(SS, false) << Corrected |
| 1290 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1291 | return true; |
| 1292 | } |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1293 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | // Emit a special diagnostic for failed member lookups. |
| 1297 | // FIXME: computing the declaration context might fail here (?) |
| 1298 | if (!SS.isEmpty()) { |
| 1299 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1300 | << Name << computeDeclContext(SS, false) |
| 1301 | << SS.getRange(); |
| 1302 | return true; |
| 1303 | } |
| 1304 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1305 | // Give up, we can't recover. |
| 1306 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1307 | return true; |
| 1308 | } |
| 1309 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1310 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1311 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1312 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1313 | if (!IDecl) |
| 1314 | return 0; |
| 1315 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1316 | if (!ClassImpDecl) |
| 1317 | return 0; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1318 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1319 | if (!property) |
| 1320 | return 0; |
| 1321 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1322 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1323 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1324 | return 0; |
| 1325 | return property; |
| 1326 | } |
| 1327 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1328 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1329 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1330 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1331 | if (!IDecl) |
| 1332 | return false; |
| 1333 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1334 | if (!ClassImpDecl) |
| 1335 | return false; |
| 1336 | if (ObjCPropertyImplDecl *PIDecl |
| 1337 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1338 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1339 | PIDecl->getPropertyIvarDecl()) |
| 1340 | return false; |
| 1341 | |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1345 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1346 | LookupResult &Lookup, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1347 | IdentifierInfo *II, |
| 1348 | SourceLocation NameLoc) { |
| 1349 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1350 | bool LookForIvars; |
| 1351 | if (Lookup.empty()) |
| 1352 | LookForIvars = true; |
| 1353 | else if (CurMeth->isClassMethod()) |
| 1354 | LookForIvars = false; |
| 1355 | else |
| 1356 | LookForIvars = (Lookup.isSingleResult() && |
| 1357 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1358 | if (!LookForIvars) |
| 1359 | return 0; |
| 1360 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1361 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1362 | if (!IDecl) |
| 1363 | return 0; |
| 1364 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1365 | if (!ClassImpDecl) |
| 1366 | return 0; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1367 | bool DynamicImplSeen = false; |
| 1368 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1369 | if (!property) |
| 1370 | return 0; |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1371 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1372 | DynamicImplSeen = |
| 1373 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1374 | // property implementation has a designated ivar. No need to assume a new |
| 1375 | // one. |
| 1376 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1377 | return 0; |
| 1378 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1379 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1380 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1381 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1382 | NameLoc, |
| 1383 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1384 | ObjCIvarDecl::Private, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1385 | (Expr *)0, true); |
| 1386 | ClassImpDecl->addDecl(Ivar); |
| 1387 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1388 | property->setPropertyIvarDecl(Ivar); |
| 1389 | return Ivar; |
| 1390 | } |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1394 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1395 | CXXScopeSpec &SS, |
| 1396 | UnqualifiedId &Id, |
| 1397 | bool HasTrailingLParen, |
| 1398 | bool isAddressOfOperand) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1399 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1400 | "cannot be direct & operand and have a trailing lparen"); |
| 1401 | |
| 1402 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1403 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1404 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1405 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1408 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1409 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1410 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1411 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1412 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1413 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1414 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1415 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1416 | // C++ [temp.dep.expr]p3: |
| 1417 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1418 | // -- an identifier that was declared with a dependent type, |
| 1419 | // (note: handled after lookup) |
| 1420 | // -- a template-id that is dependent, |
| 1421 | // (note: handled in BuildTemplateIdExpr) |
| 1422 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1423 | // -- a nested-name-specifier that contains a class-name that |
| 1424 | // names a dependent type. |
| 1425 | // Determine whether this is a member of an unknown specialization; |
| 1426 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1427 | bool DependentID = false; |
| 1428 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1429 | Name.getCXXNameType()->isDependentType()) { |
| 1430 | DependentID = true; |
| 1431 | } else if (SS.isSet()) { |
| 1432 | DeclContext *DC = computeDeclContext(SS, false); |
| 1433 | if (DC) { |
| 1434 | if (RequireCompleteDeclContext(SS, DC)) |
| 1435 | return ExprError(); |
| 1436 | // FIXME: We should be checking whether DC is the current instantiation. |
| 1437 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) |
| 1438 | DependentID = !IsFullyFormedScope(*this, RD); |
| 1439 | } else { |
| 1440 | DependentID = true; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | if (DependentID) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1445 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1446 | TemplateArgs); |
| 1447 | } |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1448 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1449 | // Perform the required lookup. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1450 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1451 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1452 | // Lookup the template name again to correctly establish the context in |
| 1453 | // which it was found. This is really unfortunate as we already did the |
| 1454 | // lookup to determine that it was a template name in the first place. If |
| 1455 | // this becomes a performance hit, we can work harder to preserve those |
| 1456 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1457 | bool MemberOfUnknownSpecialization; |
| 1458 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1459 | MemberOfUnknownSpecialization); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1460 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1461 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1462 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1464 | // If this reference is in an Objective-C method, then we need to do |
| 1465 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1466 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1467 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1468 | if (E.isInvalid()) |
| 1469 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1471 | Expr *Ex = E.takeAs<Expr>(); |
| 1472 | if (Ex) return Owned(Ex); |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1473 | // Synthesize ivars lazily |
Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1474 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1475 | getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1476 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) { |
| 1477 | if (const ObjCPropertyDecl *Property = |
| 1478 | canSynthesizeProvisionalIvar(II)) { |
| 1479 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1480 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1481 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1482 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1483 | isAddressOfOperand); |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1484 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1485 | } |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1486 | // for further use, this must be set to false if in class method. |
| 1487 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1488 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1489 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1490 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1491 | if (R.isAmbiguous()) |
| 1492 | return ExprError(); |
| 1493 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1494 | // Determine whether this name might be a candidate for |
| 1495 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1496 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1497 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1498 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1499 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1500 | // in C90, extension in C99, forbidden in C++). |
| 1501 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1502 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1503 | if (D) R.addDecl(D); |
| 1504 | } |
| 1505 | |
| 1506 | // If this name wasn't predeclared and if this is not a function |
| 1507 | // call, diagnose the problem. |
| 1508 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1509 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1510 | return ExprError(); |
| 1511 | |
| 1512 | assert(!R.empty() && |
| 1513 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1514 | |
| 1515 | // If we found an Objective-C instance variable, let |
| 1516 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1517 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1518 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1519 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1520 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1521 | assert(E.isInvalid() || E.get()); |
| 1522 | return move(E); |
| 1523 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1524 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1527 | // This is guaranteed from this point on. |
| 1528 | assert(!R.empty() || ADL); |
| 1529 | |
| 1530 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1531 | if (getLangOptions().ObjCNonFragileABI && IvarLookupFollowUp && |
Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1532 | !(getLangOptions().ObjCDefaultSynthProperties && |
| 1533 | getLangOptions().ObjCNonFragileABI2) && |
Fariborz Jahanian | c15dfd8 | 2010-07-29 16:53:53 +0000 | [diff] [blame] | 1534 | Var->isFileVarDecl()) { |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1535 | ObjCPropertyDecl *Property = canSynthesizeProvisionalIvar(II); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1536 | if (Property) { |
| 1537 | Diag(NameLoc, diag::warn_ivar_variable_conflict) << Var->getDeclName(); |
| 1538 | Diag(Property->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1539 | Diag(Var->getLocation(), diag::note_global_declared_at); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1540 | } |
| 1541 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1542 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1543 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1544 | // C99 DR 316 says that, if a function type comes from a |
| 1545 | // function definition (without a prototype), that type is only |
| 1546 | // used for checking compatibility. Therefore, when referencing |
| 1547 | // the function, we pretend that we don't have the full function |
| 1548 | // type. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1549 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1550 | return ExprError(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1551 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1552 | QualType T = Func->getType(); |
| 1553 | QualType NoProtoType = T; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1554 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Eli Friedman | b41ad0f | 2010-05-17 02:50:18 +0000 | [diff] [blame] | 1555 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType(), |
| 1556 | Proto->getExtInfo()); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1557 | // Note that functions are r-values in C. |
| 1558 | return BuildDeclRefExpr(Func, NoProtoType, VK_RValue, NameLoc, &SS); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1559 | } |
| 1560 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1562 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1563 | // C++ [class.mfct.non-static]p3: |
| 1564 | // When an id-expression that is not part of a class member access |
| 1565 | // syntax and not used to form a pointer to member is used in the |
| 1566 | // body of a non-static member function of class X, if name lookup |
| 1567 | // resolves the name in the id-expression to a non-static non-type |
| 1568 | // member of some class C, the id-expression is transformed into a |
| 1569 | // class member access expression using (*this) as the |
| 1570 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1571 | // |
| 1572 | // But we don't actually need to do this for '&' operands if R |
| 1573 | // resolved to a function or overloaded function set, because the |
| 1574 | // expression is ill-formed if it actually works out to be a |
| 1575 | // non-static member function: |
| 1576 | // |
| 1577 | // C++ [expr.ref]p4: |
| 1578 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1579 | // [t]he expression can be used only as the left-hand operand of a |
| 1580 | // member function call. |
| 1581 | // |
| 1582 | // There are other safeguards against such uses, but it's important |
| 1583 | // to get this right here so that we don't end up making a |
| 1584 | // spuriously dependent expression if we're inside a dependent |
| 1585 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1586 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1587 | bool MightBeImplicitMember; |
| 1588 | if (!isAddressOfOperand) |
| 1589 | MightBeImplicitMember = true; |
| 1590 | else if (!SS.isEmpty()) |
| 1591 | MightBeImplicitMember = false; |
| 1592 | else if (R.isOverloadedResult()) |
| 1593 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1594 | else if (R.isUnresolvableResult()) |
| 1595 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1596 | else |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1597 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1598 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1599 | |
| 1600 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1601 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1604 | if (TemplateArgs) |
| 1605 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1606 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1607 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1608 | } |
| 1609 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1610 | /// Builds an expression which might be an implicit member expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1611 | ExprResult |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1612 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1613 | LookupResult &R, |
| 1614 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1615 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1616 | case IMA_Instance: |
| 1617 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1618 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1619 | case IMA_Mixed: |
| 1620 | case IMA_Mixed_Unrelated: |
| 1621 | case IMA_Unresolved: |
| 1622 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1623 | |
| 1624 | case IMA_Static: |
| 1625 | case IMA_Mixed_StaticContext: |
| 1626 | case IMA_Unresolved_StaticContext: |
| 1627 | if (TemplateArgs) |
| 1628 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1629 | return BuildDeclarationNameExpr(SS, R, false); |
| 1630 | |
| 1631 | case IMA_Error_StaticContext: |
| 1632 | case IMA_Error_Unrelated: |
| 1633 | DiagnoseInstanceReference(*this, SS, R); |
| 1634 | return ExprError(); |
| 1635 | } |
| 1636 | |
| 1637 | llvm_unreachable("unexpected instance member access kind"); |
| 1638 | return ExprError(); |
| 1639 | } |
| 1640 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1641 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1642 | /// declaration name, generally during template instantiation. |
| 1643 | /// There's a large number of things which don't need to be done along |
| 1644 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1645 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1646 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1647 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1648 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1649 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1650 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1651 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1652 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1653 | return ExprError(); |
| 1654 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1655 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1656 | LookupQualifiedName(R, DC); |
| 1657 | |
| 1658 | if (R.isAmbiguous()) |
| 1659 | return ExprError(); |
| 1660 | |
| 1661 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1662 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1663 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1664 | return ExprError(); |
| 1665 | } |
| 1666 | |
| 1667 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1668 | } |
| 1669 | |
| 1670 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1671 | /// detected that we're currently inside an ObjC method. Perform some |
| 1672 | /// additional lookup. |
| 1673 | /// |
| 1674 | /// Ideally, most of this would be done by lookup, but there's |
| 1675 | /// actually quite a lot of extra work involved. |
| 1676 | /// |
| 1677 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1678 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1679 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1680 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1681 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1682 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1683 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1684 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1685 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1686 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1687 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1688 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1689 | |
| 1690 | // If we're in a class method, we don't normally want to look for |
| 1691 | // ivars. But if we don't find anything else, and there's an |
| 1692 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1693 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1694 | |
| 1695 | bool LookForIvars; |
| 1696 | if (Lookup.empty()) |
| 1697 | LookForIvars = true; |
| 1698 | else if (IsClassMethod) |
| 1699 | LookForIvars = false; |
| 1700 | else |
| 1701 | LookForIvars = (Lookup.isSingleResult() && |
| 1702 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1703 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1704 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1705 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1706 | ObjCInterfaceDecl *ClassDeclared; |
| 1707 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1708 | // Diagnose using an ivar in a class method. |
| 1709 | if (IsClassMethod) |
| 1710 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1711 | << IV->getDeclName()); |
| 1712 | |
| 1713 | // If we're referencing an invalid decl, just return this as a silent |
| 1714 | // error node. The error diagnostic was already emitted on the decl. |
| 1715 | if (IV->isInvalidDecl()) |
| 1716 | return ExprError(); |
| 1717 | |
| 1718 | // Check if referencing a field with __attribute__((deprecated)). |
| 1719 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1720 | return ExprError(); |
| 1721 | |
| 1722 | // Diagnose the use of an ivar outside of the declaring class. |
| 1723 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1724 | ClassDeclared != IFace) |
| 1725 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1726 | |
| 1727 | // FIXME: This should use a new expr for a direct reference, don't |
| 1728 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1729 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1730 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1731 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1732 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1733 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1734 | SelfName, false, false); |
| 1735 | if (SelfExpr.isInvalid()) |
| 1736 | return ExprError(); |
| 1737 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1738 | Expr *SelfE = SelfExpr.take(); |
| 1739 | DefaultLvalueConversion(SelfE); |
| 1740 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1741 | MarkDeclarationReferenced(Loc, IV); |
| 1742 | return Owned(new (Context) |
| 1743 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1744 | SelfE, true, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1745 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1746 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1747 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1748 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1749 | ObjCInterfaceDecl *ClassDeclared; |
| 1750 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1751 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1752 | IFace == ClassDeclared) |
| 1753 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1754 | } |
| 1755 | } |
| 1756 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1757 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1758 | // FIXME. Consolidate this with similar code in LookupName. |
| 1759 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1760 | if (!(getLangOptions().CPlusPlus && |
| 1761 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1762 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1763 | S, Lookup.isForRedeclaration(), |
| 1764 | Lookup.getNameLoc()); |
| 1765 | if (D) Lookup.addDecl(D); |
| 1766 | } |
| 1767 | } |
| 1768 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1769 | // Sentinel value saying that we didn't do anything special. |
| 1770 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1771 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1772 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1773 | /// \brief Cast a base object to a member's actual type. |
| 1774 | /// |
| 1775 | /// Logically this happens in three phases: |
| 1776 | /// |
| 1777 | /// * First we cast from the base type to the naming class. |
| 1778 | /// The naming class is the class into which we were looking |
| 1779 | /// when we found the member; it's the qualifier type if a |
| 1780 | /// qualifier was provided, and otherwise it's the base type. |
| 1781 | /// |
| 1782 | /// * Next we cast from the naming class to the declaring class. |
| 1783 | /// If the member we found was brought into a class's scope by |
| 1784 | /// a using declaration, this is that class; otherwise it's |
| 1785 | /// the class declaring the member. |
| 1786 | /// |
| 1787 | /// * Finally we cast from the declaring class to the "true" |
| 1788 | /// declaring class of the member. This conversion does not |
| 1789 | /// obey access control. |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1790 | bool |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1791 | Sema::PerformObjectMemberConversion(Expr *&From, |
| 1792 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1793 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1794 | NamedDecl *Member) { |
| 1795 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1796 | if (!RD) |
| 1797 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1799 | QualType DestRecordType; |
| 1800 | QualType DestType; |
| 1801 | QualType FromRecordType; |
| 1802 | QualType FromType = From->getType(); |
| 1803 | bool PointerConversions = false; |
| 1804 | if (isa<FieldDecl>(Member)) { |
| 1805 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1807 | if (FromType->getAs<PointerType>()) { |
| 1808 | DestType = Context.getPointerType(DestRecordType); |
| 1809 | FromRecordType = FromType->getPointeeType(); |
| 1810 | PointerConversions = true; |
| 1811 | } else { |
| 1812 | DestType = DestRecordType; |
| 1813 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1814 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1815 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1816 | if (Method->isStatic()) |
| 1817 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1819 | DestType = Method->getThisType(Context); |
| 1820 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1822 | if (FromType->getAs<PointerType>()) { |
| 1823 | FromRecordType = FromType->getPointeeType(); |
| 1824 | PointerConversions = true; |
| 1825 | } else { |
| 1826 | FromRecordType = FromType; |
| 1827 | DestType = DestRecordType; |
| 1828 | } |
| 1829 | } else { |
| 1830 | // No conversion necessary. |
| 1831 | return false; |
| 1832 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1834 | if (DestType->isDependentType() || FromType->isDependentType()) |
| 1835 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1837 | // If the unqualified types are the same, no conversion is necessary. |
| 1838 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1839 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1840 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1841 | SourceRange FromRange = From->getSourceRange(); |
| 1842 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1843 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1844 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1845 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1846 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1847 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1848 | // class name. |
| 1849 | // |
| 1850 | // If the member was a qualified name and the qualified referred to a |
| 1851 | // specific base subobject type, we'll cast to that intermediate type |
| 1852 | // first and then to the object in which the member is declared. That allows |
| 1853 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1854 | // |
| 1855 | // class Base { public: int x; }; |
| 1856 | // class Derived1 : public Base { }; |
| 1857 | // class Derived2 : public Base { }; |
| 1858 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1859 | // |
| 1860 | // void VeryDerived::f() { |
| 1861 | // x = 17; // error: ambiguous base subobjects |
| 1862 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1863 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1864 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1865 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1866 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1867 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1868 | |
| 1869 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1870 | |
| 1871 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1872 | // type of the object type, in which case we just ignore it. |
| 1873 | // Otherwise build the appropriate casts. |
| 1874 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1875 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1876 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1877 | FromLoc, FromRange, &BasePath)) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1878 | return true; |
| 1879 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1880 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1881 | QType = Context.getPointerType(QType); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1882 | ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 1883 | VK, &BasePath); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1884 | |
| 1885 | FromType = QType; |
| 1886 | FromRecordType = QRecordType; |
| 1887 | |
| 1888 | // If the qualifier type was the same as the destination type, |
| 1889 | // we're done. |
| 1890 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1891 | return false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1894 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1895 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1896 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1897 | // If we actually found the member through a using declaration, cast |
| 1898 | // down to the using declaration's type. |
| 1899 | // |
| 1900 | // Pointer equality is fine here because only one declaration of a |
| 1901 | // class ever has member declarations. |
| 1902 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 1903 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 1904 | QualType URecordType = Context.getTypeDeclType( |
| 1905 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 1906 | |
| 1907 | // We only need to do this if the naming-class to declaring-class |
| 1908 | // conversion is non-trivial. |
| 1909 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 1910 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1911 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1912 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1913 | FromLoc, FromRange, &BasePath)) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1914 | return true; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1915 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1916 | QualType UType = URecordType; |
| 1917 | if (PointerConversions) |
| 1918 | UType = Context.getPointerType(UType); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1919 | ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1920 | VK, &BasePath); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1921 | FromType = UType; |
| 1922 | FromRecordType = URecordType; |
| 1923 | } |
| 1924 | |
| 1925 | // We don't do access control for the conversion from the |
| 1926 | // declaring class to the true declaring class. |
| 1927 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1928 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1929 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1930 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1931 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 1932 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1933 | IgnoreAccess)) |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1934 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1935 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1936 | ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1937 | VK, &BasePath); |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1938 | return false; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1939 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1940 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1941 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1942 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | 2cfcef6 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1943 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1944 | DeclAccessPair FoundDecl, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1945 | const DeclarationNameInfo &MemberNameInfo, |
| 1946 | QualType Ty, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1947 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1948 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1949 | NestedNameSpecifier *Qualifier = 0; |
| 1950 | SourceRange QualifierRange; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1951 | if (SS.isSet()) { |
| 1952 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1953 | QualifierRange = SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1954 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1955 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1956 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1957 | Member, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1958 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1961 | static ExprResult |
| 1962 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1963 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1964 | DeclAccessPair FoundDecl, |
| 1965 | const DeclarationNameInfo &MemberNameInfo) { |
| 1966 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 1967 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 1968 | // that *x is always an l-value), except that if the base isn't |
| 1969 | // an ordinary object then we must have an rvalue. |
| 1970 | ExprValueKind VK = VK_LValue; |
| 1971 | ExprObjectKind OK = OK_Ordinary; |
| 1972 | if (!IsArrow) { |
| 1973 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 1974 | VK = BaseExpr->getValueKind(); |
| 1975 | else |
| 1976 | VK = VK_RValue; |
| 1977 | } |
| 1978 | if (VK != VK_RValue && Field->isBitField()) |
| 1979 | OK = OK_BitField; |
| 1980 | |
| 1981 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 1982 | QualType MemberType = Field->getType(); |
| 1983 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 1984 | MemberType = Ref->getPointeeType(); |
| 1985 | VK = VK_LValue; |
| 1986 | } else { |
| 1987 | QualType BaseType = BaseExpr->getType(); |
| 1988 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 1989 | |
| 1990 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 1991 | |
| 1992 | // GC attributes are never picked up by members. |
| 1993 | BaseQuals.removeObjCGCAttr(); |
| 1994 | |
| 1995 | // CVR attributes from the base are picked up by members, |
| 1996 | // except that 'mutable' members don't pick up 'const'. |
| 1997 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 1998 | |
| 1999 | Qualifiers MemberQuals |
| 2000 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2001 | |
| 2002 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2003 | assert(!MemberQuals.hasAddressSpace()); |
| 2004 | |
| 2005 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2006 | if (Combined != MemberQuals) |
| 2007 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2008 | } |
| 2009 | |
| 2010 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
| 2011 | if (S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2012 | FoundDecl, Field)) |
| 2013 | return ExprError(); |
| 2014 | return S.Owned(BuildMemberExpr(S.Context, BaseExpr, IsArrow, SS, |
| 2015 | Field, FoundDecl, MemberNameInfo, |
| 2016 | MemberType, VK, OK)); |
| 2017 | } |
| 2018 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2019 | /// Builds an implicit member access expression. The current context |
| 2020 | /// is known to be an instance method, and the given unqualified lookup |
| 2021 | /// set is known to contain only instance members, at least one of which |
| 2022 | /// is from an appropriate type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2023 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2024 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2025 | LookupResult &R, |
| 2026 | const TemplateArgumentListInfo *TemplateArgs, |
| 2027 | bool IsKnownInstance) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2028 | assert(!R.empty() && !R.isAmbiguous()); |
| 2029 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2030 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2032 | // We may have found a field within an anonymous union or struct |
| 2033 | // (C++ [class.union]). |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 2034 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2035 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2036 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
| 2037 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
| 2038 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2039 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2040 | // If this is known to be an instance access, go ahead and build a |
| 2041 | // 'this' expression now. |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 2042 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 2043 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2044 | Expr *This = 0; // null signifies implicit access |
| 2045 | if (IsKnownInstance) { |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2046 | SourceLocation Loc = R.getNameLoc(); |
| 2047 | if (SS.getRange().isValid()) |
| 2048 | Loc = SS.getRange().getBegin(); |
| 2049 | This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2052 | return BuildMemberReferenceExpr(This, ThisType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2053 | /*OpLoc*/ SourceLocation(), |
| 2054 | /*IsArrow*/ true, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2055 | SS, |
| 2056 | /*FirstQualifierInScope*/ 0, |
| 2057 | R, TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2060 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2061 | const LookupResult &R, |
| 2062 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2063 | // Only when used directly as the postfix-expression of a call. |
| 2064 | if (!HasTrailingLParen) |
| 2065 | return false; |
| 2066 | |
| 2067 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2068 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2069 | return false; |
| 2070 | |
| 2071 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2072 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2073 | return false; |
| 2074 | |
| 2075 | // Turn off ADL when we find certain kinds of declarations during |
| 2076 | // normal lookup: |
| 2077 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2078 | NamedDecl *D = *I; |
| 2079 | |
| 2080 | // C++0x [basic.lookup.argdep]p3: |
| 2081 | // -- a declaration of a class member |
| 2082 | // Since using decls preserve this property, we check this on the |
| 2083 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2084 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2085 | return false; |
| 2086 | |
| 2087 | // C++0x [basic.lookup.argdep]p3: |
| 2088 | // -- a block-scope function declaration that is not a |
| 2089 | // using-declaration |
| 2090 | // NOTE: we also trigger this for function templates (in fact, we |
| 2091 | // don't check the decl type at all, since all other decl types |
| 2092 | // turn off ADL anyway). |
| 2093 | if (isa<UsingShadowDecl>(D)) |
| 2094 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2095 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2096 | return false; |
| 2097 | |
| 2098 | // C++0x [basic.lookup.argdep]p3: |
| 2099 | // -- a declaration that is neither a function or a function |
| 2100 | // template |
| 2101 | // And also for builtin functions. |
| 2102 | if (isa<FunctionDecl>(D)) { |
| 2103 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2104 | |
| 2105 | // But also builtin functions. |
| 2106 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2107 | return false; |
| 2108 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2109 | return false; |
| 2110 | } |
| 2111 | |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
| 2115 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2116 | /// Diagnoses obvious problems with the use of the given declaration |
| 2117 | /// as an expression. This is only actually called for lookups that |
| 2118 | /// were not overloaded, and it doesn't promise that the declaration |
| 2119 | /// will in fact be used. |
| 2120 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 2121 | if (isa<TypedefDecl>(D)) { |
| 2122 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2123 | return true; |
| 2124 | } |
| 2125 | |
| 2126 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2127 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2128 | return true; |
| 2129 | } |
| 2130 | |
| 2131 | if (isa<NamespaceDecl>(D)) { |
| 2132 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2133 | return true; |
| 2134 | } |
| 2135 | |
| 2136 | return false; |
| 2137 | } |
| 2138 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2139 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2140 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2141 | LookupResult &R, |
| 2142 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2143 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2144 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2145 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2146 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2147 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2148 | |
| 2149 | // We only need to check the declaration if there's exactly one |
| 2150 | // result, because in the overloaded case the results can only be |
| 2151 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2152 | if (R.isSingleResult() && |
| 2153 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2154 | return ExprError(); |
| 2155 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2156 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2157 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2158 | // we've picked a target. |
| 2159 | R.suppressDiagnostics(); |
| 2160 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2161 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2162 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2163 | (NestedNameSpecifier*) SS.getScopeRep(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2164 | SS.getRange(), R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2165 | NeedsADL, R.isOverloadedResult(), |
| 2166 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2167 | |
| 2168 | return Owned(ULE); |
| 2169 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2170 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2171 | static ExprValueKind getValueKindForDecl(ASTContext &Context, |
| 2172 | const ValueDecl *D) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2173 | // FIXME: It's not clear to me why NonTypeTemplateParmDecl is a VarDecl. |
| 2174 | if (isa<VarDecl>(D) && !isa<NonTypeTemplateParmDecl>(D)) return VK_LValue; |
| 2175 | if (isa<FieldDecl>(D)) return VK_LValue; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2176 | if (!Context.getLangOptions().CPlusPlus) return VK_RValue; |
| 2177 | if (isa<FunctionDecl>(D)) { |
| 2178 | if (isa<CXXMethodDecl>(D) && cast<CXXMethodDecl>(D)->isInstance()) |
| 2179 | return VK_RValue; |
| 2180 | return VK_LValue; |
| 2181 | } |
| 2182 | return Expr::getValueKindForType(D->getType()); |
| 2183 | } |
| 2184 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2185 | |
| 2186 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2187 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2188 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2189 | const DeclarationNameInfo &NameInfo, |
| 2190 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2191 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2192 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2193 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2194 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2195 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2196 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2197 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2199 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2200 | // Specifically diagnose references to class templates that are missing |
| 2201 | // a template argument list. |
| 2202 | Diag(Loc, diag::err_template_decl_ref) |
| 2203 | << Template << SS.getRange(); |
| 2204 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2205 | return ExprError(); |
| 2206 | } |
| 2207 | |
| 2208 | // Make sure that we're referring to a value. |
| 2209 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2210 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2211 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2212 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2213 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2214 | return ExprError(); |
| 2215 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2216 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2217 | // Check whether this declaration can be used. Note that we suppress |
| 2218 | // this check when we're going to perform argument-dependent lookup |
| 2219 | // on this function name, because this might not be the function |
| 2220 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2221 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2222 | return ExprError(); |
| 2223 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2224 | // Only create DeclRefExpr's for valid Decl's. |
| 2225 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2226 | return ExprError(); |
| 2227 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2228 | // Handle anonymous. |
| 2229 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(VD)) |
| 2230 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
| 2231 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2232 | ExprValueKind VK = getValueKindForDecl(Context, VD); |
| 2233 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2234 | // If the identifier reference is inside a block, and it refers to a value |
| 2235 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2236 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2237 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2238 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2239 | // We do not do this for things like enum constants, global variables, etc, |
| 2240 | // as they do not get snapshotted. |
| 2241 | // |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2242 | if (getCurBlock() && |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 2243 | ShouldSnapshotBlockValueReference(*this, getCurBlock(), VD)) { |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2244 | if (VD->getType().getTypePtr()->isVariablyModifiedType()) { |
| 2245 | Diag(Loc, diag::err_ref_vm_type); |
| 2246 | Diag(D->getLocation(), diag::note_declared_at); |
| 2247 | return ExprError(); |
| 2248 | } |
| 2249 | |
Fariborz Jahanian | fa24e10 | 2010-03-16 23:39:51 +0000 | [diff] [blame] | 2250 | if (VD->getType()->isArrayType()) { |
Mike Stump | 8971a86 | 2010-01-05 03:10:36 +0000 | [diff] [blame] | 2251 | Diag(Loc, diag::err_ref_array_type); |
| 2252 | Diag(D->getLocation(), diag::note_declared_at); |
| 2253 | return ExprError(); |
| 2254 | } |
| 2255 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 2256 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 2257 | QualType ExprTy = VD->getType().getNonReferenceType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2258 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 2259 | // The BlocksAttr indicates the variable is bound by-reference. |
Fariborz Jahanian | a3e54bd | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 2260 | bool byrefVar = (VD->getAttr<BlocksAttr>() != 0); |
Fariborz Jahanian | 70c0b08 | 2010-07-12 17:26:57 +0000 | [diff] [blame] | 2261 | QualType T = VD->getType(); |
Fariborz Jahanian | a3e54bd | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 2262 | BlockDeclRefExpr *BDRE; |
| 2263 | |
| 2264 | if (!byrefVar) { |
| 2265 | // This is to record that a 'const' was actually synthesize and added. |
| 2266 | bool constAdded = !ExprTy.isConstQualified(); |
| 2267 | // Variable will be bound by-copy, make it const within the closure. |
| 2268 | ExprTy.addConst(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2269 | BDRE = new (Context) BlockDeclRefExpr(VD, ExprTy, VK, |
| 2270 | Loc, false, constAdded); |
Fariborz Jahanian | a3e54bd | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 2271 | } |
| 2272 | else |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2273 | BDRE = new (Context) BlockDeclRefExpr(VD, ExprTy, VK, Loc, true); |
Fariborz Jahanian | a3e54bd | 2010-11-16 19:29:39 +0000 | [diff] [blame] | 2274 | |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 2275 | if (getLangOptions().CPlusPlus) { |
| 2276 | if (!T->isDependentType() && !T->isReferenceType()) { |
| 2277 | Expr *E = new (Context) |
| 2278 | DeclRefExpr(const_cast<ValueDecl*>(BDRE->getDecl()), T, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2279 | VK, SourceLocation()); |
Fariborz Jahanian | 2a5deb5 | 2010-11-11 00:11:38 +0000 | [diff] [blame] | 2280 | if (T->getAs<RecordType>()) |
| 2281 | if (!T->isUnionType()) { |
| 2282 | ExprResult Res = PerformCopyInitialization( |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 2283 | InitializedEntity::InitializeBlock(VD->getLocation(), |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2284 | T, false), |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 2285 | SourceLocation(), |
| 2286 | Owned(E)); |
Fariborz Jahanian | 2a5deb5 | 2010-11-11 00:11:38 +0000 | [diff] [blame] | 2287 | if (!Res.isInvalid()) { |
Douglas Gregor | a40433a | 2010-12-07 00:41:46 +0000 | [diff] [blame] | 2288 | Res = MaybeCreateExprWithCleanups(Res); |
Fariborz Jahanian | 2a5deb5 | 2010-11-11 00:11:38 +0000 | [diff] [blame] | 2289 | Expr *Init = Res.takeAs<Expr>(); |
| 2290 | BDRE->setCopyConstructorExpr(Init); |
| 2291 | } |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 2292 | } |
Fariborz Jahanian | ea882cd | 2010-06-04 21:35:44 +0000 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | return Owned(BDRE); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 2296 | } |
| 2297 | // If this reference is not in a block or if the referenced variable is |
| 2298 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2299 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2300 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2301 | NameInfo, &SS); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2302 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2303 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2304 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2305 | tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2306 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2307 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2308 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2309 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2310 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2311 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2312 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2313 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2314 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2315 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2316 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2317 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2318 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2319 | if (!currentDecl && getCurBlock()) |
| 2320 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2321 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2322 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2323 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2324 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2325 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2326 | QualType ResTy; |
| 2327 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2328 | ResTy = Context.DependentTy; |
| 2329 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2330 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2331 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2332 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2333 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2334 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2335 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2336 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2339 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2340 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2341 | bool Invalid = false; |
| 2342 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2343 | if (Invalid) |
| 2344 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2345 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2346 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2347 | PP); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2348 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2349 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2350 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2351 | QualType Ty; |
| 2352 | if (!getLangOptions().CPlusPlus) |
| 2353 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2354 | else if (Literal.isWide()) |
| 2355 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2356 | else if (Literal.isMultiChar()) |
| 2357 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2358 | else |
| 2359 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2360 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2361 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2362 | Literal.isWide(), |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2363 | Ty, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2366 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2367 | // 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] | 2368 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2369 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2370 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2371 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2372 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2373 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2374 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2375 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2376 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2377 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2378 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2379 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2380 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2381 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2382 | bool Invalid = false; |
| 2383 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2384 | if (Invalid) |
| 2385 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2386 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2387 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2388 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2389 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2390 | return ExprError(); |
| 2391 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2392 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2393 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2394 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2395 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2396 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2397 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2398 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2399 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2400 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2401 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2402 | |
| 2403 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2404 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2405 | using llvm::APFloat; |
| 2406 | APFloat Val(Format); |
| 2407 | |
| 2408 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2409 | |
| 2410 | // Overflow is always an error, but underflow is only an error if |
| 2411 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2412 | if ((result & APFloat::opOverflow) || |
| 2413 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2414 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2415 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2416 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2417 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2418 | APFloat::getLargest(Format).toString(buffer); |
| 2419 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2420 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2421 | APFloat::getSmallest(Format).toString(buffer); |
| 2422 | } |
| 2423 | |
| 2424 | Diag(Tok.getLocation(), diagnostic) |
| 2425 | << Ty |
| 2426 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2427 | } |
| 2428 | |
| 2429 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2430 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2431 | |
Peter Collingbourne | 0b69e1a | 2010-12-04 01:50:56 +0000 | [diff] [blame] | 2432 | if (getLangOptions().SinglePrecisionConstants && Ty == Context.DoubleTy) |
| 2433 | ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast); |
| 2434 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2435 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2436 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2437 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2438 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2439 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2440 | // long long is a C99 feature. |
| 2441 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2442 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2443 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2444 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2445 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2446 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2448 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2449 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2450 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2451 | Ty = Context.UnsignedLongLongTy; |
| 2452 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2453 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2454 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2455 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2456 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2457 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2458 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2459 | // be an unsigned int. |
| 2460 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2461 | |
| 2462 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2463 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2464 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2465 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2466 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2467 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2468 | // Does it fit in a unsigned int? |
| 2469 | if (ResultVal.isIntN(IntSize)) { |
| 2470 | // Does it fit in a signed int? |
| 2471 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2472 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2473 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2474 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2475 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2476 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2477 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2478 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2479 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2480 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2481 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2482 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2483 | // Does it fit in a unsigned long? |
| 2484 | if (ResultVal.isIntN(LongSize)) { |
| 2485 | // Does it fit in a signed long? |
| 2486 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2487 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2488 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2489 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2490 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2491 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2494 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2495 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2496 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2497 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2498 | // Does it fit in a unsigned long long? |
| 2499 | if (ResultVal.isIntN(LongLongSize)) { |
| 2500 | // Does it fit in a signed long long? |
Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2501 | // To be compatible with MSVC, hex integer literals ending with the |
| 2502 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2503 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2504 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2505 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2506 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2507 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2508 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2509 | } |
| 2510 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2511 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2512 | // If we still couldn't decide a type, we probably have something that |
| 2513 | // 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] | 2514 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2515 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2516 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2517 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2518 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2519 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2520 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2521 | ResultVal = ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2522 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2523 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2524 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2525 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2526 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2527 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2529 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2530 | |
| 2531 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2534 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2535 | SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2536 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2537 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 2540 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 2541 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2542 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2543 | SourceLocation OpLoc, |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2544 | SourceRange ExprRange, |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2545 | bool isSizeof) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2546 | if (exprType->isDependentType()) |
| 2547 | return false; |
| 2548 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2549 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2550 | // the result is the size of the referenced type." |
| 2551 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2552 | // result shall be the alignment of the referenced type." |
| 2553 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2554 | exprType = Ref->getPointeeType(); |
| 2555 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2556 | // C99 6.5.3.4p1: |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2557 | if (exprType->isFunctionType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2558 | // alignof(function) is allowed as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2559 | if (isSizeof) |
| 2560 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 2561 | return false; |
| 2562 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2564 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2565 | if (exprType->isVoidType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2566 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 2567 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2568 | return false; |
| 2569 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2570 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2571 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2572 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2573 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2574 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2576 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2577 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2578 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2579 | << exprType << isSizeof << ExprRange; |
| 2580 | return true; |
Chris Lattner | 37920f5 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 2581 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2583 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2586 | static bool CheckAlignOfExpr(Sema &S, Expr *E, SourceLocation OpLoc, |
| 2587 | SourceRange ExprRange) { |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2588 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2589 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2591 | if (isa<DeclRefExpr>(E)) |
| 2592 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2593 | |
| 2594 | // Cannot know anything else if the expression is dependent. |
| 2595 | if (E->isTypeDependent()) |
| 2596 | return false; |
| 2597 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2598 | if (E->getBitField()) { |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2599 | S. Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2600 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2601 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2602 | |
| 2603 | // Alignment of a field access is always okay, so long as it isn't a |
| 2604 | // bit-field. |
| 2605 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2606 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2607 | return false; |
| 2608 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2609 | return S.CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2612 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2613 | ExprResult |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2614 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2615 | SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2616 | bool isSizeOf, SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2617 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2618 | return ExprError(); |
| 2619 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2620 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2621 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2622 | if (!T->isDependentType() && |
| 2623 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 2624 | return ExprError(); |
| 2625 | |
| 2626 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2627 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2628 | Context.getSizeType(), OpLoc, |
| 2629 | R.getEnd())); |
| 2630 | } |
| 2631 | |
| 2632 | /// \brief Build a sizeof or alignof expression given an expression |
| 2633 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2634 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2636 | bool isSizeOf, SourceRange R) { |
| 2637 | // Verify that the operand is valid. |
| 2638 | bool isInvalid = false; |
| 2639 | if (E->isTypeDependent()) { |
| 2640 | // Delay type-checking for type-dependent expressions. |
| 2641 | } else if (!isSizeOf) { |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 2642 | isInvalid = CheckAlignOfExpr(*this, E, OpLoc, R); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2643 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2644 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 2645 | isInvalid = true; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2646 | } else if (E->getType()->isPlaceholderType()) { |
| 2647 | ExprResult PE = CheckPlaceholderExpr(E, OpLoc); |
| 2648 | if (PE.isInvalid()) return ExprError(); |
| 2649 | return CreateSizeOfAlignOfExpr(PE.take(), OpLoc, isSizeOf, R); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2650 | } else { |
| 2651 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 2652 | } |
| 2653 | |
| 2654 | if (isInvalid) |
| 2655 | return ExprError(); |
| 2656 | |
| 2657 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 2658 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 2659 | Context.getSizeType(), OpLoc, |
| 2660 | R.getEnd())); |
| 2661 | } |
| 2662 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2663 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 2664 | /// the same for @c alignof and @c __alignof |
| 2665 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2666 | ExprResult |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2667 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 2668 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 2669 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2670 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2671 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2672 | if (isType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2673 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2674 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2675 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2678 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2679 | ExprResult Result |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2680 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 2681 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2682 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2685 | static QualType CheckRealImagOperand(Sema &S, Expr *&V, SourceLocation Loc, |
| 2686 | bool isReal) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2687 | if (V->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2688 | return S.Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2690 | // _Real and _Imag are only l-values for normal l-values. |
| 2691 | if (V->getObjectKind() != OK_Ordinary) |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2692 | S.DefaultLvalueConversion(V); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2693 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2694 | // These operators return the element type of a complex type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2695 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2696 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2697 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2698 | // Otherwise they pass through real integer and floating point types here. |
| 2699 | if (V->getType()->isArithmeticType()) |
| 2700 | return V->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2702 | // Test for placeholders. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2703 | ExprResult PR = S.CheckPlaceholderExpr(V, Loc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2704 | if (PR.isInvalid()) return QualType(); |
| 2705 | if (PR.take() != V) { |
| 2706 | V = PR.take(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2707 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2710 | // Reject anything else. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2711 | S.Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2712 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2713 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2717 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2718 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2719 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2720 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2721 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2722 | switch (Kind) { |
| 2723 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2724 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2725 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2726 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2727 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2728 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2731 | /// Expressions of certain arbitrary types are forbidden by C from |
| 2732 | /// having l-value type. These are: |
| 2733 | /// - 'void', but not qualified void |
| 2734 | /// - function types |
| 2735 | /// |
| 2736 | /// The exact rule here is C99 6.3.2.1: |
| 2737 | /// An lvalue is an expression with an object type or an incomplete |
| 2738 | /// type other than void. |
| 2739 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 2740 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 2741 | T->isFunctionType()); |
| 2742 | } |
| 2743 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2744 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2745 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2746 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2747 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2748 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2749 | if (Result.isInvalid()) return ExprError(); |
| 2750 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2751 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2752 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2754 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2755 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2756 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2757 | Context.DependentTy, |
| 2758 | VK_LValue, OK_Ordinary, |
| 2759 | RLoc)); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2762 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2763 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2764 | LHSExp->getType()->isEnumeralType() || |
| 2765 | RHSExp->getType()->isRecordType() || |
| 2766 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2767 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2770 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2774 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2775 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 2776 | Expr *Idx, SourceLocation RLoc) { |
| 2777 | Expr *LHSExp = Base; |
| 2778 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2779 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2780 | // Perform default conversions. |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2781 | if (!LHSExp->getType()->getAs<VectorType>()) |
| 2782 | DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2783 | DefaultFunctionArrayLvalueConversion(RHSExp); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2784 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2785 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2786 | ExprValueKind VK = VK_LValue; |
| 2787 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2788 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 2789 | // 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] | 2790 | // 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] | 2791 | // 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] | 2792 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2793 | Expr *BaseExpr, *IndexExpr; |
| 2794 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2795 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2796 | BaseExpr = LHSExp; |
| 2797 | IndexExpr = RHSExp; |
| 2798 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2799 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2800 | BaseExpr = LHSExp; |
| 2801 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2802 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2803 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2804 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2805 | BaseExpr = RHSExp; |
| 2806 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2807 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2808 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2809 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2810 | BaseExpr = LHSExp; |
| 2811 | IndexExpr = RHSExp; |
| 2812 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2814 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2815 | // Handle the uncommon case of "123[Ptr]". |
| 2816 | BaseExpr = RHSExp; |
| 2817 | IndexExpr = LHSExp; |
| 2818 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2819 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2820 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2821 | IndexExpr = RHSExp; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2822 | VK = LHSExp->getValueKind(); |
| 2823 | if (VK != VK_RValue) |
| 2824 | OK = OK_VectorComponent; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2825 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2826 | // FIXME: need to deal with const... |
| 2827 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2828 | } else if (LHSTy->isArrayType()) { |
| 2829 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2830 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2831 | // wasn't promoted because of the C90 rule that doesn't |
| 2832 | // allow promoting non-lvalue arrays. Warn, then |
| 2833 | // force the promotion here. |
| 2834 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2835 | LHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2836 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2837 | CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2838 | LHSTy = LHSExp->getType(); |
| 2839 | |
| 2840 | BaseExpr = LHSExp; |
| 2841 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2842 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2843 | } else if (RHSTy->isArrayType()) { |
| 2844 | // Same as previous, except for 123[f().a] case |
| 2845 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2846 | RHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2847 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2848 | CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2849 | RHSTy = RHSExp->getType(); |
| 2850 | |
| 2851 | BaseExpr = RHSExp; |
| 2852 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2853 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 2854 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2855 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2856 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2857 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 2858 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2859 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2860 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2861 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 2862 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2863 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2864 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2865 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2866 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2867 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2868 | // 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] | 2869 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2870 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2871 | // incomplete types are not object types. |
| 2872 | if (ResultType->isFunctionType()) { |
| 2873 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2874 | << ResultType << BaseExpr->getSourceRange(); |
| 2875 | return ExprError(); |
| 2876 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 2878 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 2879 | // GNU extension: subscripting on pointer to void |
| 2880 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 2881 | << BaseExpr->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2882 | |
| 2883 | // C forbids expressions of unqualified void type from being l-values. |
| 2884 | // See IsCForbiddenLValueType. |
| 2885 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 2886 | } else if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2888 | PDiag(diag::err_subscript_incomplete_type) |
| 2889 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2890 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2891 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2892 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2893 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2894 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2895 | << ResultType << BaseExpr->getSourceRange(); |
| 2896 | return ExprError(); |
| 2897 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2898 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2899 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 2900 | !IsCForbiddenLValueType(Context, ResultType)); |
| 2901 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2902 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2903 | ResultType, VK, OK, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2906 | /// Check an ext-vector component access expression. |
| 2907 | /// |
| 2908 | /// VK should be set in advance to the value kind of the base |
| 2909 | /// expression. |
| 2910 | static QualType |
| 2911 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 2912 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2913 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2914 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2915 | // see FIXME there. |
| 2916 | // |
| 2917 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2918 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2919 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2920 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2921 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2922 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2923 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2924 | // 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] | 2925 | // special names that indicate a subset of exactly half the elements are |
| 2926 | // to be selected. |
| 2927 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2928 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2929 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2930 | // 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] | 2931 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2932 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2933 | bool HasRepeated = false; |
| 2934 | bool HasIndex[16] = {}; |
| 2935 | |
| 2936 | int Idx; |
| 2937 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2938 | // Check that we've found one of the special components, or that the component |
| 2939 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2940 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2941 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2942 | HalvingSwizzle = true; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2943 | } else if (!HexSwizzle && |
| 2944 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 2945 | do { |
| 2946 | if (HasIndex[Idx]) HasRepeated = true; |
| 2947 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2948 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2949 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 2950 | } else { |
| 2951 | if (HexSwizzle) compStr++; |
| 2952 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 2953 | if (HasIndex[Idx]) HasRepeated = true; |
| 2954 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2955 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2956 | } |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2957 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2958 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2959 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2960 | // We didn't get to the end of the string. This means the component names |
| 2961 | // 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] | 2962 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 2963 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2964 | return QualType(); |
| 2965 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2966 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2967 | // Ensure no component accessor exceeds the width of the vector type it |
| 2968 | // operates on. |
| 2969 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2970 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2971 | |
| 2972 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2973 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2974 | |
| 2975 | while (*compStr) { |
| 2976 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2977 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2978 | << baseType << SourceRange(CompLoc); |
| 2979 | return QualType(); |
| 2980 | } |
| 2981 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2982 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2983 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2984 | // 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] | 2985 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2986 | // 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] | 2987 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2988 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | ac8183a | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2989 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2990 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2991 | if (HexSwizzle) |
| 2992 | CompSize--; |
| 2993 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2994 | if (CompSize == 1) |
| 2995 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2996 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2997 | if (HasRepeated) VK = VK_RValue; |
| 2998 | |
| 2999 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3000 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3001 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3002 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3003 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3004 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3005 | } |
| 3006 | 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] | 3007 | } |
| 3008 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3009 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3010 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3011 | const Selector &Sel, |
| 3012 | ASTContext &Context) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3013 | if (Member) |
| 3014 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3015 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3016 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3017 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3019 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3020 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3021 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3022 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3023 | return D; |
| 3024 | } |
| 3025 | return 0; |
| 3026 | } |
| 3027 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3028 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3029 | IdentifierInfo *Member, |
| 3030 | const Selector &Sel, |
| 3031 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3032 | // Check protocols on qualified interfaces. |
| 3033 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3034 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3035 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3036 | if (Member) |
| 3037 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3038 | GDecl = PD; |
| 3039 | break; |
| 3040 | } |
| 3041 | // 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] | 3042 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3043 | GDecl = OMD; |
| 3044 | break; |
| 3045 | } |
| 3046 | } |
| 3047 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3048 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3049 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3050 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3051 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3052 | Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3053 | if (GDecl) |
| 3054 | return GDecl; |
| 3055 | } |
| 3056 | } |
| 3057 | return GDecl; |
| 3058 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3059 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3060 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3061 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3062 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3063 | const CXXScopeSpec &SS, |
| 3064 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3065 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3066 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3067 | // Even in dependent contexts, try to diagnose base expressions with |
| 3068 | // obviously wrong types, e.g.: |
| 3069 | // |
| 3070 | // T* t; |
| 3071 | // t.f; |
| 3072 | // |
| 3073 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3074 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3075 | // allows this, while still reporting an error if T is a struct pointer. |
| 3076 | if (!IsArrow) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3077 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3078 | if (PT && (!getLangOptions().ObjC1 || |
| 3079 | PT->getPointeeType()->isRecordType())) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3080 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3081 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3082 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3083 | return ExprError(); |
| 3084 | } |
| 3085 | } |
| 3086 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3087 | assert(BaseType->isDependentType() || |
| 3088 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3089 | isDependentScopeSpecifier(SS)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3090 | |
| 3091 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3092 | // must have pointer type, and the accessed type is the pointee. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3093 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3094 | IsArrow, OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3095 | SS.getScopeRep(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3096 | SS.getRange(), |
| 3097 | FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3098 | NameInfo, TemplateArgs)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /// We know that the given qualified member reference points only to |
| 3102 | /// declarations which do not belong to the static type of the base |
| 3103 | /// expression. Diagnose the problem. |
| 3104 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3105 | Expr *BaseExpr, |
| 3106 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3107 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3108 | const LookupResult &R) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3109 | // If this is an implicit member access, use a different set of |
| 3110 | // diagnostics. |
| 3111 | if (!BaseExpr) |
| 3112 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3113 | |
John McCall | 1e67dd6 | 2010-04-27 01:43:38 +0000 | [diff] [blame] | 3114 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_of_unrelated) |
| 3115 | << SS.getRange() << R.getRepresentativeDecl() << BaseType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | // Check whether the declarations we found through a nested-name |
| 3119 | // specifier in a member expression are actually members of the base |
| 3120 | // type. The restriction here is: |
| 3121 | // |
| 3122 | // C++ [expr.ref]p2: |
| 3123 | // ... In these cases, the id-expression shall name a |
| 3124 | // member of the class or of one of its base classes. |
| 3125 | // |
| 3126 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3127 | // an unrelated class, and for us to find an overload set including |
| 3128 | // decls from classes which are not superclasses, as long as the decl |
| 3129 | // we actually pick through overload resolution is from a superclass. |
| 3130 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3131 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3132 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3133 | const LookupResult &R) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3134 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3135 | if (!BaseRT) { |
| 3136 | // We can't check this yet because the base type is still |
| 3137 | // dependent. |
| 3138 | assert(BaseType->isDependentType()); |
| 3139 | return false; |
| 3140 | } |
| 3141 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3142 | |
| 3143 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3144 | // If this is an implicit member reference and we find a |
| 3145 | // non-instance member, it's not an error. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3146 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3147 | return false; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3148 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3149 | // 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] | 3150 | DeclContext *DC = (*I)->getDeclContext(); |
| 3151 | while (DC->isTransparentContext()) |
| 3152 | DC = DC->getParent(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3153 | |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3154 | if (!DC->isRecord()) |
| 3155 | continue; |
| 3156 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3157 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3158 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3159 | |
| 3160 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3161 | return false; |
| 3162 | } |
| 3163 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3164 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3165 | return true; |
| 3166 | } |
| 3167 | |
| 3168 | static bool |
| 3169 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3170 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3171 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3172 | bool HasTemplateArgs) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3173 | RecordDecl *RDecl = RTy->getDecl(); |
| 3174 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3175 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3176 | << BaseRange)) |
| 3177 | return true; |
| 3178 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3179 | if (HasTemplateArgs) { |
| 3180 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3181 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3182 | |
| 3183 | bool MOUS; |
| 3184 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3185 | return false; |
| 3186 | } |
| 3187 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3188 | DeclContext *DC = RDecl; |
| 3189 | if (SS.isSet()) { |
| 3190 | // If the member name was a qualified-id, look into the |
| 3191 | // nested-name-specifier. |
| 3192 | DC = SemaRef.computeDeclContext(SS, false); |
| 3193 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3194 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3195 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3196 | << SS.getRange() << DC; |
| 3197 | return true; |
| 3198 | } |
| 3199 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3200 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3201 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3202 | if (!isa<TypeDecl>(DC)) { |
| 3203 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3204 | << DC << SS.getRange(); |
| 3205 | return true; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3206 | } |
| 3207 | } |
| 3208 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3209 | // The record definition is complete, now look up the member. |
| 3210 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3211 | |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3212 | if (!R.empty()) |
| 3213 | return false; |
| 3214 | |
| 3215 | // We didn't find anything with the given name, so try to correct |
| 3216 | // for typos. |
| 3217 | DeclarationName Name = R.getLookupName(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3218 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3219 | !R.empty() && |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3220 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3221 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3222 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3223 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3224 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3225 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3226 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3227 | << ND->getDeclName(); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3228 | return false; |
| 3229 | } else { |
| 3230 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3231 | R.setLookupName(Name); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3234 | return false; |
| 3235 | } |
| 3236 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3237 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3238 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3239 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3240 | CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3241 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3242 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3243 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3244 | if (BaseType->isDependentType() || |
| 3245 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3246 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3247 | IsArrow, OpLoc, |
| 3248 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3249 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3250 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3251 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3252 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3253 | // Implicit member accesses. |
| 3254 | if (!Base) { |
| 3255 | QualType RecordTy = BaseType; |
| 3256 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3257 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3258 | RecordTy->getAs<RecordType>(), |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3259 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3260 | return ExprError(); |
| 3261 | |
| 3262 | // Explicit member accesses. |
| 3263 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3264 | ExprResult Result = |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3265 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3266 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3267 | |
| 3268 | if (Result.isInvalid()) { |
| 3269 | Owned(Base); |
| 3270 | return ExprError(); |
| 3271 | } |
| 3272 | |
| 3273 | if (Result.get()) |
| 3274 | return move(Result); |
Sebastian Redl | fa1f70f | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3275 | |
| 3276 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3277 | BaseType = Base->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3280 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3281 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3282 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3283 | } |
| 3284 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3285 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3286 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3287 | SourceLocation OpLoc, bool IsArrow, |
| 3288 | const CXXScopeSpec &SS, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3289 | NamedDecl *FirstQualifierInScope, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3290 | LookupResult &R, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3291 | const TemplateArgumentListInfo *TemplateArgs, |
| 3292 | bool SuppressQualifierCheck) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3293 | QualType BaseType = BaseExprType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3294 | if (IsArrow) { |
| 3295 | assert(BaseType->isPointerType()); |
| 3296 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3297 | } |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3298 | R.setBaseObjectType(BaseType); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3299 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3300 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3301 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3302 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3303 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3304 | |
| 3305 | if (R.isAmbiguous()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3306 | return ExprError(); |
| 3307 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3308 | if (R.empty()) { |
| 3309 | // Rederive where we looked up. |
| 3310 | DeclContext *DC = (SS.isSet() |
| 3311 | ? computeDeclContext(SS, false) |
| 3312 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3313 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3314 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3315 | << MemberName << DC |
| 3316 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3317 | return ExprError(); |
| 3318 | } |
| 3319 | |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3320 | // Diagnose lookups that find only declarations from a non-base |
| 3321 | // type. This is possible for either qualified lookups (which may |
| 3322 | // have been qualified with an unrelated type) or implicit member |
| 3323 | // expressions (which were found with unqualified lookup and thus |
| 3324 | // may have come from an enclosing scope). Note that it's okay for |
| 3325 | // lookup to find declarations from a non-base type as long as those |
| 3326 | // aren't the ones picked by overload resolution. |
| 3327 | if ((SS.isSet() || !BaseExpr || |
| 3328 | (isa<CXXThisExpr>(BaseExpr) && |
| 3329 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3330 | !SuppressQualifierCheck && |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3331 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3332 | return ExprError(); |
| 3333 | |
| 3334 | // Construct an unresolved result if we in fact got an unresolved |
| 3335 | // result. |
| 3336 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3337 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 3338 | // pick a member. |
| 3339 | R.suppressDiagnostics(); |
| 3340 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3341 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3342 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3343 | BaseExpr, BaseExprType, |
| 3344 | IsArrow, OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3345 | Qualifier, SS.getRange(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3346 | MemberNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 3347 | TemplateArgs, R.begin(), R.end()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3348 | |
| 3349 | return Owned(MemExpr); |
| 3350 | } |
| 3351 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3352 | assert(R.isSingleResult()); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3353 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3354 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 3355 | |
| 3356 | // FIXME: diagnose the presence of template arguments now. |
| 3357 | |
| 3358 | // If the decl being referenced had an error, return an error for this |
| 3359 | // sub-expr without emitting another error, in order to avoid cascading |
| 3360 | // error cases. |
| 3361 | if (MemberDecl->isInvalidDecl()) |
| 3362 | return ExprError(); |
| 3363 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3364 | // Handle the implicit-member-access case. |
| 3365 | if (!BaseExpr) { |
| 3366 | // 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] | 3367 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3368 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 3370 | SourceLocation Loc = R.getNameLoc(); |
| 3371 | if (SS.getRange().isValid()) |
| 3372 | Loc = SS.getRange().getBegin(); |
| 3373 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3376 | bool ShouldCheckUse = true; |
| 3377 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 3378 | // Don't diagnose the use of a virtual member function unless it's |
| 3379 | // explicitly qualified. |
| 3380 | if (MD->isVirtual() && !SS.isSet()) |
| 3381 | ShouldCheckUse = false; |
| 3382 | } |
| 3383 | |
| 3384 | // Check the use of this member. |
| 3385 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 3386 | Owned(BaseExpr); |
| 3387 | return ExprError(); |
| 3388 | } |
| 3389 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3390 | // Perform a property load on the base regardless of whether we |
| 3391 | // actually need it for the declaration. |
| 3392 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) |
| 3393 | ConvertPropertyForRValue(BaseExpr); |
| 3394 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 3395 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 3396 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 3397 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3398 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3399 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 3400 | // We may have found a field within an anonymous union or struct |
| 3401 | // (C++ [class.union]). |
| 3402 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3403 | BaseExpr, OpLoc); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 3404 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3405 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 3406 | MarkDeclarationReferenced(MemberLoc, Var); |
| 3407 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3408 | Var, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3409 | Var->getType().getNonReferenceType(), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3410 | VK_LValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3413 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3414 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3415 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3416 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3417 | MemberFn->getType(), |
| 3418 | MemberFn->isInstance() ? VK_RValue : VK_LValue, |
| 3419 | OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3420 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3421 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 3424 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 3425 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3426 | Enum, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3427 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | Owned(BaseExpr); |
| 3431 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3432 | // We found something that we didn't expect. Complain. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3433 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3434 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3435 | << MemberName << BaseType << int(IsArrow); |
| 3436 | else |
| 3437 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 3438 | << MemberName << BaseType << int(IsArrow); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3440 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 3441 | << MemberName; |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3442 | R.suppressDiagnostics(); |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 3443 | return ExprError(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3446 | /// Given that normal member access failed on the given expression, |
| 3447 | /// and given that the expression's type involves builtin-id or |
| 3448 | /// builtin-Class, decide whether substituting in the redefinition |
| 3449 | /// types would be profitable. The redefinition type is whatever |
| 3450 | /// this translation unit tried to typedef to id/Class; we store |
| 3451 | /// it to the side and then re-use it in places like this. |
| 3452 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, Expr *&base) { |
| 3453 | const ObjCObjectPointerType *opty |
| 3454 | = base->getType()->getAs<ObjCObjectPointerType>(); |
| 3455 | if (!opty) return false; |
| 3456 | |
| 3457 | const ObjCObjectType *ty = opty->getObjectType(); |
| 3458 | |
| 3459 | QualType redef; |
| 3460 | if (ty->isObjCId()) { |
| 3461 | redef = S.Context.ObjCIdRedefinitionType; |
| 3462 | } else if (ty->isObjCClass()) { |
| 3463 | redef = S.Context.ObjCClassRedefinitionType; |
| 3464 | } else { |
| 3465 | return false; |
| 3466 | } |
| 3467 | |
| 3468 | // Do the substitution as long as the redefinition type isn't just a |
| 3469 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 3470 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 3471 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 3472 | return false; |
| 3473 | |
| 3474 | S.ImpCastExprToType(base, redef, CK_BitCast); |
| 3475 | return true; |
| 3476 | } |
| 3477 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3478 | /// Look up the given member of the given non-type-dependent |
| 3479 | /// expression. This can return in one of two ways: |
| 3480 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 3481 | /// assume that lookup was performed and the results written into |
| 3482 | /// the provided structure. It will take over from there. |
| 3483 | /// * Otherwise, the returned expression will be produced in place of |
| 3484 | /// an ordinary member expression. |
| 3485 | /// |
| 3486 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 3487 | /// fixed for ObjC++. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3488 | ExprResult |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3489 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3490 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3491 | CXXScopeSpec &SS, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3492 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3493 | assert(BaseExpr && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3494 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 3495 | // Perform default conversions. |
| 3496 | DefaultFunctionArrayConversion(BaseExpr); |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3497 | if (IsArrow) DefaultLvalueConversion(BaseExpr); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3498 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 3499 | QualType BaseType = BaseExpr->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3500 | assert(!BaseType->isDependentType()); |
| 3501 | |
| 3502 | DeclarationName MemberName = R.getLookupName(); |
| 3503 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3504 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3505 | // For later type-checking purposes, turn arrow accesses into dot |
| 3506 | // accesses. The only access type we support that doesn't follow |
| 3507 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 3508 | // and those never use arrows, so this is unaffected. |
| 3509 | if (IsArrow) { |
| 3510 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 3511 | BaseType = Ptr->getPointeeType(); |
| 3512 | else if (const ObjCObjectPointerType *Ptr |
| 3513 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 3514 | BaseType = Ptr->getPointeeType(); |
| 3515 | else if (BaseType->isRecordType()) { |
| 3516 | // Recover from arrow accesses to records, e.g.: |
| 3517 | // struct MyRecord foo; |
| 3518 | // foo->bar |
| 3519 | // This is actually well-formed in C++ if MyRecord has an |
| 3520 | // overloaded operator->, but that should have been dealt with |
| 3521 | // by now. |
| 3522 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3523 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 3524 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 3525 | IsArrow = false; |
| 3526 | } else { |
| 3527 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 3528 | << BaseType << BaseExpr->getSourceRange(); |
| 3529 | return ExprError(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3530 | } |
| 3531 | } |
| 3532 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3533 | // Handle field access to simple records. |
| 3534 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| 3535 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 3536 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 3537 | return ExprError(); |
| 3538 | |
| 3539 | // Returning valid-but-null is how we indicate to the caller that |
| 3540 | // the lookup result was filled in. |
| 3541 | return Owned((Expr*) 0); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3542 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3543 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3544 | // Handle ivar access to Objective-C objects. |
| 3545 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3546 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3547 | |
| 3548 | // There are three cases for the base type: |
| 3549 | // - builtin id (qualified or unqualified) |
| 3550 | // - builtin Class (qualified or unqualified) |
| 3551 | // - an interface |
| 3552 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 3553 | if (!IDecl) { |
| 3554 | // There's an implicit 'isa' ivar on all objects. |
| 3555 | // But we only actually find it this way on objects of type 'id', |
| 3556 | // apparently. |
| 3557 | if (OTy->isObjCId() && Member->isStr("isa")) |
| 3558 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, IsArrow, MemberLoc, |
| 3559 | Context.getObjCClassType())); |
| 3560 | |
| 3561 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3562 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3563 | ObjCImpDecl, HasTemplateArgs); |
| 3564 | goto fail; |
| 3565 | } |
| 3566 | |
| 3567 | ObjCInterfaceDecl *ClassDeclared; |
| 3568 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 3569 | |
| 3570 | if (!IV) { |
| 3571 | // Attempt to correct for typos in ivar names. |
| 3572 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3573 | LookupMemberName); |
| 3574 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 3575 | IsArrow ? CTC_ObjCIvarLookup |
| 3576 | : CTC_ObjCPropertyLookup) && |
| 3577 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 3578 | Diag(R.getNameLoc(), |
| 3579 | diag::err_typecheck_member_reference_ivar_suggest) |
| 3580 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 3581 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3582 | IV->getNameAsString()); |
| 3583 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 3584 | << IV->getDeclName(); |
| 3585 | } else { |
| 3586 | Res.clear(); |
| 3587 | Res.setLookupName(Member); |
| 3588 | |
| 3589 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 3590 | << IDecl->getDeclName() << MemberName |
| 3591 | << BaseExpr->getSourceRange(); |
| 3592 | return ExprError(); |
| 3593 | } |
| 3594 | } |
| 3595 | |
| 3596 | // If the decl being referenced had an error, return an error for this |
| 3597 | // sub-expr without emitting another error, in order to avoid cascading |
| 3598 | // error cases. |
| 3599 | if (IV->isInvalidDecl()) |
| 3600 | return ExprError(); |
| 3601 | |
| 3602 | // Check whether we can reference this field. |
| 3603 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 3604 | return ExprError(); |
| 3605 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 3606 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 3607 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 3608 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 3609 | ClassOfMethodDecl = MD->getClassInterface(); |
| 3610 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 3611 | // Case of a c-function declared inside an objc implementation. |
| 3612 | // FIXME: For a c-style function nested inside an objc implementation |
| 3613 | // class, there is no implementation context available, so we pass |
| 3614 | // down the context as argument to this routine. Ideally, this context |
| 3615 | // need be passed down in the AST node and somehow calculated from the |
| 3616 | // AST for a function decl. |
| 3617 | if (ObjCImplementationDecl *IMPD = |
| 3618 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 3619 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 3620 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 3621 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 3622 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 3623 | } |
| 3624 | |
| 3625 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 3626 | if (ClassDeclared != IDecl || |
| 3627 | ClassOfMethodDecl != ClassDeclared) |
| 3628 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 3629 | << IV->getDeclName(); |
| 3630 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 3631 | // @protected |
| 3632 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 3633 | << IV->getDeclName(); |
| 3634 | } |
| 3635 | |
| 3636 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 3637 | MemberLoc, BaseExpr, |
| 3638 | IsArrow)); |
| 3639 | } |
| 3640 | |
| 3641 | // Objective-C property access. |
| 3642 | const ObjCObjectPointerType *OPT; |
| 3643 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 3644 | // This actually uses the base as an r-value. |
| 3645 | DefaultLvalueConversion(BaseExpr); |
| 3646 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr->getType())); |
| 3647 | |
| 3648 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3649 | |
| 3650 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 3651 | |
| 3652 | // id, with and without qualifiers. |
| 3653 | if (OT->isObjCId()) { |
| 3654 | // Check protocols on qualified interfaces. |
| 3655 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 3656 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 3657 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 3658 | // Check the use of this declaration |
| 3659 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3660 | return ExprError(); |
| 3661 | |
| 3662 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 3663 | VK_LValue, |
| 3664 | OK_ObjCProperty, |
| 3665 | MemberLoc, |
| 3666 | BaseExpr)); |
| 3667 | } |
| 3668 | |
| 3669 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3670 | // Check the use of this method. |
| 3671 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3672 | return ExprError(); |
| 3673 | Selector SetterSel = |
| 3674 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3675 | PP.getSelectorTable(), Member); |
| 3676 | ObjCMethodDecl *SMD = 0; |
| 3677 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 3678 | SetterSel, Context)) |
| 3679 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| 3680 | QualType PType = OMD->getSendResultType(); |
| 3681 | |
| 3682 | ExprValueKind VK = VK_LValue; |
| 3683 | if (!getLangOptions().CPlusPlus && |
| 3684 | IsCForbiddenLValueType(Context, PType)) |
| 3685 | VK = VK_RValue; |
| 3686 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 3687 | |
| 3688 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 3689 | VK, OK, |
| 3690 | MemberLoc, BaseExpr)); |
| 3691 | } |
| 3692 | } |
| 3693 | |
| 3694 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3695 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3696 | ObjCImpDecl, HasTemplateArgs); |
| 3697 | |
| 3698 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 3699 | << MemberName << BaseType); |
| 3700 | } |
| 3701 | |
| 3702 | // 'Class', unqualified only. |
| 3703 | if (OT->isObjCClass()) { |
| 3704 | // Only works in a method declaration (??!). |
| 3705 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 3706 | if (!MD) { |
| 3707 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3708 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3709 | ObjCImpDecl, HasTemplateArgs); |
| 3710 | |
| 3711 | goto fail; |
| 3712 | } |
| 3713 | |
| 3714 | // Also must look for a getter name which uses property syntax. |
| 3715 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3716 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 3717 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3718 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 3719 | // Check the use of this method. |
| 3720 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3721 | return ExprError(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3722 | } else |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3723 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3724 | // If we found a getter then this may be a valid dot-reference, we |
| 3725 | // will look for the matching setter, in case it is needed. |
| 3726 | Selector SetterSel = |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3727 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3728 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3729 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 3730 | if (!Setter) { |
| 3731 | // If this reference is in an @implementation, also check for 'private' |
| 3732 | // methods. |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 3733 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3734 | } |
| 3735 | // Look through local category implementations associated with the class. |
| 3736 | if (!Setter) |
| 3737 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3738 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3739 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3740 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3741 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3742 | if (Getter || Setter) { |
| 3743 | QualType PType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3744 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3745 | ExprValueKind VK = VK_LValue; |
| 3746 | if (Getter) { |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3747 | PType = Getter->getSendResultType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3748 | if (!getLangOptions().CPlusPlus && |
| 3749 | IsCForbiddenLValueType(Context, PType)) |
| 3750 | VK = VK_RValue; |
| 3751 | } else { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3752 | // Get the expression type from Setter's incoming parameter. |
| 3753 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3754 | } |
| 3755 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 3756 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3757 | // FIXME: we must check that the setter has property type. |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 3758 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 3759 | PType, VK, OK, |
| 3760 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3761 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3762 | |
| 3763 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 3764 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3765 | ObjCImpDecl, HasTemplateArgs); |
| 3766 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3767 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3768 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3769 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3770 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3771 | // Normal property access. |
| 3772 | return HandleExprPropertyRefExpr(OPT, BaseExpr, MemberName, MemberLoc, |
| 3773 | SourceLocation(), QualType(), false); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3774 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3775 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3776 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3777 | if (BaseType->isExtVectorType()) { |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3778 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3779 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3780 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr->getValueKind()); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3781 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 3782 | Member, MemberLoc); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3783 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3784 | return ExprError(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3785 | |
| 3786 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr, |
| 3787 | *Member, MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3788 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3789 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 3790 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 3791 | // not just a pointer to builtin-sel again. |
| 3792 | if (IsArrow && |
| 3793 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 3794 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
| 3795 | ImpCastExprToType(BaseExpr, Context.ObjCSelRedefinitionType, CK_BitCast); |
| 3796 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3797 | ObjCImpDecl, HasTemplateArgs); |
| 3798 | } |
| 3799 | |
| 3800 | // Failure cases. |
| 3801 | fail: |
| 3802 | |
| 3803 | // There's a possible road to recovery for function types. |
| 3804 | const FunctionType *Fun = 0; |
| 3805 | |
| 3806 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 3807 | if ((Fun = Ptr->getPointeeType()->getAs<FunctionType>())) { |
| 3808 | // fall out, handled below. |
| 3809 | |
| 3810 | // Recover from dot accesses to pointers, e.g.: |
| 3811 | // type *foo; |
| 3812 | // foo.bar |
| 3813 | // This is actually well-formed in two cases: |
| 3814 | // - 'type' is an Objective C type |
| 3815 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 3816 | // the appropriate pointer type |
| 3817 | } else if (Ptr->getPointeeType()->isRecordType() && |
| 3818 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 3819 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3820 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 3821 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 3822 | |
| 3823 | // Recurse as an -> access. |
| 3824 | IsArrow = true; |
| 3825 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3826 | ObjCImpDecl, HasTemplateArgs); |
| 3827 | } |
| 3828 | } else { |
| 3829 | Fun = BaseType->getAs<FunctionType>(); |
| 3830 | } |
| 3831 | |
| 3832 | // If the user is trying to apply -> or . to a function pointer |
| 3833 | // type, it's probably because they forgot parentheses to call that |
| 3834 | // function. Suggest the addition of those parentheses, build the |
| 3835 | // call, and continue on. |
| 3836 | if (Fun || BaseType == Context.OverloadTy) { |
| 3837 | bool TryCall; |
| 3838 | if (BaseType == Context.OverloadTy) { |
| 3839 | TryCall = true; |
| 3840 | } else { |
| 3841 | if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(Fun)) { |
| 3842 | TryCall = (FPT->getNumArgs() == 0); |
| 3843 | } else { |
| 3844 | TryCall = true; |
| 3845 | } |
| 3846 | |
| 3847 | if (TryCall) { |
| 3848 | QualType ResultTy = Fun->getResultType(); |
| 3849 | TryCall = (!IsArrow && ResultTy->isRecordType()) || |
| 3850 | (IsArrow && ResultTy->isPointerType() && |
| 3851 | ResultTy->getAs<PointerType>()->getPointeeType()->isRecordType()); |
| 3852 | } |
| 3853 | } |
| 3854 | |
| 3855 | |
| 3856 | if (TryCall) { |
| 3857 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 3858 | Diag(BaseExpr->getExprLoc(), diag::err_member_reference_needs_call) |
| 3859 | << QualType(Fun, 0) |
| 3860 | << FixItHint::CreateInsertion(Loc, "()"); |
| 3861 | |
| 3862 | ExprResult NewBase |
| 3863 | = ActOnCallExpr(0, BaseExpr, Loc, MultiExprArg(*this, 0, 0), Loc); |
| 3864 | if (NewBase.isInvalid()) |
| 3865 | return ExprError(); |
| 3866 | BaseExpr = NewBase.takeAs<Expr>(); |
| 3867 | |
| 3868 | |
| 3869 | DefaultFunctionArrayConversion(BaseExpr); |
| 3870 | BaseType = BaseExpr->getType(); |
| 3871 | |
| 3872 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 3873 | ObjCImpDecl, HasTemplateArgs); |
| 3874 | } |
| 3875 | } |
| 3876 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3877 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 3878 | << BaseType << BaseExpr->getSourceRange(); |
| 3879 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3880 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3883 | /// The main callback when the parser finds something like |
| 3884 | /// expression . [nested-name-specifier] identifier |
| 3885 | /// expression -> [nested-name-specifier] identifier |
| 3886 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3887 | /// possibilities, including destructor and operator references. |
| 3888 | /// |
| 3889 | /// \param OpKind either tok::arrow or tok::period |
| 3890 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3891 | /// is used to diagnose mis-uses of special members that can |
| 3892 | /// only be called |
| 3893 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3894 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3895 | /// aren't properly put in the context chain |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3896 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 3897 | SourceLocation OpLoc, |
| 3898 | tok::TokenKind OpKind, |
| 3899 | CXXScopeSpec &SS, |
| 3900 | UnqualifiedId &Id, |
| 3901 | Decl *ObjCImpDecl, |
| 3902 | bool HasTrailingLParen) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3903 | if (SS.isSet() && SS.isInvalid()) |
| 3904 | return ExprError(); |
| 3905 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 3906 | // Warn about the explicit constructor calls Microsoft extension. |
| 3907 | if (getLangOptions().Microsoft && |
| 3908 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 3909 | Diag(Id.getSourceRange().getBegin(), |
| 3910 | diag::ext_ms_explicit_constructor_call); |
| 3911 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3912 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3913 | |
| 3914 | // Decompose the name into its component parts. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3915 | DeclarationNameInfo NameInfo; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3916 | const TemplateArgumentListInfo *TemplateArgs; |
| 3917 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3918 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3919 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3920 | DeclarationName Name = NameInfo.getName(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3921 | bool IsArrow = (OpKind == tok::arrow); |
| 3922 | |
| 3923 | NamedDecl *FirstQualifierInScope |
| 3924 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3925 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3926 | |
| 3927 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3928 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3929 | if (Result.isInvalid()) return ExprError(); |
| 3930 | Base = Result.take(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3931 | |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3932 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 3933 | isDependentScopeSpecifier(SS)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3934 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3935 | IsArrow, OpLoc, |
| 3936 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3937 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3938 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3939 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3940 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 3941 | SS, ObjCImpDecl, TemplateArgs != 0); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3942 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3943 | if (Result.isInvalid()) { |
| 3944 | Owned(Base); |
| 3945 | return ExprError(); |
| 3946 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3947 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3948 | if (Result.get()) { |
| 3949 | // The only way a reference to a destructor can be used is to |
| 3950 | // immediately call it, which falls into this case. If the |
| 3951 | // next token is not a '(', produce a diagnostic and build the |
| 3952 | // call now. |
| 3953 | if (!HasTrailingLParen && |
| 3954 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3955 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3956 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3957 | return move(Result); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3960 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3961 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3962 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
| 3965 | return move(Result); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3968 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3969 | FunctionDecl *FD, |
| 3970 | ParmVarDecl *Param) { |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3971 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3972 | Diag(CallLoc, |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3973 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3974 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3976 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3977 | return ExprError(); |
| 3978 | } |
| 3979 | |
| 3980 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3981 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3983 | // Instantiate the expression. |
| 3984 | MultiLevelTemplateArgumentList ArgList |
| 3985 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3986 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3987 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3988 | = ArgList.getInnermost(); |
| 3989 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3990 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3991 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3992 | ExprResult Result; |
| 3993 | { |
| 3994 | // C++ [dcl.fct.default]p5: |
| 3995 | // The names in the [default argument] expression are bound, and |
| 3996 | // the semantic constraints are checked, at the point where the |
| 3997 | // default argument expression appears. |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3998 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3999 | Result = SubstExpr(UninstExpr, ArgList); |
| 4000 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4001 | if (Result.isInvalid()) |
| 4002 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4003 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4004 | // Check the expression as an initializer for the parameter. |
| 4005 | InitializedEntity Entity |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4006 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4007 | InitializationKind Kind |
| 4008 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4009 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4010 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4011 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4012 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4013 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4014 | MultiExprArg(*this, &ResultE, 1)); |
| 4015 | if (Result.isInvalid()) |
| 4016 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4017 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4018 | // Build the default argument expression. |
| 4019 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4020 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4023 | // If the default expression creates temporaries, we need to |
| 4024 | // push them to the current stack of expression temporaries so they'll |
| 4025 | // be properly destroyed. |
| 4026 | // FIXME: We should really be rebuilding the default argument with new |
| 4027 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4028 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4029 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4030 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4031 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4032 | ExprTemporaries.push_back(Temporary); |
| 4033 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4034 | |
| 4035 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4036 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4037 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4038 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4039 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4042 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4043 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4044 | /// function prototype Proto. Call is the call expression itself, and |
| 4045 | /// Fn is the function expression. For a C++ member function, this |
| 4046 | /// routine does not attempt to convert the object argument. Returns |
| 4047 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4048 | bool |
| 4049 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4050 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4051 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4052 | Expr **Args, unsigned NumArgs, |
| 4053 | SourceLocation RParenLoc) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4054 | // 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] | 4055 | // assignment, to the types of the corresponding parameter, ... |
| 4056 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4057 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4058 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4059 | // If too few arguments are available (and we don't have default |
| 4060 | // arguments for the remaining parameters), don't make the call. |
| 4061 | if (NumArgs < NumArgsInProto) { |
| 4062 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4063 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4064 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4065 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4066 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | // If too many are passed and not variadic, error on the extras and drop |
| 4070 | // them. |
| 4071 | if (NumArgs > NumArgsInProto) { |
| 4072 | if (!Proto->isVariadic()) { |
| 4073 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4074 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4075 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4076 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4077 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4078 | Args[NumArgs-1]->getLocEnd()); |
| 4079 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4080 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4081 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4082 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4083 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4084 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4085 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4086 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4087 | if (Fn->getType()->isBlockPointerType()) |
| 4088 | CallType = VariadicBlock; // Block |
| 4089 | else if (isa<MemberExpr>(Fn)) |
| 4090 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4091 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4092 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4093 | if (Invalid) |
| 4094 | return true; |
| 4095 | unsigned TotalNumArgs = AllArgs.size(); |
| 4096 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4097 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4098 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4099 | return false; |
| 4100 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4101 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4102 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4103 | FunctionDecl *FDecl, |
| 4104 | const FunctionProtoType *Proto, |
| 4105 | unsigned FirstProtoArg, |
| 4106 | Expr **Args, unsigned NumArgs, |
| 4107 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4108 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4109 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4110 | unsigned NumArgsToCheck = NumArgs; |
| 4111 | bool Invalid = false; |
| 4112 | if (NumArgs != NumArgsInProto) |
| 4113 | // Use default arguments for missing arguments |
| 4114 | NumArgsToCheck = NumArgsInProto; |
| 4115 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4116 | // 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] | 4117 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4118 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4119 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4120 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4121 | if (ArgIx < NumArgs) { |
| 4122 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4123 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4124 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4125 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4126 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4127 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4128 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4129 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4130 | // Pass the argument |
| 4131 | ParmVarDecl *Param = 0; |
| 4132 | if (FDecl && i < FDecl->getNumParams()) |
| 4133 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4134 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4135 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4136 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4137 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4138 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4139 | SourceLocation(), |
| 4140 | Owned(Arg)); |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4141 | if (ArgE.isInvalid()) |
| 4142 | return true; |
| 4143 | |
| 4144 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4145 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4146 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4147 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4148 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4149 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4150 | if (ArgExpr.isInvalid()) |
| 4151 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4152 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4153 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4154 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4155 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4156 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4157 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4158 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4159 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4160 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4161 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4162 | Expr *Arg = Args[i]; |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 4163 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType, FDecl); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4164 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4165 | } |
| 4166 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4167 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4168 | } |
| 4169 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4170 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4171 | /// This provides the location of the left/right parens and a list of comma |
| 4172 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4173 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4174 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4175 | MultiExprArg args, SourceLocation RParenLoc) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4176 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4177 | |
| 4178 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4179 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4180 | if (Result.isInvalid()) return ExprError(); |
| 4181 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4182 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4183 | Expr **Args = args.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4185 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4186 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4187 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4188 | if (NumArgs > 0) { |
| 4189 | // Pseudo-destructor calls should not have any arguments. |
| 4190 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4191 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4192 | SourceRange(Args[0]->getLocStart(), |
| 4193 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4194 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4195 | NumArgs = 0; |
| 4196 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4198 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4199 | VK_RValue, RParenLoc)); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4200 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4201 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4202 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4203 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4204 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4205 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4206 | bool Dependent = false; |
| 4207 | if (Fn->isTypeDependent()) |
| 4208 | Dependent = true; |
| 4209 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4210 | Dependent = true; |
| 4211 | |
| 4212 | if (Dependent) |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 4213 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4214 | Context.DependentTy, VK_RValue, |
| 4215 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4216 | |
| 4217 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4218 | if (Fn->getType()->isRecordType()) |
| 4219 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4220 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4221 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4222 | Expr *NakedFn = Fn->IgnoreParens(); |
| 4223 | |
| 4224 | // Determine whether this is a call to an unresolved member function. |
| 4225 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 4226 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 4227 | // an unresolved using declaration), it has to be an overloaded |
| 4228 | // function set, which means it must contain either multiple |
| 4229 | // declarations (all methods or method templates) or a single |
| 4230 | // method template. |
| 4231 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4232 | isa<FunctionTemplateDecl>( |
| 4233 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 8f184a3 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 4234 | (void)MemE; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4235 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4236 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4237 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4238 | } |
| 4239 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4240 | // Determine whether this is a call to a member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4241 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4242 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4243 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4244 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4245 | RParenLoc); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 4246 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4247 | |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4248 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4249 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4250 | if (BO->getOpcode() == BO_PtrMemD || |
| 4251 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | c8be952 | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 4252 | if (const FunctionProtoType *FPT |
| 4253 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4254 | QualType ResultTy = FPT->getCallResultType(Context); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4255 | ExprValueKind VK = Expr::getValueKindForType(FPT->getResultType()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4256 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4257 | CXXMemberCallExpr *TheCall |
Abramo Bagnara | 21e9d86 | 2010-12-03 21:39:42 +0000 | [diff] [blame] | 4258 | = new (Context) CXXMemberCallExpr(Context, Fn, Args, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4259 | NumArgs, ResultTy, VK, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4260 | RParenLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4261 | |
| 4262 | if (CheckCallReturnType(FPT->getResultType(), |
| 4263 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4264 | TheCall, 0)) |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4265 | return ExprError(); |
Anders Carlsson | 63dce02 | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 4266 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4267 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4268 | RParenLoc)) |
| 4269 | return ExprError(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4270 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4271 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4272 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4273 | return ExprError(Diag(Fn->getLocStart(), |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 4274 | diag::err_typecheck_call_not_function) |
| 4275 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4276 | } |
| 4277 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4278 | } |
| 4279 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4280 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4281 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 4282 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4283 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4284 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4285 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 4286 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 4287 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 4288 | RParenLoc); |
| 4289 | } |
| 4290 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4291 | NamedDecl *NDecl = 0; |
Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 4292 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 4293 | if (UnOp->getOpcode() == UO_AddrOf) |
| 4294 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 4295 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4296 | if (isa<DeclRefExpr>(NakedFn)) |
| 4297 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 4298 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4299 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 4300 | } |
| 4301 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4302 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 4303 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4304 | /// unary-convert to an expression of function-pointer or |
| 4305 | /// block-pointer type. |
| 4306 | /// |
| 4307 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4308 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4309 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 4310 | SourceLocation LParenLoc, |
| 4311 | Expr **Args, unsigned NumArgs, |
| 4312 | SourceLocation RParenLoc) { |
| 4313 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 4314 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4315 | // Promote the function operand. |
| 4316 | UsualUnaryConversions(Fn); |
| 4317 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4318 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 4319 | // of arguments and function on error. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4320 | CallExpr *TheCall = new (Context) CallExpr(Context, Fn, |
| 4321 | Args, NumArgs, |
| 4322 | Context.BoolTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4323 | VK_RValue, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4324 | RParenLoc); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4325 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4326 | const FunctionType *FuncT; |
| 4327 | if (!Fn->getType()->isBlockPointerType()) { |
| 4328 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 4329 | // have type pointer to function". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4330 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4331 | if (PT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4332 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4333 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4334 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4335 | } else { // This is a block call. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4336 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4337 | getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 4338 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4339 | if (FuncT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4340 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4341 | << Fn->getType() << Fn->getSourceRange()); |
| 4342 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4343 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4344 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4345 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4346 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4347 | return ExprError(); |
| 4348 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4349 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4350 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4351 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4353 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4354 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4355 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4356 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4357 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4358 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4360 | if (FDecl) { |
| 4361 | // Check if we have too few/too many template arguments, based |
| 4362 | // on our knowledge of the function definition. |
| 4363 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4364 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4365 | const FunctionProtoType *Proto |
| 4366 | = Def->getType()->getAs<FunctionProtoType>(); |
| 4367 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4368 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4369 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4370 | } |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4371 | |
| 4372 | // If the function we're calling isn't a function prototype, but we have |
| 4373 | // a function prototype from a prior declaratiom, use that prototype. |
| 4374 | if (!FDecl->hasPrototype()) |
| 4375 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4376 | } |
| 4377 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4378 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4379 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4380 | Expr *Arg = Args[i]; |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4381 | |
| 4382 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4383 | InitializedEntity Entity |
| 4384 | = InitializedEntity::InitializeParameter(Context, |
| 4385 | Proto->getArgType(i)); |
| 4386 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4387 | SourceLocation(), |
| 4388 | Owned(Arg)); |
| 4389 | if (ArgE.isInvalid()) |
| 4390 | return true; |
| 4391 | |
| 4392 | Arg = ArgE.takeAs<Expr>(); |
| 4393 | |
| 4394 | } else { |
| 4395 | DefaultArgumentPromotion(Arg); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4396 | } |
| 4397 | |
Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4398 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4399 | Arg->getType(), |
| 4400 | PDiag(diag::err_call_incomplete_argument) |
| 4401 | << Arg->getSourceRange())) |
| 4402 | return ExprError(); |
| 4403 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4404 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4405 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 4406 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4408 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4409 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4410 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4411 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4412 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4413 | // Check for sentinels |
| 4414 | if (NDecl) |
| 4415 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4416 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4417 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4418 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4419 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4420 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4421 | |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4422 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
| 4423 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4424 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4425 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4426 | return ExprError(); |
| 4427 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4428 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4429 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4432 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4433 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4434 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4435 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4436 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4437 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4438 | |
| 4439 | TypeSourceInfo *TInfo; |
| 4440 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4441 | if (!TInfo) |
| 4442 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4443 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4444 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4447 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4448 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4449 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4450 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4451 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4452 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4453 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 4454 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 4455 | << SourceRange(LParenLoc, |
| 4456 | literalExpr->getSourceRange().getEnd()))) |
| 4457 | return ExprError(); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4458 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4459 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 4460 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4461 | } else if (!literalType->isDependentType() && |
| 4462 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4463 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4464 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4465 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4466 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4467 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4468 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4469 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4470 | InitializationKind Kind |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4471 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4472 | /*IsCStyleCast=*/true); |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4473 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4474 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 4475 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4476 | &literalType); |
| 4477 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4478 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4479 | literalExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4480 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4481 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4482 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4483 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4484 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4485 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4486 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4487 | // In C, compound literals are l-values for some reason. |
| 4488 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 4489 | |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 4490 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4491 | VK, literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4494 | ExprResult |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4495 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4496 | SourceLocation RBraceLoc) { |
| 4497 | unsigned NumInit = initlist.size(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4498 | Expr **InitList = initlist.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 4499 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4500 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4501 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4502 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4503 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 4504 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4505 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4506 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4507 | } |
| 4508 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4509 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4510 | /// except the final cast and returning the kind required. |
| 4511 | static CastKind PrepareScalarCast(Sema &S, Expr *&Src, QualType DestTy) { |
| 4512 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4513 | // Also, callers should have filtered out the invalid cases with |
| 4514 | // pointers. Everything else should be possible. |
| 4515 | |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4516 | QualType SrcTy = Src->getType(); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4517 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4518 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4519 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4520 | switch (SrcTy->getScalarTypeKind()) { |
| 4521 | case Type::STK_MemberPointer: |
| 4522 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4523 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4524 | case Type::STK_Pointer: |
| 4525 | switch (DestTy->getScalarTypeKind()) { |
| 4526 | case Type::STK_Pointer: |
| 4527 | return DestTy->isObjCObjectPointerType() ? |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4528 | CK_AnyPointerToObjCPointerCast : |
| 4529 | CK_BitCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4530 | case Type::STK_Bool: |
| 4531 | return CK_PointerToBoolean; |
| 4532 | case Type::STK_Integral: |
| 4533 | return CK_PointerToIntegral; |
| 4534 | case Type::STK_Floating: |
| 4535 | case Type::STK_FloatingComplex: |
| 4536 | case Type::STK_IntegralComplex: |
| 4537 | case Type::STK_MemberPointer: |
| 4538 | llvm_unreachable("illegal cast from pointer"); |
| 4539 | } |
| 4540 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4541 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4542 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4543 | case Type::STK_Integral: |
| 4544 | switch (DestTy->getScalarTypeKind()) { |
| 4545 | case Type::STK_Pointer: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4546 | if (Src->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4547 | return CK_NullToPointer; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4548 | return CK_IntegralToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4549 | case Type::STK_Bool: |
| 4550 | return CK_IntegralToBoolean; |
| 4551 | case Type::STK_Integral: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4552 | return CK_IntegralCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4553 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4554 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4555 | case Type::STK_IntegralComplex: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4556 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4557 | CK_IntegralCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4558 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4559 | case Type::STK_FloatingComplex: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4560 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4561 | CK_IntegralToFloating); |
| 4562 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4563 | case Type::STK_MemberPointer: |
| 4564 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4565 | } |
| 4566 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4567 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4568 | case Type::STK_Floating: |
| 4569 | switch (DestTy->getScalarTypeKind()) { |
| 4570 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4571 | return CK_FloatingCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4572 | case Type::STK_Bool: |
| 4573 | return CK_FloatingToBoolean; |
| 4574 | case Type::STK_Integral: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4575 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4576 | case Type::STK_FloatingComplex: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4577 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4578 | CK_FloatingCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4579 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4580 | case Type::STK_IntegralComplex: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4581 | S.ImpCastExprToType(Src, DestTy->getAs<ComplexType>()->getElementType(), |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4582 | CK_FloatingToIntegral); |
| 4583 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4584 | case Type::STK_Pointer: |
| 4585 | llvm_unreachable("valid float->pointer cast?"); |
| 4586 | case Type::STK_MemberPointer: |
| 4587 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4588 | } |
| 4589 | break; |
| 4590 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4591 | case Type::STK_FloatingComplex: |
| 4592 | switch (DestTy->getScalarTypeKind()) { |
| 4593 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4594 | return CK_FloatingComplexCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4595 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4596 | return CK_FloatingComplexToIntegralComplex; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4597 | case Type::STK_Floating: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4598 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4599 | if (S.Context.hasSameType(ET, DestTy)) |
| 4600 | return CK_FloatingComplexToReal; |
| 4601 | S.ImpCastExprToType(Src, ET, CK_FloatingComplexToReal); |
| 4602 | return CK_FloatingCast; |
| 4603 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4604 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4605 | return CK_FloatingComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4606 | case Type::STK_Integral: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4607 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4608 | CK_FloatingComplexToReal); |
| 4609 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4610 | case Type::STK_Pointer: |
| 4611 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4612 | case Type::STK_MemberPointer: |
| 4613 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4614 | } |
| 4615 | break; |
| 4616 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4617 | case Type::STK_IntegralComplex: |
| 4618 | switch (DestTy->getScalarTypeKind()) { |
| 4619 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4620 | return CK_IntegralComplexToFloatingComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4621 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4622 | return CK_IntegralComplexCast; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4623 | case Type::STK_Integral: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4624 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4625 | if (S.Context.hasSameType(ET, DestTy)) |
| 4626 | return CK_IntegralComplexToReal; |
| 4627 | S.ImpCastExprToType(Src, ET, CK_IntegralComplexToReal); |
| 4628 | return CK_IntegralCast; |
| 4629 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4630 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4631 | return CK_IntegralComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4632 | case Type::STK_Floating: |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4633 | S.ImpCastExprToType(Src, SrcTy->getAs<ComplexType>()->getElementType(), |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4634 | CK_IntegralComplexToReal); |
| 4635 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4636 | case Type::STK_Pointer: |
| 4637 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4638 | case Type::STK_MemberPointer: |
| 4639 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4640 | } |
| 4641 | break; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4642 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4643 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4644 | llvm_unreachable("Unhandled scalar cast"); |
| 4645 | return CK_BitCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4648 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4649 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 4650 | Expr *&castExpr, CastKind& Kind, ExprValueKind &VK, |
| 4651 | CXXCastPath &BasePath, bool FunctionalStyle) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4652 | if (getLangOptions().CPlusPlus) |
Douglas Gregor | 15417cf | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 4653 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 4654 | castExpr->getLocEnd()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4655 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4656 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4657 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4658 | // We only support r-value casts in C. |
| 4659 | VK = VK_RValue; |
| 4660 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4661 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4662 | // type needs to be scalar. |
| 4663 | if (castType->isVoidType()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4664 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| 4665 | IgnoredValueConversions(castExpr); |
| 4666 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4667 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4668 | Kind = CK_ToVoid; |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4669 | return false; |
| 4670 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4671 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4672 | DefaultFunctionArrayLvalueConversion(castExpr); |
| 4673 | |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4674 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 4675 | diag::err_typecheck_cast_to_incomplete)) |
| 4676 | return true; |
| 4677 | |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4678 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4679 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4680 | (castType->isStructureType() || castType->isUnionType())) { |
| 4681 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4682 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4683 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4684 | << castType << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4685 | Kind = CK_NoOp; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4686 | return false; |
| 4687 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4688 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4689 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4690 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4691 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4692 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4693 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4694 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4695 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4696 | castExpr->getType()) && |
| 4697 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4698 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4699 | << castExpr->getSourceRange(); |
| 4700 | break; |
| 4701 | } |
| 4702 | } |
| 4703 | if (Field == FieldEnd) |
| 4704 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 4705 | << castExpr->getType() << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4706 | Kind = CK_ToUnion; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4707 | return false; |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4708 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4709 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4710 | // Reject any other conversions to non-scalar types. |
| 4711 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 4712 | << castType << castExpr->getSourceRange(); |
| 4713 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4714 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4715 | // The type we're casting to is known to be a scalar or vector. |
| 4716 | |
| 4717 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4718 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4719 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4720 | return Diag(castExpr->getLocStart(), |
| 4721 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4722 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4723 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4724 | |
| 4725 | if (castType->isExtVectorType()) |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4726 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4727 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4728 | if (castType->isVectorType()) |
| 4729 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 4730 | if (castExpr->getType()->isVectorType()) |
| 4731 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 4732 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4733 | // The source and target types are both scalars, i.e. |
| 4734 | // - arithmetic types (fundamental, enum, and complex) |
| 4735 | // - all kinds of pointers |
| 4736 | // Note that member pointers were filtered out with C++, above. |
| 4737 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4738 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 4739 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4740 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4741 | // If either type is a pointer, the other type has to be either an |
| 4742 | // integer or a pointer. |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4743 | if (!castType->isArithmeticType()) { |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4744 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4745 | if (!castExprType->isIntegralType(Context) && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4746 | castExprType->isArithmeticType()) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4747 | return Diag(castExpr->getLocStart(), |
| 4748 | diag::err_cast_pointer_from_non_pointer_int) |
| 4749 | << castExprType << castExpr->getSourceRange(); |
| 4750 | } else if (!castExpr->getType()->isArithmeticType()) { |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4751 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4752 | return Diag(castExpr->getLocStart(), |
| 4753 | diag::err_cast_pointer_to_non_pointer_int) |
| 4754 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4755 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4756 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4757 | Kind = PrepareScalarCast(*this, castExpr, castType); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4758 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4759 | if (Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4760 | CheckCastAlign(castExpr, castType, TyR); |
| 4761 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4762 | return false; |
| 4763 | } |
| 4764 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4765 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4766 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4767 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4768 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4769 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4770 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4771 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4772 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4773 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4774 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4775 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4776 | } else |
| 4777 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4778 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4779 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4780 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4781 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4782 | return false; |
| 4783 | } |
| 4784 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4785 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4786 | CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4787 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4788 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4789 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4790 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4791 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4792 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4793 | if (SrcTy->isVectorType()) { |
| 4794 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 4795 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 4796 | << DestTy << SrcTy << R; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4797 | Kind = CK_BitCast; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4798 | return false; |
| 4799 | } |
| 4800 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4801 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4802 | // conversion will take place first from scalar to elt type, and then |
| 4803 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4804 | if (SrcTy->isPointerType()) |
| 4805 | return Diag(R.getBegin(), |
| 4806 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4807 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4808 | |
| 4809 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 4810 | ImpCastExprToType(CastExpr, DestElemTy, |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4811 | PrepareScalarCast(*this, CastExpr, DestElemTy)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4812 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4813 | Kind = CK_VectorSplat; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4814 | return false; |
| 4815 | } |
| 4816 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4817 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4818 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4819 | SourceLocation RParenLoc, Expr *castExpr) { |
| 4820 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4821 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4822 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4823 | TypeSourceInfo *castTInfo; |
| 4824 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 4825 | if (!castTInfo) |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4826 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4827 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4828 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 4829 | if (isa<ParenListExpr>(castExpr)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4830 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4831 | castTInfo); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4832 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4833 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4836 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4837 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4838 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4839 | CastKind Kind = CK_Invalid; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4840 | ExprValueKind VK = VK_RValue; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4841 | CXXCastPath BasePath; |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4842 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4843 | Kind, VK, BasePath)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4844 | return ExprError(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4845 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4846 | return Owned(CStyleCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 4847 | Ty->getType().getNonLValueExprType(Context), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4848 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4849 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4850 | } |
| 4851 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4852 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4853 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4854 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4855 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4856 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4857 | if (!E) |
| 4858 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4859 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4860 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4861 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4862 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4863 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4864 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4866 | if (Result.isInvalid()) return ExprError(); |
| 4867 | |
| 4868 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4869 | } |
| 4870 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4871 | ExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4872 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4873 | SourceLocation RParenLoc, Expr *Op, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4874 | TypeSourceInfo *TInfo) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4875 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4876 | QualType Ty = TInfo->getType(); |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4877 | bool isAltiVecLiteral = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4878 | |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4879 | // Check for an altivec literal, |
| 4880 | // i.e. all the elements are integer constants. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4881 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 4882 | if (PE->getNumExprs() == 0) { |
| 4883 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4884 | return ExprError(); |
| 4885 | } |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4886 | if (PE->getNumExprs() == 1) { |
| 4887 | if (!PE->getExpr(0)->getType()->isVectorType()) |
| 4888 | isAltiVecLiteral = true; |
| 4889 | } |
| 4890 | else |
| 4891 | isAltiVecLiteral = true; |
| 4892 | } |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4893 | |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4894 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
| 4895 | // then handle it as such. |
| 4896 | if (isAltiVecLiteral) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4897 | llvm::SmallVector<Expr *, 8> initExprs; |
| 4898 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 4899 | initExprs.push_back(PE->getExpr(i)); |
| 4900 | |
| 4901 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4902 | // braces instead of the original commas. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4903 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 4904 | &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4905 | initExprs.size(), RParenLoc); |
| 4906 | E->setType(Ty); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4907 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4908 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4909 | // 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] | 4910 | // sequence of BinOp comma operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4911 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4912 | if (Result.isInvalid()) return ExprError(); |
| 4913 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4914 | } |
| 4915 | } |
| 4916 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4917 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4918 | SourceLocation R, |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4919 | MultiExprArg Val, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4920 | ParsedType TypeOfCast) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4921 | unsigned nexprs = Val.size(); |
| 4922 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4923 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4924 | Expr *expr; |
| 4925 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 4926 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4927 | else |
| 4928 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4929 | return Owned(expr); |
| 4930 | } |
| 4931 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4932 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4933 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4934 | /// C99 6.5.15 |
| 4935 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4936 | Expr *&SAVE, ExprValueKind &VK, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4937 | ExprObjectKind &OK, |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4938 | SourceLocation QuestionLoc) { |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4939 | // If both LHS and RHS are overloaded functions, try to resolve them. |
| 4940 | if (Context.hasSameType(LHS->getType(), RHS->getType()) && |
| 4941 | LHS->getType()->isSpecificBuiltinType(BuiltinType::Overload)) { |
| 4942 | ExprResult LHSResult = CheckPlaceholderExpr(LHS, QuestionLoc); |
| 4943 | if (LHSResult.isInvalid()) |
| 4944 | return QualType(); |
| 4945 | |
| 4946 | ExprResult RHSResult = CheckPlaceholderExpr(RHS, QuestionLoc); |
| 4947 | if (RHSResult.isInvalid()) |
| 4948 | return QualType(); |
| 4949 | |
| 4950 | LHS = LHSResult.take(); |
| 4951 | RHS = RHSResult.take(); |
| 4952 | } |
| 4953 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4954 | // C++ is sufficiently different to merit its own checker. |
| 4955 | if (getLangOptions().CPlusPlus) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4956 | return CXXCheckConditionalOperands(Cond, LHS, RHS, SAVE, |
| 4957 | VK, OK, QuestionLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4958 | |
| 4959 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4960 | OK = OK_Ordinary; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4961 | |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4962 | UsualUnaryConversions(Cond); |
Fariborz Jahanian | 2b1d88a | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 4963 | if (SAVE) { |
| 4964 | SAVE = LHS = Cond; |
| 4965 | } |
| 4966 | else |
| 4967 | UsualUnaryConversions(LHS); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4968 | UsualUnaryConversions(RHS); |
| 4969 | QualType CondTy = Cond->getType(); |
| 4970 | QualType LHSTy = LHS->getType(); |
| 4971 | QualType RHSTy = RHS->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4972 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4973 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4974 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4975 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4976 | // Throw an error if its not either. |
| 4977 | if (getLangOptions().OpenCL) { |
| 4978 | if (!CondTy->isVectorType()) { |
| 4979 | Diag(Cond->getLocStart(), |
| 4980 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4981 | << CondTy; |
| 4982 | return QualType(); |
| 4983 | } |
| 4984 | } |
| 4985 | else { |
| 4986 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4987 | << CondTy; |
| 4988 | return QualType(); |
| 4989 | } |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4990 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4991 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4992 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4993 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 4994 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4995 | |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4996 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4997 | // attempt to implicity convert them to the vector type to act like the |
| 4998 | // built in select. |
| 4999 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5000 | // Both operands should be of scalar type. |
| 5001 | if (!LHSTy->isScalarType()) { |
| 5002 | Diag(LHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5003 | << CondTy; |
| 5004 | return QualType(); |
| 5005 | } |
| 5006 | if (!RHSTy->isScalarType()) { |
| 5007 | Diag(RHS->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 5008 | << CondTy; |
| 5009 | return QualType(); |
| 5010 | } |
| 5011 | // Implicity convert these scalars to the type of the condition. |
| 5012 | ImpCastExprToType(LHS, CondTy, CK_IntegralCast); |
| 5013 | ImpCastExprToType(RHS, CondTy, CK_IntegralCast); |
| 5014 | } |
| 5015 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5016 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5017 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5018 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5019 | UsualArithmeticConversions(LHS, RHS); |
| 5020 | return LHS->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5021 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5022 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5023 | // If both operands are the same structure or union type, the result is that |
| 5024 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5025 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5026 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5027 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5028 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5029 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5030 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5031 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5032 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5033 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5034 | // 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] | 5035 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5036 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5037 | if (!LHSTy->isVoidType()) |
| 5038 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5039 | << RHS->getSourceRange(); |
| 5040 | if (!RHSTy->isVoidType()) |
| 5041 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5042 | << LHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5043 | ImpCastExprToType(LHS, Context.VoidTy, CK_ToVoid); |
| 5044 | ImpCastExprToType(RHS, Context.VoidTy, CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5045 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5046 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5047 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5048 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5049 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5050 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5051 | // promote the null to a pointer. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5052 | ImpCastExprToType(RHS, LHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5053 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5054 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5055 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5056 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5057 | ImpCastExprToType(LHS, RHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5058 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5059 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5060 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5061 | // All objective-c pointer type analysis is done here. |
| 5062 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5063 | QuestionLoc); |
| 5064 | if (!compositeType.isNull()) |
| 5065 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5066 | |
| 5067 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5068 | // Handle block pointer types. |
| 5069 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5070 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5071 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5072 | QualType destType = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5073 | ImpCastExprToType(LHS, destType, CK_BitCast); |
| 5074 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5075 | return destType; |
| 5076 | } |
| 5077 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5078 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5079 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5080 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5081 | // We have 2 block pointer types. |
| 5082 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5083 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5084 | return LHSTy; |
| 5085 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5086 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5087 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5088 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5089 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5090 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5091 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5092 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5093 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5094 | // In this situation, we assume void* type. No especially good |
| 5095 | // reason, but this is what gcc does, and we do have to pick |
| 5096 | // to get a consistent AST. |
| 5097 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5098 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5099 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5100 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5101 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5102 | // The block pointer types are compatible. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5103 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5104 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 5105 | return LHSTy; |
| 5106 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5107 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5108 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 5109 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 5110 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5111 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5112 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5113 | |
| 5114 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 5115 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 5116 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5117 | QualType destPointee |
| 5118 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5119 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5120 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5121 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5122 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5123 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5124 | return destType; |
| 5125 | } |
| 5126 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 5127 | QualType destPointee |
| 5128 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5129 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5130 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5131 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5132 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5133 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5134 | return destType; |
| 5135 | } |
| 5136 | |
| 5137 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5138 | // Two identical pointer types are always compatible. |
| 5139 | return LHSTy; |
| 5140 | } |
| 5141 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5142 | rhptee.getUnqualifiedType())) { |
| 5143 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 5144 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5145 | // In this situation, we assume void* type. No especially good |
| 5146 | // reason, but this is what gcc does, and we do have to pick |
| 5147 | // to get a consistent AST. |
| 5148 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5149 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5150 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5151 | return incompatTy; |
| 5152 | } |
| 5153 | // The pointer types are compatible. |
| 5154 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 5155 | // differently qualified versions of compatible types, the result type is |
| 5156 | // a pointer to an appropriately qualified version of the *composite* |
| 5157 | // type. |
| 5158 | // FIXME: Need to calculate the composite type. |
| 5159 | // FIXME: Need to add qualifiers |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5160 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 5161 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5162 | return LHSTy; |
| 5163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5165 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 5166 | // null pointers have been filtered out by this point. |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5167 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 5168 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5169 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5170 | ImpCastExprToType(LHS, RHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5171 | return RHSTy; |
| 5172 | } |
| 5173 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 5174 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 5175 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5176 | ImpCastExprToType(RHS, LHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5177 | return LHSTy; |
| 5178 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 5179 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5180 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5181 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 5182 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5183 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 5184 | } |
| 5185 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5186 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5187 | /// two objective-c pointer types of the two input expressions. |
| 5188 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 5189 | SourceLocation QuestionLoc) { |
| 5190 | QualType LHSTy = LHS->getType(); |
| 5191 | QualType RHSTy = RHS->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5192 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5193 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5194 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5195 | // redefinition type if an attempt is made to access its fields. |
| 5196 | if (LHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5197 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5198 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5199 | return LHSTy; |
| 5200 | } |
| 5201 | if (RHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5202 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5203 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5204 | return RHSTy; |
| 5205 | } |
| 5206 | // And the same for struct objc_object* / id |
| 5207 | if (LHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5208 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5209 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5210 | return LHSTy; |
| 5211 | } |
| 5212 | if (RHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5213 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5214 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5215 | return RHSTy; |
| 5216 | } |
| 5217 | // And the same for struct objc_selector* / SEL |
| 5218 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5219 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5220 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5221 | return LHSTy; |
| 5222 | } |
| 5223 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5224 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5225 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5226 | return RHSTy; |
| 5227 | } |
| 5228 | // Check constraints for Objective-C object pointers types. |
| 5229 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5230 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5231 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5232 | // Two identical object pointer types are always compatible. |
| 5233 | return LHSTy; |
| 5234 | } |
| 5235 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 5236 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 5237 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5238 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5239 | // If both operands are interfaces and either operand can be |
| 5240 | // assigned to the other, use that type as the composite |
| 5241 | // type. This allows |
| 5242 | // xxx ? (A*) a : (B*) b |
| 5243 | // where B is a subclass of A. |
| 5244 | // |
| 5245 | // Additionally, as for assignment, if either type is 'id' |
| 5246 | // allow silent coercion. Finally, if the types are |
| 5247 | // incompatible then make sure to use 'id' as the composite |
| 5248 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5249 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5250 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5251 | // It could return the composite type. |
| 5252 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5253 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5254 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5255 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5256 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5257 | RHSTy->isObjCQualifiedIdType()) && |
| 5258 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5259 | // Need to handle "id<xx>" explicitly. |
| 5260 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5261 | // id. Currently localizing to here until clear this should be |
| 5262 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5263 | compositeType = Context.getObjCIdType(); |
| 5264 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5265 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5266 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5267 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5268 | ; |
| 5269 | else { |
| 5270 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5271 | << LHSTy << RHSTy |
| 5272 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5273 | QualType incompatTy = Context.getObjCIdType(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5274 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 5275 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5276 | return incompatTy; |
| 5277 | } |
| 5278 | // The object pointer types are compatible. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5279 | ImpCastExprToType(LHS, compositeType, CK_BitCast); |
| 5280 | ImpCastExprToType(RHS, compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5281 | return compositeType; |
| 5282 | } |
| 5283 | // Check Objective-C object pointer types and 'void *' |
| 5284 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 5285 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5286 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5287 | QualType destPointee |
| 5288 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5289 | QualType destType = Context.getPointerType(destPointee); |
| 5290 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5291 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5292 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5293 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5294 | return destType; |
| 5295 | } |
| 5296 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 5297 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5298 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5299 | QualType destPointee |
| 5300 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5301 | QualType destType = Context.getPointerType(destPointee); |
| 5302 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5303 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5304 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5305 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5306 | return destType; |
| 5307 | } |
| 5308 | return QualType(); |
| 5309 | } |
| 5310 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5311 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5312 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5313 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5314 | SourceLocation ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5315 | Expr *CondExpr, Expr *LHSExpr, |
| 5316 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5317 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5318 | // was the condition. |
| 5319 | bool isLHSNull = LHSExpr == 0; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5320 | Expr *SAVEExpr = 0; |
| 5321 | if (isLHSNull) { |
| 5322 | LHSExpr = SAVEExpr = CondExpr; |
| 5323 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5324 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5325 | ExprValueKind VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5326 | ExprObjectKind OK = OK_Ordinary; |
Fariborz Jahanian | 2b1d88a | 2010-09-18 19:38:38 +0000 | [diff] [blame] | 5327 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, RHSExpr, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5328 | SAVEExpr, VK, OK, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 5329 | if (result.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5330 | return ExprError(); |
| 5331 | |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 5332 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5333 | LHSExpr, ColonLoc, |
| 5334 | RHSExpr, SAVEExpr, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5335 | result, VK, OK)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5338 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5339 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5340 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5341 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5342 | // FIXME: add a couple examples in this comment. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5343 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5344 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5345 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5346 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5347 | if ((lhsType->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5348 | (Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType))) || |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5349 | (rhsType->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5350 | (Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)))) { |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5351 | return Compatible; |
| 5352 | } |
| 5353 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5354 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5355 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 5356 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5357 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5358 | // make sure we operate on the canonical type |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5359 | lhptee = Context.getCanonicalType(lhptee); |
| 5360 | rhptee = Context.getCanonicalType(rhptee); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5361 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5362 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5363 | |
| 5364 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5365 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5366 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | ece8582 | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 5367 | // FIXME: Handle ExtQualType |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5368 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5369 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5370 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5371 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5372 | // 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] | 5373 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5374 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5375 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5376 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5377 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5378 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5379 | assert(rhptee->isFunctionType()); |
| 5380 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5381 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5382 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5383 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5384 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5385 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5386 | |
| 5387 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5388 | assert(lhptee->isFunctionType()); |
| 5389 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5390 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5391 | // 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] | 5392 | // unqualified versions of compatible types, ... |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5393 | lhptee = lhptee.getUnqualifiedType(); |
| 5394 | rhptee = rhptee.getUnqualifiedType(); |
| 5395 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 5396 | // Check if the pointee types are compatible ignoring the sign. |
| 5397 | // We explicitly check for char so that we catch "char" vs |
| 5398 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5399 | if (lhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5400 | lhptee = Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5401 | else if (lhptee->hasSignedIntegerRepresentation()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5402 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5403 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5404 | if (rhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5405 | rhptee = Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5406 | else if (rhptee->hasSignedIntegerRepresentation()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5407 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5408 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5409 | if (lhptee == rhptee) { |
| 5410 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5411 | // takes priority over sign incompatibility because the sign |
| 5412 | // warning can be disabled. |
| 5413 | if (ConvTy != Compatible) |
| 5414 | return ConvTy; |
| 5415 | return IncompatiblePointerSign; |
| 5416 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5417 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5418 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5419 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5420 | // the eventual target type is the same and the pointers have the same |
| 5421 | // level of indirection, this must be the issue. |
| 5422 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 5423 | do { |
| 5424 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 5425 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5426 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5427 | lhptee = Context.getCanonicalType(lhptee); |
| 5428 | rhptee = Context.getCanonicalType(rhptee); |
| 5429 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5431 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 5432 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5433 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5434 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5435 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5436 | return IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5437 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5438 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5441 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 5442 | /// block pointer types are compatible or whether a block and normal pointer |
| 5443 | /// are compatible. It is more restrict than comparing two function pointer |
| 5444 | // types. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5445 | Sema::AssignConvertType |
| 5446 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5447 | QualType rhsType) { |
| 5448 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5449 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5450 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5451 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 5452 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5453 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5454 | // make sure we operate on the canonical type |
| 5455 | lhptee = Context.getCanonicalType(lhptee); |
| 5456 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5457 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5458 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5459 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5460 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5461 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5462 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5463 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 5464 | if (!getLangOptions().CPlusPlus) { |
| 5465 | if (!Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5466 | return IncompatibleBlockPointer; |
| 5467 | } |
| 5468 | else if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5469 | return IncompatibleBlockPointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5470 | return ConvTy; |
| 5471 | } |
| 5472 | |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5473 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 5474 | /// for assignment compatibility. |
| 5475 | Sema::AssignConvertType |
| 5476 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5477 | if (lhsType->isObjCBuiltinType()) { |
| 5478 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5479 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5480 | !rhsType->isObjCQualifiedClassType()) |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5481 | return IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5482 | return Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5483 | } |
| 5484 | if (rhsType->isObjCBuiltinType()) { |
| 5485 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5486 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5487 | !lhsType->isObjCQualifiedClassType()) |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5488 | return IncompatiblePointer; |
| 5489 | return Compatible; |
| 5490 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5491 | QualType lhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5492 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5493 | QualType rhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5494 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5495 | // make sure we operate on the canonical type |
| 5496 | lhptee = Context.getCanonicalType(lhptee); |
| 5497 | rhptee = Context.getCanonicalType(rhptee); |
| 5498 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5499 | return CompatiblePointerDiscardsQualifiers; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5500 | |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5501 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 5502 | return Compatible; |
| 5503 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 5504 | return IncompatibleObjCQualifiedId; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5505 | return IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5508 | Sema::AssignConvertType |
| 5509 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 5510 | // Fake up an opaque expression. We don't actually care about what |
| 5511 | // cast operations are required, so if CheckAssignmentConstraints |
| 5512 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5513 | // usually happen on valid code. |
| 5514 | OpaqueValueExpr rhs(rhsType, VK_RValue); |
| 5515 | Expr *rhsPtr = &rhs; |
| 5516 | CastKind K = CK_Invalid; |
| 5517 | |
| 5518 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5519 | } |
| 5520 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5521 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5522 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5523 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5524 | /// |
| 5525 | /// int a, *pint; |
| 5526 | /// short *pshort; |
| 5527 | /// struct foo *pfoo; |
| 5528 | /// |
| 5529 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5530 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5531 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5532 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5533 | /// |
| 5534 | /// 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] | 5535 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5536 | /// |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5537 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5538 | Sema::AssignConvertType |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5539 | Sema::CheckAssignmentConstraints(QualType lhsType, Expr *&rhs, |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5540 | CastKind &Kind) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5541 | QualType rhsType = rhs->getType(); |
| 5542 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5543 | // Get canonical types. We're not formatting these types, just comparing |
| 5544 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5545 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5546 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5547 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5548 | if (lhsType == rhsType) { |
| 5549 | Kind = CK_NoOp; |
Chris Lattner | f5c973d | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 5550 | return Compatible; // Common case: fast path an exact match. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5551 | } |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 5552 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5553 | if ((lhsType->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5554 | (Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType))) || |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5555 | (rhsType->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 5556 | (Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)))) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5557 | Kind = CK_BitCast; |
| 5558 | return Compatible; |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5561 | // If the left-hand side is a reference type, then we are in a |
| 5562 | // (rare!) case where we've allowed the use of references in C, |
| 5563 | // e.g., as a parameter type in a built-in function. In this case, |
| 5564 | // just make sure that the type referenced is compatible with the |
| 5565 | // right-hand side type. The caller is responsible for adjusting |
| 5566 | // lhsType so that the resulting expression does not have reference |
| 5567 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5568 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5569 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5570 | Kind = CK_LValueBitCast; |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5571 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5572 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5573 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5574 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5575 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5576 | // to the same ExtVector type. |
| 5577 | if (lhsType->isExtVectorType()) { |
| 5578 | if (rhsType->isExtVectorType()) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5579 | return Incompatible; |
| 5580 | if (rhsType->isArithmeticType()) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5581 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5582 | // element type. |
| 5583 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5584 | if (elType != rhsType) { |
| 5585 | Kind = PrepareScalarCast(*this, rhs, elType); |
| 5586 | ImpCastExprToType(rhs, elType, Kind); |
| 5587 | } |
| 5588 | Kind = CK_VectorSplat; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5589 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5590 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5591 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5592 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5593 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5594 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5595 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5596 | // vector type and vice versa |
| 5597 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5598 | Kind = CK_BitCast; |
| 5599 | return Compatible; |
| 5600 | } |
| 5601 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5602 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5603 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5604 | // no bits are changed but the result type is different. |
| 5605 | if (getLangOptions().LaxVectorConversions && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5606 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5607 | Kind = CK_BitCast; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5608 | return IncompatibleVectors; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5609 | } |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5610 | } |
| 5611 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5612 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5613 | |
Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5614 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5615 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5616 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5617 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5618 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5619 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5620 | if (isa<PointerType>(lhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5621 | if (rhsType->isIntegerType()) { |
| 5622 | Kind = CK_IntegralToPointer; // FIXME: null? |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5623 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5624 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5625 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5626 | if (isa<PointerType>(rhsType)) { |
| 5627 | Kind = CK_BitCast; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5628 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5629 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5630 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5631 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5632 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5633 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5634 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 5635 | return Compatible; |
| 5636 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5637 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5638 | if (rhsType->getAs<BlockPointerType>()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5639 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| 5640 | Kind = CK_BitCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5641 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5642 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5643 | |
| 5644 | // Treat block pointers as objects. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5645 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) { |
| 5646 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5647 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5648 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5649 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5650 | return Incompatible; |
| 5651 | } |
| 5652 | |
| 5653 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5654 | if (rhsType->isIntegerType()) { |
| 5655 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5656 | return IntToBlockPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | Kind = CK_AnyPointerToObjCPointerCast; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5660 | |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5661 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5662 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5663 | return Compatible; |
| 5664 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5665 | if (rhsType->isBlockPointerType()) |
| 5666 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5667 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5668 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5669 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5670 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5671 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5672 | return Incompatible; |
| 5673 | } |
| 5674 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5675 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5676 | if (rhsType->isIntegerType()) { |
| 5677 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5678 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
| 5681 | Kind = CK_BitCast; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5682 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5683 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5684 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5685 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 5686 | return Compatible; |
| 5687 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5688 | } |
| 5689 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5690 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5691 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5692 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5693 | if (RHSPT->getPointeeType()->isVoidType()) |
| 5694 | return Compatible; |
| 5695 | } |
| 5696 | // Treat block pointers as objects. |
| 5697 | if (rhsType->isBlockPointerType()) |
| 5698 | return Compatible; |
| 5699 | return Incompatible; |
| 5700 | } |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5701 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5702 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5703 | if (lhsType == Context.BoolTy) { |
| 5704 | Kind = CK_PointerToBoolean; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5705 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5706 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5707 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5708 | if (lhsType->isIntegerType()) { |
| 5709 | Kind = CK_PointerToIntegral; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5710 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5711 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5712 | |
| 5713 | if (isa<BlockPointerType>(lhsType) && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5714 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| 5715 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5716 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5717 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5718 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5719 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5720 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5721 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5722 | if (lhsType == Context.BoolTy) { |
| 5723 | Kind = CK_PointerToBoolean; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5724 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5725 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5726 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5727 | if (lhsType->isIntegerType()) { |
| 5728 | Kind = CK_PointerToIntegral; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5729 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | Kind = CK_BitCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5733 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5734 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5735 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5736 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 5737 | return Compatible; |
| 5738 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5739 | } |
| 5740 | if (isa<BlockPointerType>(lhsType) && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5741 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| 5742 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5743 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5744 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5745 | return Incompatible; |
| 5746 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5747 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5748 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5749 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 5750 | Kind = CK_NoOp; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5751 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5752 | } |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 5753 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5754 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 5755 | } |
| 5756 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5757 | /// \brief Constructs a transparent union from an expression that is |
| 5758 | /// used to initialize the transparent union. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5760 | QualType UnionType, FieldDecl *Field) { |
| 5761 | // Build an initializer list that designates the appropriate member |
| 5762 | // of the transparent union. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5763 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5764 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5765 | SourceLocation()); |
| 5766 | Initializer->setType(UnionType); |
| 5767 | Initializer->setInitializedFieldInUnion(Field); |
| 5768 | |
| 5769 | // Build a compound literal constructing a value of the transparent |
| 5770 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5771 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5772 | E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5773 | VK_RValue, Initializer, false); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5774 | } |
| 5775 | |
| 5776 | Sema::AssignConvertType |
| 5777 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 5778 | QualType FromType = rExpr->getType(); |
| 5779 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5780 | // 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] | 5781 | // transparent_union GCC extension. |
| 5782 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5783 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5784 | return Incompatible; |
| 5785 | |
| 5786 | // The field to initialize within the transparent union. |
| 5787 | RecordDecl *UD = UT->getDecl(); |
| 5788 | FieldDecl *InitField = 0; |
| 5789 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5790 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5791 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5792 | it != itend; ++it) { |
| 5793 | if (it->getType()->isPointerType()) { |
| 5794 | // If the transparent union contains a pointer type, we allow: |
| 5795 | // 1) void pointer |
| 5796 | // 2) null pointer constant |
| 5797 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5798 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5799 | ImpCastExprToType(rExpr, it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5800 | InitField = *it; |
| 5801 | break; |
| 5802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5803 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5804 | if (rExpr->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5805 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5806 | ImpCastExprToType(rExpr, it->getType(), CK_NullToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5807 | InitField = *it; |
| 5808 | break; |
| 5809 | } |
| 5810 | } |
| 5811 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5812 | Expr *rhs = rExpr; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5813 | CastKind Kind = CK_Invalid; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5814 | if (CheckAssignmentConstraints(it->getType(), rhs, Kind) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5815 | == Compatible) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5816 | ImpCastExprToType(rhs, it->getType(), Kind); |
| 5817 | rExpr = rhs; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5818 | InitField = *it; |
| 5819 | break; |
| 5820 | } |
| 5821 | } |
| 5822 | |
| 5823 | if (!InitField) |
| 5824 | return Incompatible; |
| 5825 | |
| 5826 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 5827 | return Compatible; |
| 5828 | } |
| 5829 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5830 | Sema::AssignConvertType |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5831 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5832 | if (getLangOptions().CPlusPlus) { |
| 5833 | if (!lhsType->isRecordType()) { |
| 5834 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5835 | // expression is implicitly converted (C++ 4) to the |
| 5836 | // cv-unqualified type of the left operand. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 5837 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5838 | AA_Assigning)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5839 | return Incompatible; |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 5840 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5841 | } |
| 5842 | |
| 5843 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5844 | // structures. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5845 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5846 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5847 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5848 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5849 | if ((lhsType->isPointerType() || |
| 5850 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5851 | lhsType->isBlockPointerType()) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5852 | && rExpr->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5853 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5854 | ImpCastExprToType(rExpr, lhsType, CK_NullToPointer); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5855 | return Compatible; |
| 5856 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5857 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5858 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5859 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5860 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5861 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5862 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5863 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5864 | if (!lhsType->isReferenceType()) |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 5865 | DefaultFunctionArrayLvalueConversion(rExpr); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5866 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5867 | CastKind Kind = CK_Invalid; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5868 | Sema::AssignConvertType result = |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5869 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5870 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5871 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5872 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5873 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5874 | // so that we can use references in built-in functions even in C. |
| 5875 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5876 | // does not have reference type. |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5877 | if (result != Incompatible && rExpr->getType() != lhsType) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5878 | ImpCastExprToType(rExpr, lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5879 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5880 | } |
| 5881 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5882 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5883 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5884 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5885 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5886 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 5887 | } |
| 5888 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5889 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5890 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5891 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5892 | QualType lhsType = |
| 5893 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 5894 | QualType rhsType = |
| 5895 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5896 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5897 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5898 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5899 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5900 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5901 | // Handle the case of a vector & extvector type of the same size and element |
| 5902 | // 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] | 5903 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5904 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 5905 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5906 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5907 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5908 | if (lhsType->isExtVectorType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5909 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5910 | return lhsType; |
| 5911 | } |
| 5912 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5913 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5914 | return rhsType; |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 5915 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 5916 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5917 | // vectors, the total size only needs to be the same. This is a |
| 5918 | // bitcast; no bits are changed but the result type is different. |
| 5919 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
| 5920 | return lhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5921 | } |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 5922 | } |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 5923 | } |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5924 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5925 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5926 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5927 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5928 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5929 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5930 | return rhsType; |
| 5931 | } |
| 5932 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5933 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5934 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5935 | bool swapped = false; |
| 5936 | if (rhsType->isExtVectorType()) { |
| 5937 | swapped = true; |
| 5938 | std::swap(rex, lex); |
| 5939 | std::swap(rhsType, lhsType); |
| 5940 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5941 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5942 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5943 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5944 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5945 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5946 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 5947 | if (order > 0) |
| 5948 | ImpCastExprToType(rex, EltTy, CK_IntegralCast); |
| 5949 | if (order >= 0) { |
| 5950 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5951 | if (swapped) std::swap(rex, lex); |
| 5952 | return lhsType; |
| 5953 | } |
| 5954 | } |
| 5955 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5956 | rhsType->isRealFloatingType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5957 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 5958 | if (order > 0) |
| 5959 | ImpCastExprToType(rex, EltTy, CK_FloatingCast); |
| 5960 | if (order >= 0) { |
| 5961 | ImpCastExprToType(rex, lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5962 | if (swapped) std::swap(rex, lex); |
| 5963 | return lhsType; |
| 5964 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5965 | } |
| 5966 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5967 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5968 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5969 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5970 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5971 | << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5972 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5975 | QualType Sema::CheckMultiplyDivideOperands( |
| 5976 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 060d5e2 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 5977 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5978 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5979 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5980 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5981 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5982 | if (!lex->getType()->isArithmeticType() || |
| 5983 | !rex->getType()->isArithmeticType()) |
| 5984 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5985 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5986 | // Check for division by zero. |
| 5987 | if (isDiv && |
| 5988 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5989 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_division_by_zero) |
Chris Lattner | 7011795 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 5990 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5991 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5992 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5993 | } |
| 5994 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5995 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5997 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5998 | if (lex->getType()->hasIntegerRepresentation() && |
| 5999 | rex->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6000 | return CheckVectorOperands(Loc, lex, rex); |
| 6001 | return InvalidOperands(Loc, lex, rex); |
| 6002 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6003 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6004 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6005 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6006 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 6007 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6008 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6009 | // Check for remainder by zero. |
| 6010 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | 7011795 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 6011 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_remainder_by_zero) |
| 6012 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6013 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6014 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6017 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6018 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6019 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6020 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6021 | if (CompLHSTy) *CompLHSTy = compType; |
| 6022 | return compType; |
| 6023 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6024 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6025 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6026 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 6027 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6028 | if (lex->getType()->isArithmeticType() && |
| 6029 | rex->getType()->isArithmeticType()) { |
| 6030 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6031 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6032 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6033 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6034 | // Put any potential pointer into PExp |
| 6035 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6036 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6037 | std::swap(PExp, IExp); |
| 6038 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6039 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6040 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6041 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6042 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6043 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6044 | // Check for arithmetic on pointers to incomplete types. |
| 6045 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6046 | if (getLangOptions().CPlusPlus) { |
| 6047 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 6048 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6049 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6050 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6051 | |
| 6052 | // GNU extension: arithmetic on pointer to void |
| 6053 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6054 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6055 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6056 | if (getLangOptions().CPlusPlus) { |
| 6057 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 6058 | << lex->getType() << lex->getSourceRange(); |
| 6059 | return QualType(); |
| 6060 | } |
| 6061 | |
| 6062 | // GNU extension: arithmetic on pointer to function |
| 6063 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 6064 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6065 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6066 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6067 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 6068 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6069 | PExp->getType()->isObjCObjectPointerType()) && |
| 6070 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6071 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 6072 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6073 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 6074 | return QualType(); |
| 6075 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6076 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6077 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6078 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6079 | << PointeeTy << PExp->getSourceRange(); |
| 6080 | return QualType(); |
| 6081 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6082 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6083 | if (CompLHSTy) { |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 6084 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 6085 | if (LHSTy.isNull()) { |
| 6086 | LHSTy = lex->getType(); |
| 6087 | if (LHSTy->isPromotableIntegerType()) |
| 6088 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 6089 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6090 | *CompLHSTy = LHSTy; |
| 6091 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6092 | return PExp->getType(); |
| 6093 | } |
| 6094 | } |
| 6095 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6096 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6097 | } |
| 6098 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6099 | // C99 6.5.6 |
| 6100 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6101 | SourceLocation Loc, QualType* CompLHSTy) { |
| 6102 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6103 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 6104 | if (CompLHSTy) *CompLHSTy = compType; |
| 6105 | return compType; |
| 6106 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6107 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6108 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6109 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6110 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6111 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6112 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6113 | if (lex->getType()->isArithmeticType() |
| 6114 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6115 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6116 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6117 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6118 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6119 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6120 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 4eed7a1 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 6121 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6122 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6123 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6124 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6125 | bool ComplainAboutVoid = false; |
| 6126 | Expr *ComplainAboutFunc = 0; |
| 6127 | if (lpointee->isVoidType()) { |
| 6128 | if (getLangOptions().CPlusPlus) { |
| 6129 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6130 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6131 | return QualType(); |
| 6132 | } |
| 6133 | |
| 6134 | // GNU C extension: arithmetic on pointer to void |
| 6135 | ComplainAboutVoid = true; |
| 6136 | } else if (lpointee->isFunctionType()) { |
| 6137 | if (getLangOptions().CPlusPlus) { |
| 6138 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6139 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6140 | return QualType(); |
| 6141 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6142 | |
| 6143 | // GNU C extension: arithmetic on pointer to function |
| 6144 | ComplainAboutFunc = lex; |
| 6145 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6146 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6147 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6148 | << lex->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6149 | << lex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6150 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6151 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6152 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6153 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6154 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 6155 | << lpointee << lex->getSourceRange(); |
| 6156 | return QualType(); |
| 6157 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6158 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6159 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6160 | if (rex->getType()->isIntegerType()) { |
| 6161 | if (ComplainAboutVoid) |
| 6162 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6163 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6164 | if (ComplainAboutFunc) |
| 6165 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6166 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6167 | << ComplainAboutFunc->getSourceRange(); |
| 6168 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6169 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6170 | return lex->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6171 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6172 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6173 | // Handle pointer-pointer subtractions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6174 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6175 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6176 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6177 | // RHS must be a completely-type object type. |
| 6178 | // Handle the GNU void* extension. |
| 6179 | if (rpointee->isVoidType()) { |
| 6180 | if (getLangOptions().CPlusPlus) { |
| 6181 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 6182 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6183 | return QualType(); |
| 6184 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6185 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6186 | ComplainAboutVoid = true; |
| 6187 | } else if (rpointee->isFunctionType()) { |
| 6188 | if (getLangOptions().CPlusPlus) { |
| 6189 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6190 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6191 | return QualType(); |
| 6192 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6193 | |
| 6194 | // GNU extension: arithmetic on pointer to function |
| 6195 | if (!ComplainAboutFunc) |
| 6196 | ComplainAboutFunc = rex; |
| 6197 | } else if (!rpointee->isDependentType() && |
| 6198 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6199 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 6200 | << rex->getSourceRange() |
| 6201 | << rex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6202 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6203 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6204 | if (getLangOptions().CPlusPlus) { |
| 6205 | // Pointee types must be the same: C++ [expr.add] |
| 6206 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6207 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6208 | << lex->getType() << rex->getType() |
| 6209 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6210 | return QualType(); |
| 6211 | } |
| 6212 | } else { |
| 6213 | // Pointee types must be compatible C99 6.5.6p3 |
| 6214 | if (!Context.typesAreCompatible( |
| 6215 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6216 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6217 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 6218 | << lex->getType() << rex->getType() |
| 6219 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6220 | return QualType(); |
| 6221 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6222 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6223 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6224 | if (ComplainAboutVoid) |
| 6225 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 6226 | << lex->getSourceRange() << rex->getSourceRange(); |
| 6227 | if (ComplainAboutFunc) |
| 6228 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6229 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6230 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6231 | |
| 6232 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6233 | return Context.getPointerDiffType(); |
| 6234 | } |
| 6235 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6236 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6237 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6238 | } |
| 6239 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6240 | static bool isScopedEnumerationType(QualType T) { |
| 6241 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6242 | return ET->getDecl()->isScoped(); |
| 6243 | return false; |
| 6244 | } |
| 6245 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6246 | // C99 6.5.7 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6247 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6248 | bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6249 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6250 | if (!lex->getType()->hasIntegerRepresentation() || |
| 6251 | !rex->getType()->hasIntegerRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6252 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6253 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6254 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6255 | // hasIntegerRepresentation() above instead of this. |
| 6256 | if (isScopedEnumerationType(lex->getType()) || |
| 6257 | isScopedEnumerationType(rex->getType())) { |
| 6258 | return InvalidOperands(Loc, lex, rex); |
| 6259 | } |
| 6260 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6261 | // Vector shifts promote their scalar inputs to vector type. |
| 6262 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 6263 | return CheckVectorOperands(Loc, lex, rex); |
| 6264 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6265 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6266 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6267 | |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6268 | // For the LHS, do usual unary conversions, but then reset them away |
| 6269 | // if this is a compound assignment. |
| 6270 | Expr *old_lex = lex; |
| 6271 | UsualUnaryConversions(lex); |
| 6272 | QualType LHSTy = lex->getType(); |
| 6273 | if (isCompAssign) lex = old_lex; |
| 6274 | |
| 6275 | // The RHS is simpler. |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6276 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6277 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6278 | // Sanity-check shift operands |
| 6279 | llvm::APSInt Right; |
| 6280 | // Check right/shifter operand |
Daniel Dunbar | 687fa86 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 6281 | if (!rex->isValueDependent() && |
| 6282 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 2f08571 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 6283 | if (Right.isNegative()) |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6284 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 6285 | else { |
| 6286 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 6287 | Context.getTypeSize(lex->getType())); |
| 6288 | if (Right.uge(LeftBits)) |
| 6289 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 6290 | } |
| 6291 | } |
| 6292 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6293 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6294 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6295 | } |
| 6296 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6297 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6298 | if (DeclContext *DC = D->getDeclContext()) { |
| 6299 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6300 | return true; |
| 6301 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6302 | return FD->isFunctionTemplateSpecialization(); |
| 6303 | } |
| 6304 | return false; |
| 6305 | } |
| 6306 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6307 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6308 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6309 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6310 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6311 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6312 | // Handle vector comparisons separately. |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6313 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6314 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6315 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6316 | QualType lType = lex->getType(); |
| 6317 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6318 | |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6319 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6320 | !(lType->isBlockPointerType() && isRelational) && |
| 6321 | !lex->getLocStart().isMacroID() && |
| 6322 | !rex->getLocStart().isMacroID()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6323 | // For non-floating point types, check for self-comparisons of the form |
| 6324 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6325 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6326 | // |
| 6327 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6328 | // expansion. Also don't warn about comparisons which are only self |
| 6329 | // comparisons within a template specialization. The warnings should catch |
| 6330 | // obvious cases in the definition of the template anyways. The idea is to |
| 6331 | // warn when the typed comparison operator will always evaluate to the same |
| 6332 | // result. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6333 | Expr *LHSStripped = lex->IgnoreParenImpCasts(); |
| 6334 | Expr *RHSStripped = rex->IgnoreParenImpCasts(); |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6335 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6336 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6337 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6338 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6339 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_comparison_always) |
| 6340 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6341 | << (Opc == BO_EQ |
| 6342 | || Opc == BO_LE |
| 6343 | || Opc == BO_GE)); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6344 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6345 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6346 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6347 | // what is it always going to eval to? |
| 6348 | char always_evals_to; |
| 6349 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6350 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6351 | always_evals_to = 0; // false |
| 6352 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6353 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6354 | always_evals_to = 1; // true |
| 6355 | break; |
| 6356 | default: |
| 6357 | // best we can say is 'a constant' |
| 6358 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6359 | break; |
| 6360 | } |
| 6361 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_comparison_always) |
| 6362 | << 1 // array |
| 6363 | << always_evals_to); |
| 6364 | } |
| 6365 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6366 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6367 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6368 | if (isa<CastExpr>(LHSStripped)) |
| 6369 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6370 | if (isa<CastExpr>(RHSStripped)) |
| 6371 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6372 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6373 | // Warn about comparisons against a string constant (unless the other |
| 6374 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6375 | Expr *literalString = 0; |
| 6376 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6377 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6378 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6379 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6380 | literalString = lex; |
| 6381 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6382 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6383 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6384 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6385 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6386 | literalString = rex; |
| 6387 | literalStringStripped = RHSStripped; |
| 6388 | } |
| 6389 | |
| 6390 | if (literalString) { |
| 6391 | std::string resultComparison; |
| 6392 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6393 | case BO_LT: resultComparison = ") < 0"; break; |
| 6394 | case BO_GT: resultComparison = ") > 0"; break; |
| 6395 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6396 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6397 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6398 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6399 | default: assert(false && "Invalid comparison operator"); |
| 6400 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6401 | |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6402 | DiagRuntimeBehavior(Loc, |
| 6403 | PDiag(diag::warn_stringcompare) |
| 6404 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6405 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6406 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6407 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6408 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6409 | // C99 6.5.8p3 / C99 6.5.9p4 |
| 6410 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 6411 | UsualArithmeticConversions(lex, rex); |
| 6412 | else { |
| 6413 | UsualUnaryConversions(lex); |
| 6414 | UsualUnaryConversions(rex); |
| 6415 | } |
| 6416 | |
| 6417 | lType = lex->getType(); |
| 6418 | rType = rex->getType(); |
| 6419 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6420 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6421 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6422 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6423 | if (isRelational) { |
| 6424 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6425 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6426 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6427 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6428 | if (lType->hasFloatingRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6429 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6430 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6431 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6432 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6433 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6434 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6435 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6436 | Expr::NPC_ValueDependentIsNull); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6437 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6438 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6439 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6440 | // All of the following pointer-related warnings are GCC extensions, except |
| 6441 | // when handling null pointer constants. |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6442 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6443 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6444 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6445 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6446 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6447 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6448 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6449 | if (LCanPointeeTy == RCanPointeeTy) |
| 6450 | return ResultTy; |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6451 | if (!isRelational && |
| 6452 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6453 | // Valid unless comparison between non-null pointer and function pointer |
| 6454 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6455 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6456 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6457 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6458 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6459 | Diag(Loc, |
| 6460 | isSFINAEContext()? |
| 6461 | diag::err_typecheck_comparison_of_fptr_to_void |
| 6462 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6463 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6464 | |
| 6465 | if (isSFINAEContext()) |
| 6466 | return QualType(); |
| 6467 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6468 | ImpCastExprToType(rex, lType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6469 | return ResultTy; |
| 6470 | } |
| 6471 | } |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6472 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6473 | // C++ [expr.rel]p2: |
| 6474 | // [...] Pointer conversions (4.10) and qualification |
| 6475 | // conversions (4.4) are performed on pointer operands (or on |
| 6476 | // a pointer operand and a null pointer constant) to bring |
| 6477 | // them to their composite pointer type. [...] |
| 6478 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6479 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6480 | // comparisons of pointers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6481 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6482 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6483 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6484 | if (T.isNull()) { |
| 6485 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 6486 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6487 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6488 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6489 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6490 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6491 | << lType << rType << T |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6492 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6493 | } |
| 6494 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6495 | ImpCastExprToType(lex, T, CK_BitCast); |
| 6496 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6497 | return ResultTy; |
| 6498 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6499 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6500 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6501 | RCanPointeeTy.getUnqualifiedType())) { |
| 6502 | // Valid unless a relational comparison of function pointers |
| 6503 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6504 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 6505 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6506 | } |
| 6507 | } else if (!isRelational && |
| 6508 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6509 | // Valid unless comparison between non-null pointer and function pointer |
| 6510 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6511 | && !LHSIsNull && !RHSIsNull) { |
| 6512 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 6513 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 6514 | } |
| 6515 | } else { |
| 6516 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6517 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6518 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 6519 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6520 | if (LCanPointeeTy != RCanPointeeTy) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6521 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6522 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6523 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6524 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6525 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6526 | // Comparison of nullptr_t with itself. |
| 6527 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6528 | return ResultTy; |
| 6529 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6530 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6531 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6532 | if (RHSIsNull && |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6533 | ((lType->isPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6534 | (!isRelational && lType->isMemberPointerType()))) { |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6535 | ImpCastExprToType(rex, lType, |
| 6536 | lType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6537 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6538 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6539 | return ResultTy; |
| 6540 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6541 | if (LHSIsNull && |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6542 | ((rType->isPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6543 | (!isRelational && rType->isMemberPointerType()))) { |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6544 | ImpCastExprToType(lex, rType, |
| 6545 | rType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6546 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6547 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6548 | return ResultTy; |
| 6549 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6550 | |
| 6551 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6552 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6553 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 6554 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6555 | // In addition, pointers to members can be compared, or a pointer to |
| 6556 | // member and a null pointer constant. Pointer to member conversions |
| 6557 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6558 | // them to a common type. If one operand is a null pointer constant, |
| 6559 | // the common type is the type of the other operand. Otherwise, the |
| 6560 | // common type is a pointer to member type similar (4.4) to the type |
| 6561 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6562 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6563 | // types. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6564 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6565 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6566 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6567 | if (T.isNull()) { |
| 6568 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6569 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6570 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6571 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6572 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6573 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6574 | << lType << rType << T |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6575 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6576 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6577 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6578 | ImpCastExprToType(lex, T, CK_BitCast); |
| 6579 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6580 | return ResultTy; |
| 6581 | } |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6582 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6583 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6584 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6585 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6586 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6587 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6588 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6589 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6590 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6591 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6592 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6593 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6594 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6595 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6596 | } |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6597 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6598 | if (!isRelational |
| 6599 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6600 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6601 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6602 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6603 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6604 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6605 | ->getPointeeType()->isVoidType()))) |
| 6606 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 6607 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6608 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6609 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6610 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6611 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6612 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6613 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6614 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6615 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 6616 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6617 | bool LPtrToVoid = LPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6618 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6619 | bool RPtrToVoid = RPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6620 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6621 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6622 | if (!LPtrToVoid && !RPtrToVoid && |
| 6623 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6624 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6625 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6626 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6627 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6628 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6629 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6630 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6631 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6632 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 6633 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6634 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6635 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6636 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6637 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6638 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 6639 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6640 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6641 | bool isError = false; |
| 6642 | if ((LHSIsNull && lType->isIntegerType()) || |
| 6643 | (RHSIsNull && rType->isIntegerType())) { |
| 6644 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6645 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6646 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6647 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6648 | else if (getLangOptions().CPlusPlus) { |
| 6649 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6650 | isError = true; |
| 6651 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6652 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6653 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6654 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6655 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 6656 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6657 | if (isError) |
| 6658 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6659 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6660 | |
| 6661 | if (lType->isIntegerType()) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6662 | ImpCastExprToType(lex, rType, |
| 6663 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6664 | else |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6665 | ImpCastExprToType(rex, lType, |
| 6666 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6667 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 6668 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6669 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6670 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6671 | if (!isRelational && RHSIsNull |
| 6672 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6673 | ImpCastExprToType(rex, lType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6674 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6675 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6676 | if (!isRelational && LHSIsNull |
| 6677 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6678 | ImpCastExprToType(lex, rType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6679 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6680 | } |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6681 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6682 | } |
| 6683 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6684 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6685 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6686 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6687 | /// types. |
| 6688 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6689 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6690 | bool isRelational) { |
| 6691 | // Check to make sure we're operating on vectors of the same type and width, |
| 6692 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6693 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6694 | if (vType.isNull()) |
| 6695 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6696 | |
Anton Yartsev | 3f8f288 | 2010-11-18 03:19:30 +0000 | [diff] [blame] | 6697 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6698 | // bool for C++, int for C |
| 6699 | if (getLangOptions().AltiVec) |
| 6700 | return (getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy); |
| 6701 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6702 | QualType lType = lex->getType(); |
| 6703 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6704 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6705 | // For non-floating point types, check for self-comparisons of the form |
| 6706 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6707 | // often indicate logic errors in the program. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6708 | if (!lType->hasFloatingRepresentation()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6709 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 6710 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 6711 | if (DRL->getDecl() == DRR->getDecl()) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6712 | DiagRuntimeBehavior(Loc, |
| 6713 | PDiag(diag::warn_comparison_always) |
| 6714 | << 0 // self- |
| 6715 | << 2 // "a constant" |
| 6716 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6717 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6718 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6719 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6720 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 6721 | assert (rType->hasFloatingRepresentation()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6722 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6723 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6724 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6725 | // Return the type for the comparison, which is the same as vector type for |
| 6726 | // integer vectors, or an integer type of identical size and number of |
| 6727 | // elements for floating point vectors. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6728 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6729 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6730 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6731 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6732 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6733 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6734 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6735 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6736 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6737 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6738 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6739 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6740 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6741 | } |
| 6742 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6743 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6744 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6745 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 6746 | if (lex->getType()->hasIntegerRepresentation() && |
| 6747 | rex->getType()->hasIntegerRepresentation()) |
| 6748 | return CheckVectorOperands(Loc, lex, rex); |
| 6749 | |
| 6750 | return InvalidOperands(Loc, lex, rex); |
| 6751 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6752 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6753 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6754 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6755 | if (lex->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6756 | rex->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6757 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6758 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6761 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6762 | Expr *&lex, Expr *&rex, SourceLocation Loc, unsigned Opc) { |
| 6763 | |
| 6764 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6765 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6766 | // is a constant. |
| 6767 | if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() && |
Eli Friedman | 6b197e0 | 2010-07-27 19:14:53 +0000 | [diff] [blame] | 6768 | rex->getType()->isIntegerType() && !rex->isValueDependent() && |
Chris Lattner | deee7a3 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 6769 | // Don't warn in macros. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6770 | !Loc.isMacroID()) { |
| 6771 | // If the RHS can be constant folded, and if it constant folds to something |
| 6772 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6773 | // happened to fold to true/false) then warn. |
| 6774 | Expr::EvalResult Result; |
| 6775 | if (rex->Evaluate(Result, Context) && !Result.HasSideEffects && |
| 6776 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 6777 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 6778 | << rex->getSourceRange() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6779 | << (Opc == BO_LAnd ? "&&" : "||") |
| 6780 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6781 | } |
| 6782 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6783 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6784 | if (!Context.getLangOptions().CPlusPlus) { |
| 6785 | UsualUnaryConversions(lex); |
| 6786 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6787 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6788 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 6789 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6790 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6791 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6792 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6793 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6794 | // The following is safe because we only use this method for |
| 6795 | // non-overloadable operands. |
| 6796 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6797 | // C++ [expr.log.and]p1 |
| 6798 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6799 | // The operands are both contextually converted to type bool. |
| 6800 | if (PerformContextuallyConvertToBool(lex) || |
| 6801 | PerformContextuallyConvertToBool(rex)) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6802 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6803 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6804 | // C++ [expr.log.and]p2 |
| 6805 | // C++ [expr.log.or]p2 |
| 6806 | // The result is a bool. |
| 6807 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 6808 | } |
| 6809 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6810 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6811 | /// is a read-only property; return true if so. A readonly property expression |
| 6812 | /// depends on various declarations and thus must be treated specially. |
| 6813 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6814 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6815 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6816 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6817 | if (PropExpr->isImplicitProperty()) return false; |
| 6818 | |
| 6819 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6820 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6821 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6822 | PropExpr->getBase()->getType(); |
| 6823 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6824 | if (const ObjCObjectPointerType *OPT = |
| 6825 | BaseType->getAsObjCInterfacePointerType()) |
| 6826 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6827 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6828 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6829 | } |
| 6830 | return false; |
| 6831 | } |
| 6832 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6833 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6834 | /// emit an error and return true. If so, return false. |
| 6835 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6836 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6837 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6838 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6839 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6840 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6841 | if (IsLV == Expr::MLV_Valid) |
| 6842 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6843 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6844 | unsigned Diag = 0; |
| 6845 | bool NeedType = false; |
| 6846 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6847 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6848 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6849 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6850 | NeedType = true; |
| 6851 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6852 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6853 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6854 | NeedType = true; |
| 6855 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6856 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6857 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6858 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6859 | case Expr::MLV_Valid: |
| 6860 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6861 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6862 | case Expr::MLV_MemberFunction: |
| 6863 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6864 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 6865 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6866 | case Expr::MLV_IncompleteType: |
| 6867 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 6868 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6869 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 6870 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6871 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6872 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 6873 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 6874 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6875 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 6876 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 6877 | case Expr::MLV_ReadonlyProperty: |
| 6878 | Diag = diag::error_readonly_property_assignment; |
| 6879 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 6880 | case Expr::MLV_NoSetterProperty: |
| 6881 | Diag = diag::error_nosetter_property_assignment; |
| 6882 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 6883 | case Expr::MLV_SubObjCPropertySetting: |
| 6884 | Diag = diag::error_no_subobject_property_setting; |
| 6885 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6886 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6887 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6888 | SourceRange Assign; |
| 6889 | if (Loc != OrigLoc) |
| 6890 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6891 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6892 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6893 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6894 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6895 | return true; |
| 6896 | } |
| 6897 | |
| 6898 | |
| 6899 | |
| 6900 | // C99 6.5.16.1 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6901 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 6902 | SourceLocation Loc, |
| 6903 | QualType CompoundType) { |
| 6904 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 6905 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6906 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6907 | |
| 6908 | QualType LHSType = LHS->getType(); |
| 6909 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6910 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6911 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6912 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6913 | // Simple assignment "x = y". |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6914 | if (LHS->getObjectKind() == OK_ObjCProperty) |
| 6915 | ConvertPropertyForLValue(LHS, RHS, LHSTy); |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6916 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6917 | // Special case of NSObject attributes on c-style pointer types. |
| 6918 | if (ConvTy == IncompatiblePointer && |
| 6919 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6920 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6921 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6922 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6923 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6924 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6925 | if (ConvTy == Compatible && |
| 6926 | getLangOptions().ObjCNonFragileABI && |
| 6927 | LHSType->isObjCObjectType()) |
| 6928 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 6929 | << LHSType; |
| 6930 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6931 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 6932 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 6933 | // instead of "x += 4". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6934 | Expr *RHSCheck = RHS; |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6935 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 6936 | RHSCheck = ICE->getSubExpr(); |
| 6937 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6938 | if ((UO->getOpcode() == UO_Plus || |
| 6939 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6940 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6941 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6942 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 6943 | // And there is a space or other character before the subexpr of the |
| 6944 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 6945 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 6946 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6947 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6948 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6949 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6950 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6951 | } |
| 6952 | } else { |
| 6953 | // Compound assignment "x += y" |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6954 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6955 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6956 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6957 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6958 | RHS, AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6959 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6960 | |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 6961 | |
| 6962 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 6963 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 6964 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 6965 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 6966 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 6967 | // check. |
| 6968 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6969 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 6970 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 6971 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 6972 | !UO->getType().isVolatileQualified()) { |
| 6973 | Diag(UO->getOperatorLoc(), diag::warn_indirection_through_null) |
| 6974 | << UO->getSubExpr()->getSourceRange(); |
| 6975 | Diag(UO->getOperatorLoc(), diag::note_indirection_through_null); |
| 6976 | } |
| 6977 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6978 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 6979 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6980 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6981 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 6982 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6983 | // 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] | 6984 | // operand. |
John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 6985 | return (getLangOptions().CPlusPlus |
| 6986 | ? LHSType : LHSType.getUnqualifiedType()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6989 | // C99 6.5.17 |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6990 | static QualType CheckCommaOperands(Sema &S, Expr *&LHS, Expr *&RHS, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6991 | SourceLocation Loc) { |
| 6992 | S.DiagnoseUnusedExprResult(LHS); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 6993 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6994 | ExprResult LHSResult = S.CheckPlaceholderExpr(LHS, Loc); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 6995 | if (LHSResult.isInvalid()) |
| 6996 | return QualType(); |
| 6997 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6998 | ExprResult RHSResult = S.CheckPlaceholderExpr(RHS, Loc); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 6999 | if (RHSResult.isInvalid()) |
| 7000 | return QualType(); |
| 7001 | RHS = RHSResult.take(); |
| 7002 | |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7003 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7004 | // operands, but not unary promotions. |
| 7005 | // 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] | 7006 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7007 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7008 | // containing site to determine what should be done with the RHS. |
| 7009 | S.IgnoredValueConversions(LHS); |
| 7010 | |
| 7011 | if (!S.getLangOptions().CPlusPlus) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7012 | S.DefaultFunctionArrayLvalueConversion(RHS); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7013 | if (!RHS->getType()->isVoidType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7014 | S.RequireCompleteType(Loc, RHS->getType(), diag::err_incomplete_type); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7015 | } |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7016 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7017 | return RHS->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 7018 | } |
| 7019 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7020 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7021 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7022 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7023 | ExprValueKind &VK, |
| 7024 | SourceLocation OpLoc, |
| 7025 | bool isInc, bool isPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7026 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7027 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7028 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7029 | QualType ResType = Op->getType(); |
| 7030 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 7031 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7032 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7033 | // Decrement of bool is not allowed. |
| 7034 | if (!isInc) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7035 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7036 | return QualType(); |
| 7037 | } |
| 7038 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7039 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7040 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7041 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7042 | } else if (ResType->isAnyPointerType()) { |
| 7043 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7044 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7045 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7046 | if (PointeeTy->isVoidType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7047 | if (S.getLangOptions().CPlusPlus) { |
| 7048 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7049 | << Op->getSourceRange(); |
| 7050 | return QualType(); |
| 7051 | } |
| 7052 | |
| 7053 | // Pointer to void is a GNU extension in C. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7054 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7055 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7056 | if (S.getLangOptions().CPlusPlus) { |
| 7057 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7058 | << Op->getType() << Op->getSourceRange(); |
| 7059 | return QualType(); |
| 7060 | } |
| 7061 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7062 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7063 | << ResType << Op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7064 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 7065 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7066 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7067 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7068 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7069 | // Diagnose bad cases where we step over interface counts. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7070 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7071 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7072 | << PointeeTy << Op->getSourceRange(); |
| 7073 | return QualType(); |
| 7074 | } |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7075 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7076 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7077 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7078 | << ResType << Op->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7079 | } else if (ResType->isPlaceholderType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7080 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7081 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7082 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7083 | isInc, isPrefix); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7084 | } else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7085 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7086 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7087 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 7088 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7089 | // 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] | 7090 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7091 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7092 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7093 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7094 | // (in C or with postfix), the increment is the unqualified type of the |
| 7095 | // operand. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7096 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7097 | VK = VK_LValue; |
| 7098 | return ResType; |
| 7099 | } else { |
| 7100 | VK = VK_RValue; |
| 7101 | return ResType.getUnqualifiedType(); |
| 7102 | } |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7105 | void Sema::ConvertPropertyForRValue(Expr *&E) { |
| 7106 | assert(E->getValueKind() == VK_LValue && |
| 7107 | E->getObjectKind() == OK_ObjCProperty); |
| 7108 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7109 | |
| 7110 | ExprValueKind VK = VK_RValue; |
| 7111 | if (PRE->isImplicitProperty()) { |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7112 | if (const ObjCMethodDecl *GetterMethod = |
| 7113 | PRE->getImplicitPropertyGetter()) { |
| 7114 | QualType Result = GetterMethod->getResultType(); |
| 7115 | VK = Expr::getValueKindForType(Result); |
| 7116 | } |
| 7117 | else { |
| 7118 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7119 | << PRE->getBase()->getType(); |
| 7120 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
| 7123 | E = ImplicitCastExpr::Create(Context, E->getType(), CK_GetObjCProperty, |
| 7124 | E, 0, VK); |
John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7125 | |
| 7126 | ExprResult Result = MaybeBindToTemporary(E); |
| 7127 | if (!Result.isInvalid()) |
| 7128 | E = Result.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7129 | } |
| 7130 | |
| 7131 | void Sema::ConvertPropertyForLValue(Expr *&LHS, Expr *&RHS, QualType &LHSTy) { |
| 7132 | assert(LHS->getValueKind() == VK_LValue && |
| 7133 | LHS->getObjectKind() == OK_ObjCProperty); |
| 7134 | const ObjCPropertyRefExpr *PRE = LHS->getObjCProperty(); |
| 7135 | |
| 7136 | if (PRE->isImplicitProperty()) { |
| 7137 | // If using property-dot syntax notation for assignment, and there is a |
| 7138 | // setter, RHS expression is being passed to the setter argument. So, |
| 7139 | // type conversion (and comparison) is RHS to setter's argument type. |
| 7140 | if (const ObjCMethodDecl *SetterMD = PRE->getImplicitPropertySetter()) { |
| 7141 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7142 | LHSTy = (*P)->getType(); |
| 7143 | |
| 7144 | // Otherwise, if the getter returns an l-value, just call that. |
| 7145 | } else { |
| 7146 | QualType Result = PRE->getImplicitPropertyGetter()->getResultType(); |
| 7147 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7148 | if (VK == VK_LValue) { |
| 7149 | LHS = ImplicitCastExpr::Create(Context, LHS->getType(), |
| 7150 | CK_GetObjCProperty, LHS, 0, VK); |
| 7151 | return; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7152 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7153 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7154 | } |
| 7155 | |
| 7156 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7157 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7158 | InitializedEntity::InitializeParameter(Context, LHSTy); |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7159 | Expr *Arg = RHS; |
| 7160 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), |
| 7161 | Owned(Arg)); |
| 7162 | if (!ArgE.isInvalid()) |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 7163 | RHS = ArgE.takeAs<Expr>(); |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7164 | } |
| 7165 | } |
| 7166 | |
| 7167 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7168 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7169 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7170 | /// where the declaration is needed for type checking. We only need to |
| 7171 | /// handle cases when the expression references a function designator |
| 7172 | /// or is an lvalue. Here are some examples: |
| 7173 | /// - &(x) => x |
| 7174 | /// - &*****f => f for f a function designator. |
| 7175 | /// - &s.xx => s |
| 7176 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7177 | /// - *(x + 1) -> x, if x is an array |
| 7178 | /// - &"123"[2] -> 0 |
| 7179 | /// - & __real__ x -> x |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7180 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7181 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7182 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7183 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7184 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7185 | // If this is an arrow operator, the address is an offset from |
| 7186 | // the base's value, so the object the base refers to is |
| 7187 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7188 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7189 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7190 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7191 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7192 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7193 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7194 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7195 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7196 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7197 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7198 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7199 | } |
| 7200 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7201 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7202 | case Stmt::UnaryOperatorClass: { |
| 7203 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7204 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7205 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7206 | case UO_Real: |
| 7207 | case UO_Imag: |
| 7208 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7209 | return getPrimaryDecl(UO->getSubExpr()); |
| 7210 | default: |
| 7211 | return 0; |
| 7212 | } |
| 7213 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7214 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7215 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7216 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7217 | // If the result of an implicit cast is an l-value, we care about |
| 7218 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7219 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7220 | default: |
| 7221 | return 0; |
| 7222 | } |
| 7223 | } |
| 7224 | |
| 7225 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7226 | /// 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] | 7227 | /// 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] | 7228 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 7229 | /// 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] | 7230 | /// 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] | 7231 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7232 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7233 | SourceLocation OpLoc) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7234 | if (OrigOp->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7235 | return S.Context.DependentTy; |
| 7236 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7237 | return S.Context.OverloadTy; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7238 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7239 | ExprResult PR = S.CheckPlaceholderExpr(OrigOp, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7240 | if (PR.isInvalid()) return QualType(); |
| 7241 | OrigOp = PR.take(); |
| 7242 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7243 | // Make sure to ignore parentheses in subsequent checks |
| 7244 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7245 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7246 | if (S.getLangOptions().C99) { |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7247 | // Implement C99-only parts of addressof rules. |
| 7248 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7249 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7250 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7251 | // (assuming the deref expression is valid). |
| 7252 | return uOp->getSubExpr()->getType(); |
| 7253 | } |
| 7254 | // Technically, there should be a check for array subscript |
| 7255 | // expressions here, but the result of one is always an lvalue anyway. |
| 7256 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 7257 | NamedDecl *dcl = getPrimaryDecl(op); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7258 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7259 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7260 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7261 | bool sfinae = S.isSFINAEContext(); |
| 7262 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7263 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7264 | << op->getType() << op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7265 | if (sfinae) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7266 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7267 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7268 | return S.Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7269 | } else if (lval == Expr::LV_MemberFunction) { |
| 7270 | // If it's an instance method, make a member pointer. |
| 7271 | // The expression must have exactly the form &A::foo. |
| 7272 | |
| 7273 | // If the underlying expression isn't a decl ref, give up. |
| 7274 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7275 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7276 | << OrigOp->getSourceRange(); |
| 7277 | return QualType(); |
| 7278 | } |
| 7279 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7280 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7281 | |
| 7282 | // The id-expression was parenthesized. |
| 7283 | if (OrigOp != DRE) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7284 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7285 | << OrigOp->getSourceRange(); |
| 7286 | |
| 7287 | // The method was named without a qualifier. |
| 7288 | } else if (!DRE->getQualifier()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7289 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7290 | << op->getSourceRange(); |
| 7291 | } |
| 7292 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7293 | return S.Context.getMemberPointerType(op->getType(), |
| 7294 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7295 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7296 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7297 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7298 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7299 | // FIXME: emit more specific diag... |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7300 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7301 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7302 | return QualType(); |
| 7303 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7304 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7305 | // The operand cannot be a bit-field |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7306 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7307 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7308 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7309 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7310 | // The operand cannot be an element of a vector |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7311 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7312 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7313 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7314 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7315 | // cannot take address of a property expression. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7316 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7317 | << "property expression" << op->getSourceRange(); |
| 7318 | return QualType(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7319 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7320 | // 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] | 7321 | // with the register storage-class specifier. |
| 7322 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7323 | // in C++ it is not error to take address of a register |
| 7324 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7325 | if (vd->getStorageClass() == SC_Register && |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7326 | !S.getLangOptions().CPlusPlus) { |
| 7327 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7328 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7329 | return QualType(); |
| 7330 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7331 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7332 | return S.Context.OverloadTy; |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7333 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7334 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7335 | // Could be a pointer to member, though, if there is an explicit |
| 7336 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7337 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7338 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7339 | if (Ctx && Ctx->isRecord()) { |
| 7340 | if (FD->getType()->isReferenceType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7341 | S.Diag(OpLoc, |
| 7342 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7343 | << FD->getDeclName() << FD->getType(); |
| 7344 | return QualType(); |
| 7345 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7346 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7347 | return S.Context.getMemberPointerType(op->getType(), |
| 7348 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7349 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7350 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7351 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 7352 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7353 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7354 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7355 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7356 | // Taking the address of a void variable is technically illegal, but we |
| 7357 | // allow it in cases which are otherwise valid. |
| 7358 | // Example: "extern void x; void* y = &x;". |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7359 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7360 | } |
| 7361 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7362 | // 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] | 7363 | if (op->getType()->isObjCObjectType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7364 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7365 | return S.Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7366 | } |
| 7367 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7368 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7369 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7370 | SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7371 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7372 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7373 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7374 | S.UsualUnaryConversions(Op); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7375 | QualType OpTy = Op->getType(); |
| 7376 | QualType Result; |
| 7377 | |
| 7378 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7379 | // is an incomplete type or void. It would be possible to warn about |
| 7380 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7381 | // warning is unlikely to catch any mistakes. |
| 7382 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7383 | Result = PT->getPointeeType(); |
| 7384 | else if (const ObjCObjectPointerType *OPT = |
| 7385 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7386 | Result = OPT->getPointeeType(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7387 | else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7388 | ExprResult PR = S.CheckPlaceholderExpr(Op, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7389 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7390 | if (PR.take() != Op) |
| 7391 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7392 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7393 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7394 | if (Result.isNull()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7395 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7396 | << OpTy << Op->getSourceRange(); |
| 7397 | return QualType(); |
| 7398 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7399 | |
| 7400 | // Dereferences are usually l-values... |
| 7401 | VK = VK_LValue; |
| 7402 | |
| 7403 | // ...except that certain expressions are never l-values in C. |
| 7404 | if (!S.getLangOptions().CPlusPlus && |
| 7405 | IsCForbiddenLValueType(S.Context, Result)) |
| 7406 | VK = VK_RValue; |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7407 | |
| 7408 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 7409 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7410 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7411 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7412 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7413 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7414 | switch (Kind) { |
| 7415 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7416 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7417 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7418 | case tok::star: Opc = BO_Mul; break; |
| 7419 | case tok::slash: Opc = BO_Div; break; |
| 7420 | case tok::percent: Opc = BO_Rem; break; |
| 7421 | case tok::plus: Opc = BO_Add; break; |
| 7422 | case tok::minus: Opc = BO_Sub; break; |
| 7423 | case tok::lessless: Opc = BO_Shl; break; |
| 7424 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7425 | case tok::lessequal: Opc = BO_LE; break; |
| 7426 | case tok::less: Opc = BO_LT; break; |
| 7427 | case tok::greaterequal: Opc = BO_GE; break; |
| 7428 | case tok::greater: Opc = BO_GT; break; |
| 7429 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7430 | case tok::equalequal: Opc = BO_EQ; break; |
| 7431 | case tok::amp: Opc = BO_And; break; |
| 7432 | case tok::caret: Opc = BO_Xor; break; |
| 7433 | case tok::pipe: Opc = BO_Or; break; |
| 7434 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7435 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7436 | case tok::equal: Opc = BO_Assign; break; |
| 7437 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7438 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7439 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7440 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7441 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7442 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7443 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7444 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7445 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7446 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7447 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7448 | } |
| 7449 | return Opc; |
| 7450 | } |
| 7451 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7452 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7453 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7454 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7455 | switch (Kind) { |
| 7456 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7457 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7458 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7459 | case tok::amp: Opc = UO_AddrOf; break; |
| 7460 | case tok::star: Opc = UO_Deref; break; |
| 7461 | case tok::plus: Opc = UO_Plus; break; |
| 7462 | case tok::minus: Opc = UO_Minus; break; |
| 7463 | case tok::tilde: Opc = UO_Not; break; |
| 7464 | case tok::exclaim: Opc = UO_LNot; break; |
| 7465 | case tok::kw___real: Opc = UO_Real; break; |
| 7466 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7467 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7468 | } |
| 7469 | return Opc; |
| 7470 | } |
| 7471 | |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7472 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7473 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7474 | /// suppressed in the event of macro expansions. |
| 7475 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7476 | SourceLocation OpLoc) { |
| 7477 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7478 | return; |
| 7479 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7480 | return; |
| 7481 | lhs = lhs->IgnoreParenImpCasts(); |
| 7482 | rhs = rhs->IgnoreParenImpCasts(); |
| 7483 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7484 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7485 | if (!LeftDeclRef || !RightDeclRef || |
| 7486 | LeftDeclRef->getLocation().isMacroID() || |
| 7487 | RightDeclRef->getLocation().isMacroID()) |
| 7488 | return; |
| 7489 | const ValueDecl *LeftDecl = |
| 7490 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7491 | const ValueDecl *RightDecl = |
| 7492 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7493 | if (LeftDecl != RightDecl) |
| 7494 | return; |
| 7495 | if (LeftDecl->getType().isVolatileQualified()) |
| 7496 | return; |
| 7497 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7498 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7499 | return; |
| 7500 | |
| 7501 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7502 | << LeftDeclRef->getType() |
| 7503 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7504 | } |
| 7505 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7506 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7507 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7508 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7509 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7510 | BinaryOperatorKind Opc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7511 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7512 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7513 | // The following two variables are used for compound assignment operators |
| 7514 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7515 | QualType CompResultTy; // Type of computation result |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7516 | ExprValueKind VK = VK_RValue; |
| 7517 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7518 | |
| 7519 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7520 | case BO_Assign: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7521 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7522 | if (getLangOptions().CPlusPlus && |
| 7523 | lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7524 | VK = lhs->getValueKind(); |
| 7525 | OK = lhs->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7526 | } |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7527 | if (!ResultTy.isNull()) |
| 7528 | DiagnoseSelfAssignment(*this, lhs, rhs, OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7529 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7530 | case BO_PtrMemD: |
| 7531 | case BO_PtrMemI: |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7532 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7533 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7534 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7535 | case BO_Mul: |
| 7536 | case BO_Div: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7537 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7538 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7539 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7540 | case BO_Rem: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7541 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7542 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7543 | case BO_Add: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7544 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7545 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7546 | case BO_Sub: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7547 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7548 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7549 | case BO_Shl: |
| 7550 | case BO_Shr: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7551 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 7552 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7553 | case BO_LE: |
| 7554 | case BO_LT: |
| 7555 | case BO_GE: |
| 7556 | case BO_GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7557 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7558 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7559 | case BO_EQ: |
| 7560 | case BO_NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7561 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7562 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7563 | case BO_And: |
| 7564 | case BO_Xor: |
| 7565 | case BO_Or: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7566 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7567 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7568 | case BO_LAnd: |
| 7569 | case BO_LOr: |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7570 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7571 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7572 | case BO_MulAssign: |
| 7573 | case BO_DivAssign: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7574 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7575 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7576 | CompLHSTy = CompResultTy; |
| 7577 | if (!CompResultTy.isNull()) |
| 7578 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7579 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7580 | case BO_RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7581 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7582 | CompLHSTy = CompResultTy; |
| 7583 | if (!CompResultTy.isNull()) |
| 7584 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7585 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7586 | case BO_AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7587 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 7588 | if (!CompResultTy.isNull()) |
| 7589 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7590 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7591 | case BO_SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7592 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 7593 | if (!CompResultTy.isNull()) |
| 7594 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7595 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7596 | case BO_ShlAssign: |
| 7597 | case BO_ShrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7598 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 7599 | CompLHSTy = CompResultTy; |
| 7600 | if (!CompResultTy.isNull()) |
| 7601 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7602 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7603 | case BO_AndAssign: |
| 7604 | case BO_XorAssign: |
| 7605 | case BO_OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7606 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 7607 | CompLHSTy = CompResultTy; |
| 7608 | if (!CompResultTy.isNull()) |
| 7609 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7610 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7611 | case BO_Comma: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7612 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7613 | if (getLangOptions().CPlusPlus) { |
| 7614 | VK = rhs->getValueKind(); |
| 7615 | OK = rhs->getObjectKind(); |
| 7616 | } |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7617 | break; |
| 7618 | } |
| 7619 | if (ResultTy.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7620 | return ExprError(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7621 | if (CompResultTy.isNull()) |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7622 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, |
| 7623 | VK, OK, OpLoc)); |
| 7624 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7625 | if (getLangOptions().CPlusPlus && lhs->getObjectKind() != OK_ObjCProperty) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7626 | VK = VK_LValue; |
| 7627 | OK = lhs->getObjectKind(); |
| 7628 | } |
| 7629 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
| 7630 | VK, OK, CompLHSTy, |
| 7631 | CompResultTy, OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7632 | } |
| 7633 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7634 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 7635 | /// ParenRange in parentheses. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7636 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 7637 | const PartialDiagnostic &PD, |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7638 | const PartialDiagnostic &FirstNote, |
| 7639 | SourceRange FirstParenRange, |
| 7640 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7641 | SourceRange SecondParenRange) { |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7642 | Self.Diag(Loc, PD); |
| 7643 | |
| 7644 | if (!FirstNote.getDiagID()) |
| 7645 | return; |
| 7646 | |
| 7647 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 7648 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 7649 | // We can't display the parentheses, so just return. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7650 | return; |
| 7651 | } |
| 7652 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7653 | Self.Diag(Loc, FirstNote) |
| 7654 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7655 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7656 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7657 | if (!SecondNote.getDiagID()) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7658 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7659 | |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7660 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 7661 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 7662 | // We can't display the parentheses, so just dig the |
| 7663 | // warning/error and return. |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7664 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7665 | return; |
| 7666 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7667 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7668 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7669 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 7670 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7671 | } |
| 7672 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7673 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7674 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7675 | /// comparison operators have higher precedence. The most typical example of |
| 7676 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7677 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7678 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7679 | typedef BinaryOperator BinOp; |
| 7680 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 7681 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 7682 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7683 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7684 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7685 | rhsopc = BO->getOpcode(); |
| 7686 | |
| 7687 | // Subs are not binary operators. |
| 7688 | if (lhsopc == -1 && rhsopc == -1) |
| 7689 | return; |
| 7690 | |
| 7691 | // Bitwise operations are sometimes used as eager logical ops. |
| 7692 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7693 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 7694 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7695 | return; |
| 7696 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7697 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7698 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7699 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7700 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 7701 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7702 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7703 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7704 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 7705 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7706 | << BinOp::getOpcodeStr(lhsopc), |
| 7707 | lhs->getSourceRange()); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7708 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7709 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7710 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7711 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 7712 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7713 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7714 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7715 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 7716 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7717 | << BinOp::getOpcodeStr(rhsopc), |
| 7718 | rhs->getSourceRange()); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7719 | } |
| 7720 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7721 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7722 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7723 | /// in parentheses. |
| 7724 | static void |
| 7725 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| 7726 | Expr *E) { |
| 7727 | assert(isa<BinaryOperator>(E) && |
| 7728 | cast<BinaryOperator>(E)->getOpcode() == BO_LAnd); |
| 7729 | SuggestParentheses(Self, OpLoc, |
| 7730 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| 7731 | << E->getSourceRange(), |
| 7732 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| 7733 | E->getSourceRange(), |
| 7734 | Self.PDiag(0), SourceRange()); |
| 7735 | } |
| 7736 | |
| 7737 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7738 | /// 'true'. |
| 7739 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7740 | bool Res; |
| 7741 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7742 | } |
| 7743 | |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7744 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7745 | /// 'false'. |
| 7746 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7747 | bool Res; |
| 7748 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7749 | } |
| 7750 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7751 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7752 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7753 | Expr *OrLHS, Expr *OrRHS) { |
| 7754 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7755 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7756 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 7757 | if (EvaluatesAsFalse(S, OrRHS)) |
| 7758 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7759 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7760 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7761 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7762 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7763 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7764 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7765 | // "a || b && 1", but warn now. |
| 7766 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7767 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7768 | } |
| 7769 | } |
| 7770 | } |
| 7771 | } |
| 7772 | |
| 7773 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7774 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7775 | Expr *OrLHS, Expr *OrRHS) { |
| 7776 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7777 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7778 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 7779 | if (EvaluatesAsFalse(S, OrLHS)) |
| 7780 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7781 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7782 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7783 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7784 | } |
| 7785 | } |
| 7786 | } |
| 7787 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7788 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7789 | /// precedence. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7790 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7791 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7792 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7793 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7794 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 7795 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7796 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7797 | // 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] | 7798 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7799 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 7800 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7801 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7804 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7805 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7806 | tok::TokenKind Kind, |
| 7807 | Expr *lhs, Expr *rhs) { |
| 7808 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 7809 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 7810 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7811 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7812 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 7813 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 7814 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7815 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 7816 | } |
| 7817 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7818 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7819 | BinaryOperatorKind Opc, |
| 7820 | Expr *lhs, Expr *rhs) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7821 | if (getLangOptions().CPlusPlus) { |
| 7822 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7823 | |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7824 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 7825 | UseBuiltinOperator = false; |
| 7826 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 7827 | UseBuiltinOperator = true; |
| 7828 | } else { |
| 7829 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 7830 | !rhs->getType()->isOverloadableType(); |
| 7831 | } |
| 7832 | |
| 7833 | if (!UseBuiltinOperator) { |
| 7834 | // Find all of the overloaded operators visible from this |
| 7835 | // point. We perform both an operator-name lookup from the local |
| 7836 | // scope and an argument-dependent lookup based on the types of |
| 7837 | // the arguments. |
| 7838 | UnresolvedSet<16> Functions; |
| 7839 | OverloadedOperatorKind OverOp |
| 7840 | = BinaryOperator::getOverloadedOperator(Opc); |
| 7841 | if (S && OverOp != OO_None) |
| 7842 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 7843 | Functions); |
| 7844 | |
| 7845 | // Build the (potentially-overloaded, potentially-dependent) |
| 7846 | // binary operation. |
| 7847 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 7848 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7849 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7850 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7851 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7852 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7853 | } |
| 7854 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7855 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7856 | UnaryOperatorKind Opc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7857 | Expr *Input) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7858 | ExprValueKind VK = VK_RValue; |
| 7859 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7860 | QualType resultType; |
| 7861 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7862 | case UO_PreInc: |
| 7863 | case UO_PreDec: |
| 7864 | case UO_PostInc: |
| 7865 | case UO_PostDec: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7866 | resultType = CheckIncrementDecrementOperand(*this, Input, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7867 | Opc == UO_PreInc || |
| 7868 | Opc == UO_PostInc, |
| 7869 | Opc == UO_PreInc || |
| 7870 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7871 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7872 | case UO_AddrOf: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7873 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7874 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7875 | case UO_Deref: |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 7876 | DefaultFunctionArrayLvalueConversion(Input); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7877 | resultType = CheckIndirectionOperand(*this, Input, VK, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7878 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7879 | case UO_Plus: |
| 7880 | case UO_Minus: |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 7881 | UsualUnaryConversions(Input); |
| 7882 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7883 | if (resultType->isDependentType()) |
| 7884 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 7885 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 7886 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7887 | break; |
| 7888 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 7889 | resultType->isEnumeralType()) |
| 7890 | break; |
| 7891 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7892 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7893 | resultType->isPointerType()) |
| 7894 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7895 | else if (resultType->isPlaceholderType()) { |
| 7896 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 7897 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7898 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7899 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 7900 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7901 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 7902 | << resultType << Input->getSourceRange()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7903 | case UO_Not: // bitwise complement |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 7904 | UsualUnaryConversions(Input); |
| 7905 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7906 | if (resultType->isDependentType()) |
| 7907 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 7908 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 7909 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 7910 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7911 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7912 | << resultType << Input->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7913 | else if (resultType->hasIntegerRepresentation()) |
| 7914 | break; |
| 7915 | else if (resultType->isPlaceholderType()) { |
| 7916 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 7917 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7918 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7919 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7920 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 7921 | << resultType << Input->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7922 | } |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7923 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7924 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 7925 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 7926 | DefaultFunctionArrayLvalueConversion(Input); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 7927 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7928 | if (resultType->isDependentType()) |
| 7929 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7930 | if (resultType->isScalarType()) { // C99 6.5.3.3p1 |
| 7931 | // ok, fallthrough |
| 7932 | } else if (resultType->isPlaceholderType()) { |
| 7933 | ExprResult PR = CheckPlaceholderExpr(Input, OpLoc); |
| 7934 | if (PR.isInvalid()) return ExprError(); |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7935 | return CreateBuiltinUnaryOp(OpLoc, Opc, PR.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7936 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7937 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 7938 | << resultType << Input->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7939 | } |
Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 7940 | |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 7941 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7942 | // In C++, it's bool. C++ 5.3.1p8 |
| 7943 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7944 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7945 | case UO_Real: |
| 7946 | case UO_Imag: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7947 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7948 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
| 7949 | if (Input->getValueKind() != VK_RValue && |
| 7950 | Input->getObjectKind() == OK_Ordinary) |
| 7951 | VK = Input->getValueKind(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 7952 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7953 | case UO_Extension: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 7954 | resultType = Input->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7955 | VK = Input->getValueKind(); |
| 7956 | OK = Input->getObjectKind(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 7957 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7958 | } |
| 7959 | if (resultType.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 7960 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7961 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7962 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, |
| 7963 | VK, OK, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7964 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 7965 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7966 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7967 | UnaryOperatorKind Opc, |
| 7968 | Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 7969 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 7970 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7971 | // Find all of the overloaded operators visible from this |
| 7972 | // point. We perform both an operator-name lookup from the local |
| 7973 | // scope and an argument-dependent lookup based on the types of |
| 7974 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7975 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7976 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 7977 | if (S && OverOp != OO_None) |
| 7978 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 7979 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7980 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7981 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7982 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7983 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7984 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 7985 | } |
| 7986 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7987 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7988 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 7989 | tok::TokenKind Op, Expr *Input) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7990 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7991 | } |
| 7992 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 7993 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7994 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7995 | SourceLocation LabLoc, |
| 7996 | IdentifierInfo *LabelII) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 7997 | // Look up the record for this label identifier. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 7998 | LabelStmt *&LabelDecl = getCurFunction()->LabelMap[LabelII]; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7999 | |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 8000 | // If we haven't seen this label yet, create a forward reference. It |
| 8001 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | 846b1ec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 8002 | if (LabelDecl == 0) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 8003 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8004 | |
Argyrios Kyrtzidis | 72664df | 2010-09-19 21:21:25 +0000 | [diff] [blame] | 8005 | LabelDecl->setUsed(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8006 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8007 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 8008 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8009 | } |
| 8010 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8011 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8012 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8013 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8014 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8015 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8016 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8017 | bool isFileScope |
| 8018 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8019 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8020 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8021 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8022 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8023 | // example, it is not possible to goto into a stmt expression apparently. |
| 8024 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8025 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8026 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8027 | // as the type of the stmtexpr. |
| 8028 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8029 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8030 | if (!Compound->body_empty()) { |
| 8031 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8032 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8033 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8034 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8035 | LastLabelStmt = Label; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8036 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8037 | } |
| 8038 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8039 | // Do function/array conversion on the last expression, but not |
| 8040 | // lvalue-to-rvalue. However, initialize an unqualified type. |
| 8041 | DefaultFunctionArrayConversion(LastExpr); |
| 8042 | Ty = LastExpr->getType().getUnqualifiedType(); |
| 8043 | |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8044 | if (!Ty->isDependentType() && !LastExpr->isTypeDependent()) { |
| 8045 | ExprResult Res = PerformCopyInitialization( |
| 8046 | InitializedEntity::InitializeResult(LPLoc, |
| 8047 | Ty, |
| 8048 | false), |
| 8049 | SourceLocation(), |
| 8050 | Owned(LastExpr)); |
| 8051 | if (Res.isInvalid()) |
| 8052 | return ExprError(); |
| 8053 | if ((LastExpr = Res.takeAs<Expr>())) { |
| 8054 | if (!LastLabelStmt) |
| 8055 | Compound->setLastStmt(LastExpr); |
| 8056 | else |
| 8057 | LastLabelStmt->setSubStmt(LastExpr); |
| 8058 | StmtExprMayBindToTemp = true; |
| 8059 | } |
| 8060 | } |
| 8061 | } |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8062 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8063 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8064 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8065 | // expressions are not lvalues. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8066 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8067 | if (StmtExprMayBindToTemp) |
| 8068 | return MaybeBindToTemporary(ResStmtExpr); |
| 8069 | return Owned(ResStmtExpr); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8070 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8071 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8072 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8073 | TypeSourceInfo *TInfo, |
| 8074 | OffsetOfComponent *CompPtr, |
| 8075 | unsigned NumComponents, |
| 8076 | SourceLocation RParenLoc) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8077 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8078 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8079 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8080 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8081 | // We must have at least one component that refers to the type, and the first |
| 8082 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8083 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8084 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8085 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8086 | << ArgTy << TypeRange); |
| 8087 | |
| 8088 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8089 | // with an incomplete type would be ill-formed. |
| 8090 | if (!Dependent |
| 8091 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8092 | PDiag(diag::err_offsetof_incomplete_type) |
| 8093 | << TypeRange)) |
| 8094 | return ExprError(); |
| 8095 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8096 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8097 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8098 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8099 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8100 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8101 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8102 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8103 | |
| 8104 | bool DidWarnAboutNonPOD = false; |
| 8105 | QualType CurrentType = ArgTy; |
| 8106 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 8107 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 8108 | llvm::SmallVector<Expr*, 4> Exprs; |
| 8109 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8110 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8111 | if (OC.isBrackets) { |
| 8112 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8113 | if (!CurrentType->isDependentType()) { |
| 8114 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8115 | if(!AT) |
| 8116 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8117 | << CurrentType); |
| 8118 | CurrentType = AT->getElementType(); |
| 8119 | } else |
| 8120 | CurrentType = Context.DependentTy; |
| 8121 | |
| 8122 | // The expression must be an integral expression. |
| 8123 | // FIXME: An integral constant expression? |
| 8124 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8125 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8126 | !Idx->getType()->isIntegerType()) |
| 8127 | return ExprError(Diag(Idx->getLocStart(), |
| 8128 | diag::err_typecheck_subscript_not_integer) |
| 8129 | << Idx->getSourceRange()); |
| 8130 | |
| 8131 | // Record this array index. |
| 8132 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8133 | Exprs.push_back(Idx); |
| 8134 | continue; |
| 8135 | } |
| 8136 | |
| 8137 | // Offset of a field. |
| 8138 | if (CurrentType->isDependentType()) { |
| 8139 | // We have the offset of a field, but we can't look into the dependent |
| 8140 | // type. Just record the identifier of the field. |
| 8141 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8142 | CurrentType = Context.DependentTy; |
| 8143 | continue; |
| 8144 | } |
| 8145 | |
| 8146 | // We need to have a complete type to look into. |
| 8147 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8148 | diag::err_offsetof_incomplete_type)) |
| 8149 | return ExprError(); |
| 8150 | |
| 8151 | // Look for the designated field. |
| 8152 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8153 | if (!RC) |
| 8154 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8155 | << CurrentType); |
| 8156 | RecordDecl *RD = RC->getDecl(); |
| 8157 | |
| 8158 | // C++ [lib.support.types]p5: |
| 8159 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8160 | // International Standard. type shall be a POD structure or a POD union |
| 8161 | // (clause 9). |
| 8162 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8163 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| 8164 | DiagRuntimeBehavior(BuiltinLoc, |
| 8165 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8166 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8167 | << CurrentType)) |
| 8168 | DidWarnAboutNonPOD = true; |
| 8169 | } |
| 8170 | |
| 8171 | // Look for the field. |
| 8172 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8173 | LookupQualifiedName(R, RD); |
| 8174 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8175 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8176 | if (!MemberDecl) { |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8177 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8178 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8179 | } |
| 8180 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8181 | if (!MemberDecl) |
| 8182 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8183 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8184 | OC.LocEnd)); |
| 8185 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8186 | // C99 7.17p3: |
| 8187 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8188 | // |
| 8189 | // We diagnose this as an error. |
| 8190 | if (MemberDecl->getBitWidth()) { |
| 8191 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8192 | << MemberDecl->getDeclName() |
| 8193 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8194 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8195 | return ExprError(); |
| 8196 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8197 | |
| 8198 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8199 | if (IndirectMemberDecl) |
| 8200 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8201 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8202 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8203 | // the base class indirections. |
| 8204 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8205 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8206 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8207 | CXXBasePath &Path = Paths.front(); |
| 8208 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8209 | B != BEnd; ++B) |
| 8210 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8211 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8212 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8213 | if (IndirectMemberDecl) { |
| 8214 | for (IndirectFieldDecl::chain_iterator FI = |
| 8215 | IndirectMemberDecl->chain_begin(), |
| 8216 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8217 | assert(isa<FieldDecl>(*FI)); |
| 8218 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8219 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8220 | } |
| 8221 | } else |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8222 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8223 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8224 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8225 | } |
| 8226 | |
| 8227 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8228 | TInfo, Comps.data(), Comps.size(), |
| 8229 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8230 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8231 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8232 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8233 | SourceLocation BuiltinLoc, |
| 8234 | SourceLocation TypeLoc, |
| 8235 | ParsedType argty, |
| 8236 | OffsetOfComponent *CompPtr, |
| 8237 | unsigned NumComponents, |
| 8238 | SourceLocation RPLoc) { |
| 8239 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8240 | TypeSourceInfo *ArgTInfo; |
| 8241 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8242 | if (ArgTy.isNull()) |
| 8243 | return ExprError(); |
| 8244 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8245 | if (!ArgTInfo) |
| 8246 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8247 | |
| 8248 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8249 | RPLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8250 | } |
| 8251 | |
| 8252 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8253 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8254 | Expr *CondExpr, |
| 8255 | Expr *LHSExpr, Expr *RHSExpr, |
| 8256 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8257 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8258 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8259 | ExprValueKind VK = VK_RValue; |
| 8260 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8261 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8262 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8263 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8264 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8265 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8266 | } else { |
| 8267 | // The conditional expression is required to be a constant expression. |
| 8268 | llvm::APSInt condEval(32); |
| 8269 | SourceLocation ExpLoc; |
| 8270 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8271 | return ExprError(Diag(ExpLoc, |
| 8272 | diag::err_typecheck_choose_expr_requires_constant) |
| 8273 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8274 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8275 | // 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] | 8276 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8277 | |
| 8278 | resType = ActiveExpr->getType(); |
| 8279 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8280 | VK = ActiveExpr->getValueKind(); |
| 8281 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8282 | } |
| 8283 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8284 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8285 | resType, VK, OK, RPLoc, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8286 | resType->isDependentType(), |
| 8287 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8288 | } |
| 8289 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8290 | //===----------------------------------------------------------------------===// |
| 8291 | // Clang Extensions. |
| 8292 | //===----------------------------------------------------------------------===// |
| 8293 | |
| 8294 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8295 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8296 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8297 | PushBlockScope(BlockScope, Block); |
| 8298 | CurContext->addDecl(Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8299 | if (BlockScope) |
| 8300 | PushDeclContext(BlockScope, Block); |
| 8301 | else |
| 8302 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8303 | } |
| 8304 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8305 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8306 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8307 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8308 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8309 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8310 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8311 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8312 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8313 | // GetTypeForDeclarator always produces a function type for a block |
| 8314 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8315 | // unless the function was written with a typedef. |
| 8316 | assert(T->isFunctionType() && |
| 8317 | "GetTypeForDeclarator made a non-function block signature"); |
| 8318 | |
| 8319 | // Look for an explicit signature in that function type. |
| 8320 | FunctionProtoTypeLoc ExplicitSignature; |
| 8321 | |
| 8322 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8323 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8324 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8325 | |
| 8326 | // Check whether that explicit signature was synthesized by |
| 8327 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8328 | // written signature. |
| 8329 | if (ExplicitSignature.getLParenLoc() == |
| 8330 | ExplicitSignature.getRParenLoc()) { |
| 8331 | // This would be much cheaper if we stored TypeLocs instead of |
| 8332 | // TypeSourceInfos. |
| 8333 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8334 | unsigned Size = Result.getFullDataSize(); |
| 8335 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8336 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8337 | |
| 8338 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8339 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8340 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8341 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8342 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8343 | CurBlock->FunctionType = T; |
| 8344 | |
| 8345 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8346 | QualType RetTy = Fn->getResultType(); |
| 8347 | bool isVariadic = |
| 8348 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8349 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8350 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8351 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8352 | // Don't allow returning a objc interface by value. |
| 8353 | if (RetTy->isObjCObjectType()) { |
| 8354 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8355 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8356 | return; |
| 8357 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8358 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8359 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8360 | // return type. TODO: what should we do with declarators like: |
| 8361 | // ^ * { ... } |
| 8362 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8363 | if (RetTy != Context.DependentTy) |
| 8364 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8365 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8366 | // Push block parameters from the declarator if we had them. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8367 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8368 | if (ExplicitSignature) { |
| 8369 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8370 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8371 | if (Param->getIdentifier() == 0 && |
| 8372 | !Param->isImplicit() && |
| 8373 | !Param->isInvalidDecl() && |
| 8374 | !getLangOptions().CPlusPlus) |
| 8375 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8376 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8377 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8378 | |
| 8379 | // Fake up parameter variables if we have a typedef, like |
| 8380 | // ^ fntype { ... } |
| 8381 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8382 | for (FunctionProtoType::arg_type_iterator |
| 8383 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8384 | ParmVarDecl *Param = |
| 8385 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8386 | ParamInfo.getSourceRange().getBegin(), |
| 8387 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8388 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8389 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8390 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8391 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8392 | // Set the parameters on the block decl. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8393 | if (!Params.empty()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8394 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8395 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8396 | CurBlock->TheDecl->param_end(), |
| 8397 | /*CheckParameterNames=*/false); |
| 8398 | } |
| 8399 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8400 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8401 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8402 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8403 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8404 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8405 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8406 | // FIXME: remove the attribute. |
| 8407 | } |
| 8408 | |
| 8409 | // Put the parameter variables in scope. We can bail out immediately |
| 8410 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8411 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8412 | return; |
| 8413 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8414 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8415 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8416 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8417 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8418 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8419 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8420 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8421 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8422 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8423 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8424 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8425 | } |
| 8426 | |
| 8427 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8428 | /// is invoked to pop the information about the block from the action impl. |
| 8429 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8430 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8431 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8432 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8433 | } |
| 8434 | |
| 8435 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8436 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8437 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8438 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8439 | // If blocks are disabled, emit an error. |
| 8440 | if (!LangOpts.Blocks) |
| 8441 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8442 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8443 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8444 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8445 | PopDeclContext(); |
| 8446 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8447 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8448 | if (!BSI->ReturnType.isNull()) |
| 8449 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8450 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8451 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8452 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8453 | |
| 8454 | // If the user wrote a function type in some form, try to use that. |
| 8455 | if (!BSI->FunctionType.isNull()) { |
| 8456 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8457 | |
| 8458 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8459 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8460 | |
| 8461 | // Turn protoless block types into nullary block types. |
| 8462 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8463 | FunctionProtoType::ExtProtoInfo EPI; |
| 8464 | EPI.ExtInfo = Ext; |
| 8465 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8466 | |
| 8467 | // Otherwise, if we don't need to change anything about the function type, |
| 8468 | // preserve its sugar structure. |
| 8469 | } else if (FTy->getResultType() == RetTy && |
| 8470 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8471 | BlockTy = BSI->FunctionType; |
| 8472 | |
| 8473 | // Otherwise, make the minimal modifications to the function type. |
| 8474 | } else { |
| 8475 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8476 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8477 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8478 | EPI.ExtInfo = Ext; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8479 | BlockTy = Context.getFunctionType(RetTy, |
| 8480 | FPT->arg_type_begin(), |
| 8481 | FPT->getNumArgs(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8482 | EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8483 | } |
| 8484 | |
| 8485 | // If we don't have a function type, just build one from nothing. |
| 8486 | } else { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8487 | FunctionProtoType::ExtProtoInfo EPI; |
| 8488 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, 0, CC_Default); |
| 8489 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8490 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8491 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8492 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8493 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8494 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8495 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8496 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 8497 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8498 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8499 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8500 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 8501 | |
| 8502 | bool Good = true; |
| 8503 | // Check goto/label use. |
| 8504 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 8505 | I = BSI->LabelMap.begin(), E = BSI->LabelMap.end(); I != E; ++I) { |
| 8506 | LabelStmt *L = I->second; |
| 8507 | |
| 8508 | // Verify that we have no forward references left. If so, there was a goto |
| 8509 | // or address of a label taken, but no definition of it. |
Argyrios Kyrtzidis | 72664df | 2010-09-19 21:21:25 +0000 | [diff] [blame] | 8510 | if (L->getSubStmt() != 0) { |
| 8511 | if (!L->isUsed()) |
| 8512 | Diag(L->getIdentLoc(), diag::warn_unused_label) << L->getName(); |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 8513 | continue; |
Argyrios Kyrtzidis | 72664df | 2010-09-19 21:21:25 +0000 | [diff] [blame] | 8514 | } |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 8515 | |
| 8516 | // Emit error. |
| 8517 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName(); |
| 8518 | Good = false; |
| 8519 | } |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8520 | if (!Good) { |
| 8521 | PopFunctionOrBlockScope(); |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 8522 | return ExprError(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8523 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8524 | |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 8525 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 8526 | BSI->hasBlockDeclRefExprs); |
| 8527 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 8528 | // Issue any analysis-based warnings. |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 8529 | const sema::AnalysisBasedWarnings::Policy &WP = |
| 8530 | AnalysisWarnings.getDefaultPolicy(); |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 8531 | AnalysisWarnings.IssueWarnings(WP, Result); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 8532 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8533 | PopFunctionOrBlockScope(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8534 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8535 | } |
| 8536 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8537 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8538 | Expr *expr, ParsedType type, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8539 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8540 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8541 | GetTypeFromParser(type, &TInfo); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8542 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8543 | } |
| 8544 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8545 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8546 | Expr *E, TypeSourceInfo *TInfo, |
| 8547 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8548 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8549 | |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8550 | // Get the va_list type |
| 8551 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8552 | if (VaListType->isArrayType()) { |
| 8553 | // Deal with implicit array decay; for example, on x86-64, |
| 8554 | // va_list is an array, but it's supposed to decay to |
| 8555 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8556 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8557 | // Make sure the input expression also decays appropriately. |
| 8558 | UsualUnaryConversions(E); |
| 8559 | } else { |
| 8560 | // Otherwise, the va_list argument must be an l-value because |
| 8561 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8562 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8563 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8564 | return ExprError(); |
| 8565 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8566 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8567 | if (!E->isTypeDependent() && |
| 8568 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8569 | return ExprError(Diag(E->getLocStart(), |
| 8570 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8571 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8572 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8573 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8574 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8575 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8576 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8577 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8578 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8579 | } |
| 8580 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8581 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8582 | // The type of __null will be int or long, depending on the size of |
| 8583 | // pointers on the target. |
| 8584 | QualType Ty; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8585 | unsigned pw = Context.Target.getPointerWidth(0); |
| 8586 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8587 | Ty = Context.IntTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8588 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8589 | Ty = Context.LongTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8590 | else if (pw == Context.Target.getLongLongWidth()) |
| 8591 | Ty = Context.LongLongTy; |
| 8592 | else { |
| 8593 | assert(!"I don't know size of pointer!"); |
| 8594 | Ty = Context.IntTy; |
| 8595 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8596 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8597 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8600 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8601 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8602 | if (!SemaRef.getLangOptions().ObjC1) |
| 8603 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8604 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8605 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8606 | if (!PT) |
| 8607 | return; |
| 8608 | |
| 8609 | // Check if the destination is of type 'id'. |
| 8610 | if (!PT->isObjCIdType()) { |
| 8611 | // Check if the destination is the 'NSString' interface. |
| 8612 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8613 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8614 | return; |
| 8615 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8616 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8617 | // Strip off any parens and casts. |
| 8618 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 8619 | if (!SL || SL->isWide()) |
| 8620 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8621 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8622 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8623 | } |
| 8624 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8625 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8626 | SourceLocation Loc, |
| 8627 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8628 | Expr *SrcExpr, AssignmentAction Action, |
| 8629 | bool *Complained) { |
| 8630 | if (Complained) |
| 8631 | *Complained = false; |
| 8632 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8633 | // Decode the result (notice that AST's are still created for extensions). |
| 8634 | bool isInvalid = false; |
| 8635 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8636 | FixItHint Hint; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8637 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8638 | switch (ConvTy) { |
| 8639 | default: assert(0 && "Unknown conversion type"); |
| 8640 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8641 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8642 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 8643 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8644 | case IntToPointer: |
| 8645 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 8646 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8647 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8648 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8649 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 8650 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8651 | case IncompatiblePointerSign: |
| 8652 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8653 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8654 | case FunctionVoidPointer: |
| 8655 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8656 | break; |
| 8657 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8658 | // If the qualifiers lost were because we were applying the |
| 8659 | // (deprecated) C++ conversion from a string literal to a char* |
| 8660 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 8661 | // Ideally, this check would be performed in |
| 8662 | // CheckPointerTypesForAssignment. However, that would require a |
| 8663 | // bit of refactoring (so that the second argument is an |
| 8664 | // expression, rather than a type), which should be done as part |
| 8665 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 8666 | // C++ semantics. |
| 8667 | if (getLangOptions().CPlusPlus && |
| 8668 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 8669 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8670 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 8671 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 8672 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 8673 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 8674 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8675 | case IntToBlockPointer: |
| 8676 | DiagKind = diag::err_int_to_block_pointer; |
| 8677 | break; |
| 8678 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 8679 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8680 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8681 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8682 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8683 | // it can give a more specific diagnostic. |
| 8684 | DiagKind = diag::warn_incompatible_qualified_id; |
| 8685 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 8686 | case IncompatibleVectors: |
| 8687 | DiagKind = diag::warn_incompatible_vectors; |
| 8688 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8689 | case Incompatible: |
| 8690 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 8691 | isInvalid = true; |
| 8692 | break; |
| 8693 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8694 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8695 | QualType FirstType, SecondType; |
| 8696 | switch (Action) { |
| 8697 | case AA_Assigning: |
| 8698 | case AA_Initializing: |
| 8699 | // The destination type comes first. |
| 8700 | FirstType = DstType; |
| 8701 | SecondType = SrcType; |
| 8702 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8703 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8704 | case AA_Returning: |
| 8705 | case AA_Passing: |
| 8706 | case AA_Converting: |
| 8707 | case AA_Sending: |
| 8708 | case AA_Casting: |
| 8709 | // The source type comes first. |
| 8710 | FirstType = SrcType; |
| 8711 | SecondType = DstType; |
| 8712 | break; |
| 8713 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8714 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8715 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8716 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8717 | if (Complained) |
| 8718 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8719 | return isInvalid; |
| 8720 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8721 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 8722 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8723 | llvm::APSInt ICEResult; |
| 8724 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 8725 | if (Result) |
| 8726 | *Result = ICEResult; |
| 8727 | return false; |
| 8728 | } |
| 8729 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8730 | Expr::EvalResult EvalResult; |
| 8731 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8732 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8733 | EvalResult.HasSideEffects) { |
| 8734 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 8735 | |
| 8736 | if (EvalResult.Diag) { |
| 8737 | // We only show the note if it's not the usual "invalid subexpression" |
| 8738 | // or if it's actually in a subexpression. |
| 8739 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 8740 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 8741 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 8742 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8743 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8744 | return true; |
| 8745 | } |
| 8746 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8747 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 8748 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8749 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8750 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8751 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 8752 | != Diagnostic::Ignored) |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8753 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8754 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8755 | if (Result) |
| 8756 | *Result = EvalResult.Val.getInt(); |
| 8757 | return false; |
| 8758 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8759 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8760 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8761 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8762 | ExprEvalContexts.push_back( |
| 8763 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8764 | } |
| 8765 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8766 | void |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8767 | Sema::PopExpressionEvaluationContext() { |
| 8768 | // Pop the current expression evaluation context off the stack. |
| 8769 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 8770 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8771 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 8772 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 8773 | if (Rec.PotentiallyReferenced) { |
| 8774 | // Mark any remaining declarations in the current position of the stack |
| 8775 | // as "referenced". If they were not meant to be referenced, semantic |
| 8776 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8777 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 8778 | I = Rec.PotentiallyReferenced->begin(), |
| 8779 | IEnd = Rec.PotentiallyReferenced->end(); |
| 8780 | I != IEnd; ++I) |
| 8781 | MarkDeclarationReferenced(I->first, I->second); |
| 8782 | } |
| 8783 | |
| 8784 | if (Rec.PotentiallyDiagnosed) { |
| 8785 | // Emit any pending diagnostics. |
| 8786 | for (PotentiallyEmittedDiagnostics::iterator |
| 8787 | I = Rec.PotentiallyDiagnosed->begin(), |
| 8788 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 8789 | I != IEnd; ++I) |
| 8790 | Diag(I->first, I->second); |
| 8791 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8792 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8793 | |
| 8794 | // When are coming out of an unevaluated context, clear out any |
| 8795 | // temporaries that we may have created as part of the evaluation of |
| 8796 | // the expression in that context: they aren't relevant because they |
| 8797 | // will never be constructed. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8798 | if (Rec.Context == Unevaluated && |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8799 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 8800 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 8801 | ExprTemporaries.end()); |
| 8802 | |
| 8803 | // Destroy the popped expression evaluation record. |
| 8804 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8805 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8806 | |
| 8807 | /// \brief Note that the given declaration was referenced in the source code. |
| 8808 | /// |
| 8809 | /// This routine should be invoke whenever a given declaration is referenced |
| 8810 | /// in the source code, and where that reference occurred. If this declaration |
| 8811 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 8812 | /// C99 6.9p3), then the declaration will be marked as used. |
| 8813 | /// |
| 8814 | /// \param Loc the location where the declaration was referenced. |
| 8815 | /// |
| 8816 | /// \param D the declaration that has been referenced by the source code. |
| 8817 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 8818 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8819 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8820 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 8821 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8822 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 8823 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 8824 | // template or not. The reason for this is that unevaluated expressions |
| 8825 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 8826 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8827 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8828 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 8829 | D->setUsed(); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8830 | return; |
| 8831 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8832 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 8833 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 8834 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8835 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8836 | // Do not mark anything as "used" within a dependent context; wait for |
| 8837 | // an instantiation. |
| 8838 | if (CurContext->isDependentContext()) |
| 8839 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8840 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8841 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8842 | case Unevaluated: |
| 8843 | // We are in an expression that is not potentially evaluated; do nothing. |
| 8844 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8845 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8846 | case PotentiallyEvaluated: |
| 8847 | // We are in a potentially-evaluated expression, so this declaration is |
| 8848 | // "used"; handle this below. |
| 8849 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8850 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8851 | case PotentiallyPotentiallyEvaluated: |
| 8852 | // We are in an expression that may be potentially evaluated; queue this |
| 8853 | // declaration reference until we know whether the expression is |
| 8854 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 8855 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8856 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 8857 | |
| 8858 | case PotentiallyEvaluatedIfUsed: |
| 8859 | // Referenced declarations will only be used if the construct in the |
| 8860 | // containing expression is used. |
| 8861 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 8862 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8863 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8864 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 8865 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8866 | unsigned TypeQuals; |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 8867 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 8868 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 8869 | return; |
| 8870 | if (!Constructor->isUsed(false)) |
| 8871 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8872 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 8873 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8874 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 8875 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 8876 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8877 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8878 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8879 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8880 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 8881 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8882 | if (Destructor->isVirtual()) |
| 8883 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8884 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 8885 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 8886 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 8887 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 8888 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 8889 | } else if (MethodDecl->isVirtual()) |
| 8890 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 8891 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 8892 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8893 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 8894 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 8895 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8896 | bool AlreadyInstantiated = false; |
| 8897 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 8898 | = Function->getTemplateSpecializationInfo()) { |
| 8899 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 8900 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8901 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 8902 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8903 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8904 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8905 | = Function->getMemberSpecializationInfo()) { |
| 8906 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 8907 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8908 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 8909 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8910 | AlreadyInstantiated = true; |
| 8911 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8912 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 8913 | if (!AlreadyInstantiated) { |
| 8914 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 8915 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 8916 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 8917 | Loc)); |
| 8918 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 8919 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 8920 | } |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 8921 | } else // Walk redefinitions, as some of them may be instantiable. |
| 8922 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 8923 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 8924 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 8925 | MarkDeclarationReferenced(Loc, *i); |
| 8926 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8927 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8928 | // FIXME: keep track of references to static functions |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 8929 | |
| 8930 | // Recursive functions should be marked when used from another function. |
| 8931 | if (CurContext != Function) |
| 8932 | Function->setUsed(true); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8933 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8934 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 8935 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8936 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8937 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 8938 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8939 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8940 | Var->getInstantiatedFromStaticDataMember()) { |
| 8941 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 8942 | assert(MSInfo && "Missing member specialization information?"); |
| 8943 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 8944 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 8945 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 8946 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 8947 | } |
| 8948 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8949 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8950 | // FIXME: keep track of references to static data? |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 8951 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8952 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 8953 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 8954 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 8955 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 8956 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8957 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8958 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8959 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8960 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8961 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 8962 | Sema &S; |
| 8963 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8964 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8965 | public: |
| 8966 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8967 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8968 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8969 | |
| 8970 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 8971 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8972 | }; |
| 8973 | } |
| 8974 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8975 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 8976 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8977 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 8978 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 8979 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8980 | |
| 8981 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8982 | } |
| 8983 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8984 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8985 | if (ClassTemplateSpecializationDecl *Spec |
| 8986 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 8987 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 8988 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 8991 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8992 | } |
| 8993 | |
| 8994 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 8995 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 8996 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 8997 | } |
| 8998 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 8999 | namespace { |
| 9000 | /// \brief Helper class that marks all of the declarations referenced by |
| 9001 | /// potentially-evaluated subexpressions as "referenced". |
| 9002 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9003 | Sema &S; |
| 9004 | |
| 9005 | public: |
| 9006 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9007 | |
| 9008 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9009 | |
| 9010 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9011 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9012 | } |
| 9013 | |
| 9014 | void VisitMemberExpr(MemberExpr *E) { |
| 9015 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9016 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9017 | } |
| 9018 | |
| 9019 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9020 | if (E->getConstructor()) |
| 9021 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9022 | if (E->getOperatorNew()) |
| 9023 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9024 | if (E->getOperatorDelete()) |
| 9025 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9026 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9027 | } |
| 9028 | |
| 9029 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9030 | if (E->getOperatorDelete()) |
| 9031 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9032 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9033 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9034 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9035 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9036 | S.LookupDestructor(Record)); |
| 9037 | } |
| 9038 | |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9039 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9040 | } |
| 9041 | |
| 9042 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9043 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9044 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9045 | } |
| 9046 | |
| 9047 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9048 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9049 | } |
Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9050 | |
| 9051 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9052 | Visit(E->getExpr()); |
| 9053 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9054 | }; |
| 9055 | } |
| 9056 | |
| 9057 | /// \brief Mark any declarations that appear within this expression or any |
| 9058 | /// potentially-evaluated subexpressions as "referenced". |
| 9059 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9060 | EvaluatedExprMarker(*this).Visit(E); |
| 9061 | } |
| 9062 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9063 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9064 | /// of the program being compiled. |
| 9065 | /// |
| 9066 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9067 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9068 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9069 | /// expressions, code used only during overload resolution, etc., are not |
| 9070 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9071 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9072 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9073 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9074 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9075 | /// This routine should be used for all diagnostics that describe the run-time |
| 9076 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9077 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9078 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9079 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9080 | const PartialDiagnostic &PD) { |
| 9081 | switch (ExprEvalContexts.back().Context ) { |
| 9082 | case Unevaluated: |
| 9083 | // The argument will never be evaluated, so don't complain. |
| 9084 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9085 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9086 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9087 | case PotentiallyEvaluatedIfUsed: |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9088 | Diag(Loc, PD); |
| 9089 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9090 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9091 | case PotentiallyPotentiallyEvaluated: |
| 9092 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9093 | break; |
| 9094 | } |
| 9095 | |
| 9096 | return false; |
| 9097 | } |
| 9098 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9099 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9100 | CallExpr *CE, FunctionDecl *FD) { |
| 9101 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9102 | return false; |
| 9103 | |
| 9104 | PartialDiagnostic Note = |
| 9105 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9106 | << FD->getDeclName() : PDiag(); |
| 9107 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9108 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9109 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9110 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9111 | PDiag(diag::err_call_function_incomplete_return) |
| 9112 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9113 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9114 | << CE->getSourceRange(), |
| 9115 | std::make_pair(NoteLoc, Note))) |
| 9116 | return true; |
| 9117 | |
| 9118 | return false; |
| 9119 | } |
| 9120 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9121 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9122 | // will prevent this condition from triggering, which is what we want. |
| 9123 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9124 | SourceLocation Loc; |
| 9125 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9126 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9127 | bool IsOrAssign = false; |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9128 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9129 | if (isa<BinaryOperator>(E)) { |
| 9130 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9131 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9132 | return; |
| 9133 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9134 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9135 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9136 | // Greylist some idioms by putting them into a warning subcategory. |
| 9137 | if (ObjCMessageExpr *ME |
| 9138 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9139 | Selector Sel = ME->getSelector(); |
| 9140 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9141 | // self = [<foo> init...] |
| 9142 | if (isSelfExpr(Op->getLHS()) |
| 9143 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 9144 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9145 | |
| 9146 | // <foo> = [<bar> nextObject] |
| 9147 | else if (Sel.isUnarySelector() && |
| 9148 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 9149 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9150 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9151 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9152 | Loc = Op->getOperatorLoc(); |
| 9153 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9154 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9155 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9156 | return; |
| 9157 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9158 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9159 | Loc = Op->getOperatorLoc(); |
| 9160 | } else { |
| 9161 | // Not an assignment. |
| 9162 | return; |
| 9163 | } |
| 9164 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9165 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | e724ae9 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 9166 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9167 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9168 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame^] | 9169 | |
| 9170 | if (IsOrAssign) |
| 9171 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9172 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9173 | else |
| 9174 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9175 | << FixItHint::CreateReplacement(Loc, "=="); |
| 9176 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9177 | Diag(Loc, diag::note_condition_assign_silence) |
| 9178 | << FixItHint::CreateInsertion(Open, "(") |
| 9179 | << FixItHint::CreateInsertion(Close, ")"); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9180 | } |
| 9181 | |
| 9182 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 9183 | DiagnoseAssignmentAsCondition(E); |
| 9184 | |
| 9185 | if (!E->isTypeDependent()) { |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9186 | if (E->isBoundMemberFunction(Context)) |
| 9187 | return Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9188 | << E->getSourceRange(); |
| 9189 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9190 | if (getLangOptions().CPlusPlus) |
| 9191 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9192 | |
| 9193 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9194 | |
| 9195 | QualType T = E->getType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9196 | if (!T->isScalarType()) // C99 6.8.4.1p1 |
| 9197 | return Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9198 | << T << E->getSourceRange(); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9199 | } |
| 9200 | |
| 9201 | return false; |
| 9202 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9203 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9204 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9205 | Expr *Sub) { |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9206 | if (!Sub) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9207 | return ExprError(); |
| 9208 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 9209 | if (CheckBooleanCondition(Sub, Loc)) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9210 | return ExprError(); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9211 | |
| 9212 | return Owned(Sub); |
| 9213 | } |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9214 | |
| 9215 | /// Check for operands with placeholder types and complain if found. |
| 9216 | /// Returns true if there was an error and no recovery was possible. |
| 9217 | ExprResult Sema::CheckPlaceholderExpr(Expr *E, SourceLocation Loc) { |
| 9218 | const BuiltinType *BT = E->getType()->getAs<BuiltinType>(); |
| 9219 | if (!BT || !BT->isPlaceholderType()) return Owned(E); |
| 9220 | |
| 9221 | // If this is overload, check for a single overload. |
| 9222 | if (BT->getKind() == BuiltinType::Overload) { |
| 9223 | if (FunctionDecl *Specialization |
| 9224 | = ResolveSingleFunctionTemplateSpecialization(E)) { |
| 9225 | // The access doesn't really matter in this case. |
| 9226 | DeclAccessPair Found = DeclAccessPair::make(Specialization, |
| 9227 | Specialization->getAccess()); |
| 9228 | E = FixOverloadedFunctionReference(E, Found, Specialization); |
| 9229 | if (!E) return ExprError(); |
| 9230 | return Owned(E); |
| 9231 | } |
| 9232 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9233 | Diag(Loc, diag::err_ovl_unresolvable) << E->getSourceRange(); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9234 | return ExprError(); |
| 9235 | } |
| 9236 | |
| 9237 | // Otherwise it's a use of undeduced auto. |
| 9238 | assert(BT->getKind() == BuiltinType::UndeducedAuto); |
| 9239 | |
| 9240 | DeclRefExpr *DRE = cast<DeclRefExpr>(E->IgnoreParens()); |
| 9241 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 9242 | << DRE->getDecl() << E->getSourceRange(); |
| 9243 | return ExprError(); |
| 9244 | } |