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 | /// |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 52 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 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 | /// |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 58 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 59 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 60 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 61 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 64 | // See if the decl is unavailable |
| 65 | if (D->getAttr<UnavailableAttr>()) { |
Ted Kremenek | 1ddd6d2 | 2010-07-21 20:43:11 +0000 | [diff] [blame] | 66 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 67 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 68 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 69 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 70 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 71 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 72 | if (FD->isDeleted()) { |
| 73 | Diag(Loc, diag::err_deleted_function_use); |
| 74 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 75 | return true; |
| 76 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 77 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 79 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 82 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 84 | /// attribute. It warns if call does not have the sentinel argument. |
| 85 | /// |
| 86 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 88 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 89 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 90 | return; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 91 | |
| 92 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 93 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 94 | int sentinelPos = attr->getSentinel(); |
| 95 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 97 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 98 | // 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] | 99 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 100 | bool warnNotEnoughArgs = false; |
| 101 | int isMethod = 0; |
| 102 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 103 | // skip over named parameters. |
| 104 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 105 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 106 | if (nullPos) |
| 107 | --nullPos; |
| 108 | else |
| 109 | ++i; |
| 110 | } |
| 111 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 112 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 113 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 114 | // skip over named parameters. |
| 115 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 116 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 117 | if (nullPos) |
| 118 | --nullPos; |
| 119 | else |
| 120 | ++i; |
| 121 | } |
| 122 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 123 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 124 | // block or function pointer call. |
| 125 | QualType Ty = V->getType(); |
| 126 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 128 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 129 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 130 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 131 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 132 | unsigned k; |
| 133 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 134 | if (nullPos) |
| 135 | --nullPos; |
| 136 | else |
| 137 | ++i; |
| 138 | } |
| 139 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 140 | } |
| 141 | if (Ty->isBlockPointerType()) |
| 142 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 143 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 144 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 145 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 146 | return; |
| 147 | |
| 148 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 149 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 150 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 151 | return; |
| 152 | } |
| 153 | int sentinel = i; |
| 154 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 155 | --sentinelPos; |
| 156 | ++i; |
| 157 | } |
| 158 | if (sentinelPos > 0) { |
| 159 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 160 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | while (i < NumArgs-1) { |
| 164 | ++i; |
| 165 | ++sentinel; |
| 166 | } |
| 167 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 168 | if (!sentinelExpr) return; |
| 169 | if (sentinelExpr->isTypeDependent()) return; |
| 170 | if (sentinelExpr->isValueDependent()) return; |
Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 171 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 172 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 173 | Expr::NPC_ValueDependentIsNull)) |
| 174 | return; |
| 175 | |
| 176 | // Unfortunately, __null has type 'int'. |
| 177 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 178 | |
| 179 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 180 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 183 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 184 | Expr *Ex = (Expr *)E; |
| 185 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 186 | } |
| 187 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // Standard Promotions and Conversions |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 192 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 193 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 194 | QualType Ty = E->getType(); |
| 195 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 196 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 197 | if (Ty->isFunctionType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | ImpCastExprToType(E, Context.getPointerType(Ty), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 199 | CK_FunctionToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 200 | else if (Ty->isArrayType()) { |
| 201 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 202 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 203 | // type 'array of type' is converted to an expression that has type 'pointer |
| 204 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 205 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 206 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 207 | // |
| 208 | // C++ 4.2p1: |
| 209 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 210 | // T" can be converted to an rvalue of type "pointer to T". |
| 211 | // |
| 212 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 213 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 8fc489d | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 214 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 215 | CK_ArrayToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 219 | void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) { |
| 220 | DefaultFunctionArrayConversion(E); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 222 | QualType Ty = E->getType(); |
| 223 | assert(!Ty.isNull() && "DefaultFunctionArrayLvalueConversion - missing type"); |
| 224 | if (!Ty->isDependentType() && Ty.hasQualifiers() && |
| 225 | (!getLangOptions().CPlusPlus || !Ty->isRecordType()) && |
| 226 | E->isLvalue(Context) == Expr::LV_Valid) { |
| 227 | // C++ [conv.lval]p1: |
| 228 | // [...] If T is a non-class type, the type of the rvalue is the |
| 229 | // cv-unqualified version of T. Otherwise, the type of the |
| 230 | // rvalue is T |
| 231 | // |
| 232 | // C99 6.3.2.1p2: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 233 | // If the lvalue has qualified type, the value has the unqualified |
| 234 | // version of the type of the lvalue; otherwise, the value has the |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 235 | // type of the lvalue. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 236 | ImpCastExprToType(E, Ty.getUnqualifiedType(), CK_NoOp); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
| 240 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 241 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 243 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 244 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 245 | /// In these instances, this routine should *not* be called. |
| 246 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 247 | QualType Ty = Expr->getType(); |
| 248 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 249 | |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 250 | // C99 6.3.1.1p2: |
| 251 | // |
| 252 | // The following may be used in an expression wherever an int or |
| 253 | // unsigned int may be used: |
| 254 | // - an object or expression with an integer type whose integer |
| 255 | // conversion rank is less than or equal to the rank of int |
| 256 | // and unsigned int. |
| 257 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 258 | // |
| 259 | // If an int can represent all values of the original type, the |
| 260 | // value is converted to an int; otherwise, it is converted to an |
| 261 | // unsigned int. These are called the integer promotions. All |
| 262 | // other types are unchanged by the integer promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 263 | QualType PTy = Context.isPromotableBitField(Expr); |
| 264 | if (!PTy.isNull()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 265 | ImpCastExprToType(Expr, PTy, CK_IntegralCast); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 266 | return Expr; |
| 267 | } |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 268 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 269 | QualType PT = Context.getPromotedIntegerType(Ty); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 270 | ImpCastExprToType(Expr, PT, CK_IntegralCast); |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 271 | return Expr; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 274 | DefaultFunctionArrayLvalueConversion(Expr); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 275 | return Expr; |
| 276 | } |
| 277 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 278 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | /// 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] | 280 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 281 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 282 | QualType Ty = Expr->getType(); |
| 283 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 285 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 286 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
| 287 | return ImpCastExprToType(Expr, Context.DoubleTy, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 288 | CK_FloatingCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 290 | UsualUnaryConversions(Expr); |
| 291 | } |
| 292 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 293 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 294 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 295 | /// interfaces passed by value. This returns true if the argument type is |
| 296 | /// completely illegal. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 297 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT, |
| 298 | FunctionDecl *FDecl) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 299 | DefaultArgumentPromotion(Expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 301 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 302 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 303 | // etc. |
| 304 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
| 305 | return false; |
| 306 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 307 | if (Expr->getType()->isObjCObjectType() && |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 308 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 309 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 310 | << Expr->getType() << CT)) |
| 311 | return true; |
Douglas Gregor | 7ca84af | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 313 | if (!Expr->getType()->isPODType() && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 314 | DiagRuntimeBehavior(Expr->getLocStart(), |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 315 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 316 | << Expr->getType() << CT)) |
| 317 | return true; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 318 | |
| 319 | return false; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 323 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 324 | /// 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] | 325 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 326 | /// responsible for emitting appropriate error diagnostics. |
| 327 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 328 | /// GCC. |
| 329 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 330 | bool isCompAssign) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 331 | if (!isCompAssign) |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 332 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 333 | |
| 334 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 335 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 337 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 338 | QualType lhs = |
| 339 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | QualType rhs = |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 341 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 342 | |
| 343 | // If both types are identical, no conversion is needed. |
| 344 | if (lhs == rhs) |
| 345 | return lhs; |
| 346 | |
| 347 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 348 | // The caller can deal with this (e.g. pointer + int). |
| 349 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 350 | return lhs; |
| 351 | |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 352 | // Perform bitfield promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 353 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 354 | if (!LHSBitfieldPromoteTy.isNull()) |
| 355 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 356 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 357 | if (!RHSBitfieldPromoteTy.isNull()) |
| 358 | rhs = RHSBitfieldPromoteTy; |
| 359 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 360 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 361 | if (!isCompAssign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 362 | ImpCastExprToType(lhsExpr, destType, CK_Unknown); |
| 363 | ImpCastExprToType(rhsExpr, destType, CK_Unknown); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 364 | return destType; |
| 365 | } |
| 366 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 367 | //===----------------------------------------------------------------------===// |
| 368 | // Semantic Analysis for various Expression Types |
| 369 | //===----------------------------------------------------------------------===// |
| 370 | |
| 371 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 372 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 373 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 374 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 375 | /// multiple tokens. However, the common case is that StringToks points to one |
| 376 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 377 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 378 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 379 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 380 | assert(NumStringToks && "Must have at least one string!"); |
| 381 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 382 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 383 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 384 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 386 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 387 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 388 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 390 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | cbad725 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 391 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 392 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 393 | |
| 394 | // 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] | 395 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 396 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 398 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 399 | // the nul terminator character as well as the string length for pascal |
| 400 | // strings. |
| 401 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 402 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 403 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 404 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 405 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 406 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 407 | Literal.GetStringLength(), |
| 408 | Literal.AnyWide, StrTy, |
| 409 | &StringTokLocs[0], |
| 410 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 413 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 414 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 415 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 416 | /// for values inside the block or for globals). |
| 417 | /// |
Douglas Gregor | 4f13beb | 2010-03-01 20:44:28 +0000 | [diff] [blame] | 418 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockScopeInfo records |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 419 | /// up-to-date. |
| 420 | /// |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 421 | static bool ShouldSnapshotBlockValueReference(Sema &S, BlockScopeInfo *CurBlock, |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 422 | ValueDecl *VD) { |
| 423 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 424 | // we wanted to. |
| 425 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 426 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 428 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 429 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 430 | return false; |
| 431 | |
| 432 | // If this is a reference to an extern, static, or global variable, no need to |
| 433 | // snapshot it. |
| 434 | // FIXME: What about 'const' variables in C++? |
| 435 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 436 | if (!Var->hasLocalStorage()) |
| 437 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 439 | // Blocks that have these can't be constant. |
| 440 | CurBlock->hasBlockDeclRefExprs = true; |
| 441 | |
| 442 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 443 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 444 | // be defined outside all of the current blocks (in which case the blocks do |
| 445 | // all get the bit). Walk the nesting chain. |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 446 | for (unsigned I = S.FunctionScopes.size() - 1; I; --I) { |
| 447 | BlockScopeInfo *NextBlock = dyn_cast<BlockScopeInfo>(S.FunctionScopes[I]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 448 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 449 | if (!NextBlock) |
| 450 | continue; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 452 | // If we found the defining block for the variable, don't mark the block as |
| 453 | // having a reference outside it. |
| 454 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 455 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 457 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 458 | // a snapshot as well. |
| 459 | NextBlock->hasBlockDeclRefExprs = true; |
| 460 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 462 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 465 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 466 | ExprResult |
John McCall | ce54657 | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 467 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 468 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 469 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
| 470 | return BuildDeclRefExpr(D, Ty, NameInfo, SS); |
| 471 | } |
| 472 | |
| 473 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 474 | ExprResult |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 475 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, |
| 476 | const DeclarationNameInfo &NameInfo, |
| 477 | const CXXScopeSpec *SS) { |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 478 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 479 | Diag(NameInfo.getLoc(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 481 | << D->getDeclName(); |
| 482 | return ExprError(); |
| 483 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 485 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 1524333 | 2010-04-27 21:10:04 +0000 | [diff] [blame] | 486 | if (isa<NonTypeTemplateParmDecl>(VD)) { |
| 487 | // Non-type template parameters can be referenced anywhere they are |
| 488 | // visible. |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 489 | Ty = Ty.getNonLValueExprType(Context); |
Douglas Gregor | 1524333 | 2010-04-27 21:10:04 +0000 | [diff] [blame] | 490 | } else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 491 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 492 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 493 | Diag(NameInfo.getLoc(), |
| 494 | diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 495 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 497 | << D->getIdentifier(); |
| 498 | return ExprError(); |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 503 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 504 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 506 | return Owned(DeclRefExpr::Create(Context, |
| 507 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 508 | SS? SS->getRange() : SourceRange(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 509 | D, NameInfo, Ty)); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 512 | /// \brief Given a field that represents a member of an anonymous |
| 513 | /// struct/union, build the path from that field's context to the |
| 514 | /// actual member. |
| 515 | /// |
| 516 | /// Construct the sequence of field member references we'll have to |
| 517 | /// perform to get to the field in the anonymous union/struct. The |
| 518 | /// list of members is built from the field outward, so traverse it |
| 519 | /// backwards to go from an object in the current context to the field |
| 520 | /// we found. |
| 521 | /// |
| 522 | /// \returns The variable from which the field access should begin, |
| 523 | /// for an anonymous struct/union that is not a member of another |
| 524 | /// class. Otherwise, returns NULL. |
| 525 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 526 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 527 | assert(Field->getDeclContext()->isRecord() && |
| 528 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 529 | && "Field must be stored inside an anonymous struct or union"); |
| 530 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 531 | Path.push_back(Field); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 532 | VarDecl *BaseObject = 0; |
| 533 | DeclContext *Ctx = Field->getDeclContext(); |
| 534 | do { |
| 535 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
John McCall | 61925b0 | 2010-05-21 01:17:40 +0000 | [diff] [blame] | 536 | ValueDecl *AnonObject = Record->getAnonymousStructOrUnionObject(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 537 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 538 | Path.push_back(AnonField); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 539 | else { |
| 540 | BaseObject = cast<VarDecl>(AnonObject); |
| 541 | break; |
| 542 | } |
| 543 | Ctx = Ctx->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 544 | } while (Ctx->isRecord() && |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 545 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 546 | |
| 547 | return BaseObject; |
| 548 | } |
| 549 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 550 | ExprResult |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 551 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 552 | FieldDecl *Field, |
| 553 | Expr *BaseObjectExpr, |
| 554 | SourceLocation OpLoc) { |
| 555 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 557 | AnonFields); |
| 558 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 559 | // Build the expression that refers to the base object, from |
| 560 | // which we will build a sequence of member references to each |
| 561 | // of the anonymous union objects and, eventually, the field we |
| 562 | // found via name lookup. |
| 563 | bool BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 564 | Qualifiers BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 565 | if (BaseObject) { |
| 566 | // BaseObject is an anonymous struct/union variable (and is, |
| 567 | // therefore, not part of another non-anonymous record). |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 568 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 569 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 570 | SourceLocation()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 571 | BaseQuals |
| 572 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 573 | } else if (BaseObjectExpr) { |
| 574 | // The caller provided the base object expression. Determine |
| 575 | // whether its a pointer and whether it adds any qualifiers to the |
| 576 | // anonymous struct/union fields we're looking into. |
| 577 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 578 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 579 | BaseObjectIsPointer = true; |
| 580 | ObjectType = ObjectPtr->getPointeeType(); |
| 581 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 582 | BaseQuals |
| 583 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 584 | } else { |
| 585 | // We've found a member of an anonymous struct/union that is |
| 586 | // inside a non-anonymous struct/union, so in a well-formed |
| 587 | // program our base object expression is "this". |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 588 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 589 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 590 | if (!MD->isStatic()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | QualType AnonFieldType |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 592 | = Context.getTagDeclType( |
| 593 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 594 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 596 | == Context.getCanonicalType(ThisType)) || |
| 597 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 598 | // Our base object expression is "this". |
Douglas Gregor | 4b65441 | 2009-12-24 20:23:34 +0000 | [diff] [blame] | 599 | BaseObjectExpr = new (Context) CXXThisExpr(Loc, |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 600 | MD->getThisType(Context), |
| 601 | /*isImplicit=*/true); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 602 | BaseObjectIsPointer = true; |
| 603 | } |
| 604 | } else { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 605 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 606 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 607 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 608 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | if (!BaseObjectExpr) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 612 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 613 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | // Build the implicit member references to the field of the |
| 617 | // anonymous struct/union. |
| 618 | Expr *Result = BaseObjectExpr; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 619 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 620 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 621 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 622 | FI != FIEnd; ++FI) { |
| 623 | QualType MemberType = (*FI)->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 624 | Qualifiers MemberTypeQuals = |
| 625 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 626 | |
| 627 | // CVR attributes from the base are picked up by members, |
| 628 | // except that 'mutable' members don't pick up 'const'. |
| 629 | if ((*FI)->isMutable()) |
| 630 | ResultQuals.removeConst(); |
| 631 | |
| 632 | // GC attributes are never picked up by members. |
| 633 | ResultQuals.removeObjCGCAttr(); |
| 634 | |
| 635 | // TR 18037 does not allow fields to be declared with address spaces. |
| 636 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 637 | |
| 638 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 639 | if (NewQuals != MemberTypeQuals) |
| 640 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 641 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 642 | MarkDeclarationReferenced(Loc, *FI); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 643 | PerformObjectMemberConversion(Result, /*FIXME:Qualifier=*/0, *FI, *FI); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 644 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 645 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 646 | OpLoc, MemberType); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 647 | BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 648 | ResultQuals = NewQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 651 | return Owned(Result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 654 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 655 | /// possibly a list of template arguments. |
| 656 | /// |
| 657 | /// If this produces template arguments, it is permitted to call |
| 658 | /// DecomposeTemplateName. |
| 659 | /// |
| 660 | /// This actually loses a lot of source location information for |
| 661 | /// non-standard name kinds; we should consider preserving that in |
| 662 | /// some way. |
| 663 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 664 | const UnqualifiedId &Id, |
| 665 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 666 | DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 667 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 668 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 669 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 670 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 671 | |
| 672 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 673 | Id.TemplateId->getTemplateArgs(), |
| 674 | Id.TemplateId->NumArgs); |
| 675 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 676 | TemplateArgsPtr.release(); |
| 677 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 678 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 679 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 680 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 681 | TemplateArgs = &Buffer; |
| 682 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 683 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 684 | TemplateArgs = 0; |
| 685 | } |
| 686 | } |
| 687 | |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 688 | /// Determines whether the given record is "fully-formed" at the given |
| 689 | /// location, i.e. whether a qualified lookup into it is assured of |
| 690 | /// getting consistent results already. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 691 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 692 | if (!Record->hasDefinition()) |
| 693 | return false; |
| 694 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 695 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 696 | E = Record->bases_end(); I != E; ++I) { |
| 697 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 698 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 699 | if (!BaseRT) return false; |
| 700 | |
| 701 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 69f9dbc | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 702 | if (!BaseRecord->hasDefinition() || |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 703 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 704 | return false; |
| 705 | } |
| 706 | |
| 707 | return true; |
| 708 | } |
| 709 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 710 | /// Determines if the given class is provably not derived from all of |
| 711 | /// the prospective base classes. |
| 712 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 713 | CXXRecordDecl *Record, |
| 714 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 715 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 716 | return false; |
| 717 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 718 | RecordDecl *RD = Record->getDefinition(); |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 719 | if (!RD) return false; |
| 720 | Record = cast<CXXRecordDecl>(RD); |
| 721 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 722 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 723 | E = Record->bases_end(); I != E; ++I) { |
| 724 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 725 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 726 | if (!BaseRT) return false; |
| 727 | |
| 728 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 729 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 730 | return false; |
| 731 | } |
| 732 | |
| 733 | return true; |
| 734 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 735 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 736 | enum IMAKind { |
| 737 | /// The reference is definitely not an instance member access. |
| 738 | IMA_Static, |
| 739 | |
| 740 | /// The reference may be an implicit instance member access. |
| 741 | IMA_Mixed, |
| 742 | |
| 743 | /// The reference may be to an instance member, but it is invalid if |
| 744 | /// so, because the context is not an instance method. |
| 745 | IMA_Mixed_StaticContext, |
| 746 | |
| 747 | /// The reference may be to an instance member, but it is invalid if |
| 748 | /// so, because the context is from an unrelated class. |
| 749 | IMA_Mixed_Unrelated, |
| 750 | |
| 751 | /// The reference is definitely an implicit instance member access. |
| 752 | IMA_Instance, |
| 753 | |
| 754 | /// The reference may be to an unresolved using declaration. |
| 755 | IMA_Unresolved, |
| 756 | |
| 757 | /// The reference may be to an unresolved using declaration and the |
| 758 | /// context is not an instance method. |
| 759 | IMA_Unresolved_StaticContext, |
| 760 | |
| 761 | /// The reference is to a member of an anonymous structure in a |
| 762 | /// non-class context. |
| 763 | IMA_AnonymousMember, |
| 764 | |
| 765 | /// All possible referrents are instance members and the current |
| 766 | /// context is not an instance method. |
| 767 | IMA_Error_StaticContext, |
| 768 | |
| 769 | /// All possible referrents are instance members of an unrelated |
| 770 | /// class. |
| 771 | IMA_Error_Unrelated |
| 772 | }; |
| 773 | |
| 774 | /// The given lookup names class member(s) and is not being used for |
| 775 | /// an address-of-member expression. Classify the type of access |
| 776 | /// according to whether it's possible that this reference names an |
| 777 | /// instance member. This is best-effort; it is okay to |
| 778 | /// conservatively answer "yes", in which case some errors will simply |
| 779 | /// not be caught until template-instantiation. |
| 780 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 781 | const LookupResult &R) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 782 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 783 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 784 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 785 | bool isStaticContext = |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 786 | (!isa<CXXMethodDecl>(DC) || |
| 787 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 788 | |
| 789 | if (R.isUnresolvableResult()) |
| 790 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 791 | |
| 792 | // Collect all the declaring classes of instance members we find. |
| 793 | bool hasNonInstance = false; |
| 794 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 795 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 796 | NamedDecl *D = *I; |
| 797 | if (D->isCXXInstanceMember()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 798 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 799 | |
| 800 | // If this is a member of an anonymous record, move out to the |
| 801 | // innermost non-anonymous struct or union. If there isn't one, |
| 802 | // that's a special case. |
| 803 | while (R->isAnonymousStructOrUnion()) { |
| 804 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 805 | if (!R) return IMA_AnonymousMember; |
| 806 | } |
| 807 | Classes.insert(R->getCanonicalDecl()); |
| 808 | } |
| 809 | else |
| 810 | hasNonInstance = true; |
| 811 | } |
| 812 | |
| 813 | // If we didn't find any instance members, it can't be an implicit |
| 814 | // member reference. |
| 815 | if (Classes.empty()) |
| 816 | return IMA_Static; |
| 817 | |
| 818 | // If the current context is not an instance method, it can't be |
| 819 | // an implicit member reference. |
| 820 | if (isStaticContext) |
| 821 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 822 | |
| 823 | // If we can prove that the current context is unrelated to all the |
| 824 | // declaring classes, it can't be an implicit member reference (in |
| 825 | // which case it's an error if any of those members are selected). |
| 826 | if (IsProvablyNotDerivedFrom(SemaRef, |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 827 | cast<CXXMethodDecl>(DC)->getParent(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 828 | Classes)) |
| 829 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 830 | |
| 831 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 832 | } |
| 833 | |
| 834 | /// Diagnose a reference to a field with no object available. |
| 835 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 836 | const CXXScopeSpec &SS, |
| 837 | const LookupResult &R) { |
| 838 | SourceLocation Loc = R.getNameLoc(); |
| 839 | SourceRange Range(Loc); |
| 840 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 841 | |
| 842 | if (R.getAsSingle<FieldDecl>()) { |
| 843 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 844 | if (MD->isStatic()) { |
| 845 | // "invalid use of member 'x' in static member function" |
| 846 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 847 | << Range << R.getLookupName(); |
| 848 | return; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 853 | << R.getLookupName() << Range; |
| 854 | return; |
| 855 | } |
| 856 | |
| 857 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 860 | /// Diagnose an empty lookup. |
| 861 | /// |
| 862 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 863 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 864 | CorrectTypoContext CTC) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 865 | DeclarationName Name = R.getLookupName(); |
| 866 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 867 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 868 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 869 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 870 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 871 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 872 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 873 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 874 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 875 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 876 | // If the original lookup was an unqualified lookup, fake an |
| 877 | // unqualified lookup. This is useful when (for example) the |
| 878 | // original lookup would not have found something because it was a |
| 879 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 880 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 881 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 882 | if (isa<CXXRecordDecl>(DC)) { |
| 883 | LookupQualifiedName(R, DC); |
| 884 | |
| 885 | if (!R.empty()) { |
| 886 | // Don't give errors about ambiguities in this lookup. |
| 887 | R.suppressDiagnostics(); |
| 888 | |
| 889 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 890 | bool isInstance = CurMethod && |
| 891 | CurMethod->isInstance() && |
| 892 | DC == CurMethod->getParent(); |
| 893 | |
| 894 | // Give a code modification hint to insert 'this->'. |
| 895 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 896 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 897 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 898 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 899 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 900 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 901 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 902 | if (DepMethod) { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 903 | Diag(R.getNameLoc(), diagnostic) << Name |
| 904 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 905 | QualType DepThisType = DepMethod->getThisType(Context); |
| 906 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 907 | R.getNameLoc(), DepThisType, false); |
| 908 | TemplateArgumentListInfo TList; |
| 909 | if (ULE->hasExplicitTemplateArgs()) |
| 910 | ULE->copyTemplateArgumentsInto(TList); |
| 911 | CXXDependentScopeMemberExpr *DepExpr = |
| 912 | CXXDependentScopeMemberExpr::Create( |
| 913 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 914 | ULE->getQualifier(), ULE->getQualifierRange(), NULL, |
| 915 | R.getLookupNameInfo(), &TList); |
| 916 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 917 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 918 | // FIXME: we should be able to handle this case too. It is correct |
| 919 | // to add this-> here. This is a workaround for PR7947. |
| 920 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 921 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 922 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 923 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 924 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 925 | |
| 926 | // Do we really want to note all of these? |
| 927 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 928 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 929 | |
| 930 | // Tell the callee to try to recover. |
| 931 | return false; |
| 932 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 933 | |
| 934 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 938 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 939 | DeclarationName Corrected; |
Daniel Dunbar | f7ced25 | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 940 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 941 | if (!R.empty()) { |
| 942 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 943 | if (SS.isEmpty()) |
| 944 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 945 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 946 | R.getLookupName().getAsString()); |
| 947 | else |
| 948 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 949 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 950 | << SS.getRange() |
| 951 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 952 | R.getLookupName().getAsString()); |
| 953 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 954 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 955 | << ND->getDeclName(); |
| 956 | |
| 957 | // Tell the callee to try to recover. |
| 958 | return false; |
| 959 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 961 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 962 | // FIXME: If we ended up with a typo for a type name or |
| 963 | // Objective-C class name, we're in trouble because the parser |
| 964 | // is in the wrong place to recover. Suggest the typo |
| 965 | // correction, but don't make it a fix-it since we're not going |
| 966 | // to recover well anyway. |
| 967 | if (SS.isEmpty()) |
| 968 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 969 | else |
| 970 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 971 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 972 | << SS.getRange(); |
| 973 | |
| 974 | // Don't try to recover; it won't work. |
| 975 | return true; |
| 976 | } |
| 977 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 978 | // 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] | 979 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 980 | if (SS.isEmpty()) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 981 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 982 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 983 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 984 | << Name << computeDeclContext(SS, false) << Corrected |
| 985 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 986 | return true; |
| 987 | } |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 988 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | // Emit a special diagnostic for failed member lookups. |
| 992 | // FIXME: computing the declaration context might fail here (?) |
| 993 | if (!SS.isEmpty()) { |
| 994 | Diag(R.getNameLoc(), diag::err_no_member) |
| 995 | << Name << computeDeclContext(SS, false) |
| 996 | << SS.getRange(); |
| 997 | return true; |
| 998 | } |
| 999 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1000 | // Give up, we can't recover. |
| 1001 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1005 | static ObjCPropertyDecl *OkToSynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1006 | IdentifierInfo *II, |
| 1007 | SourceLocation NameLoc) { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1008 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
| 1009 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1010 | if (!IDecl) |
| 1011 | return 0; |
| 1012 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1013 | if (!ClassImpDecl) |
| 1014 | return 0; |
| 1015 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1016 | if (!property) |
| 1017 | return 0; |
| 1018 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
| 1019 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) |
| 1020 | return 0; |
| 1021 | return property; |
| 1022 | } |
| 1023 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1024 | static ObjCIvarDecl *SynthesizeProvisionalIvar(Sema &SemaRef, |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1025 | LookupResult &Lookup, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1026 | IdentifierInfo *II, |
| 1027 | SourceLocation NameLoc) { |
| 1028 | ObjCMethodDecl *CurMeth = SemaRef.getCurMethodDecl(); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1029 | bool LookForIvars; |
| 1030 | if (Lookup.empty()) |
| 1031 | LookForIvars = true; |
| 1032 | else if (CurMeth->isClassMethod()) |
| 1033 | LookForIvars = false; |
| 1034 | else |
| 1035 | LookForIvars = (Lookup.isSingleResult() && |
| 1036 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 1037 | if (!LookForIvars) |
| 1038 | return 0; |
| 1039 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1040 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1041 | if (!IDecl) |
| 1042 | return 0; |
| 1043 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1044 | if (!ClassImpDecl) |
| 1045 | return 0; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1046 | bool DynamicImplSeen = false; |
| 1047 | ObjCPropertyDecl *property = SemaRef.LookupPropertyDecl(IDecl, II); |
| 1048 | if (!property) |
| 1049 | return 0; |
| 1050 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
| 1051 | DynamicImplSeen = |
| 1052 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| 1053 | if (!DynamicImplSeen) { |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1054 | QualType PropType = SemaRef.Context.getCanonicalType(property->getType()); |
| 1055 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(SemaRef.Context, ClassImpDecl, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1056 | NameLoc, |
| 1057 | II, PropType, /*Dinfo=*/0, |
| 1058 | ObjCIvarDecl::Protected, |
| 1059 | (Expr *)0, true); |
| 1060 | ClassImpDecl->addDecl(Ivar); |
| 1061 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1062 | property->setPropertyIvarDecl(Ivar); |
| 1063 | return Ivar; |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1068 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1069 | CXXScopeSpec &SS, |
| 1070 | UnqualifiedId &Id, |
| 1071 | bool HasTrailingLParen, |
| 1072 | bool isAddressOfOperand) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1073 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1074 | "cannot be direct & operand and have a trailing lparen"); |
| 1075 | |
| 1076 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1077 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1078 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1079 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1080 | |
| 1081 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1082 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1083 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1084 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1085 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1086 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1087 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1088 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1089 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1090 | // C++ [temp.dep.expr]p3: |
| 1091 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1092 | // -- an identifier that was declared with a dependent type, |
| 1093 | // (note: handled after lookup) |
| 1094 | // -- a template-id that is dependent, |
| 1095 | // (note: handled in BuildTemplateIdExpr) |
| 1096 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1097 | // -- a nested-name-specifier that contains a class-name that |
| 1098 | // names a dependent type. |
| 1099 | // Determine whether this is a member of an unknown specialization; |
| 1100 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1101 | bool DependentID = false; |
| 1102 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1103 | Name.getCXXNameType()->isDependentType()) { |
| 1104 | DependentID = true; |
| 1105 | } else if (SS.isSet()) { |
| 1106 | DeclContext *DC = computeDeclContext(SS, false); |
| 1107 | if (DC) { |
| 1108 | if (RequireCompleteDeclContext(SS, DC)) |
| 1109 | return ExprError(); |
| 1110 | // FIXME: We should be checking whether DC is the current instantiation. |
| 1111 | if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(DC)) |
| 1112 | DependentID = !IsFullyFormedScope(*this, RD); |
| 1113 | } else { |
| 1114 | DependentID = true; |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | if (DependentID) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1119 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1120 | TemplateArgs); |
| 1121 | } |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1122 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1123 | // Perform the required lookup. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1124 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1125 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1126 | // Lookup the template name again to correctly establish the context in |
| 1127 | // which it was found. This is really unfortunate as we already did the |
| 1128 | // lookup to determine that it was a template name in the first place. If |
| 1129 | // this becomes a performance hit, we can work harder to preserve those |
| 1130 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1131 | bool MemberOfUnknownSpecialization; |
| 1132 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1133 | MemberOfUnknownSpecialization); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1134 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1135 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1136 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1138 | // If this reference is in an Objective-C method, then we need to do |
| 1139 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1140 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1141 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1142 | if (E.isInvalid()) |
| 1143 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1145 | Expr *Ex = E.takeAs<Expr>(); |
| 1146 | if (Ex) return Owned(Ex); |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1147 | // Synthesize ivars lazily |
| 1148 | if (getLangOptions().ObjCNonFragileABI2) { |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1149 | if (SynthesizeProvisionalIvar(*this, R, II, NameLoc)) |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1150 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1151 | isAddressOfOperand); |
| 1152 | } |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1153 | // for further use, this must be set to false if in class method. |
| 1154 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1155 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1156 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1157 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1158 | if (R.isAmbiguous()) |
| 1159 | return ExprError(); |
| 1160 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1161 | // Determine whether this name might be a candidate for |
| 1162 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1163 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1164 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1165 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1166 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1167 | // in C90, extension in C99, forbidden in C++). |
| 1168 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1169 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1170 | if (D) R.addDecl(D); |
| 1171 | } |
| 1172 | |
| 1173 | // If this name wasn't predeclared and if this is not a function |
| 1174 | // call, diagnose the problem. |
| 1175 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1176 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1177 | return ExprError(); |
| 1178 | |
| 1179 | assert(!R.empty() && |
| 1180 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1181 | |
| 1182 | // If we found an Objective-C instance variable, let |
| 1183 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1184 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1185 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1186 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1187 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1188 | assert(E.isInvalid() || E.get()); |
| 1189 | return move(E); |
| 1190 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1191 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1192 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1193 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1194 | // This is guaranteed from this point on. |
| 1195 | assert(!R.empty() || ADL); |
| 1196 | |
| 1197 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1198 | if (getLangOptions().ObjCNonFragileABI && IvarLookupFollowUp && |
Fariborz Jahanian | c15dfd8 | 2010-07-29 16:53:53 +0000 | [diff] [blame] | 1199 | !getLangOptions().ObjCNonFragileABI2 && |
| 1200 | Var->isFileVarDecl()) { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1201 | ObjCPropertyDecl *Property = |
| 1202 | OkToSynthesizeProvisionalIvar(*this, II, NameLoc); |
| 1203 | if (Property) { |
| 1204 | Diag(NameLoc, diag::warn_ivar_variable_conflict) << Var->getDeclName(); |
| 1205 | Diag(Property->getLocation(), diag::note_property_declare); |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1206 | Diag(Var->getLocation(), diag::note_global_declared_at); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1207 | } |
| 1208 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1209 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1210 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1211 | // C99 DR 316 says that, if a function type comes from a |
| 1212 | // function definition (without a prototype), that type is only |
| 1213 | // used for checking compatibility. Therefore, when referencing |
| 1214 | // the function, we pretend that we don't have the full function |
| 1215 | // type. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1216 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1217 | return ExprError(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1218 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1219 | QualType T = Func->getType(); |
| 1220 | QualType NoProtoType = T; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1221 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Eli Friedman | b41ad0f | 2010-05-17 02:50:18 +0000 | [diff] [blame] | 1222 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType(), |
| 1223 | Proto->getExtInfo()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1224 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1225 | } |
| 1226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1228 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1229 | // C++ [class.mfct.non-static]p3: |
| 1230 | // When an id-expression that is not part of a class member access |
| 1231 | // syntax and not used to form a pointer to member is used in the |
| 1232 | // body of a non-static member function of class X, if name lookup |
| 1233 | // resolves the name in the id-expression to a non-static non-type |
| 1234 | // member of some class C, the id-expression is transformed into a |
| 1235 | // class member access expression using (*this) as the |
| 1236 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1237 | // |
| 1238 | // But we don't actually need to do this for '&' operands if R |
| 1239 | // resolved to a function or overloaded function set, because the |
| 1240 | // expression is ill-formed if it actually works out to be a |
| 1241 | // non-static member function: |
| 1242 | // |
| 1243 | // C++ [expr.ref]p4: |
| 1244 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1245 | // [t]he expression can be used only as the left-hand operand of a |
| 1246 | // member function call. |
| 1247 | // |
| 1248 | // There are other safeguards against such uses, but it's important |
| 1249 | // to get this right here so that we don't end up making a |
| 1250 | // spuriously dependent expression if we're inside a dependent |
| 1251 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1252 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1253 | bool MightBeImplicitMember; |
| 1254 | if (!isAddressOfOperand) |
| 1255 | MightBeImplicitMember = true; |
| 1256 | else if (!SS.isEmpty()) |
| 1257 | MightBeImplicitMember = false; |
| 1258 | else if (R.isOverloadedResult()) |
| 1259 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1260 | else if (R.isUnresolvableResult()) |
| 1261 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1262 | else |
| 1263 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()); |
| 1264 | |
| 1265 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1266 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1269 | if (TemplateArgs) |
| 1270 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1271 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1272 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1273 | } |
| 1274 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1275 | /// Builds an expression which might be an implicit member expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1276 | ExprResult |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1277 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1278 | LookupResult &R, |
| 1279 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1280 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1281 | case IMA_Instance: |
| 1282 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1283 | |
| 1284 | case IMA_AnonymousMember: |
| 1285 | assert(R.isSingleResult()); |
| 1286 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1287 | R.getAsSingle<FieldDecl>()); |
| 1288 | |
| 1289 | case IMA_Mixed: |
| 1290 | case IMA_Mixed_Unrelated: |
| 1291 | case IMA_Unresolved: |
| 1292 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1293 | |
| 1294 | case IMA_Static: |
| 1295 | case IMA_Mixed_StaticContext: |
| 1296 | case IMA_Unresolved_StaticContext: |
| 1297 | if (TemplateArgs) |
| 1298 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1299 | return BuildDeclarationNameExpr(SS, R, false); |
| 1300 | |
| 1301 | case IMA_Error_StaticContext: |
| 1302 | case IMA_Error_Unrelated: |
| 1303 | DiagnoseInstanceReference(*this, SS, R); |
| 1304 | return ExprError(); |
| 1305 | } |
| 1306 | |
| 1307 | llvm_unreachable("unexpected instance member access kind"); |
| 1308 | return ExprError(); |
| 1309 | } |
| 1310 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1311 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1312 | /// declaration name, generally during template instantiation. |
| 1313 | /// There's a large number of things which don't need to be done along |
| 1314 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1315 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1316 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1317 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1318 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1319 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1320 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1321 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1322 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1323 | return ExprError(); |
| 1324 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1325 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1326 | LookupQualifiedName(R, DC); |
| 1327 | |
| 1328 | if (R.isAmbiguous()) |
| 1329 | return ExprError(); |
| 1330 | |
| 1331 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1332 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1333 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1334 | return ExprError(); |
| 1335 | } |
| 1336 | |
| 1337 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1338 | } |
| 1339 | |
| 1340 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1341 | /// detected that we're currently inside an ObjC method. Perform some |
| 1342 | /// additional lookup. |
| 1343 | /// |
| 1344 | /// Ideally, most of this would be done by lookup, but there's |
| 1345 | /// actually quite a lot of extra work involved. |
| 1346 | /// |
| 1347 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1348 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1349 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1350 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1351 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1352 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1353 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1354 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1355 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1356 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1357 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1358 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1359 | |
| 1360 | // If we're in a class method, we don't normally want to look for |
| 1361 | // ivars. But if we don't find anything else, and there's an |
| 1362 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1363 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1364 | |
| 1365 | bool LookForIvars; |
| 1366 | if (Lookup.empty()) |
| 1367 | LookForIvars = true; |
| 1368 | else if (IsClassMethod) |
| 1369 | LookForIvars = false; |
| 1370 | else |
| 1371 | LookForIvars = (Lookup.isSingleResult() && |
| 1372 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1373 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1374 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1375 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1376 | ObjCInterfaceDecl *ClassDeclared; |
| 1377 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1378 | // Diagnose using an ivar in a class method. |
| 1379 | if (IsClassMethod) |
| 1380 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1381 | << IV->getDeclName()); |
| 1382 | |
| 1383 | // If we're referencing an invalid decl, just return this as a silent |
| 1384 | // error node. The error diagnostic was already emitted on the decl. |
| 1385 | if (IV->isInvalidDecl()) |
| 1386 | return ExprError(); |
| 1387 | |
| 1388 | // Check if referencing a field with __attribute__((deprecated)). |
| 1389 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1390 | return ExprError(); |
| 1391 | |
| 1392 | // Diagnose the use of an ivar outside of the declaring class. |
| 1393 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1394 | ClassDeclared != IFace) |
| 1395 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1396 | |
| 1397 | // FIXME: This should use a new expr for a direct reference, don't |
| 1398 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1399 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1400 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1401 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1402 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1403 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1404 | SelfName, false, false); |
| 1405 | MarkDeclarationReferenced(Loc, IV); |
| 1406 | return Owned(new (Context) |
| 1407 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1408 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1409 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1410 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1411 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1412 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1413 | ObjCInterfaceDecl *ClassDeclared; |
| 1414 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1415 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1416 | IFace == ClassDeclared) |
| 1417 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1418 | } |
| 1419 | } |
| 1420 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1421 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1422 | // FIXME. Consolidate this with similar code in LookupName. |
| 1423 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1424 | if (!(getLangOptions().CPlusPlus && |
| 1425 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1426 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1427 | S, Lookup.isForRedeclaration(), |
| 1428 | Lookup.getNameLoc()); |
| 1429 | if (D) Lookup.addDecl(D); |
| 1430 | } |
| 1431 | } |
| 1432 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1433 | // Sentinel value saying that we didn't do anything special. |
| 1434 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1435 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1436 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1437 | /// \brief Cast a base object to a member's actual type. |
| 1438 | /// |
| 1439 | /// Logically this happens in three phases: |
| 1440 | /// |
| 1441 | /// * First we cast from the base type to the naming class. |
| 1442 | /// The naming class is the class into which we were looking |
| 1443 | /// when we found the member; it's the qualifier type if a |
| 1444 | /// qualifier was provided, and otherwise it's the base type. |
| 1445 | /// |
| 1446 | /// * Next we cast from the naming class to the declaring class. |
| 1447 | /// If the member we found was brought into a class's scope by |
| 1448 | /// a using declaration, this is that class; otherwise it's |
| 1449 | /// the class declaring the member. |
| 1450 | /// |
| 1451 | /// * Finally we cast from the declaring class to the "true" |
| 1452 | /// declaring class of the member. This conversion does not |
| 1453 | /// obey access control. |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1454 | bool |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1455 | Sema::PerformObjectMemberConversion(Expr *&From, |
| 1456 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1457 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1458 | NamedDecl *Member) { |
| 1459 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1460 | if (!RD) |
| 1461 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1463 | QualType DestRecordType; |
| 1464 | QualType DestType; |
| 1465 | QualType FromRecordType; |
| 1466 | QualType FromType = From->getType(); |
| 1467 | bool PointerConversions = false; |
| 1468 | if (isa<FieldDecl>(Member)) { |
| 1469 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1471 | if (FromType->getAs<PointerType>()) { |
| 1472 | DestType = Context.getPointerType(DestRecordType); |
| 1473 | FromRecordType = FromType->getPointeeType(); |
| 1474 | PointerConversions = true; |
| 1475 | } else { |
| 1476 | DestType = DestRecordType; |
| 1477 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1478 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1479 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1480 | if (Method->isStatic()) |
| 1481 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1482 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1483 | DestType = Method->getThisType(Context); |
| 1484 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1485 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1486 | if (FromType->getAs<PointerType>()) { |
| 1487 | FromRecordType = FromType->getPointeeType(); |
| 1488 | PointerConversions = true; |
| 1489 | } else { |
| 1490 | FromRecordType = FromType; |
| 1491 | DestType = DestRecordType; |
| 1492 | } |
| 1493 | } else { |
| 1494 | // No conversion necessary. |
| 1495 | return false; |
| 1496 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1498 | if (DestType->isDependentType() || FromType->isDependentType()) |
| 1499 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1501 | // If the unqualified types are the same, no conversion is necessary. |
| 1502 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1503 | return false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1504 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1505 | SourceRange FromRange = From->getSourceRange(); |
| 1506 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1507 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1508 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1509 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1510 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1511 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1512 | // class name. |
| 1513 | // |
| 1514 | // If the member was a qualified name and the qualified referred to a |
| 1515 | // specific base subobject type, we'll cast to that intermediate type |
| 1516 | // first and then to the object in which the member is declared. That allows |
| 1517 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1518 | // |
| 1519 | // class Base { public: int x; }; |
| 1520 | // class Derived1 : public Base { }; |
| 1521 | // class Derived2 : public Base { }; |
| 1522 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1523 | // |
| 1524 | // void VeryDerived::f() { |
| 1525 | // x = 17; // error: ambiguous base subobjects |
| 1526 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 1527 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1528 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1529 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 1530 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 1531 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 1532 | |
| 1533 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 1534 | |
| 1535 | // In C++98, the qualifier type doesn't actually have to be a base |
| 1536 | // type of the object type, in which case we just ignore it. |
| 1537 | // Otherwise build the appropriate casts. |
| 1538 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1539 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1540 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1541 | FromLoc, FromRange, &BasePath)) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1542 | return true; |
| 1543 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1544 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1545 | QType = Context.getPointerType(QType); |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1546 | ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 1547 | VK, &BasePath); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1548 | |
| 1549 | FromType = QType; |
| 1550 | FromRecordType = QRecordType; |
| 1551 | |
| 1552 | // If the qualifier type was the same as the destination type, |
| 1553 | // we're done. |
| 1554 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| 1555 | return false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1558 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1559 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1560 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1561 | // If we actually found the member through a using declaration, cast |
| 1562 | // down to the using declaration's type. |
| 1563 | // |
| 1564 | // Pointer equality is fine here because only one declaration of a |
| 1565 | // class ever has member declarations. |
| 1566 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 1567 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 1568 | QualType URecordType = Context.getTypeDeclType( |
| 1569 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 1570 | |
| 1571 | // We only need to do this if the naming-class to declaring-class |
| 1572 | // conversion is non-trivial. |
| 1573 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 1574 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1575 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1576 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1577 | FromLoc, FromRange, &BasePath)) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1578 | return true; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1579 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1580 | QualType UType = URecordType; |
| 1581 | if (PointerConversions) |
| 1582 | UType = Context.getPointerType(UType); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1583 | ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1584 | VK, &BasePath); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1585 | FromType = UType; |
| 1586 | FromRecordType = URecordType; |
| 1587 | } |
| 1588 | |
| 1589 | // We don't do access control for the conversion from the |
| 1590 | // declaring class to the true declaring class. |
| 1591 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1592 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1593 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1594 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 1595 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 1596 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1597 | IgnoreAccess)) |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1598 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1599 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1600 | ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1601 | VK, &BasePath); |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1602 | return false; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1603 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1605 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | 2cfcef6 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1607 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1608 | DeclAccessPair FoundDecl, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1609 | const DeclarationNameInfo &MemberNameInfo, |
| 1610 | QualType Ty, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1611 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1612 | NestedNameSpecifier *Qualifier = 0; |
| 1613 | SourceRange QualifierRange; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1614 | if (SS.isSet()) { |
| 1615 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1616 | QualifierRange = SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1617 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1619 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1620 | Member, FoundDecl, MemberNameInfo, |
| 1621 | TemplateArgs, Ty); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1624 | /// Builds an implicit member access expression. The current context |
| 1625 | /// is known to be an instance method, and the given unqualified lookup |
| 1626 | /// set is known to contain only instance members, at least one of which |
| 1627 | /// is from an appropriate type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1628 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1629 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1630 | LookupResult &R, |
| 1631 | const TemplateArgumentListInfo *TemplateArgs, |
| 1632 | bool IsKnownInstance) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1633 | assert(!R.empty() && !R.isAmbiguous()); |
| 1634 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1635 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1637 | // We may have found a field within an anonymous union or struct |
| 1638 | // (C++ [class.union]). |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1639 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1640 | // FIXME: template-ids inside anonymous structs? |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1641 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1642 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1643 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1644 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1645 | // If this is known to be an instance access, go ahead and build a |
| 1646 | // 'this' expression now. |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1647 | DeclContext *DC = getFunctionLevelDeclContext(); |
| 1648 | QualType ThisType = cast<CXXMethodDecl>(DC)->getThisType(Context); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1649 | Expr *This = 0; // null signifies implicit access |
| 1650 | if (IsKnownInstance) { |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 1651 | SourceLocation Loc = R.getNameLoc(); |
| 1652 | if (SS.getRange().isValid()) |
| 1653 | Loc = SS.getRange().getBegin(); |
| 1654 | This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1657 | return BuildMemberReferenceExpr(This, ThisType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1658 | /*OpLoc*/ SourceLocation(), |
| 1659 | /*IsArrow*/ true, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 1660 | SS, |
| 1661 | /*FirstQualifierInScope*/ 0, |
| 1662 | R, TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1665 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1666 | const LookupResult &R, |
| 1667 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1668 | // Only when used directly as the postfix-expression of a call. |
| 1669 | if (!HasTrailingLParen) |
| 1670 | return false; |
| 1671 | |
| 1672 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1673 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1674 | return false; |
| 1675 | |
| 1676 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1677 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1678 | return false; |
| 1679 | |
| 1680 | // Turn off ADL when we find certain kinds of declarations during |
| 1681 | // normal lookup: |
| 1682 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1683 | NamedDecl *D = *I; |
| 1684 | |
| 1685 | // C++0x [basic.lookup.argdep]p3: |
| 1686 | // -- a declaration of a class member |
| 1687 | // Since using decls preserve this property, we check this on the |
| 1688 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1689 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1690 | return false; |
| 1691 | |
| 1692 | // C++0x [basic.lookup.argdep]p3: |
| 1693 | // -- a block-scope function declaration that is not a |
| 1694 | // using-declaration |
| 1695 | // NOTE: we also trigger this for function templates (in fact, we |
| 1696 | // don't check the decl type at all, since all other decl types |
| 1697 | // turn off ADL anyway). |
| 1698 | if (isa<UsingShadowDecl>(D)) |
| 1699 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1700 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1701 | return false; |
| 1702 | |
| 1703 | // C++0x [basic.lookup.argdep]p3: |
| 1704 | // -- a declaration that is neither a function or a function |
| 1705 | // template |
| 1706 | // And also for builtin functions. |
| 1707 | if (isa<FunctionDecl>(D)) { |
| 1708 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1709 | |
| 1710 | // But also builtin functions. |
| 1711 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1712 | return false; |
| 1713 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1714 | return false; |
| 1715 | } |
| 1716 | |
| 1717 | return true; |
| 1718 | } |
| 1719 | |
| 1720 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1721 | /// Diagnoses obvious problems with the use of the given declaration |
| 1722 | /// as an expression. This is only actually called for lookups that |
| 1723 | /// were not overloaded, and it doesn't promise that the declaration |
| 1724 | /// will in fact be used. |
| 1725 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1726 | if (isa<TypedefDecl>(D)) { |
| 1727 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1728 | return true; |
| 1729 | } |
| 1730 | |
| 1731 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1732 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1733 | return true; |
| 1734 | } |
| 1735 | |
| 1736 | if (isa<NamespaceDecl>(D)) { |
| 1737 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1738 | return true; |
| 1739 | } |
| 1740 | |
| 1741 | return false; |
| 1742 | } |
| 1743 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1744 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1745 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1746 | LookupResult &R, |
| 1747 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1748 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1749 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 1750 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1751 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 1752 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1753 | |
| 1754 | // We only need to check the declaration if there's exactly one |
| 1755 | // result, because in the overloaded case the results can only be |
| 1756 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1757 | if (R.isSingleResult() && |
| 1758 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1759 | return ExprError(); |
| 1760 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1761 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 1762 | // any lookup-related diagnostics; we'll hash these out later, when |
| 1763 | // we've picked a target. |
| 1764 | R.suppressDiagnostics(); |
| 1765 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1766 | bool Dependent |
| 1767 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1768 | UnresolvedLookupExpr *ULE |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1769 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1770 | (NestedNameSpecifier*) SS.getScopeRep(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1771 | SS.getRange(), R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 1772 | NeedsADL, R.isOverloadedResult(), |
| 1773 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1774 | |
| 1775 | return Owned(ULE); |
| 1776 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1777 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1778 | |
| 1779 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1780 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1781 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1782 | const DeclarationNameInfo &NameInfo, |
| 1783 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1784 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1785 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1786 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1787 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1788 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1789 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1790 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1792 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1793 | // Specifically diagnose references to class templates that are missing |
| 1794 | // a template argument list. |
| 1795 | Diag(Loc, diag::err_template_decl_ref) |
| 1796 | << Template << SS.getRange(); |
| 1797 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1798 | return ExprError(); |
| 1799 | } |
| 1800 | |
| 1801 | // Make sure that we're referring to a value. |
| 1802 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1803 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1804 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1805 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 1806 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1807 | return ExprError(); |
| 1808 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1810 | // Check whether this declaration can be used. Note that we suppress |
| 1811 | // this check when we're going to perform argument-dependent lookup |
| 1812 | // on this function name, because this might not be the function |
| 1813 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1814 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1815 | return ExprError(); |
| 1816 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1817 | // Only create DeclRefExpr's for valid Decl's. |
| 1818 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1819 | return ExprError(); |
| 1820 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1821 | // If the identifier reference is inside a block, and it refers to a value |
| 1822 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1823 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1824 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1825 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1826 | // We do not do this for things like enum constants, global variables, etc, |
| 1827 | // as they do not get snapshotted. |
| 1828 | // |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1829 | if (getCurBlock() && |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 1830 | ShouldSnapshotBlockValueReference(*this, getCurBlock(), VD)) { |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 1831 | if (VD->getType().getTypePtr()->isVariablyModifiedType()) { |
| 1832 | Diag(Loc, diag::err_ref_vm_type); |
| 1833 | Diag(D->getLocation(), diag::note_declared_at); |
| 1834 | return ExprError(); |
| 1835 | } |
| 1836 | |
Fariborz Jahanian | fa24e10 | 2010-03-16 23:39:51 +0000 | [diff] [blame] | 1837 | if (VD->getType()->isArrayType()) { |
Mike Stump | 8971a86 | 2010-01-05 03:10:36 +0000 | [diff] [blame] | 1838 | Diag(Loc, diag::err_ref_array_type); |
| 1839 | Diag(D->getLocation(), diag::note_declared_at); |
| 1840 | return ExprError(); |
| 1841 | } |
| 1842 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1843 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1844 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1845 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1846 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1847 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1848 | // This is to record that a 'const' was actually synthesize and added. |
| 1849 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1850 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1851 | |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1852 | ExprTy.addConst(); |
Fariborz Jahanian | 70c0b08 | 2010-07-12 17:26:57 +0000 | [diff] [blame] | 1853 | QualType T = VD->getType(); |
Fariborz Jahanian | ea882cd | 2010-06-04 21:35:44 +0000 | [diff] [blame] | 1854 | BlockDeclRefExpr *BDRE = new (Context) BlockDeclRefExpr(VD, |
| 1855 | ExprTy, Loc, false, |
Fariborz Jahanian | c289bce | 2010-07-12 18:12:03 +0000 | [diff] [blame] | 1856 | constAdded); |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 1857 | if (getLangOptions().CPlusPlus) { |
| 1858 | if (!T->isDependentType() && !T->isReferenceType()) { |
| 1859 | Expr *E = new (Context) |
| 1860 | DeclRefExpr(const_cast<ValueDecl*>(BDRE->getDecl()), T, |
| 1861 | SourceLocation()); |
Fariborz Jahanian | ea882cd | 2010-06-04 21:35:44 +0000 | [diff] [blame] | 1862 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1863 | ExprResult Res = PerformCopyInitialization( |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 1864 | InitializedEntity::InitializeBlock(VD->getLocation(), |
Fariborz Jahanian | 28ed927 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 1865 | T, false), |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 1866 | SourceLocation(), |
| 1867 | Owned(E)); |
| 1868 | if (!Res.isInvalid()) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1869 | Res = MaybeCreateCXXExprWithTemporaries(Res.get()); |
Fariborz Jahanian | 4239aa1 | 2010-07-09 22:21:32 +0000 | [diff] [blame] | 1870 | Expr *Init = Res.takeAs<Expr>(); |
| 1871 | BDRE->setCopyConstructorExpr(Init); |
| 1872 | } |
Fariborz Jahanian | ea882cd | 2010-06-04 21:35:44 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
| 1875 | return Owned(BDRE); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1876 | } |
| 1877 | // If this reference is not in a block or if the referenced variable is |
| 1878 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1879 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1880 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), |
| 1881 | NameInfo, &SS); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1882 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1883 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1884 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1885 | tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1886 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1888 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1889 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1890 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1891 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1892 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1893 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1894 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1895 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1896 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1897 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1898 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 1899 | if (!currentDecl && getCurBlock()) |
| 1900 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1901 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1902 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1903 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1904 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1905 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1906 | QualType ResTy; |
| 1907 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1908 | ResTy = Context.DependentTy; |
| 1909 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1910 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1911 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1912 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1913 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1914 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1915 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1916 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1919 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1920 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1921 | bool Invalid = false; |
| 1922 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 1923 | if (Invalid) |
| 1924 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1925 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 1926 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 1927 | PP); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1928 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1929 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1930 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1931 | QualType Ty; |
| 1932 | if (!getLangOptions().CPlusPlus) |
| 1933 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 1934 | else if (Literal.isWide()) |
| 1935 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 1936 | else if (Literal.isMultiChar()) |
| 1937 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1938 | else |
| 1939 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1940 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1941 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1942 | Literal.isWide(), |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1943 | Ty, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1946 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1947 | // 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] | 1948 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1949 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1950 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1951 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 1952 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1953 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1954 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1955 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1956 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1957 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1958 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 1959 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1960 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1961 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1962 | bool Invalid = false; |
| 1963 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 1964 | if (Invalid) |
| 1965 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1966 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1967 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 1968 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1969 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1970 | return ExprError(); |
| 1971 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1972 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1973 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1974 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1975 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1976 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1977 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1978 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1979 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1980 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1981 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1982 | |
| 1983 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1984 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1985 | using llvm::APFloat; |
| 1986 | APFloat Val(Format); |
| 1987 | |
| 1988 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 1989 | |
| 1990 | // Overflow is always an error, but underflow is only an error if |
| 1991 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 1992 | if ((result & APFloat::opOverflow) || |
| 1993 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1994 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1995 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1996 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 1997 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1998 | APFloat::getLargest(Format).toString(buffer); |
| 1999 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2000 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2001 | APFloat::getSmallest(Format).toString(buffer); |
| 2002 | } |
| 2003 | |
| 2004 | Diag(Tok.getLocation(), diagnostic) |
| 2005 | << Ty |
| 2006 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2007 | } |
| 2008 | |
| 2009 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2010 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2011 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2012 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2013 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2014 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2015 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2016 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2017 | // long long is a C99 feature. |
| 2018 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2019 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2020 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2021 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2022 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2023 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2024 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2025 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2026 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2027 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2028 | Ty = Context.UnsignedLongLongTy; |
| 2029 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2030 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2031 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2032 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2033 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2035 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2036 | // be an unsigned int. |
| 2037 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2038 | |
| 2039 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2040 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2041 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2042 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2043 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2044 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2045 | // Does it fit in a unsigned int? |
| 2046 | if (ResultVal.isIntN(IntSize)) { |
| 2047 | // Does it fit in a signed int? |
| 2048 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2049 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2050 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2051 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2052 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2053 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2054 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2056 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2057 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2058 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2060 | // Does it fit in a unsigned long? |
| 2061 | if (ResultVal.isIntN(LongSize)) { |
| 2062 | // Does it fit in a signed long? |
| 2063 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2064 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2065 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2066 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2067 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2068 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2069 | } |
| 2070 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2071 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2072 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2073 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2074 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2075 | // Does it fit in a unsigned long long? |
| 2076 | if (ResultVal.isIntN(LongLongSize)) { |
| 2077 | // Does it fit in a signed long long? |
| 2078 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2079 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2080 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2081 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2082 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2083 | } |
| 2084 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2085 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2086 | // If we still couldn't decide a type, we probably have something that |
| 2087 | // 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] | 2088 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2089 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2090 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2091 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2092 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2094 | if (ResultVal.getBitWidth() != Width) |
| 2095 | ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2096 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2097 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2098 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2099 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2100 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2101 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2102 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2103 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2104 | |
| 2105 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2108 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2109 | SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2110 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2111 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 2114 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 2115 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2116 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2117 | SourceLocation OpLoc, |
| 2118 | const SourceRange &ExprRange, |
| 2119 | bool isSizeof) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2120 | if (exprType->isDependentType()) |
| 2121 | return false; |
| 2122 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2123 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2124 | // the result is the size of the referenced type." |
| 2125 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2126 | // result shall be the alignment of the referenced type." |
| 2127 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2128 | exprType = Ref->getPointeeType(); |
| 2129 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2130 | // C99 6.5.3.4p1: |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2131 | if (exprType->isFunctionType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2132 | // alignof(function) is allowed as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2133 | if (isSizeof) |
| 2134 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 2135 | return false; |
| 2136 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2138 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2139 | if (exprType->isVoidType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2140 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 2141 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2145 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2146 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2147 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2148 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2150 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2151 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCObjectType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2152 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2153 | << exprType << isSizeof << ExprRange; |
| 2154 | return true; |
Chris Lattner | 37920f5 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 2155 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | 2788782 | 2010-05-23 19:43:23 +0000 | [diff] [blame] | 2157 | if (Context.hasSameUnqualifiedType(exprType, Context.OverloadTy)) { |
| 2158 | Diag(OpLoc, diag::err_sizeof_alignof_overloaded_function_type) |
| 2159 | << !isSizeof << ExprRange; |
| 2160 | return true; |
| 2161 | } |
| 2162 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2163 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2166 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 2167 | const SourceRange &ExprRange) { |
| 2168 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2169 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2171 | if (isa<DeclRefExpr>(E)) |
| 2172 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2173 | |
| 2174 | // Cannot know anything else if the expression is dependent. |
| 2175 | if (E->isTypeDependent()) |
| 2176 | return false; |
| 2177 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2178 | if (E->getBitField()) { |
| 2179 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 2180 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2181 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2182 | |
| 2183 | // Alignment of a field access is always okay, so long as it isn't a |
| 2184 | // bit-field. |
| 2185 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2186 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2187 | return false; |
| 2188 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2189 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 2190 | } |
| 2191 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2192 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2193 | ExprResult |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2194 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2195 | SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2196 | bool isSizeOf, SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2197 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2198 | return ExprError(); |
| 2199 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2200 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2202 | if (!T->isDependentType() && |
| 2203 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 2204 | return ExprError(); |
| 2205 | |
| 2206 | // 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] | 2207 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2208 | Context.getSizeType(), OpLoc, |
| 2209 | R.getEnd())); |
| 2210 | } |
| 2211 | |
| 2212 | /// \brief Build a sizeof or alignof expression given an expression |
| 2213 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2214 | ExprResult |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2216 | bool isSizeOf, SourceRange R) { |
| 2217 | // Verify that the operand is valid. |
| 2218 | bool isInvalid = false; |
| 2219 | if (E->isTypeDependent()) { |
| 2220 | // Delay type-checking for type-dependent expressions. |
| 2221 | } else if (!isSizeOf) { |
| 2222 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2223 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2224 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 2225 | isInvalid = true; |
| 2226 | } else { |
| 2227 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 2228 | } |
| 2229 | |
| 2230 | if (isInvalid) |
| 2231 | return ExprError(); |
| 2232 | |
| 2233 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 2234 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 2235 | Context.getSizeType(), OpLoc, |
| 2236 | R.getEnd())); |
| 2237 | } |
| 2238 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2239 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 2240 | /// the same for @c alignof and @c __alignof |
| 2241 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2242 | ExprResult |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2243 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 2244 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 2245 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2246 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2247 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2248 | if (isType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2249 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2250 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2251 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2253 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2254 | Expr *ArgEx = (Expr *)TyOrEx; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2255 | ExprResult Result |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2256 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 2257 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2258 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2259 | } |
| 2260 | |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2261 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2262 | if (V->isTypeDependent()) |
| 2263 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2265 | // These operators return the element type of a complex type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2266 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2267 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2269 | // Otherwise they pass through real integer and floating point types here. |
| 2270 | if (V->getType()->isArithmeticType()) |
| 2271 | return V->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2273 | // Reject anything else. |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2274 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 2275 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2276 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2280 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2281 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2282 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2283 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2284 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2285 | switch (Kind) { |
| 2286 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2287 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2288 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2289 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2290 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2291 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2294 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2295 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2296 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2297 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2298 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2299 | if (Result.isInvalid()) return ExprError(); |
| 2300 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2301 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2302 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2303 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2304 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2305 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2306 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 2307 | Context.DependentTy, RLoc)); |
| 2308 | } |
| 2309 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2310 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2311 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2312 | LHSExp->getType()->isEnumeralType() || |
| 2313 | RHSExp->getType()->isRecordType() || |
| 2314 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2315 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2318 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2322 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2323 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 2324 | Expr *Idx, SourceLocation RLoc) { |
| 2325 | Expr *LHSExp = Base; |
| 2326 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2327 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2328 | // Perform default conversions. |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2329 | if (!LHSExp->getType()->getAs<VectorType>()) |
| 2330 | DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2331 | DefaultFunctionArrayLvalueConversion(RHSExp); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2332 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2333 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2334 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 2335 | // 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] | 2336 | // 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] | 2337 | // 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] | 2338 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2339 | Expr *BaseExpr, *IndexExpr; |
| 2340 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2341 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2342 | BaseExpr = LHSExp; |
| 2343 | IndexExpr = RHSExp; |
| 2344 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2345 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2346 | BaseExpr = LHSExp; |
| 2347 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2348 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2349 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2350 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2351 | BaseExpr = RHSExp; |
| 2352 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2353 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2355 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2356 | BaseExpr = LHSExp; |
| 2357 | IndexExpr = RHSExp; |
| 2358 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2359 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2360 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2361 | // Handle the uncommon case of "123[Ptr]". |
| 2362 | BaseExpr = RHSExp; |
| 2363 | IndexExpr = LHSExp; |
| 2364 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2365 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2366 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2367 | IndexExpr = RHSExp; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2368 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2369 | // FIXME: need to deal with const... |
| 2370 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2371 | } else if (LHSTy->isArrayType()) { |
| 2372 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2373 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2374 | // wasn't promoted because of the C90 rule that doesn't |
| 2375 | // allow promoting non-lvalue arrays. Warn, then |
| 2376 | // force the promotion here. |
| 2377 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2378 | LHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2379 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2380 | CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2381 | LHSTy = LHSExp->getType(); |
| 2382 | |
| 2383 | BaseExpr = LHSExp; |
| 2384 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2385 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2386 | } else if (RHSTy->isArrayType()) { |
| 2387 | // Same as previous, except for 123[f().a] case |
| 2388 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2389 | RHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2390 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2391 | CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2392 | RHSTy = RHSExp->getType(); |
| 2393 | |
| 2394 | BaseExpr = RHSExp; |
| 2395 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2396 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 2397 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2398 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2399 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2400 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 2401 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2402 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2403 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2404 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 2405 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2406 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2407 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2408 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2409 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2410 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2411 | // 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] | 2412 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2413 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2414 | // incomplete types are not object types. |
| 2415 | if (ResultType->isFunctionType()) { |
| 2416 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2417 | << ResultType << BaseExpr->getSourceRange(); |
| 2418 | return ExprError(); |
| 2419 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2421 | if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2423 | PDiag(diag::err_subscript_incomplete_type) |
| 2424 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2425 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2426 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2427 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2428 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2429 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2430 | << ResultType << BaseExpr->getSourceRange(); |
| 2431 | return ExprError(); |
| 2432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2433 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2434 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2435 | ResultType, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2438 | QualType Sema:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2439 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2440 | const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2441 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2442 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2443 | // see FIXME there. |
| 2444 | // |
| 2445 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2446 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2447 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2448 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2449 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2450 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2451 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2452 | // 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] | 2453 | // special names that indicate a subset of exactly half the elements are |
| 2454 | // to be selected. |
| 2455 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2456 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2457 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2458 | // 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] | 2459 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2460 | |
| 2461 | // Check that we've found one of the special components, or that the component |
| 2462 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2463 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2464 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2465 | HalvingSwizzle = true; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2466 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2467 | do |
| 2468 | compStr++; |
| 2469 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2470 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2471 | do |
| 2472 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2473 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2474 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2475 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2476 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2477 | // We didn't get to the end of the string. This means the component names |
| 2478 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2479 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 2480 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2481 | return QualType(); |
| 2482 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2483 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2484 | // Ensure no component accessor exceeds the width of the vector type it |
| 2485 | // operates on. |
| 2486 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2487 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2488 | |
| 2489 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2490 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2491 | |
| 2492 | while (*compStr) { |
| 2493 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2494 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2495 | << baseType << SourceRange(CompLoc); |
| 2496 | return QualType(); |
| 2497 | } |
| 2498 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2499 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2500 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2501 | // 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] | 2502 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2503 | // 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] | 2504 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2505 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | ac8183a | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2506 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2507 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2508 | if (HexSwizzle) |
| 2509 | CompSize--; |
| 2510 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2511 | if (CompSize == 1) |
| 2512 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2513 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2514 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2515 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2516 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2517 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2518 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2519 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2520 | } |
| 2521 | 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] | 2522 | } |
| 2523 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2524 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2525 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2526 | const Selector &Sel, |
| 2527 | ASTContext &Context) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2529 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2530 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2531 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2532 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2534 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2535 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2536 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2537 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2538 | return D; |
| 2539 | } |
| 2540 | return 0; |
| 2541 | } |
| 2542 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2543 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2544 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2545 | const Selector &Sel, |
| 2546 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2547 | // Check protocols on qualified interfaces. |
| 2548 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2549 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2550 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2551 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2552 | GDecl = PD; |
| 2553 | break; |
| 2554 | } |
| 2555 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2556 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2557 | GDecl = OMD; |
| 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2562 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2563 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2564 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2565 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2566 | if (GDecl) |
| 2567 | return GDecl; |
| 2568 | } |
| 2569 | } |
| 2570 | return GDecl; |
| 2571 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2572 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2573 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2574 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2575 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2576 | const CXXScopeSpec &SS, |
| 2577 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2578 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2579 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2580 | // Even in dependent contexts, try to diagnose base expressions with |
| 2581 | // obviously wrong types, e.g.: |
| 2582 | // |
| 2583 | // T* t; |
| 2584 | // t.f; |
| 2585 | // |
| 2586 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2587 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2588 | // allows this, while still reporting an error if T is a struct pointer. |
| 2589 | if (!IsArrow) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2590 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2591 | if (PT && (!getLangOptions().ObjC1 || |
| 2592 | PT->getPointeeType()->isRecordType())) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2593 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2594 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2595 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2596 | return ExprError(); |
| 2597 | } |
| 2598 | } |
| 2599 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2600 | assert(BaseType->isDependentType() || |
| 2601 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 2602 | isDependentScopeSpecifier(SS)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2603 | |
| 2604 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2605 | // must have pointer type, and the accessed type is the pointee. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2606 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2607 | IsArrow, OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2608 | SS.getScopeRep(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2609 | SS.getRange(), |
| 2610 | FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2611 | NameInfo, TemplateArgs)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2612 | } |
| 2613 | |
| 2614 | /// We know that the given qualified member reference points only to |
| 2615 | /// declarations which do not belong to the static type of the base |
| 2616 | /// expression. Diagnose the problem. |
| 2617 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2618 | Expr *BaseExpr, |
| 2619 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2620 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2621 | const LookupResult &R) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2622 | // If this is an implicit member access, use a different set of |
| 2623 | // diagnostics. |
| 2624 | if (!BaseExpr) |
| 2625 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2626 | |
John McCall | 1e67dd6 | 2010-04-27 01:43:38 +0000 | [diff] [blame] | 2627 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_of_unrelated) |
| 2628 | << SS.getRange() << R.getRepresentativeDecl() << BaseType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | // Check whether the declarations we found through a nested-name |
| 2632 | // specifier in a member expression are actually members of the base |
| 2633 | // type. The restriction here is: |
| 2634 | // |
| 2635 | // C++ [expr.ref]p2: |
| 2636 | // ... In these cases, the id-expression shall name a |
| 2637 | // member of the class or of one of its base classes. |
| 2638 | // |
| 2639 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2640 | // an unrelated class, and for us to find an overload set including |
| 2641 | // decls from classes which are not superclasses, as long as the decl |
| 2642 | // we actually pick through overload resolution is from a superclass. |
| 2643 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2644 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2645 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2646 | const LookupResult &R) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2647 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2648 | if (!BaseRT) { |
| 2649 | // We can't check this yet because the base type is still |
| 2650 | // dependent. |
| 2651 | assert(BaseType->isDependentType()); |
| 2652 | return false; |
| 2653 | } |
| 2654 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2655 | |
| 2656 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2657 | // If this is an implicit member reference and we find a |
| 2658 | // non-instance member, it's not an error. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2659 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2660 | return false; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2661 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2662 | // 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] | 2663 | DeclContext *DC = (*I)->getDeclContext(); |
| 2664 | while (DC->isTransparentContext()) |
| 2665 | DC = DC->getParent(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2666 | |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 2667 | if (!DC->isRecord()) |
| 2668 | continue; |
| 2669 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2670 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 2671 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2672 | |
| 2673 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2674 | return false; |
| 2675 | } |
| 2676 | |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2677 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2678 | return true; |
| 2679 | } |
| 2680 | |
| 2681 | static bool |
| 2682 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2683 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 2684 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 2685 | bool HasTemplateArgs) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2686 | RecordDecl *RDecl = RTy->getDecl(); |
| 2687 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 2688 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2689 | << BaseRange)) |
| 2690 | return true; |
| 2691 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 2692 | if (HasTemplateArgs) { |
| 2693 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 2694 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 2695 | |
| 2696 | bool MOUS; |
| 2697 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 2698 | return false; |
| 2699 | } |
| 2700 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2701 | DeclContext *DC = RDecl; |
| 2702 | if (SS.isSet()) { |
| 2703 | // If the member name was a qualified-id, look into the |
| 2704 | // nested-name-specifier. |
| 2705 | DC = SemaRef.computeDeclContext(SS, false); |
| 2706 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2707 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2708 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2709 | << SS.getRange() << DC; |
| 2710 | return true; |
| 2711 | } |
| 2712 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2713 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2714 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2715 | if (!isa<TypeDecl>(DC)) { |
| 2716 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2717 | << DC << SS.getRange(); |
| 2718 | return true; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2719 | } |
| 2720 | } |
| 2721 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2722 | // The record definition is complete, now look up the member. |
| 2723 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2724 | |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2725 | if (!R.empty()) |
| 2726 | return false; |
| 2727 | |
| 2728 | // We didn't find anything with the given name, so try to correct |
| 2729 | // for typos. |
| 2730 | DeclarationName Name = R.getLookupName(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2731 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2732 | !R.empty() && |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2733 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 2734 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 2735 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2736 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 2737 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2738 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 2739 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 2740 | << ND->getDeclName(); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2741 | return false; |
| 2742 | } else { |
| 2743 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 2744 | R.setLookupName(Name); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2747 | return false; |
| 2748 | } |
| 2749 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2750 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2751 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2752 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2753 | CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2754 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2755 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2756 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2757 | if (BaseType->isDependentType() || |
| 2758 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2759 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2760 | IsArrow, OpLoc, |
| 2761 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2762 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2763 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2764 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2765 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2766 | // Implicit member accesses. |
| 2767 | if (!Base) { |
| 2768 | QualType RecordTy = BaseType; |
| 2769 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2770 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2771 | RecordTy->getAs<RecordType>(), |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 2772 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2773 | return ExprError(); |
| 2774 | |
| 2775 | // Explicit member accesses. |
| 2776 | } else { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2777 | ExprResult Result = |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2778 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2779 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2780 | |
| 2781 | if (Result.isInvalid()) { |
| 2782 | Owned(Base); |
| 2783 | return ExprError(); |
| 2784 | } |
| 2785 | |
| 2786 | if (Result.get()) |
| 2787 | return move(Result); |
Sebastian Redl | fa1f70f | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 2788 | |
| 2789 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 2790 | BaseType = Base->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2793 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2794 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 2795 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2798 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2799 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2800 | SourceLocation OpLoc, bool IsArrow, |
| 2801 | const CXXScopeSpec &SS, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2802 | NamedDecl *FirstQualifierInScope, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2803 | LookupResult &R, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2804 | const TemplateArgumentListInfo *TemplateArgs, |
| 2805 | bool SuppressQualifierCheck) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2806 | QualType BaseType = BaseExprType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2807 | if (IsArrow) { |
| 2808 | assert(BaseType->isPointerType()); |
| 2809 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2810 | } |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2811 | R.setBaseObjectType(BaseType); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2812 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2813 | NestedNameSpecifier *Qualifier = SS.getScopeRep(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2814 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 2815 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 2816 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2817 | |
| 2818 | if (R.isAmbiguous()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2819 | return ExprError(); |
| 2820 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2821 | if (R.empty()) { |
| 2822 | // Rederive where we looked up. |
| 2823 | DeclContext *DC = (SS.isSet() |
| 2824 | ? computeDeclContext(SS, false) |
| 2825 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2826 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2827 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2828 | << MemberName << DC |
| 2829 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2830 | return ExprError(); |
| 2831 | } |
| 2832 | |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2833 | // Diagnose lookups that find only declarations from a non-base |
| 2834 | // type. This is possible for either qualified lookups (which may |
| 2835 | // have been qualified with an unrelated type) or implicit member |
| 2836 | // expressions (which were found with unqualified lookup and thus |
| 2837 | // may have come from an enclosing scope). Note that it's okay for |
| 2838 | // lookup to find declarations from a non-base type as long as those |
| 2839 | // aren't the ones picked by overload resolution. |
| 2840 | if ((SS.isSet() || !BaseExpr || |
| 2841 | (isa<CXXThisExpr>(BaseExpr) && |
| 2842 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 2843 | !SuppressQualifierCheck && |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2844 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2845 | return ExprError(); |
| 2846 | |
| 2847 | // Construct an unresolved result if we in fact got an unresolved |
| 2848 | // result. |
| 2849 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2850 | bool Dependent = |
John McCall | 7173903 | 2009-12-19 02:05:44 +0000 | [diff] [blame] | 2851 | BaseExprType->isDependentType() || |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2852 | R.isUnresolvableResult() || |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2853 | OverloadExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2854 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2855 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 2856 | // pick a member. |
| 2857 | R.suppressDiagnostics(); |
| 2858 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2859 | UnresolvedMemberExpr *MemExpr |
| 2860 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2861 | R.isUnresolvableResult(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2862 | BaseExpr, BaseExprType, |
| 2863 | IsArrow, OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2864 | Qualifier, SS.getRange(), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2865 | MemberNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2866 | TemplateArgs, R.begin(), R.end()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2867 | |
| 2868 | return Owned(MemExpr); |
| 2869 | } |
| 2870 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2871 | assert(R.isSingleResult()); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2872 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2873 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2874 | |
| 2875 | // FIXME: diagnose the presence of template arguments now. |
| 2876 | |
| 2877 | // If the decl being referenced had an error, return an error for this |
| 2878 | // sub-expr without emitting another error, in order to avoid cascading |
| 2879 | // error cases. |
| 2880 | if (MemberDecl->isInvalidDecl()) |
| 2881 | return ExprError(); |
| 2882 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2883 | // Handle the implicit-member-access case. |
| 2884 | if (!BaseExpr) { |
| 2885 | // 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] | 2886 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2887 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2889 | SourceLocation Loc = R.getNameLoc(); |
| 2890 | if (SS.getRange().isValid()) |
| 2891 | Loc = SS.getRange().getBegin(); |
| 2892 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2895 | bool ShouldCheckUse = true; |
| 2896 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2897 | // Don't diagnose the use of a virtual member function unless it's |
| 2898 | // explicitly qualified. |
| 2899 | if (MD->isVirtual() && !SS.isSet()) |
| 2900 | ShouldCheckUse = false; |
| 2901 | } |
| 2902 | |
| 2903 | // Check the use of this member. |
| 2904 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2905 | Owned(BaseExpr); |
| 2906 | return ExprError(); |
| 2907 | } |
| 2908 | |
| 2909 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2910 | // We may have found a field within an anonymous union or struct |
| 2911 | // (C++ [class.union]). |
Eli Friedman | 78cde14 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2912 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2913 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2914 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2915 | BaseExpr, OpLoc); |
| 2916 | |
| 2917 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2918 | QualType MemberType = FD->getType(); |
| 2919 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2920 | MemberType = Ref->getPointeeType(); |
| 2921 | else { |
| 2922 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2923 | BaseQuals.removeObjCGCAttr(); |
| 2924 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2925 | |
| 2926 | Qualifiers MemberQuals |
| 2927 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2928 | |
| 2929 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2930 | if (Combined != MemberQuals) |
| 2931 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2932 | } |
| 2933 | |
| 2934 | MarkDeclarationReferenced(MemberLoc, FD); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2935 | if (PerformObjectMemberConversion(BaseExpr, Qualifier, FoundDecl, FD)) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2936 | return ExprError(); |
| 2937 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2938 | FD, FoundDecl, MemberNameInfo, |
| 2939 | MemberType)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2943 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2944 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2945 | Var, FoundDecl, MemberNameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2946 | Var->getType().getNonReferenceType())); |
| 2947 | } |
| 2948 | |
| 2949 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2950 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2951 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2952 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2953 | MemberFn->getType())); |
| 2954 | } |
| 2955 | |
| 2956 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2957 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2958 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2959 | Enum, FoundDecl, MemberNameInfo, |
| 2960 | Enum->getType())); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | Owned(BaseExpr); |
| 2964 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 2965 | // We found something that we didn't expect. Complain. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2966 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2967 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 2968 | << MemberName << BaseType << int(IsArrow); |
| 2969 | else |
| 2970 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 2971 | << MemberName << BaseType << int(IsArrow); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2972 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 2973 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 2974 | << MemberName; |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 2975 | R.suppressDiagnostics(); |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 2976 | return ExprError(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | /// Look up the given member of the given non-type-dependent |
| 2980 | /// expression. This can return in one of two ways: |
| 2981 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2982 | /// assume that lookup was performed and the results written into |
| 2983 | /// the provided structure. It will take over from there. |
| 2984 | /// * Otherwise, the returned expression will be produced in place of |
| 2985 | /// an ordinary member expression. |
| 2986 | /// |
| 2987 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2988 | /// fixed for ObjC++. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2989 | ExprResult |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2990 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2991 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2992 | CXXScopeSpec &SS, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2993 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2994 | assert(BaseExpr && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2996 | // Perform default conversions. |
| 2997 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2998 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2999 | QualType BaseType = BaseExpr->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3000 | assert(!BaseType->isDependentType()); |
| 3001 | |
| 3002 | DeclarationName MemberName = R.getLookupName(); |
| 3003 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3004 | |
| 3005 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3006 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3007 | // function. Suggest the addition of those parentheses, build the |
| 3008 | // call, and continue on. |
| 3009 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 3010 | if (const FunctionProtoType *Fun |
| 3011 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 3012 | QualType ResultTy = Fun->getResultType(); |
| 3013 | if (Fun->getNumArgs() == 0 && |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3014 | ((!IsArrow && ResultTy->isRecordType()) || |
| 3015 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3016 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 3017 | ->isRecordType()))) { |
| 3018 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 3019 | Diag(Loc, diag::err_member_reference_needs_call) |
| 3020 | << QualType(Fun, 0) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3021 | << FixItHint::CreateInsertion(Loc, "()"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3022 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3023 | ExprResult NewBase |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3024 | = ActOnCallExpr(0, BaseExpr, Loc, MultiExprArg(*this, 0, 0), Loc); |
Douglas Gregor | 143d367 | 2010-06-21 22:46:46 +0000 | [diff] [blame] | 3025 | BaseExpr = 0; |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3026 | if (NewBase.isInvalid()) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3027 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3028 | |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 3029 | BaseExpr = NewBase.takeAs<Expr>(); |
| 3030 | DefaultFunctionArrayConversion(BaseExpr); |
| 3031 | BaseType = BaseExpr->getType(); |
| 3032 | } |
| 3033 | } |
| 3034 | } |
| 3035 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3036 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 3037 | // use that. |
| 3038 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | c2949f9 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 3039 | if (IsArrow) { |
| 3040 | // Handle the following exceptional case PObj->isa. |
| 3041 | if (const ObjCObjectPointerType *OPT = |
| 3042 | BaseType->getAs<ObjCObjectPointerType>()) { |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3043 | if (OPT->getObjectType()->isObjCId() && |
Fariborz Jahanian | c2949f9 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 3044 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | a5fee26 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 3045 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 3046 | Context.getObjCClassType())); |
Fariborz Jahanian | c2949f9 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3049 | // We have an 'id' type. Rather than fall through, we check if this |
| 3050 | // is a reference to 'isa'. |
| 3051 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 3052 | BaseType = Context.ObjCIdRedefinitionType; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3053 | ImpCastExprToType(BaseExpr, BaseType, CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3054 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3055 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3056 | |
Fariborz Jahanian | 04b258c | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 3057 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 3058 | // use that. |
| 3059 | if (Context.isObjCSelType(BaseType)) { |
| 3060 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 3061 | // is a reference to 'sel_id'. |
| 3062 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 3063 | BaseType = Context.ObjCSelRedefinitionType; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3064 | ImpCastExprToType(BaseExpr, BaseType, CK_BitCast); |
Fariborz Jahanian | 04b258c | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 3065 | } |
| 3066 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3067 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 3068 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3069 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3070 | // Handle properties on ObjC 'Class' types. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3071 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3072 | // Also must look for a getter name which uses property syntax. |
| 3073 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3074 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 3075 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 3076 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 3077 | ObjCMethodDecl *Getter; |
| 3078 | // FIXME: need to also look locally in the implementation. |
| 3079 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 3080 | // Check the use of this method. |
| 3081 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3082 | return ExprError(); |
| 3083 | } |
| 3084 | // If we found a getter then this may be a valid dot-reference, we |
| 3085 | // will look for the matching setter, in case it is needed. |
| 3086 | Selector SetterSel = |
| 3087 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 3088 | PP.getSelectorTable(), Member); |
| 3089 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 3090 | if (!Setter) { |
| 3091 | // If this reference is in an @implementation, also check for 'private' |
| 3092 | // methods. |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3093 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3094 | } |
| 3095 | // Look through local category implementations associated with the class. |
| 3096 | if (!Setter) |
| 3097 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3098 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3099 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3100 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3101 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3102 | if (Getter || Setter) { |
| 3103 | QualType PType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3104 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3105 | if (Getter) |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3106 | PType = Getter->getSendResultType(); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3107 | else |
| 3108 | // Get the expression type from Setter's incoming parameter. |
| 3109 | PType = (*(Setter->param_end() -1))->getType(); |
| 3110 | // FIXME: we must check that the setter has property type. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3111 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3112 | PType, |
| 3113 | Setter, MemberLoc, BaseExpr)); |
| 3114 | } |
| 3115 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 3116 | << MemberName << BaseType); |
| 3117 | } |
| 3118 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3119 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3120 | if (BaseType->isObjCClassType() && |
| 3121 | BaseType != Context.ObjCClassRedefinitionType) { |
| 3122 | BaseType = Context.ObjCClassRedefinitionType; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3123 | ImpCastExprToType(BaseExpr, BaseType, CK_BitCast); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 3124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3126 | if (IsArrow) { |
| 3127 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 3128 | BaseType = PT->getPointeeType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3129 | else if (BaseType->isObjCObjectPointerType()) |
| 3130 | ; |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3131 | else if (BaseType->isRecordType()) { |
| 3132 | // Recover from arrow accesses to records, e.g.: |
| 3133 | // struct MyRecord foo; |
| 3134 | // foo->bar |
| 3135 | // This is actually well-formed in C++ if MyRecord has an |
| 3136 | // overloaded operator->, but that should have been dealt with |
| 3137 | // by now. |
| 3138 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3139 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3140 | << FixItHint::CreateReplacement(OpLoc, "."); |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3141 | IsArrow = false; |
| 3142 | } else { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3143 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 3144 | << BaseType << BaseExpr->getSourceRange(); |
| 3145 | return ExprError(); |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 3146 | } |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3147 | } else { |
| 3148 | // Recover from dot accesses to pointers, e.g.: |
| 3149 | // type *foo; |
| 3150 | // foo.bar |
| 3151 | // This is actually well-formed in two cases: |
| 3152 | // - 'type' is an Objective C type |
| 3153 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 3154 | // the appropriate pointer type |
| 3155 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 3156 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 3157 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 3158 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 3159 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3160 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 3161 | BaseType = PT->getPointeeType(); |
| 3162 | IsArrow = true; |
| 3163 | } |
| 3164 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3165 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3166 | |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3167 | // Handle field access to simple records. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3168 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3169 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3170 | RTy, OpLoc, SS, HasTemplateArgs)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 3171 | return ExprError(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3172 | return Owned((Expr*) 0); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3173 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 3175 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 3176 | // (*Obj).ivar. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3177 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3178 | (!IsArrow && BaseType->isObjCObjectType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3179 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3180 | ObjCInterfaceDecl *IDecl = |
| 3181 | OPT ? OPT->getInterfaceDecl() |
| 3182 | : BaseType->getAs<ObjCObjectType>()->getInterface(); |
| 3183 | if (IDecl) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3184 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 3185 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3186 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3187 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3188 | |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3189 | if (!IV) { |
| 3190 | // Attempt to correct for typos in ivar names. |
| 3191 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3192 | LookupMemberName); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3193 | if (CorrectTypo(Res, 0, 0, IDecl, false, CTC_MemberLookup) && |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3194 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3195 | Diag(R.getNameLoc(), |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3196 | diag::err_typecheck_member_reference_ivar_suggest) |
| 3197 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3198 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3199 | IV->getNameAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3200 | Diag(IV->getLocation(), diag::note_previous_decl) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3201 | << IV->getDeclName(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3202 | } else { |
| 3203 | Res.clear(); |
| 3204 | Res.setLookupName(Member); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3205 | } |
| 3206 | } |
| 3207 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3208 | if (IV) { |
| 3209 | // If the decl being referenced had an error, return an error for this |
| 3210 | // sub-expr without emitting another error, in order to avoid cascading |
| 3211 | // error cases. |
| 3212 | if (IV->isInvalidDecl()) |
| 3213 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3214 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3215 | // Check whether we can reference this field. |
| 3216 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 3217 | return ExprError(); |
| 3218 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 3219 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 3220 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 3221 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 3222 | ClassOfMethodDecl = MD->getClassInterface(); |
| 3223 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 3224 | // Case of a c-function declared inside an objc implementation. |
| 3225 | // FIXME: For a c-style function nested inside an objc implementation |
| 3226 | // class, there is no implementation context available, so we pass |
| 3227 | // down the context as argument to this routine. Ideally, this context |
| 3228 | // need be passed down in the AST node and somehow calculated from the |
| 3229 | // AST for a function decl. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3230 | if (ObjCImplementationDecl *IMPD = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3231 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3232 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 3233 | else if (ObjCCategoryImplDecl* CatImplClass = |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3234 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3235 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 3236 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3237 | |
| 3238 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 3239 | if (ClassDeclared != IDecl || |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3240 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3242 | << IV->getDeclName(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 3243 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 3244 | // @protected |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3246 | << IV->getDeclName(); |
Steve Naroff | d1b64be | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 3247 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3248 | |
| 3249 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 3250 | MemberLoc, BaseExpr, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3251 | IsArrow)); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 3252 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3253 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3254 | << IDecl->getDeclName() << MemberName |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 3255 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | b1378f9 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 3256 | } |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3257 | } |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3258 | // Handle properties on 'id' and qualified "id". |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3259 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 3260 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3261 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3262 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3264 | // Check protocols on qualified interfaces. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3265 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3266 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 3267 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 3268 | // Check the use of this declaration |
| 3269 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3270 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3272 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 3273 | MemberLoc, BaseExpr)); |
| 3274 | } |
| 3275 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3276 | // Check the use of this method. |
| 3277 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3278 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3280 | return Owned(ObjCMessageExpr::Create(Context, |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3281 | OMD->getSendResultType(), |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3282 | OpLoc, BaseExpr, Sel, |
| 3283 | OMD, NULL, 0, MemberLoc)); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3284 | } |
| 3285 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3286 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3287 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3288 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3289 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3290 | |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 3291 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 3292 | // pointer to a (potentially qualified) interface type. |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 3293 | if (!IsArrow) |
| 3294 | if (const ObjCObjectPointerType *OPT = |
| 3295 | BaseType->getAsObjCInterfacePointerType()) |
Chris Lattner | 90c58fa | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 3296 | return HandleExprPropertyRefExpr(OPT, BaseExpr, MemberName, MemberLoc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3297 | |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3298 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3299 | if (!IsArrow && |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3300 | BaseType->isObjCObjectType() && |
| 3301 | BaseType->getAs<ObjCObjectType>()->isObjCId() && |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3302 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3303 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | a5fee26 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 3304 | Context.getObjCClassType())); |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3305 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3306 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3307 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3308 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3309 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 3310 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3311 | return ExprError(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3312 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3313 | MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3314 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3315 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3316 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 3317 | << BaseType << BaseExpr->getSourceRange(); |
| 3318 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3319 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3322 | /// The main callback when the parser finds something like |
| 3323 | /// expression . [nested-name-specifier] identifier |
| 3324 | /// expression -> [nested-name-specifier] identifier |
| 3325 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3326 | /// possibilities, including destructor and operator references. |
| 3327 | /// |
| 3328 | /// \param OpKind either tok::arrow or tok::period |
| 3329 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3330 | /// is used to diagnose mis-uses of special members that can |
| 3331 | /// only be called |
| 3332 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3333 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3334 | /// aren't properly put in the context chain |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3335 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3336 | SourceLocation OpLoc, |
| 3337 | tok::TokenKind OpKind, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3338 | CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3339 | UnqualifiedId &Id, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3340 | Decl *ObjCImpDecl, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3341 | bool HasTrailingLParen) { |
| 3342 | if (SS.isSet() && SS.isInvalid()) |
| 3343 | return ExprError(); |
| 3344 | |
| 3345 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3346 | |
| 3347 | // Decompose the name into its component parts. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3348 | DeclarationNameInfo NameInfo; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3349 | const TemplateArgumentListInfo *TemplateArgs; |
| 3350 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3351 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3352 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3353 | DeclarationName Name = NameInfo.getName(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3354 | bool IsArrow = (OpKind == tok::arrow); |
| 3355 | |
| 3356 | NamedDecl *FirstQualifierInScope |
| 3357 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3358 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3359 | |
| 3360 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3361 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3362 | if (Result.isInvalid()) return ExprError(); |
| 3363 | Base = Result.take(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3364 | |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3365 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 3366 | isDependentScopeSpecifier(SS)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3367 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3368 | IsArrow, OpLoc, |
| 3369 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3370 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3371 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3372 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3373 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
| 3374 | SS, ObjCImpDecl, TemplateArgs != 0); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3375 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3376 | if (Result.isInvalid()) { |
| 3377 | Owned(Base); |
| 3378 | return ExprError(); |
| 3379 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3380 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3381 | if (Result.get()) { |
| 3382 | // The only way a reference to a destructor can be used is to |
| 3383 | // immediately call it, which falls into this case. If the |
| 3384 | // next token is not a '(', produce a diagnostic and build the |
| 3385 | // call now. |
| 3386 | if (!HasTrailingLParen && |
| 3387 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3388 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3389 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3390 | return move(Result); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3393 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3394 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3395 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | return move(Result); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3401 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3402 | FunctionDecl *FD, |
| 3403 | ParmVarDecl *Param) { |
| 3404 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3405 | Diag(CallLoc, |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3406 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3407 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3409 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3410 | return ExprError(); |
| 3411 | } |
| 3412 | |
| 3413 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3414 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3416 | // Instantiate the expression. |
| 3417 | MultiLevelTemplateArgumentList ArgList |
| 3418 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3420 | std::pair<const TemplateArgument *, unsigned> Innermost |
| 3421 | = ArgList.getInnermost(); |
| 3422 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3423 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3424 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3425 | ExprResult Result = SubstExpr(UninstExpr, ArgList); |
| 3426 | if (Result.isInvalid()) |
| 3427 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3429 | // Check the expression as an initializer for the parameter. |
| 3430 | InitializedEntity Entity |
| 3431 | = InitializedEntity::InitializeParameter(Param); |
| 3432 | InitializationKind Kind |
| 3433 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3434 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3435 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3436 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3437 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3438 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3439 | MultiExprArg(*this, &ResultE, 1)); |
| 3440 | if (Result.isInvalid()) |
| 3441 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3443 | // Build the default argument expression. |
| 3444 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3445 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3448 | // If the default expression creates temporaries, we need to |
| 3449 | // push them to the current stack of expression temporaries so they'll |
| 3450 | // be properly destroyed. |
| 3451 | // FIXME: We should really be rebuilding the default argument with new |
| 3452 | // bound temporaries; see the comment in PR5810. |
| 3453 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3454 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
| 3455 | |
| 3456 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame^] | 3457 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3458 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3459 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3460 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3463 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3464 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3465 | /// function prototype Proto. Call is the call expression itself, and |
| 3466 | /// Fn is the function expression. For a C++ member function, this |
| 3467 | /// routine does not attempt to convert the object argument. Returns |
| 3468 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3469 | bool |
| 3470 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3471 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3472 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3473 | Expr **Args, unsigned NumArgs, |
| 3474 | SourceLocation RParenLoc) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3475 | // 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] | 3476 | // assignment, to the types of the corresponding parameter, ... |
| 3477 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3478 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3480 | // If too few arguments are available (and we don't have default |
| 3481 | // arguments for the remaining parameters), don't make the call. |
| 3482 | if (NumArgs < NumArgsInProto) { |
| 3483 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3484 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3485 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3486 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3487 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
| 3490 | // If too many are passed and not variadic, error on the extras and drop |
| 3491 | // them. |
| 3492 | if (NumArgs > NumArgsInProto) { |
| 3493 | if (!Proto->isVariadic()) { |
| 3494 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3495 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3496 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3497 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3498 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3499 | Args[NumArgs-1]->getLocEnd()); |
| 3500 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3501 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3502 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3503 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3504 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3505 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3506 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3507 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3508 | if (Fn->getType()->isBlockPointerType()) |
| 3509 | CallType = VariadicBlock; // Block |
| 3510 | else if (isa<MemberExpr>(Fn)) |
| 3511 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3512 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3513 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3514 | if (Invalid) |
| 3515 | return true; |
| 3516 | unsigned TotalNumArgs = AllArgs.size(); |
| 3517 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3518 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3519 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3520 | return false; |
| 3521 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3522 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3523 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3524 | FunctionDecl *FDecl, |
| 3525 | const FunctionProtoType *Proto, |
| 3526 | unsigned FirstProtoArg, |
| 3527 | Expr **Args, unsigned NumArgs, |
| 3528 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3529 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3530 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3531 | unsigned NumArgsToCheck = NumArgs; |
| 3532 | bool Invalid = false; |
| 3533 | if (NumArgs != NumArgsInProto) |
| 3534 | // Use default arguments for missing arguments |
| 3535 | NumArgsToCheck = NumArgsInProto; |
| 3536 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3537 | // 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] | 3538 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3539 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3540 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3541 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3542 | if (ArgIx < NumArgs) { |
| 3543 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3544 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3545 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3546 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3547 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3548 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3549 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3551 | // Pass the argument |
| 3552 | ParmVarDecl *Param = 0; |
| 3553 | if (FDecl && i < FDecl->getNumParams()) |
| 3554 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3555 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3556 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3557 | InitializedEntity Entity = |
| 3558 | Param? InitializedEntity::InitializeParameter(Param) |
| 3559 | : InitializedEntity::InitializeParameter(ProtoArgType); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3560 | ExprResult ArgE = PerformCopyInitialization(Entity, |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3561 | SourceLocation(), |
| 3562 | Owned(Arg)); |
| 3563 | if (ArgE.isInvalid()) |
| 3564 | return true; |
| 3565 | |
| 3566 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3567 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3568 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3569 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3570 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3571 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3572 | if (ArgExpr.isInvalid()) |
| 3573 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3574 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3575 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3576 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3577 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3578 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3579 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3580 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3581 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3582 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 3583 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3584 | Expr *Arg = Args[i]; |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 3585 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType, FDecl); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3586 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3587 | } |
| 3588 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3589 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3592 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3593 | /// This provides the location of the left/right parens and a list of comma |
| 3594 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3595 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3596 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3597 | MultiExprArg args, SourceLocation RParenLoc) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3598 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3599 | |
| 3600 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3601 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3602 | if (Result.isInvalid()) return ExprError(); |
| 3603 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3605 | Expr **Args = args.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3607 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3608 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3609 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3610 | if (NumArgs > 0) { |
| 3611 | // Pseudo-destructor calls should not have any arguments. |
| 3612 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3613 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3614 | SourceRange(Args[0]->getLocStart(), |
| 3615 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3617 | NumArgs = 0; |
| 3618 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3619 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3620 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3621 | RParenLoc)); |
| 3622 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3623 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3624 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3625 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3626 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3627 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3628 | bool Dependent = false; |
| 3629 | if (Fn->isTypeDependent()) |
| 3630 | Dependent = true; |
| 3631 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3632 | Dependent = true; |
| 3633 | |
| 3634 | if (Dependent) |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3635 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3636 | Context.DependentTy, RParenLoc)); |
| 3637 | |
| 3638 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3639 | if (Fn->getType()->isRecordType()) |
| 3640 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3641 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3642 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3643 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3644 | |
| 3645 | // Determine whether this is a call to an unresolved member function. |
| 3646 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3647 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3648 | // an unresolved using declaration), it has to be an overloaded |
| 3649 | // function set, which means it must contain either multiple |
| 3650 | // declarations (all methods or method templates) or a single |
| 3651 | // method template. |
| 3652 | assert((MemE->getNumDecls() > 1) || |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 3653 | isa<FunctionTemplateDecl>( |
| 3654 | (*MemE->decls_begin())->getUnderlyingDecl())); |
Douglas Gregor | 8f184a3 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3655 | (void)MemE; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3656 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3657 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3658 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3659 | } |
| 3660 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3661 | // Determine whether this is a call to a member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3662 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3663 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3664 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3665 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3666 | RParenLoc); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3667 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3668 | |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3669 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3670 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3671 | if (BO->getOpcode() == BO_PtrMemD || |
| 3672 | BO->getOpcode() == BO_PtrMemI) { |
Douglas Gregor | c8be952 | 2010-05-04 18:18:31 +0000 | [diff] [blame] | 3673 | if (const FunctionProtoType *FPT |
| 3674 | = BO->getType()->getAs<FunctionProtoType>()) { |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3675 | QualType ResultTy = FPT->getCallResultType(Context); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3676 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3677 | CXXMemberCallExpr *TheCall |
| 3678 | = new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3679 | NumArgs, ResultTy, |
| 3680 | RParenLoc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3681 | |
| 3682 | if (CheckCallReturnType(FPT->getResultType(), |
| 3683 | BO->getRHS()->getSourceRange().getBegin(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3684 | TheCall, 0)) |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3685 | return ExprError(); |
Anders Carlsson | 63dce02 | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3686 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3687 | if (ConvertArgumentsForCall(TheCall, BO, 0, FPT, Args, NumArgs, |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3688 | RParenLoc)) |
| 3689 | return ExprError(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3690 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3691 | return MaybeBindToTemporary(TheCall); |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3692 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3693 | return ExprError(Diag(Fn->getLocStart(), |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3694 | diag::err_typecheck_call_not_function) |
| 3695 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3696 | } |
| 3697 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3700 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3702 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3703 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3704 | Expr *NakedFn = Fn->IgnoreParens(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3705 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 3706 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
Douglas Gregor | 2fb18b7 | 2010-04-14 20:27:54 +0000 | [diff] [blame] | 3707 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3708 | RParenLoc); |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3709 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3710 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3711 | NamedDecl *NDecl = 0; |
| 3712 | if (isa<DeclRefExpr>(NakedFn)) |
| 3713 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 3714 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3715 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3716 | } |
| 3717 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3718 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3719 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3720 | /// unary-convert to an expression of function-pointer or |
| 3721 | /// block-pointer type. |
| 3722 | /// |
| 3723 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3724 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3725 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3726 | SourceLocation LParenLoc, |
| 3727 | Expr **Args, unsigned NumArgs, |
| 3728 | SourceLocation RParenLoc) { |
| 3729 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3730 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3731 | // Promote the function operand. |
| 3732 | UsualUnaryConversions(Fn); |
| 3733 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3734 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3735 | // of arguments and function on error. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3736 | CallExpr *TheCall = new (Context) CallExpr(Context, Fn, |
| 3737 | Args, NumArgs, |
| 3738 | Context.BoolTy, |
| 3739 | RParenLoc); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3740 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3741 | const FunctionType *FuncT; |
| 3742 | if (!Fn->getType()->isBlockPointerType()) { |
| 3743 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3744 | // have type pointer to function". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3745 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3746 | if (PT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3747 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3748 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3749 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3750 | } else { // This is a block call. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3751 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3752 | getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3753 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3754 | if (FuncT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3755 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3756 | << Fn->getType() << Fn->getSourceRange()); |
| 3757 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3758 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3759 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3760 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3761 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3762 | return ExprError(); |
| 3763 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3764 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3765 | TheCall->setType(FuncT->getCallResultType(Context)); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3767 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3768 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3769 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3770 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3771 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3772 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3774 | if (FDecl) { |
| 3775 | // Check if we have too few/too many template arguments, based |
| 3776 | // on our knowledge of the function definition. |
| 3777 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3778 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3779 | const FunctionProtoType *Proto = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3780 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3781 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3782 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3783 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3784 | } |
| 3785 | } |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3788 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3789 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3790 | Expr *Arg = Args[i]; |
| 3791 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3792 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3793 | Arg->getType(), |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3794 | PDiag(diag::err_call_incomplete_argument) |
| 3795 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3796 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3797 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3798 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 3799 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3801 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3802 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3803 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3804 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3805 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3806 | // Check for sentinels |
| 3807 | if (NDecl) |
| 3808 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3809 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3810 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3811 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3812 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3813 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3814 | |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3815 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3816 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3817 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3818 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3819 | return ExprError(); |
| 3820 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3821 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3822 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3823 | } |
| 3824 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3825 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3826 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3827 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3828 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3829 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3830 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3831 | |
| 3832 | TypeSourceInfo *TInfo; |
| 3833 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3834 | if (!TInfo) |
| 3835 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3836 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3837 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3840 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3841 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3842 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3843 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3844 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3845 | if (literalType->isArrayType()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3846 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3847 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3848 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3849 | } else if (!literalType->isDependentType() && |
| 3850 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3851 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3853 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3854 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3855 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3856 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3857 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3858 | InitializationKind Kind |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3859 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3860 | /*IsCStyleCast=*/true); |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3861 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3862 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3863 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3864 | &literalType); |
| 3865 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3866 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3867 | literalExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3868 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3869 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3870 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3871 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3872 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3873 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3874 | |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 3875 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3876 | literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3877 | } |
| 3878 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3879 | ExprResult |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3880 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3881 | SourceLocation RBraceLoc) { |
| 3882 | unsigned NumInit = initlist.size(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3883 | Expr **InitList = initlist.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3884 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3885 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3886 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3887 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3888 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3889 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3890 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3891 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3894 | static CastKind getScalarCastKind(ASTContext &Context, |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3895 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3896 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3897 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3898 | |
| 3899 | if (SrcTy->hasPointerRepresentation()) { |
| 3900 | if (DestTy->hasPointerRepresentation()) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3901 | return DestTy->isObjCObjectPointerType() ? |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3902 | CK_AnyPointerToObjCPointerCast : |
| 3903 | CK_BitCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3904 | if (DestTy->isIntegerType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3905 | return CK_PointerToIntegral; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3906 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3907 | |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3908 | if (SrcTy->isIntegerType()) { |
| 3909 | if (DestTy->isIntegerType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3910 | return CK_IntegralCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3911 | if (DestTy->hasPointerRepresentation()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3912 | return CK_IntegralToPointer; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3913 | if (DestTy->isRealFloatingType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3914 | return CK_IntegralToFloating; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3915 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3916 | |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3917 | if (SrcTy->isRealFloatingType()) { |
| 3918 | if (DestTy->isRealFloatingType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3919 | return CK_FloatingCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3920 | if (DestTy->isIntegerType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3921 | return CK_FloatingToIntegral; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3922 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3923 | |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3924 | // FIXME: Assert here. |
| 3925 | // assert(false && "Unhandled cast combination!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3926 | return CK_Unknown; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3929 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | 955a067 | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3930 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3931 | CastKind& Kind, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 3932 | CXXCastPath &BasePath, |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3933 | bool FunctionalStyle) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3934 | if (getLangOptions().CPlusPlus) |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3935 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, BasePath, |
| 3936 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3937 | |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3938 | DefaultFunctionArrayLvalueConversion(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3939 | |
| 3940 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3941 | // type needs to be scalar. |
| 3942 | if (castType->isVoidType()) { |
| 3943 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3944 | Kind = CK_ToVoid; |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3945 | return false; |
| 3946 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3947 | |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 3948 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 3949 | diag::err_typecheck_cast_to_incomplete)) |
| 3950 | return true; |
| 3951 | |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3952 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3953 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3954 | (castType->isStructureType() || castType->isUnionType())) { |
| 3955 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3956 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3957 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3958 | << castType << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3959 | Kind = CK_NoOp; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3960 | return false; |
| 3961 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3962 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3963 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3964 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3965 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3966 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3967 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3968 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3969 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3970 | castExpr->getType())) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3971 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3972 | << castExpr->getSourceRange(); |
| 3973 | break; |
| 3974 | } |
| 3975 | } |
| 3976 | if (Field == FieldEnd) |
| 3977 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3978 | << castExpr->getType() << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3979 | Kind = CK_ToUnion; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3980 | return false; |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3981 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3982 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3983 | // Reject any other conversions to non-scalar types. |
| 3984 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3985 | << castType << castExpr->getSourceRange(); |
| 3986 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3987 | |
| 3988 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3989 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3990 | return Diag(castExpr->getLocStart(), |
| 3991 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3992 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3993 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3994 | |
| 3995 | if (castType->isExtVectorType()) |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3996 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3997 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3998 | if (castType->isVectorType()) |
| 3999 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 4000 | if (castExpr->getType()->isVectorType()) |
| 4001 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 4002 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4003 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 4004 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4005 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4006 | if (!castType->isArithmeticType()) { |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4007 | QualType castExprType = castExpr->getType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4008 | if (!castExprType->isIntegralType(Context) && |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 4009 | castExprType->isArithmeticType()) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4010 | return Diag(castExpr->getLocStart(), |
| 4011 | diag::err_cast_pointer_from_non_pointer_int) |
| 4012 | << castExprType << castExpr->getSourceRange(); |
| 4013 | } else if (!castExpr->getType()->isArithmeticType()) { |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4014 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4015 | return Diag(castExpr->getLocStart(), |
| 4016 | diag::err_cast_pointer_to_non_pointer_int) |
| 4017 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4018 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4019 | |
| 4020 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4021 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4022 | if (Kind == CK_Unknown || Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4023 | CheckCastAlign(castExpr, castType, TyR); |
| 4024 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4025 | return false; |
| 4026 | } |
| 4027 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4028 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4029 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4030 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4031 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4032 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4033 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4034 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4035 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4036 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4037 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4038 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4039 | } else |
| 4040 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4041 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4042 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4043 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4044 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4045 | return false; |
| 4046 | } |
| 4047 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4048 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4049 | CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4050 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4051 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4052 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4053 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4054 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4055 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4056 | if (SrcTy->isVectorType()) { |
| 4057 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 4058 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 4059 | << DestTy << SrcTy << R; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4060 | Kind = CK_BitCast; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4061 | return false; |
| 4062 | } |
| 4063 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4064 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4065 | // conversion will take place first from scalar to elt type, and then |
| 4066 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4067 | if (SrcTy->isPointerType()) |
| 4068 | return Diag(R.getBegin(), |
| 4069 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4070 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4071 | |
| 4072 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 4073 | ImpCastExprToType(CastExpr, DestElemTy, |
| 4074 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4075 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4076 | Kind = CK_VectorSplat; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4077 | return false; |
| 4078 | } |
| 4079 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4080 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4081 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4082 | SourceLocation RParenLoc, Expr *castExpr) { |
| 4083 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4084 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4085 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 4086 | TypeSourceInfo *castTInfo; |
| 4087 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 4088 | if (!castTInfo) |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4089 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4090 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4091 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 4092 | if (isa<ParenListExpr>(castExpr)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4093 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4094 | castTInfo); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4095 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4096 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4097 | } |
| 4098 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4099 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4100 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4101 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4102 | CastKind Kind = CK_Unknown; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4103 | CXXCastPath BasePath; |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4104 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4105 | Kind, BasePath)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4106 | return ExprError(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4107 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4108 | return Owned(CStyleCastExpr::Create(Context, |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 4109 | Ty->getType().getNonLValueExprType(Context), |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4110 | Kind, castExpr, &BasePath, Ty, |
| 4111 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4112 | } |
| 4113 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4114 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4115 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4116 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4117 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4118 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4119 | if (!E) |
| 4120 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4121 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4122 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4124 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4125 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4126 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4128 | if (Result.isInvalid()) return ExprError(); |
| 4129 | |
| 4130 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4133 | ExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4134 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4135 | SourceLocation RParenLoc, Expr *Op, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4136 | TypeSourceInfo *TInfo) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4137 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4138 | QualType Ty = TInfo->getType(); |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4139 | bool isAltiVecLiteral = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4141 | // Check for an altivec literal, |
| 4142 | // i.e. all the elements are integer constants. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4143 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 4144 | if (PE->getNumExprs() == 0) { |
| 4145 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4146 | return ExprError(); |
| 4147 | } |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4148 | if (PE->getNumExprs() == 1) { |
| 4149 | if (!PE->getExpr(0)->getType()->isVectorType()) |
| 4150 | isAltiVecLiteral = true; |
| 4151 | } |
| 4152 | else |
| 4153 | isAltiVecLiteral = true; |
| 4154 | } |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4155 | |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 4156 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
| 4157 | // then handle it as such. |
| 4158 | if (isAltiVecLiteral) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4159 | llvm::SmallVector<Expr *, 8> initExprs; |
| 4160 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 4161 | initExprs.push_back(PE->getExpr(i)); |
| 4162 | |
| 4163 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4164 | // braces instead of the original commas. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4165 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 4166 | &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4167 | initExprs.size(), RParenLoc); |
| 4168 | E->setType(Ty); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4169 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4170 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4171 | // 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] | 4172 | // sequence of BinOp comma operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4173 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4174 | if (Result.isInvalid()) return ExprError(); |
| 4175 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4176 | } |
| 4177 | } |
| 4178 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4179 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4180 | SourceLocation R, |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4181 | MultiExprArg Val, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4182 | ParsedType TypeOfCast) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4183 | unsigned nexprs = Val.size(); |
| 4184 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4185 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4186 | Expr *expr; |
| 4187 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 4188 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4189 | else |
| 4190 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4191 | return Owned(expr); |
| 4192 | } |
| 4193 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4194 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4195 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4196 | /// C99 6.5.15 |
| 4197 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 4198 | SourceLocation QuestionLoc) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4199 | // C++ is sufficiently different to merit its own checker. |
| 4200 | if (getLangOptions().CPlusPlus) |
| 4201 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 4202 | |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4203 | UsualUnaryConversions(Cond); |
| 4204 | UsualUnaryConversions(LHS); |
| 4205 | UsualUnaryConversions(RHS); |
| 4206 | QualType CondTy = Cond->getType(); |
| 4207 | QualType LHSTy = LHS->getType(); |
| 4208 | QualType RHSTy = RHS->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4209 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4210 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4211 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 4212 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4213 | << CondTy; |
| 4214 | return QualType(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4215 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4216 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4217 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4218 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 4219 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4220 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4221 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4222 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4223 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4224 | UsualArithmeticConversions(LHS, RHS); |
| 4225 | return LHS->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4226 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4227 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4228 | // If both operands are the same structure or union type, the result is that |
| 4229 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4230 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4231 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4232 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4233 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4234 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4235 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4236 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4237 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4238 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4239 | // 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] | 4240 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4241 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4242 | if (!LHSTy->isVoidType()) |
| 4243 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4244 | << RHS->getSourceRange(); |
| 4245 | if (!RHSTy->isVoidType()) |
| 4246 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4247 | << LHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4248 | ImpCastExprToType(LHS, Context.VoidTy, CK_ToVoid); |
| 4249 | ImpCastExprToType(RHS, Context.VoidTy, CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4250 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4251 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4252 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4253 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4254 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4255 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4256 | // promote the null to a pointer. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4257 | ImpCastExprToType(RHS, LHSTy, CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4258 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4259 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4260 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4261 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4262 | ImpCastExprToType(LHS, RHSTy, CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4263 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4264 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4265 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4266 | // All objective-c pointer type analysis is done here. |
| 4267 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4268 | QuestionLoc); |
| 4269 | if (!compositeType.isNull()) |
| 4270 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4271 | |
| 4272 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4273 | // Handle block pointer types. |
| 4274 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4275 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4276 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4277 | QualType destType = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4278 | ImpCastExprToType(LHS, destType, CK_BitCast); |
| 4279 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4280 | return destType; |
| 4281 | } |
| 4282 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4283 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4284 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4285 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4286 | // We have 2 block pointer types. |
| 4287 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4288 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4289 | return LHSTy; |
| 4290 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4291 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4292 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4293 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4294 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4295 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4296 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4297 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4298 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4299 | // In this situation, we assume void* type. No especially good |
| 4300 | // reason, but this is what gcc does, and we do have to pick |
| 4301 | // to get a consistent AST. |
| 4302 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4303 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 4304 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4305 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4306 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4307 | // The block pointer types are compatible. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4308 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 4309 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4310 | return LHSTy; |
| 4311 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4312 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4313 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4314 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4315 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4316 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4317 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4318 | |
| 4319 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4320 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4321 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4322 | QualType destPointee |
| 4323 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4324 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4325 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4326 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4327 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4328 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4329 | return destType; |
| 4330 | } |
| 4331 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4332 | QualType destPointee |
| 4333 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4334 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4335 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4336 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4337 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4338 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4339 | return destType; |
| 4340 | } |
| 4341 | |
| 4342 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4343 | // Two identical pointer types are always compatible. |
| 4344 | return LHSTy; |
| 4345 | } |
| 4346 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4347 | rhptee.getUnqualifiedType())) { |
| 4348 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4349 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4350 | // In this situation, we assume void* type. No especially good |
| 4351 | // reason, but this is what gcc does, and we do have to pick |
| 4352 | // to get a consistent AST. |
| 4353 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4354 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 4355 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4356 | return incompatTy; |
| 4357 | } |
| 4358 | // The pointer types are compatible. |
| 4359 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4360 | // differently qualified versions of compatible types, the result type is |
| 4361 | // a pointer to an appropriately qualified version of the *composite* |
| 4362 | // type. |
| 4363 | // FIXME: Need to calculate the composite type. |
| 4364 | // FIXME: Need to add qualifiers |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4365 | ImpCastExprToType(LHS, LHSTy, CK_BitCast); |
| 4366 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4367 | return LHSTy; |
| 4368 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4369 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4370 | // GCC compatibility: soften pointer/integer mismatch. |
| 4371 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4372 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4373 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4374 | ImpCastExprToType(LHS, RHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4375 | return RHSTy; |
| 4376 | } |
| 4377 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4378 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4379 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4380 | ImpCastExprToType(RHS, LHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4381 | return LHSTy; |
| 4382 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4383 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4384 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4385 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4386 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4387 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 4388 | } |
| 4389 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4390 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4391 | /// two objective-c pointer types of the two input expressions. |
| 4392 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4393 | SourceLocation QuestionLoc) { |
| 4394 | QualType LHSTy = LHS->getType(); |
| 4395 | QualType RHSTy = RHS->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4396 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4397 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4398 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4399 | // redefinition type if an attempt is made to access its fields. |
| 4400 | if (LHSTy->isObjCClassType() && |
| 4401 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4402 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4403 | return LHSTy; |
| 4404 | } |
| 4405 | if (RHSTy->isObjCClassType() && |
| 4406 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4407 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4408 | return RHSTy; |
| 4409 | } |
| 4410 | // And the same for struct objc_object* / id |
| 4411 | if (LHSTy->isObjCIdType() && |
| 4412 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4413 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4414 | return LHSTy; |
| 4415 | } |
| 4416 | if (RHSTy->isObjCIdType() && |
| 4417 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4418 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4419 | return RHSTy; |
| 4420 | } |
| 4421 | // And the same for struct objc_selector* / SEL |
| 4422 | if (Context.isObjCSelType(LHSTy) && |
| 4423 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4424 | ImpCastExprToType(RHS, LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4425 | return LHSTy; |
| 4426 | } |
| 4427 | if (Context.isObjCSelType(RHSTy) && |
| 4428 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4429 | ImpCastExprToType(LHS, RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4430 | return RHSTy; |
| 4431 | } |
| 4432 | // Check constraints for Objective-C object pointers types. |
| 4433 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4434 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4435 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4436 | // Two identical object pointer types are always compatible. |
| 4437 | return LHSTy; |
| 4438 | } |
| 4439 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4440 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4441 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4442 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4443 | // If both operands are interfaces and either operand can be |
| 4444 | // assigned to the other, use that type as the composite |
| 4445 | // type. This allows |
| 4446 | // xxx ? (A*) a : (B*) b |
| 4447 | // where B is a subclass of A. |
| 4448 | // |
| 4449 | // Additionally, as for assignment, if either type is 'id' |
| 4450 | // allow silent coercion. Finally, if the types are |
| 4451 | // incompatible then make sure to use 'id' as the composite |
| 4452 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4453 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4454 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4455 | // It could return the composite type. |
| 4456 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4457 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4458 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4459 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4460 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4461 | RHSTy->isObjCQualifiedIdType()) && |
| 4462 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4463 | // Need to handle "id<xx>" explicitly. |
| 4464 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4465 | // id. Currently localizing to here until clear this should be |
| 4466 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4467 | compositeType = Context.getObjCIdType(); |
| 4468 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4469 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4470 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4471 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4472 | ; |
| 4473 | else { |
| 4474 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4475 | << LHSTy << RHSTy |
| 4476 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4477 | QualType incompatTy = Context.getObjCIdType(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4478 | ImpCastExprToType(LHS, incompatTy, CK_BitCast); |
| 4479 | ImpCastExprToType(RHS, incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4480 | return incompatTy; |
| 4481 | } |
| 4482 | // The object pointer types are compatible. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4483 | ImpCastExprToType(LHS, compositeType, CK_BitCast); |
| 4484 | ImpCastExprToType(RHS, compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4485 | return compositeType; |
| 4486 | } |
| 4487 | // Check Objective-C object pointer types and 'void *' |
| 4488 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4489 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4490 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4491 | QualType destPointee |
| 4492 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4493 | QualType destType = Context.getPointerType(destPointee); |
| 4494 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4495 | ImpCastExprToType(LHS, destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4496 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4497 | ImpCastExprToType(RHS, destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4498 | return destType; |
| 4499 | } |
| 4500 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4501 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4502 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4503 | QualType destPointee |
| 4504 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4505 | QualType destType = Context.getPointerType(destPointee); |
| 4506 | // Add qualifiers if necessary. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4507 | ImpCastExprToType(RHS, destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4508 | // Promote to void*. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4509 | ImpCastExprToType(LHS, destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4510 | return destType; |
| 4511 | } |
| 4512 | return QualType(); |
| 4513 | } |
| 4514 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4515 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4516 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4517 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4518 | SourceLocation ColonLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4519 | Expr *CondExpr, Expr *LHSExpr, |
| 4520 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4521 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4522 | // was the condition. |
| 4523 | bool isLHSNull = LHSExpr == 0; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4524 | Expr *SAVEExpr = 0; |
| 4525 | if (isLHSNull) { |
| 4526 | LHSExpr = SAVEExpr = CondExpr; |
| 4527 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4528 | |
| 4529 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | daaa9f2 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4530 | RHSExpr, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 4531 | if (result.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4532 | return ExprError(); |
| 4533 | |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4534 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4535 | LHSExpr, ColonLoc, |
| 4536 | RHSExpr, SAVEExpr, |
| 4537 | result)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 4540 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4541 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 4542 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4543 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4544 | // FIXME: add a couple examples in this comment. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4545 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4546 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 4547 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4548 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4549 | if ((lhsType->isObjCClassType() && |
| 4550 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4551 | (rhsType->isObjCClassType() && |
| 4552 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4553 | return Compatible; |
| 4554 | } |
| 4555 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 4556 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4557 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4558 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4559 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 4560 | // make sure we operate on the canonical type |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4561 | lhptee = Context.getCanonicalType(lhptee); |
| 4562 | rhptee = Context.getCanonicalType(rhptee); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 4563 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4564 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4565 | |
| 4566 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4567 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4568 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | ece8582 | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4569 | // FIXME: Handle ExtQualType |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4570 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4571 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 4572 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4573 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4574 | // 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] | 4575 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4576 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4577 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4578 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4579 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4580 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4581 | assert(rhptee->isFunctionType()); |
| 4582 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4583 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4584 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4585 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4586 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4587 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4588 | |
| 4589 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4590 | assert(lhptee->isFunctionType()); |
| 4591 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4592 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4593 | // 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] | 4594 | // unqualified versions of compatible types, ... |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4595 | lhptee = lhptee.getUnqualifiedType(); |
| 4596 | rhptee = rhptee.getUnqualifiedType(); |
| 4597 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4598 | // Check if the pointee types are compatible ignoring the sign. |
| 4599 | // We explicitly check for char so that we catch "char" vs |
| 4600 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4601 | if (lhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4602 | lhptee = Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 4603 | else if (lhptee->hasSignedIntegerRepresentation()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4604 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4605 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4606 | if (rhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4607 | rhptee = Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 4608 | else if (rhptee->hasSignedIntegerRepresentation()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4609 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4610 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4611 | if (lhptee == rhptee) { |
| 4612 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4613 | // takes priority over sign incompatibility because the sign |
| 4614 | // warning can be disabled. |
| 4615 | if (ConvTy != Compatible) |
| 4616 | return ConvTy; |
| 4617 | return IncompatiblePointerSign; |
| 4618 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4619 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4620 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4621 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4622 | // the eventual target type is the same and the pointers have the same |
| 4623 | // level of indirection, this must be the issue. |
| 4624 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4625 | do { |
| 4626 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4627 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4628 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4629 | lhptee = Context.getCanonicalType(lhptee); |
| 4630 | rhptee = Context.getCanonicalType(rhptee); |
| 4631 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4632 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4633 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4634 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4635 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4636 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4637 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | return IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4639 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4640 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 4641 | } |
| 4642 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4643 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4644 | /// block pointer types are compatible or whether a block and normal pointer |
| 4645 | /// are compatible. It is more restrict than comparing two function pointer |
| 4646 | // types. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4647 | Sema::AssignConvertType |
| 4648 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4649 | QualType rhsType) { |
| 4650 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4651 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4652 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4653 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4654 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4655 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4656 | // make sure we operate on the canonical type |
| 4657 | lhptee = Context.getCanonicalType(lhptee); |
| 4658 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4659 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4660 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4661 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4662 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4663 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4664 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4665 | |
Fariborz Jahanian | b8b0ea3 | 2010-03-17 00:20:01 +0000 | [diff] [blame] | 4666 | if (!getLangOptions().CPlusPlus) { |
| 4667 | if (!Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 4668 | return IncompatibleBlockPointer; |
| 4669 | } |
| 4670 | else if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4671 | return IncompatibleBlockPointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4672 | return ConvTy; |
| 4673 | } |
| 4674 | |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4675 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4676 | /// for assignment compatibility. |
| 4677 | Sema::AssignConvertType |
| 4678 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 4679 | if (lhsType->isObjCBuiltinType()) { |
| 4680 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 4681 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 4682 | !rhsType->isObjCQualifiedClassType()) |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 4683 | return IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4684 | return Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 4685 | } |
| 4686 | if (rhsType->isObjCBuiltinType()) { |
| 4687 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 4688 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 4689 | !lhsType->isObjCQualifiedClassType()) |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 4690 | return IncompatiblePointer; |
| 4691 | return Compatible; |
| 4692 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4693 | QualType lhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4694 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4695 | QualType rhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4696 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4697 | // make sure we operate on the canonical type |
| 4698 | lhptee = Context.getCanonicalType(lhptee); |
| 4699 | rhptee = Context.getCanonicalType(rhptee); |
| 4700 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4701 | return CompatiblePointerDiscardsQualifiers; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4702 | |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4703 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4704 | return Compatible; |
| 4705 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4706 | return IncompatibleObjCQualifiedId; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4707 | return IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4710 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4711 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 4712 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4713 | /// |
| 4714 | /// int a, *pint; |
| 4715 | /// short *pshort; |
| 4716 | /// struct foo *pfoo; |
| 4717 | /// |
| 4718 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4719 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4720 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4721 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4722 | /// |
| 4723 | /// 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] | 4724 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 4725 | /// |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4726 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4727 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4728 | // Get canonical types. We're not formatting these types, just comparing |
| 4729 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4730 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4731 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4732 | |
| 4733 | if (lhsType == rhsType) |
Chris Lattner | f5c973d | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4734 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4735 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4736 | if ((lhsType->isObjCClassType() && |
| 4737 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4738 | (rhsType->isObjCClassType() && |
| 4739 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4740 | return Compatible; |
| 4741 | } |
| 4742 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4743 | // If the left-hand side is a reference type, then we are in a |
| 4744 | // (rare!) case where we've allowed the use of references in C, |
| 4745 | // e.g., as a parameter type in a built-in function. In this case, |
| 4746 | // just make sure that the type referenced is compatible with the |
| 4747 | // right-hand side type. The caller is responsible for adjusting |
| 4748 | // lhsType so that the resulting expression does not have reference |
| 4749 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4750 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4751 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4752 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4753 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4754 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4755 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4756 | // to the same ExtVector type. |
| 4757 | if (lhsType->isExtVectorType()) { |
| 4758 | if (rhsType->isExtVectorType()) |
| 4759 | return lhsType == rhsType ? Compatible : Incompatible; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 4760 | if (rhsType->isArithmeticType()) |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4761 | return Compatible; |
| 4762 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4763 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4764 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4765 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
| 4766 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 4767 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 4768 | // no bits are changed but the result type is different. |
| 4769 | if (getLangOptions().LaxVectorConversions && |
| 4770 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4771 | return IncompatibleVectors; |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 4772 | |
| 4773 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 4774 | // vector type and vice versa |
| 4775 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) |
| 4776 | return Compatible; |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4777 | } |
| 4778 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4779 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4780 | |
Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 4781 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
| 4782 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4783 | return Compatible; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4784 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4785 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4786 | if (rhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4787 | return IntToPointer; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4788 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4789 | if (isa<PointerType>(rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4790 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4791 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4792 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4793 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4794 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4795 | return Compatible; |
| 4796 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4797 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4798 | if (rhsType->getAs<BlockPointerType>()) { |
| 4799 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4800 | return Compatible; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4801 | |
| 4802 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4803 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4804 | return Compatible; |
| 4805 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4806 | return Incompatible; |
| 4807 | } |
| 4808 | |
| 4809 | if (isa<BlockPointerType>(lhsType)) { |
| 4810 | if (rhsType->isIntegerType()) |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4811 | return IntToBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4812 | |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4813 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4814 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4815 | return Compatible; |
| 4816 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4817 | if (rhsType->isBlockPointerType()) |
| 4818 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4819 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4820 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4821 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4822 | return Compatible; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4823 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4824 | return Incompatible; |
| 4825 | } |
| 4826 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4827 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4828 | if (rhsType->isIntegerType()) |
| 4829 | return IntToPointer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4831 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4832 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4833 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4834 | return Compatible; |
| 4835 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4836 | } |
| 4837 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4838 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4839 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4840 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4841 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4842 | return Compatible; |
| 4843 | } |
| 4844 | // Treat block pointers as objects. |
| 4845 | if (rhsType->isBlockPointerType()) |
| 4846 | return Compatible; |
| 4847 | return Incompatible; |
| 4848 | } |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4849 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4850 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4851 | if (lhsType == Context.BoolTy) |
| 4852 | return Compatible; |
| 4853 | |
| 4854 | if (lhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4855 | return PointerToInt; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4856 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4857 | if (isa<PointerType>(lhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4858 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4859 | |
| 4860 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4861 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4862 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4863 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4864 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4865 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4866 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4867 | if (lhsType == Context.BoolTy) |
| 4868 | return Compatible; |
| 4869 | |
| 4870 | if (lhsType->isIntegerType()) |
| 4871 | return PointerToInt; |
| 4872 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4873 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4874 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4875 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4876 | return Compatible; |
| 4877 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4878 | } |
| 4879 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4880 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4881 | return Compatible; |
| 4882 | return Incompatible; |
| 4883 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4884 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4885 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4886 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4887 | return Compatible; |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 4888 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 4889 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4892 | /// \brief Constructs a transparent union from an expression that is |
| 4893 | /// used to initialize the transparent union. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4894 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4895 | QualType UnionType, FieldDecl *Field) { |
| 4896 | // Build an initializer list that designates the appropriate member |
| 4897 | // of the transparent union. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 4898 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 4899 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4900 | SourceLocation()); |
| 4901 | Initializer->setType(UnionType); |
| 4902 | Initializer->setInitializedFieldInUnion(Field); |
| 4903 | |
| 4904 | // Build a compound literal constructing a value of the transparent |
| 4905 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4906 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 4907 | E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4908 | Initializer, false); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | Sema::AssignConvertType |
| 4912 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4913 | QualType FromType = rExpr->getType(); |
| 4914 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | // 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] | 4916 | // transparent_union GCC extension. |
| 4917 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4918 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4919 | return Incompatible; |
| 4920 | |
| 4921 | // The field to initialize within the transparent union. |
| 4922 | RecordDecl *UD = UT->getDecl(); |
| 4923 | FieldDecl *InitField = 0; |
| 4924 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4925 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4926 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4927 | it != itend; ++it) { |
| 4928 | if (it->getType()->isPointerType()) { |
| 4929 | // If the transparent union contains a pointer type, we allow: |
| 4930 | // 1) void pointer |
| 4931 | // 2) null pointer constant |
| 4932 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4933 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4934 | ImpCastExprToType(rExpr, it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4935 | InitField = *it; |
| 4936 | break; |
| 4937 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4938 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4939 | if (rExpr->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4940 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4941 | ImpCastExprToType(rExpr, it->getType(), CK_IntegralToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4942 | InitField = *it; |
| 4943 | break; |
| 4944 | } |
| 4945 | } |
| 4946 | |
| 4947 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4948 | == Compatible) { |
| 4949 | InitField = *it; |
| 4950 | break; |
| 4951 | } |
| 4952 | } |
| 4953 | |
| 4954 | if (!InitField) |
| 4955 | return Incompatible; |
| 4956 | |
| 4957 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4958 | return Compatible; |
| 4959 | } |
| 4960 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4961 | Sema::AssignConvertType |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4962 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4963 | if (getLangOptions().CPlusPlus) { |
| 4964 | if (!lhsType->isRecordType()) { |
| 4965 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4966 | // expression is implicitly converted (C++ 4) to the |
| 4967 | // cv-unqualified type of the left operand. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4968 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4969 | AA_Assigning)) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4970 | return Incompatible; |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4971 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
| 4974 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4975 | // structures. |
| 4976 | } |
| 4977 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4978 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4979 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4980 | if ((lhsType->isPointerType() || |
| 4981 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4982 | lhsType->isBlockPointerType()) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4983 | && rExpr->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4984 | Expr::NPC_ValueDependentIsNull)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4985 | ImpCastExprToType(rExpr, lhsType, CK_Unknown); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4986 | return Compatible; |
| 4987 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4988 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4989 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4990 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4991 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 4992 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4993 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4994 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4995 | if (!lhsType->isReferenceType()) |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 4996 | DefaultFunctionArrayLvalueConversion(rExpr); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4997 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4998 | Sema::AssignConvertType result = |
| 4999 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5000 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5001 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5002 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5003 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5004 | // so that we can use references in built-in functions even in C. |
| 5005 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5006 | // does not have reference type. |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5007 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | a8a089b | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 5008 | ImpCastExprToType(rExpr, lhsType.getNonLValueExprType(Context), |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5009 | CK_Unknown); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5010 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5011 | } |
| 5012 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5013 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5014 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5015 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5016 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5017 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 5018 | } |
| 5019 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5020 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5021 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5022 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5023 | QualType lhsType = |
| 5024 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 5025 | QualType rhsType = |
| 5026 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5027 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5028 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5029 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5030 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5031 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5032 | // Handle the case of a vector & extvector type of the same size and element |
| 5033 | // 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] | 5034 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5035 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 5036 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5037 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5038 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5039 | if (lhsType->isExtVectorType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5040 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5041 | return lhsType; |
| 5042 | } |
| 5043 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5044 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 5045 | return rhsType; |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 5046 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 5047 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5048 | // vectors, the total size only needs to be the same. This is a |
| 5049 | // bitcast; no bits are changed but the result type is different. |
| 5050 | ImpCastExprToType(rex, lhsType, CK_BitCast); |
| 5051 | return lhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5052 | } |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 5053 | } |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 5054 | } |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5055 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5056 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5057 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5058 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5059 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5060 | ImpCastExprToType(lex, rhsType, CK_BitCast); |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5061 | return rhsType; |
| 5062 | } |
| 5063 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5064 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5065 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5066 | bool swapped = false; |
| 5067 | if (rhsType->isExtVectorType()) { |
| 5068 | swapped = true; |
| 5069 | std::swap(rex, lex); |
| 5070 | std::swap(rhsType, lhsType); |
| 5071 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5072 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5073 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5074 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5075 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5076 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5077 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5078 | ImpCastExprToType(rex, lhsType, CK_IntegralCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5079 | if (swapped) std::swap(rex, lex); |
| 5080 | return lhsType; |
| 5081 | } |
| 5082 | } |
| 5083 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5084 | rhsType->isRealFloatingType()) { |
| 5085 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5086 | ImpCastExprToType(rex, lhsType, CK_FloatingCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5087 | if (swapped) std::swap(rex, lex); |
| 5088 | return lhsType; |
| 5089 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5090 | } |
| 5091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5092 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5093 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5094 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5095 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5096 | << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5097 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5098 | } |
| 5099 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5100 | QualType Sema::CheckMultiplyDivideOperands( |
| 5101 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 060d5e2 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 5102 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5103 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5104 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5105 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5106 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5107 | if (!lex->getType()->isArithmeticType() || |
| 5108 | !rex->getType()->isArithmeticType()) |
| 5109 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5110 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5111 | // Check for division by zero. |
| 5112 | if (isDiv && |
| 5113 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5114 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_division_by_zero) |
Chris Lattner | 7011795 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 5115 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5116 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5117 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5118 | } |
| 5119 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5120 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5121 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5122 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5123 | if (lex->getType()->hasIntegerRepresentation() && |
| 5124 | rex->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5125 | return CheckVectorOperands(Loc, lex, rex); |
| 5126 | return InvalidOperands(Loc, lex, rex); |
| 5127 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5128 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5129 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5130 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5131 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 5132 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5133 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5134 | // Check for remainder by zero. |
| 5135 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | 7011795 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 5136 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_remainder_by_zero) |
| 5137 | << rex->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5138 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5139 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5142 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5143 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5144 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 5145 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 5146 | if (CompLHSTy) *CompLHSTy = compType; |
| 5147 | return compType; |
| 5148 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5149 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5150 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5151 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5152 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5153 | if (lex->getType()->isArithmeticType() && |
| 5154 | rex->getType()->isArithmeticType()) { |
| 5155 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5156 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5157 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5158 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5159 | // Put any potential pointer into PExp |
| 5160 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5161 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5162 | std::swap(PExp, IExp); |
| 5163 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5164 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5165 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5166 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5167 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5168 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5169 | // Check for arithmetic on pointers to incomplete types. |
| 5170 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5171 | if (getLangOptions().CPlusPlus) { |
| 5172 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5173 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5174 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5175 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5176 | |
| 5177 | // GNU extension: arithmetic on pointer to void |
| 5178 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5179 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5180 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5181 | if (getLangOptions().CPlusPlus) { |
| 5182 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 5183 | << lex->getType() << lex->getSourceRange(); |
| 5184 | return QualType(); |
| 5185 | } |
| 5186 | |
| 5187 | // GNU extension: arithmetic on pointer to function |
| 5188 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 5189 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 5190 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5191 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5192 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 5193 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5194 | PExp->getType()->isObjCObjectPointerType()) && |
| 5195 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5196 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5197 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5198 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5199 | return QualType(); |
| 5200 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5201 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5202 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5203 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5204 | << PointeeTy << PExp->getSourceRange(); |
| 5205 | return QualType(); |
| 5206 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5207 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5208 | if (CompLHSTy) { |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5209 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5210 | if (LHSTy.isNull()) { |
| 5211 | LHSTy = lex->getType(); |
| 5212 | if (LHSTy->isPromotableIntegerType()) |
| 5213 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5214 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5215 | *CompLHSTy = LHSTy; |
| 5216 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5217 | return PExp->getType(); |
| 5218 | } |
| 5219 | } |
| 5220 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5221 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5222 | } |
| 5223 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5224 | // C99 6.5.6 |
| 5225 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5226 | SourceLocation Loc, QualType* CompLHSTy) { |
| 5227 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 5228 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 5229 | if (CompLHSTy) *CompLHSTy = compType; |
| 5230 | return compType; |
| 5231 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5232 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5233 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5234 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5235 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5236 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5237 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5238 | if (lex->getType()->isArithmeticType() |
| 5239 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5240 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5241 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5243 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5244 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5245 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 4eed7a1 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 5246 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5247 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5248 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5249 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5250 | bool ComplainAboutVoid = false; |
| 5251 | Expr *ComplainAboutFunc = 0; |
| 5252 | if (lpointee->isVoidType()) { |
| 5253 | if (getLangOptions().CPlusPlus) { |
| 5254 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5255 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5256 | return QualType(); |
| 5257 | } |
| 5258 | |
| 5259 | // GNU C extension: arithmetic on pointer to void |
| 5260 | ComplainAboutVoid = true; |
| 5261 | } else if (lpointee->isFunctionType()) { |
| 5262 | if (getLangOptions().CPlusPlus) { |
| 5263 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5264 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5265 | return QualType(); |
| 5266 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5267 | |
| 5268 | // GNU C extension: arithmetic on pointer to function |
| 5269 | ComplainAboutFunc = lex; |
| 5270 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5272 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5273 | << lex->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5274 | << lex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5275 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5276 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5277 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5278 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5279 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5280 | << lpointee << lex->getSourceRange(); |
| 5281 | return QualType(); |
| 5282 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5284 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5285 | if (rex->getType()->isIntegerType()) { |
| 5286 | if (ComplainAboutVoid) |
| 5287 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5288 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5289 | if (ComplainAboutFunc) |
| 5290 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5291 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5292 | << ComplainAboutFunc->getSourceRange(); |
| 5293 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5294 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5295 | return lex->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5296 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5297 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5298 | // Handle pointer-pointer subtractions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5299 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 5300 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5301 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5302 | // RHS must be a completely-type object type. |
| 5303 | // Handle the GNU void* extension. |
| 5304 | if (rpointee->isVoidType()) { |
| 5305 | if (getLangOptions().CPlusPlus) { |
| 5306 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5307 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5308 | return QualType(); |
| 5309 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5310 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5311 | ComplainAboutVoid = true; |
| 5312 | } else if (rpointee->isFunctionType()) { |
| 5313 | if (getLangOptions().CPlusPlus) { |
| 5314 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5315 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5316 | return QualType(); |
| 5317 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5318 | |
| 5319 | // GNU extension: arithmetic on pointer to function |
| 5320 | if (!ComplainAboutFunc) |
| 5321 | ComplainAboutFunc = rex; |
| 5322 | } else if (!rpointee->isDependentType() && |
| 5323 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5324 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 5325 | << rex->getSourceRange() |
| 5326 | << rex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5327 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5328 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5329 | if (getLangOptions().CPlusPlus) { |
| 5330 | // Pointee types must be the same: C++ [expr.add] |
| 5331 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 5332 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5333 | << lex->getType() << rex->getType() |
| 5334 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5335 | return QualType(); |
| 5336 | } |
| 5337 | } else { |
| 5338 | // Pointee types must be compatible C99 6.5.6p3 |
| 5339 | if (!Context.typesAreCompatible( |
| 5340 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 5341 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 5342 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5343 | << lex->getType() << rex->getType() |
| 5344 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5345 | return QualType(); |
| 5346 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5347 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5348 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5349 | if (ComplainAboutVoid) |
| 5350 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5351 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5352 | if (ComplainAboutFunc) |
| 5353 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5354 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5355 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5356 | |
| 5357 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5358 | return Context.getPointerDiffType(); |
| 5359 | } |
| 5360 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5361 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5362 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5365 | // C99 6.5.7 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5366 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5367 | bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5368 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5369 | if (!lex->getType()->hasIntegerRepresentation() || |
| 5370 | !rex->getType()->hasIntegerRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5371 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5372 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 5373 | // Vector shifts promote their scalar inputs to vector type. |
| 5374 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 5375 | return CheckVectorOperands(Loc, lex, rex); |
| 5376 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5377 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 5378 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5379 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5380 | if (LHSTy.isNull()) { |
| 5381 | LHSTy = lex->getType(); |
| 5382 | if (LHSTy->isPromotableIntegerType()) |
| 5383 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5384 | } |
Chris Lattner | 3c13340 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 5385 | if (!isCompAssign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5386 | ImpCastExprToType(lex, LHSTy, CK_IntegralCast); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5387 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5388 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5389 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5390 | // Sanity-check shift operands |
| 5391 | llvm::APSInt Right; |
| 5392 | // Check right/shifter operand |
Daniel Dunbar | 687fa86 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 5393 | if (!rex->isValueDependent() && |
| 5394 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 2f08571 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 5395 | if (Right.isNegative()) |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5396 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 5397 | else { |
| 5398 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 5399 | Context.getTypeSize(lex->getType())); |
| 5400 | if (Right.uge(LeftBits)) |
| 5401 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 5402 | } |
| 5403 | } |
| 5404 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5405 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5406 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 5409 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 5410 | if (DeclContext *DC = D->getDeclContext()) { |
| 5411 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 5412 | return true; |
| 5413 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 5414 | return FD->isFunctionTemplateSpecialization(); |
| 5415 | } |
| 5416 | return false; |
| 5417 | } |
| 5418 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5419 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5420 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5421 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5422 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5423 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5424 | // Handle vector comparisons separately. |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5425 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5426 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5427 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5428 | QualType lType = lex->getType(); |
| 5429 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 5431 | if (!lType->hasFloatingRepresentation() && |
| 5432 | !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5433 | // For non-floating point types, check for self-comparisons of the form |
| 5434 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5435 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 5436 | // |
| 5437 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 5438 | // expansion. Also don't warn about comparisons which are only self |
| 5439 | // comparisons within a template specialization. The warnings should catch |
| 5440 | // obvious cases in the definition of the template anyways. The idea is to |
| 5441 | // warn when the typed comparison operator will always evaluate to the same |
| 5442 | // result. |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5443 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5444 | Expr *RHSStripped = rex->IgnoreParens(); |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 5445 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5446 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 5447 | if (DRL->getDecl() == DRR->getDecl() && !Loc.isMacroID() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 5448 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5449 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_comparison_always) |
| 5450 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5451 | << (Opc == BO_EQ |
| 5452 | || Opc == BO_LE |
| 5453 | || Opc == BO_GE)); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5454 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 5455 | !DRL->getDecl()->getType()->isReferenceType() && |
| 5456 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 5457 | // what is it always going to eval to? |
| 5458 | char always_evals_to; |
| 5459 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5460 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5461 | always_evals_to = 0; // false |
| 5462 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5463 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5464 | always_evals_to = 1; // true |
| 5465 | break; |
| 5466 | default: |
| 5467 | // best we can say is 'a constant' |
| 5468 | always_evals_to = 2; // e.g. array1 <= array2 |
| 5469 | break; |
| 5470 | } |
| 5471 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_comparison_always) |
| 5472 | << 1 // array |
| 5473 | << always_evals_to); |
| 5474 | } |
| 5475 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 5476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5478 | if (isa<CastExpr>(LHSStripped)) |
| 5479 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5480 | if (isa<CastExpr>(RHSStripped)) |
| 5481 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5482 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5483 | // Warn about comparisons against a string constant (unless the other |
| 5484 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5485 | Expr *literalString = 0; |
| 5486 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5487 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5488 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5489 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5490 | literalString = lex; |
| 5491 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5492 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5493 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5494 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5495 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5496 | literalString = rex; |
| 5497 | literalStringStripped = RHSStripped; |
| 5498 | } |
| 5499 | |
| 5500 | if (literalString) { |
| 5501 | std::string resultComparison; |
| 5502 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5503 | case BO_LT: resultComparison = ") < 0"; break; |
| 5504 | case BO_GT: resultComparison = ") > 0"; break; |
| 5505 | case BO_LE: resultComparison = ") <= 0"; break; |
| 5506 | case BO_GE: resultComparison = ") >= 0"; break; |
| 5507 | case BO_EQ: resultComparison = ") == 0"; break; |
| 5508 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5509 | default: assert(false && "Invalid comparison operator"); |
| 5510 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5511 | |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5512 | DiagRuntimeBehavior(Loc, |
| 5513 | PDiag(diag::warn_stringcompare) |
| 5514 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 5515 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5516 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5517 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5519 | // C99 6.5.8p3 / C99 6.5.9p4 |
| 5520 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5521 | UsualArithmeticConversions(lex, rex); |
| 5522 | else { |
| 5523 | UsualUnaryConversions(lex); |
| 5524 | UsualUnaryConversions(rex); |
| 5525 | } |
| 5526 | |
| 5527 | lType = lex->getType(); |
| 5528 | rType = rex->getType(); |
| 5529 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5530 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5531 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5532 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5533 | if (isRelational) { |
| 5534 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5535 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5536 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5537 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 5538 | if (lType->hasFloatingRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5539 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5540 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5541 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5542 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5543 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5544 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5545 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5546 | Expr::NPC_ValueDependentIsNull); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5547 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5548 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5549 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5550 | // All of the following pointer-related warnings are GCC extensions, except |
| 5551 | // when handling null pointer constants. |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5552 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5553 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5554 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5555 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5556 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5557 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5558 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5559 | if (LCanPointeeTy == RCanPointeeTy) |
| 5560 | return ResultTy; |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5561 | if (!isRelational && |
| 5562 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5563 | // Valid unless comparison between non-null pointer and function pointer |
| 5564 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5565 | // In a SFINAE context, we treat this as a hard error to maintain |
| 5566 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5567 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5568 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5569 | Diag(Loc, |
| 5570 | isSFINAEContext()? |
| 5571 | diag::err_typecheck_comparison_of_fptr_to_void |
| 5572 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5573 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5574 | |
| 5575 | if (isSFINAEContext()) |
| 5576 | return QualType(); |
| 5577 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5578 | ImpCastExprToType(rex, lType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5579 | return ResultTy; |
| 5580 | } |
| 5581 | } |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5582 | // C++ [expr.rel]p2: |
| 5583 | // [...] Pointer conversions (4.10) and qualification |
| 5584 | // conversions (4.4) are performed on pointer operands (or on |
| 5585 | // a pointer operand and a null pointer constant) to bring |
| 5586 | // them to their composite pointer type. [...] |
| 5587 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5588 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5589 | // comparisons of pointers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5590 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5591 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5592 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5593 | if (T.isNull()) { |
| 5594 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5595 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5596 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5597 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5598 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5599 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5600 | << lType << rType << T |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5601 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5604 | ImpCastExprToType(lex, T, CK_BitCast); |
| 5605 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5606 | return ResultTy; |
| 5607 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5608 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5609 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5610 | RCanPointeeTy.getUnqualifiedType())) { |
| 5611 | // Valid unless a relational comparison of function pointers |
| 5612 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5613 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5614 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5615 | } |
| 5616 | } else if (!isRelational && |
| 5617 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5618 | // Valid unless comparison between non-null pointer and function pointer |
| 5619 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5620 | && !LHSIsNull && !RHSIsNull) { |
| 5621 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5622 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5623 | } |
| 5624 | } else { |
| 5625 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5626 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5627 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 5628 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5629 | if (LCanPointeeTy != RCanPointeeTy) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5630 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5631 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5632 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5633 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5634 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5635 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5636 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5637 | if (RHSIsNull && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5638 | (lType->isPointerType() || |
| 5639 | (!isRelational && lType->isMemberPointerType()))) { |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 5640 | ImpCastExprToType(rex, lType, |
| 5641 | lType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5642 | ? CK_NullToMemberPointer |
| 5643 | : CK_IntegralToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5644 | return ResultTy; |
| 5645 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5646 | if (LHSIsNull && |
| 5647 | (rType->isPointerType() || |
| 5648 | (!isRelational && rType->isMemberPointerType()))) { |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 5649 | ImpCastExprToType(lex, rType, |
| 5650 | rType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5651 | ? CK_NullToMemberPointer |
| 5652 | : CK_IntegralToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5653 | return ResultTy; |
| 5654 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5655 | |
| 5656 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5657 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5658 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5659 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5660 | // In addition, pointers to members can be compared, or a pointer to |
| 5661 | // member and a null pointer constant. Pointer to member conversions |
| 5662 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5663 | // them to a common type. If one operand is a null pointer constant, |
| 5664 | // the common type is the type of the other operand. Otherwise, the |
| 5665 | // common type is a pointer to member type similar (4.4) to the type |
| 5666 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5667 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5668 | // types. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5669 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 5670 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5671 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5672 | if (T.isNull()) { |
| 5673 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5674 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5675 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5676 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5677 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5678 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5679 | << lType << rType << T |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 5680 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5681 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5682 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5683 | ImpCastExprToType(lex, T, CK_BitCast); |
| 5684 | ImpCastExprToType(rex, T, CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5685 | return ResultTy; |
| 5686 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5687 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5688 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5689 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5690 | return ResultTy; |
| 5691 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5692 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5693 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5694 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5695 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5696 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5697 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5698 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5699 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5700 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5701 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5702 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5703 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5704 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5705 | } |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5706 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5707 | if (!isRelational |
| 5708 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5709 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5710 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5711 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5712 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5713 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5714 | ->getPointeeType()->isVoidType()))) |
| 5715 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5716 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5717 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5718 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5719 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5720 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5721 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5722 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5723 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5724 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5725 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5726 | bool LPtrToVoid = LPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5727 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5728 | bool RPtrToVoid = RPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5729 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5730 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5731 | if (!LPtrToVoid && !RPtrToVoid && |
| 5732 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5733 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5734 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5735 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5736 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5737 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5738 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5739 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5740 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5741 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5742 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5743 | ImpCastExprToType(rex, lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5744 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5745 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5746 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5747 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 5748 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5749 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5750 | bool isError = false; |
| 5751 | if ((LHSIsNull && lType->isIntegerType()) || |
| 5752 | (RHSIsNull && rType->isIntegerType())) { |
| 5753 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5754 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5755 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5756 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5757 | else if (getLangOptions().CPlusPlus) { |
| 5758 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 5759 | isError = true; |
| 5760 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5761 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5762 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5763 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5764 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5765 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5766 | if (isError) |
| 5767 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5768 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5769 | |
| 5770 | if (lType->isIntegerType()) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5771 | ImpCastExprToType(lex, rType, CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5772 | else |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5773 | ImpCastExprToType(rex, lType, CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5774 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 5775 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 5776 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5777 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5778 | if (!isRelational && RHSIsNull |
| 5779 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5780 | ImpCastExprToType(rex, lType, CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5781 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5782 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5783 | if (!isRelational && LHSIsNull |
| 5784 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5785 | ImpCastExprToType(lex, rType, CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5786 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5787 | } |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5788 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5791 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5792 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5793 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5794 | /// types. |
| 5795 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5796 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5797 | bool isRelational) { |
| 5798 | // Check to make sure we're operating on vectors of the same type and width, |
| 5799 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5800 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5801 | if (vType.isNull()) |
| 5802 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5803 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5804 | QualType lType = lex->getType(); |
| 5805 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5806 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5807 | // For non-floating point types, check for self-comparisons of the form |
| 5808 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5809 | // often indicate logic errors in the program. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 5810 | if (!lType->hasFloatingRepresentation()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5811 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5812 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5813 | if (DRL->getDecl() == DRR->getDecl()) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 5814 | DiagRuntimeBehavior(Loc, |
| 5815 | PDiag(diag::warn_comparison_always) |
| 5816 | << 0 // self- |
| 5817 | << 2 // "a constant" |
| 5818 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5819 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5820 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5821 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 5822 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 5823 | assert (rType->hasFloatingRepresentation()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5824 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5825 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5826 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5827 | // Return the type for the comparison, which is the same as vector type for |
| 5828 | // integer vectors, or an integer type of identical size and number of |
| 5829 | // elements for floating point vectors. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5830 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5831 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5832 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5833 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5834 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5835 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5836 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5837 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5838 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5839 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5840 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5841 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5842 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5843 | } |
| 5844 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5845 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5846 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5847 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 5848 | if (lex->getType()->hasIntegerRepresentation() && |
| 5849 | rex->getType()->hasIntegerRepresentation()) |
| 5850 | return CheckVectorOperands(Loc, lex, rex); |
| 5851 | |
| 5852 | return InvalidOperands(Loc, lex, rex); |
| 5853 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5854 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5855 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5856 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5857 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5858 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5859 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5860 | } |
| 5861 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5862 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 5863 | Expr *&lex, Expr *&rex, SourceLocation Loc, unsigned Opc) { |
| 5864 | |
| 5865 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 5866 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 5867 | // is a constant. |
| 5868 | if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() && |
Eli Friedman | 6b197e0 | 2010-07-27 19:14:53 +0000 | [diff] [blame] | 5869 | rex->getType()->isIntegerType() && !rex->isValueDependent() && |
Chris Lattner | deee7a3 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 5870 | // Don't warn in macros. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 5871 | !Loc.isMacroID()) { |
| 5872 | // If the RHS can be constant folded, and if it constant folds to something |
| 5873 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 5874 | // happened to fold to true/false) then warn. |
| 5875 | Expr::EvalResult Result; |
| 5876 | if (rex->Evaluate(Result, Context) && !Result.HasSideEffects && |
| 5877 | Result.Val.getInt() != 0 && Result.Val.getInt() != 1) { |
| 5878 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 5879 | << rex->getSourceRange() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5880 | << (Opc == BO_LAnd ? "&&" : "||") |
| 5881 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 5882 | } |
| 5883 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 5884 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5885 | if (!Context.getLangOptions().CPlusPlus) { |
| 5886 | UsualUnaryConversions(lex); |
| 5887 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5888 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5889 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5890 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5891 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5892 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5893 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5894 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 5895 | // The following is safe because we only use this method for |
| 5896 | // non-overloadable operands. |
| 5897 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5898 | // C++ [expr.log.and]p1 |
| 5899 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 5900 | // The operands are both contextually converted to type bool. |
| 5901 | if (PerformContextuallyConvertToBool(lex) || |
| 5902 | PerformContextuallyConvertToBool(rex)) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5903 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5904 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5905 | // C++ [expr.log.and]p2 |
| 5906 | // C++ [expr.log.or]p2 |
| 5907 | // The result is a bool. |
| 5908 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5911 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5912 | /// is a read-only property; return true if so. A readonly property expression |
| 5913 | /// depends on various declarations and thus must be treated specially. |
| 5914 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5915 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5916 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5917 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5918 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5919 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5920 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5921 | BaseType->getAsObjCInterfacePointerType()) |
| 5922 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5923 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5924 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5925 | } |
| 5926 | } |
| 5927 | return false; |
| 5928 | } |
| 5929 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5930 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5931 | /// emit an error and return true. If so, return false. |
| 5932 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5933 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5934 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5935 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5936 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5937 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5938 | if (IsLV == Expr::MLV_Valid) |
| 5939 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5940 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5941 | unsigned Diag = 0; |
| 5942 | bool NeedType = false; |
| 5943 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5944 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5945 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5946 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5947 | NeedType = true; |
| 5948 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5949 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5950 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5951 | NeedType = true; |
| 5952 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5953 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5954 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5955 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 5956 | case Expr::MLV_Valid: |
| 5957 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5958 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 5959 | case Expr::MLV_MemberFunction: |
| 5960 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5961 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5962 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5963 | case Expr::MLV_IncompleteType: |
| 5964 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5965 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 5966 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5967 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5968 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5969 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5970 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5971 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5972 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5973 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5974 | case Expr::MLV_ReadonlyProperty: |
| 5975 | Diag = diag::error_readonly_property_assignment; |
| 5976 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5977 | case Expr::MLV_NoSetterProperty: |
| 5978 | Diag = diag::error_nosetter_property_assignment; |
| 5979 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5980 | case Expr::MLV_SubObjCPropertySetting: |
| 5981 | Diag = diag::error_no_subobject_property_setting; |
| 5982 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5983 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5984 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5985 | SourceRange Assign; |
| 5986 | if (Loc != OrigLoc) |
| 5987 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5988 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5989 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5990 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5992 | return true; |
| 5993 | } |
| 5994 | |
| 5995 | |
| 5996 | |
| 5997 | // C99 6.5.16.1 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5998 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5999 | SourceLocation Loc, |
| 6000 | QualType CompoundType) { |
| 6001 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 6002 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6003 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6004 | |
| 6005 | QualType LHSType = LHS->getType(); |
| 6006 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6007 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6008 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6009 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6010 | // Simple assignment "x = y". |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6011 | if (const ObjCImplicitSetterGetterRefExpr *OISGE = |
| 6012 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(LHS)) { |
| 6013 | // If using property-dot syntax notation for assignment, and there is a |
| 6014 | // setter, RHS expression is being passed to the setter argument. So, |
| 6015 | // type conversion (and comparison) is RHS to setter's argument type. |
| 6016 | if (const ObjCMethodDecl *SetterMD = OISGE->getSetterMethod()) { |
| 6017 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 6018 | LHSTy = (*P)->getType(); |
| 6019 | } |
| 6020 | } |
| 6021 | |
| 6022 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6023 | // Special case of NSObject attributes on c-style pointer types. |
| 6024 | if (ConvTy == IncompatiblePointer && |
| 6025 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6026 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6027 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6028 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6029 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6030 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6031 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 6032 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 6033 | // instead of "x += 4". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6034 | Expr *RHSCheck = RHS; |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6035 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 6036 | RHSCheck = ICE->getSubExpr(); |
| 6037 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6038 | if ((UO->getOpcode() == UO_Plus || |
| 6039 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6040 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6041 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6042 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 6043 | // And there is a space or other character before the subexpr of the |
| 6044 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 6045 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 6046 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6047 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6048 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6049 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6050 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6051 | } |
| 6052 | } else { |
| 6053 | // Compound assignment "x += y" |
Eli Friedman | b05c41e | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 6054 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6055 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6056 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6057 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 7c3bbdf | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 6058 | RHS, AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6059 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6060 | |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 6061 | |
| 6062 | // Check to see if the destination operand is a dereferenced null pointer. If |
| 6063 | // so, and if not volatile-qualified, this is undefined behavior that the |
| 6064 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 6065 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 6066 | // only handles the pattern "*null = whatever", which is a very syntactic |
| 6067 | // check. |
| 6068 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(LHS->IgnoreParenCasts())) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6069 | if (UO->getOpcode() == UO_Deref && |
Chris Lattner | 3956106 | 2010-07-07 06:14:23 +0000 | [diff] [blame] | 6070 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 6071 | isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull) && |
| 6072 | !UO->getType().isVolatileQualified()) { |
| 6073 | Diag(UO->getOperatorLoc(), diag::warn_indirection_through_null) |
| 6074 | << UO->getSubExpr()->getSourceRange(); |
| 6075 | Diag(UO->getOperatorLoc(), diag::note_indirection_through_null); |
| 6076 | } |
| 6077 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6078 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 6079 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6080 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6081 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 6082 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6083 | // 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] | 6084 | // operand. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6085 | return LHSType.getUnqualifiedType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6088 | // C99 6.5.17 |
| 6089 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 6090 | DiagnoseUnusedExprResult(LHS); |
| 6091 | |
Chris Lattner | f6e1e30 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 6092 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6093 | // C++ does not perform this conversion (C++ [expr.comma]p1). |
| 6094 | if (!getLangOptions().CPlusPlus) |
| 6095 | DefaultFunctionArrayLvalueConversion(RHS); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6096 | |
| 6097 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 6098 | // incomplete in C++). |
| 6099 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6100 | return RHS->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 6101 | } |
| 6102 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6103 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 6104 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6105 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 6106 | bool isInc, bool isPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6107 | if (Op->isTypeDependent()) |
| 6108 | return Context.DependentTy; |
| 6109 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6110 | QualType ResType = Op->getType(); |
| 6111 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 6112 | |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6113 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 6114 | // Decrement of bool is not allowed. |
| 6115 | if (!isInc) { |
| 6116 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 6117 | return QualType(); |
| 6118 | } |
| 6119 | // Increment of bool sets it to true, but is deprecated. |
| 6120 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 6121 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6122 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6123 | } else if (ResType->isAnyPointerType()) { |
| 6124 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6125 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6126 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6127 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6128 | if (getLangOptions().CPlusPlus) { |
| 6129 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 6130 | << Op->getSourceRange(); |
| 6131 | return QualType(); |
| 6132 | } |
| 6133 | |
| 6134 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6135 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6136 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 6137 | if (getLangOptions().CPlusPlus) { |
| 6138 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 6139 | << Op->getType() << Op->getSourceRange(); |
| 6140 | return QualType(); |
| 6141 | } |
| 6142 | |
| 6143 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6144 | << ResType << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6145 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6146 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6147 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 6148 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 6149 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 6150 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 6151 | else if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 6152 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 6153 | << PointeeTy << Op->getSourceRange(); |
| 6154 | return QualType(); |
| 6155 | } |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 6156 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6157 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 6158 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6159 | << ResType << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6160 | } else { |
| 6161 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 6162 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6163 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 6164 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6165 | // 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] | 6166 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 6167 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6168 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 6169 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 6170 | // (in C or with postfix), the increment is the unqualified type of the |
| 6171 | // operand. |
| 6172 | return isPrefix && getLangOptions().CPlusPlus |
| 6173 | ? ResType : ResType.getUnqualifiedType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6174 | } |
| 6175 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 6176 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6177 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 6178 | /// where the declaration is needed for type checking. We only need to |
| 6179 | /// handle cases when the expression references a function designator |
| 6180 | /// or is an lvalue. Here are some examples: |
| 6181 | /// - &(x) => x |
| 6182 | /// - &*****f => f for f a function designator. |
| 6183 | /// - &s.xx => s |
| 6184 | /// - &s.zz[1].yy -> s, if zz is an array |
| 6185 | /// - *(x + 1) -> x, if x is an array |
| 6186 | /// - &"123"[2] -> 0 |
| 6187 | /// - & __real__ x -> x |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6188 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6189 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6190 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6191 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6192 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6193 | // If this is an arrow operator, the address is an offset from |
| 6194 | // the base's value, so the object the base refers to is |
| 6195 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6196 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 6197 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6198 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6199 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 6200 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6201 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 6202 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6203 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 6204 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 6205 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 6206 | return getPrimaryDecl(ICE->getSubExpr()); |
| 6207 | } |
| 6208 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 6209 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 6210 | case Stmt::UnaryOperatorClass: { |
| 6211 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6212 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 6213 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6214 | case UO_Real: |
| 6215 | case UO_Imag: |
| 6216 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 6217 | return getPrimaryDecl(UO->getSubExpr()); |
| 6218 | default: |
| 6219 | return 0; |
| 6220 | } |
| 6221 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6222 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6223 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 6224 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6225 | // If the result of an implicit cast is an l-value, we care about |
| 6226 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 6227 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6228 | default: |
| 6229 | return 0; |
| 6230 | } |
| 6231 | } |
| 6232 | |
| 6233 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6234 | /// 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] | 6235 | /// 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] | 6236 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 6237 | /// 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] | 6238 | /// 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] | 6239 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6240 | QualType Sema::CheckAddressOfOperand(Expr *OrigOp, SourceLocation OpLoc) { |
| 6241 | if (OrigOp->isTypeDependent()) |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 6242 | return Context.DependentTy; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6243 | if (OrigOp->getType() == Context.OverloadTy) |
| 6244 | return Context.OverloadTy; |
| 6245 | |
| 6246 | // Make sure to ignore parentheses in subsequent checks |
| 6247 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 6248 | |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 6249 | if (getLangOptions().C99) { |
| 6250 | // Implement C99-only parts of addressof rules. |
| 6251 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6252 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 6253 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 6254 | // (assuming the deref expression is valid). |
| 6255 | return uOp->getSubExpr()->getType(); |
| 6256 | } |
| 6257 | // Technically, there should be a check for array subscript |
| 6258 | // expressions here, but the result of one is always an lvalue anyway. |
| 6259 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 6260 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 6731544 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 6261 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 6262 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 6263 | if (lval == Expr::LV_ClassTemporary) { |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6264 | Diag(OpLoc, isSFINAEContext()? diag::err_typecheck_addrof_class_temporary |
| 6265 | : diag::ext_typecheck_addrof_class_temporary) |
| 6266 | << op->getType() << op->getSourceRange(); |
| 6267 | if (isSFINAEContext()) |
| 6268 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6269 | } else if (isa<ObjCSelectorExpr>(op)) { |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 6270 | return Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 6271 | } else if (lval == Expr::LV_MemberFunction) { |
| 6272 | // If it's an instance method, make a member pointer. |
| 6273 | // The expression must have exactly the form &A::foo. |
| 6274 | |
| 6275 | // If the underlying expression isn't a decl ref, give up. |
| 6276 | if (!isa<DeclRefExpr>(op)) { |
| 6277 | Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 6278 | << OrigOp->getSourceRange(); |
| 6279 | return QualType(); |
| 6280 | } |
| 6281 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 6282 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 6283 | |
| 6284 | // The id-expression was parenthesized. |
| 6285 | if (OrigOp != DRE) { |
| 6286 | Diag(OpLoc, diag::err_parens_pointer_member_function) |
| 6287 | << OrigOp->getSourceRange(); |
| 6288 | |
| 6289 | // The method was named without a qualifier. |
| 6290 | } else if (!DRE->getQualifier()) { |
| 6291 | Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 6292 | << op->getSourceRange(); |
| 6293 | } |
| 6294 | |
| 6295 | return Context.getMemberPointerType(op->getType(), |
| 6296 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 6297 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 6298 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6299 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 6300 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 6301 | // FIXME: emit more specific diag... |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6302 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 6303 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6304 | return QualType(); |
| 6305 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 6306 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6307 | // The operand cannot be a bit-field |
| 6308 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 6309 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 6310 | return QualType(); |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 6311 | } else if (op->refersToVectorElement()) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 6312 | // The operand cannot be an element of a vector |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6313 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 6314 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 6315 | return QualType(); |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 6316 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 6317 | // cannot take address of a property expression. |
| 6318 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 6319 | << "property expression" << op->getSourceRange(); |
| 6320 | return QualType(); |
Anders Carlsson | 3fa58d1 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 6321 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 6322 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 01ccf99 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 6323 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 6324 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 6325 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6326 | // 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] | 6327 | // with the register storage-class specifier. |
| 6328 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 6329 | // in C++ it is not error to take address of a register |
| 6330 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 6331 | if (vd->getStorageClass() == SC_Register && |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 6332 | !getLangOptions().CPlusPlus) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6333 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 6334 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6335 | return QualType(); |
| 6336 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 6337 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 6338 | return Context.OverloadTy; |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6339 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 6340 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6341 | // Could be a pointer to member, though, if there is an explicit |
| 6342 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6343 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6344 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6345 | if (Ctx && Ctx->isRecord()) { |
| 6346 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6347 | Diag(OpLoc, |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6348 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 6349 | << FD->getDeclName() << FD->getType(); |
| 6350 | return QualType(); |
| 6351 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6352 | |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6353 | return Context.getMemberPointerType(op->getType(), |
| 6354 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6355 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6356 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 6357 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 6358 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6359 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6360 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 6361 | if (lval == Expr::LV_IncompleteVoidType) { |
| 6362 | // Taking the address of a void variable is technically illegal, but we |
| 6363 | // allow it in cases which are otherwise valid. |
| 6364 | // Example: "extern void x; void* y = &x;". |
| 6365 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 6366 | } |
| 6367 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6368 | // 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] | 6369 | if (op->getType()->isObjCObjectType()) |
| 6370 | return Context.getObjCObjectPointerType(op->getType()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6371 | return Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 6372 | } |
| 6373 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 6374 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6375 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6376 | if (Op->isTypeDependent()) |
| 6377 | return Context.DependentTy; |
| 6378 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6379 | UsualUnaryConversions(Op); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 6380 | QualType OpTy = Op->getType(); |
| 6381 | QualType Result; |
| 6382 | |
| 6383 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 6384 | // is an incomplete type or void. It would be possible to warn about |
| 6385 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 6386 | // warning is unlikely to catch any mistakes. |
| 6387 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 6388 | Result = PT->getPointeeType(); |
| 6389 | else if (const ObjCObjectPointerType *OPT = |
| 6390 | OpTy->getAs<ObjCObjectPointerType>()) |
| 6391 | Result = OPT->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6392 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 6393 | if (Result.isNull()) { |
| 6394 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
| 6395 | << OpTy << Op->getSourceRange(); |
| 6396 | return QualType(); |
| 6397 | } |
| 6398 | |
| 6399 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 6400 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6401 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6402 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6403 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6404 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6405 | switch (Kind) { |
| 6406 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6407 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 6408 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 6409 | case tok::star: Opc = BO_Mul; break; |
| 6410 | case tok::slash: Opc = BO_Div; break; |
| 6411 | case tok::percent: Opc = BO_Rem; break; |
| 6412 | case tok::plus: Opc = BO_Add; break; |
| 6413 | case tok::minus: Opc = BO_Sub; break; |
| 6414 | case tok::lessless: Opc = BO_Shl; break; |
| 6415 | case tok::greatergreater: Opc = BO_Shr; break; |
| 6416 | case tok::lessequal: Opc = BO_LE; break; |
| 6417 | case tok::less: Opc = BO_LT; break; |
| 6418 | case tok::greaterequal: Opc = BO_GE; break; |
| 6419 | case tok::greater: Opc = BO_GT; break; |
| 6420 | case tok::exclaimequal: Opc = BO_NE; break; |
| 6421 | case tok::equalequal: Opc = BO_EQ; break; |
| 6422 | case tok::amp: Opc = BO_And; break; |
| 6423 | case tok::caret: Opc = BO_Xor; break; |
| 6424 | case tok::pipe: Opc = BO_Or; break; |
| 6425 | case tok::ampamp: Opc = BO_LAnd; break; |
| 6426 | case tok::pipepipe: Opc = BO_LOr; break; |
| 6427 | case tok::equal: Opc = BO_Assign; break; |
| 6428 | case tok::starequal: Opc = BO_MulAssign; break; |
| 6429 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 6430 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 6431 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 6432 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 6433 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 6434 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 6435 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 6436 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 6437 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 6438 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6439 | } |
| 6440 | return Opc; |
| 6441 | } |
| 6442 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6443 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6444 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6445 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6446 | switch (Kind) { |
| 6447 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6448 | case tok::plusplus: Opc = UO_PreInc; break; |
| 6449 | case tok::minusminus: Opc = UO_PreDec; break; |
| 6450 | case tok::amp: Opc = UO_AddrOf; break; |
| 6451 | case tok::star: Opc = UO_Deref; break; |
| 6452 | case tok::plus: Opc = UO_Plus; break; |
| 6453 | case tok::minus: Opc = UO_Minus; break; |
| 6454 | case tok::tilde: Opc = UO_Not; break; |
| 6455 | case tok::exclaim: Opc = UO_LNot; break; |
| 6456 | case tok::kw___real: Opc = UO_Real; break; |
| 6457 | case tok::kw___imag: Opc = UO_Imag; break; |
| 6458 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6459 | } |
| 6460 | return Opc; |
| 6461 | } |
| 6462 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6463 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 6464 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 6465 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6466 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6467 | unsigned Op, |
| 6468 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6469 | QualType ResultTy; // Result type of the binary operator. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6470 | BinaryOperatorKind Opc = (BinaryOperatorKind) Op; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6471 | // The following two variables are used for compound assignment operators |
| 6472 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 6473 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6474 | |
| 6475 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6476 | case BO_Assign: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6477 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 6478 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6479 | case BO_PtrMemD: |
| 6480 | case BO_PtrMemI: |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6481 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6482 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6483 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6484 | case BO_Mul: |
| 6485 | case BO_Div: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6486 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6487 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6488 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6489 | case BO_Rem: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6490 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 6491 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6492 | case BO_Add: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6493 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 6494 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6495 | case BO_Sub: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6496 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 6497 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6498 | case BO_Shl: |
| 6499 | case BO_Shr: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6500 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 6501 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6502 | case BO_LE: |
| 6503 | case BO_LT: |
| 6504 | case BO_GE: |
| 6505 | case BO_GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6506 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6507 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6508 | case BO_EQ: |
| 6509 | case BO_NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6510 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6511 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6512 | case BO_And: |
| 6513 | case BO_Xor: |
| 6514 | case BO_Or: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6515 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 6516 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6517 | case BO_LAnd: |
| 6518 | case BO_LOr: |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6519 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6520 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6521 | case BO_MulAssign: |
| 6522 | case BO_DivAssign: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6523 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6524 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6525 | CompLHSTy = CompResultTy; |
| 6526 | if (!CompResultTy.isNull()) |
| 6527 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6528 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6529 | case BO_RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6530 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6531 | CompLHSTy = CompResultTy; |
| 6532 | if (!CompResultTy.isNull()) |
| 6533 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6534 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6535 | case BO_AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6536 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6537 | if (!CompResultTy.isNull()) |
| 6538 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6539 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6540 | case BO_SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6541 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6542 | if (!CompResultTy.isNull()) |
| 6543 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6544 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6545 | case BO_ShlAssign: |
| 6546 | case BO_ShrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6547 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6548 | CompLHSTy = CompResultTy; |
| 6549 | if (!CompResultTy.isNull()) |
| 6550 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6551 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6552 | case BO_AndAssign: |
| 6553 | case BO_XorAssign: |
| 6554 | case BO_OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6555 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6556 | CompLHSTy = CompResultTy; |
| 6557 | if (!CompResultTy.isNull()) |
| 6558 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6559 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6560 | case BO_Comma: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6561 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6562 | break; |
| 6563 | } |
| 6564 | if (ResultTy.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6565 | return ExprError(); |
Fariborz Jahanian | 99311ba | 2010-08-16 21:51:12 +0000 | [diff] [blame] | 6566 | if (ResultTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6567 | if (Opc >= BO_Assign && Opc <= BO_OrAssign) |
Fariborz Jahanian | 99311ba | 2010-08-16 21:51:12 +0000 | [diff] [blame] | 6568 | Diag(OpLoc, diag::err_assignment_requires_nonfragile_object) |
| 6569 | << ResultTy; |
| 6570 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6571 | if (CompResultTy.isNull()) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6572 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6573 | else |
| 6574 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6575 | CompLHSTy, CompResultTy, |
| 6576 | OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6577 | } |
| 6578 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6579 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6580 | /// ParenRange in parentheses. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6581 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6582 | const PartialDiagnostic &PD, |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6583 | const PartialDiagnostic &FirstNote, |
| 6584 | SourceRange FirstParenRange, |
| 6585 | const PartialDiagnostic &SecondNote, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6586 | SourceRange SecondParenRange) { |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6587 | Self.Diag(Loc, PD); |
| 6588 | |
| 6589 | if (!FirstNote.getDiagID()) |
| 6590 | return; |
| 6591 | |
| 6592 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 6593 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6594 | // We can't display the parentheses, so just return. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6595 | return; |
| 6596 | } |
| 6597 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6598 | Self.Diag(Loc, FirstNote) |
| 6599 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6600 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6601 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6602 | if (!SecondNote.getDiagID()) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6603 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6604 | |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6605 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6606 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6607 | // We can't display the parentheses, so just dig the |
| 6608 | // warning/error and return. |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6609 | Self.Diag(Loc, SecondNote); |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6610 | return; |
| 6611 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6612 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6613 | Self.Diag(Loc, SecondNote) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 6614 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6615 | << FixItHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6616 | } |
| 6617 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6618 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6619 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6620 | /// comparison operators have higher precedence. The most typical example of |
| 6621 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6622 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6623 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6624 | typedef BinaryOperator BinOp; |
| 6625 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6626 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6627 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6628 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6629 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6630 | rhsopc = BO->getOpcode(); |
| 6631 | |
| 6632 | // Subs are not binary operators. |
| 6633 | if (lhsopc == -1 && rhsopc == -1) |
| 6634 | return; |
| 6635 | |
| 6636 | // Bitwise operations are sometimes used as eager logical ops. |
| 6637 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6638 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6639 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6640 | return; |
| 6641 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6642 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6643 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6644 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6645 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6646 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6647 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6648 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6649 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd()), |
| 6650 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 6651 | << BinOp::getOpcodeStr(lhsopc), |
| 6652 | lhs->getSourceRange()); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6653 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6654 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6655 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6656 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6657 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6658 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6659 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 6660 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart()), |
| 6661 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 6662 | << BinOp::getOpcodeStr(rhsopc), |
| 6663 | rhs->getSourceRange()); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6664 | } |
| 6665 | |
| 6666 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6667 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6668 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6669 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6670 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6671 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6672 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6673 | } |
| 6674 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6675 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6676 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6677 | tok::TokenKind Kind, |
| 6678 | Expr *lhs, Expr *rhs) { |
| 6679 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6680 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6681 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6682 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6683 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6684 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6685 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6686 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6687 | } |
| 6688 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6689 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6690 | BinaryOperatorKind Opc, |
| 6691 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6692 | if (getLangOptions().CPlusPlus && |
Fariborz Jahanian | e89d03f | 2010-09-09 23:01:10 +0000 | [diff] [blame] | 6693 | (!isa<ObjCImplicitSetterGetterRefExpr>(lhs) || |
| 6694 | rhs->isTypeDependent()) && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6695 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6696 | rhs->getType()->isOverloadableType())) { |
| 6697 | // Find all of the overloaded operators visible from this |
| 6698 | // point. We perform both an operator-name lookup from the local |
| 6699 | // scope and an argument-dependent lookup based on the types of |
| 6700 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6701 | UnresolvedSet<16> Functions; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6702 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6703 | if (S && OverOp != OO_None) |
| 6704 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6705 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6706 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6707 | // Build the (potentially-overloaded, potentially-dependent) |
| 6708 | // binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6709 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6710 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6711 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6712 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6713 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6714 | } |
| 6715 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6716 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6717 | unsigned OpcIn, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6718 | Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6719 | UnaryOperatorKind Opc = static_cast<UnaryOperatorKind>(OpcIn); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6720 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6721 | QualType resultType; |
| 6722 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6723 | case UO_PreInc: |
| 6724 | case UO_PreDec: |
| 6725 | case UO_PostInc: |
| 6726 | case UO_PostDec: |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6727 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6728 | Opc == UO_PreInc || |
| 6729 | Opc == UO_PostInc, |
| 6730 | Opc == UO_PreInc || |
| 6731 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6732 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6733 | case UO_AddrOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 6734 | resultType = CheckAddressOfOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6735 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6736 | case UO_Deref: |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6737 | DefaultFunctionArrayLvalueConversion(Input); |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 6738 | resultType = CheckIndirectionOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6739 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6740 | case UO_Plus: |
| 6741 | case UO_Minus: |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6742 | UsualUnaryConversions(Input); |
| 6743 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6744 | if (resultType->isDependentType()) |
| 6745 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 6746 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 6747 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6748 | break; |
| 6749 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6750 | resultType->isEnumeralType()) |
| 6751 | break; |
| 6752 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6753 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6754 | resultType->isPointerType()) |
| 6755 | break; |
| 6756 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6757 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6758 | << resultType << Input->getSourceRange()); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6759 | case UO_Not: // bitwise complement |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6760 | UsualUnaryConversions(Input); |
| 6761 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6762 | if (resultType->isDependentType()) |
| 6763 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6764 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6765 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6766 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6767 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6768 | << resultType << Input->getSourceRange(); |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6769 | else if (!resultType->hasIntegerRepresentation()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6770 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6771 | << resultType << Input->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6772 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6773 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 6774 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6775 | DefaultFunctionArrayLvalueConversion(Input); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6776 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6777 | if (resultType->isDependentType()) |
| 6778 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6779 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6780 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6781 | << resultType << Input->getSourceRange()); |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 6782 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6783 | // In C++, it's bool. C++ 5.3.1p8 |
| 6784 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6785 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6786 | case UO_Real: |
| 6787 | case UO_Imag: |
| 6788 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UO_Real); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6789 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6790 | case UO_Extension: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 6791 | resultType = Input->getType(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 6792 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6793 | } |
| 6794 | if (resultType.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6795 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6796 | |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6797 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 6798 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 6799 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6800 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6801 | UnaryOperatorKind Opc, |
| 6802 | Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6803 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 6804 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6805 | // Find all of the overloaded operators visible from this |
| 6806 | // point. We perform both an operator-name lookup from the local |
| 6807 | // scope and an argument-dependent lookup based on the types of |
| 6808 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6809 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6810 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6811 | if (S && OverOp != OO_None) |
| 6812 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 6813 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6814 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6815 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6816 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6817 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6818 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6819 | } |
| 6820 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6821 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6822 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6823 | tok::TokenKind Op, Expr *Input) { |
| 6824 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6827 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6828 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6829 | SourceLocation LabLoc, |
| 6830 | IdentifierInfo *LabelII) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 6831 | // Look up the record for this label identifier. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 6832 | LabelStmt *&LabelDecl = getCurFunction()->LabelMap[LabelII]; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6833 | |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6834 | // If we haven't seen this label yet, create a forward reference. It |
| 6835 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | 846b1ec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6836 | if (LabelDecl == 0) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6837 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6838 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 6839 | // 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] | 6840 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6841 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 6842 | } |
| 6843 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6844 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 6845 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6846 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6847 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6848 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6849 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 6850 | bool isFileScope |
| 6851 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6852 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6853 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6854 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6855 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6856 | // example, it is not possible to goto into a stmt expression apparently. |
| 6857 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6858 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6859 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6860 | // as the type of the stmtexpr. |
| 6861 | QualType Ty = Context.VoidTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6862 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6863 | if (!Compound->body_empty()) { |
| 6864 | Stmt *LastStmt = Compound->body_back(); |
| 6865 | // If LastStmt is a label, skip down through into the body. |
| 6866 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6867 | LastStmt = Label->getSubStmt(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6868 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6869 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6870 | Ty = LastExpr->getType(); |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6871 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6872 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6873 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6874 | // expressions are not lvalues. |
| 6875 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6876 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6877 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6878 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6879 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6880 | TypeSourceInfo *TInfo, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6881 | OffsetOfComponent *CompPtr, |
| 6882 | unsigned NumComponents, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6883 | SourceLocation RParenLoc) { |
| 6884 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6885 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 6886 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6887 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6888 | // We must have at least one component that refers to the type, and the first |
| 6889 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6890 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6891 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6892 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 6893 | << ArgTy << TypeRange); |
| 6894 | |
| 6895 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 6896 | // with an incomplete type would be ill-formed. |
| 6897 | if (!Dependent |
| 6898 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 6899 | PDiag(diag::err_offsetof_incomplete_type) |
| 6900 | << TypeRange)) |
| 6901 | return ExprError(); |
| 6902 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6903 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6904 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6905 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6906 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6907 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6908 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6909 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 6910 | |
| 6911 | bool DidWarnAboutNonPOD = false; |
| 6912 | QualType CurrentType = ArgTy; |
| 6913 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 6914 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 6915 | llvm::SmallVector<Expr*, 4> Exprs; |
| 6916 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6917 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6918 | if (OC.isBrackets) { |
| 6919 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6920 | if (!CurrentType->isDependentType()) { |
| 6921 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 6922 | if(!AT) |
| 6923 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6924 | << CurrentType); |
| 6925 | CurrentType = AT->getElementType(); |
| 6926 | } else |
| 6927 | CurrentType = Context.DependentTy; |
| 6928 | |
| 6929 | // The expression must be an integral expression. |
| 6930 | // FIXME: An integral constant expression? |
| 6931 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 6932 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 6933 | !Idx->getType()->isIntegerType()) |
| 6934 | return ExprError(Diag(Idx->getLocStart(), |
| 6935 | diag::err_typecheck_subscript_not_integer) |
| 6936 | << Idx->getSourceRange()); |
| 6937 | |
| 6938 | // Record this array index. |
| 6939 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 6940 | Exprs.push_back(Idx); |
| 6941 | continue; |
| 6942 | } |
| 6943 | |
| 6944 | // Offset of a field. |
| 6945 | if (CurrentType->isDependentType()) { |
| 6946 | // We have the offset of a field, but we can't look into the dependent |
| 6947 | // type. Just record the identifier of the field. |
| 6948 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 6949 | CurrentType = Context.DependentTy; |
| 6950 | continue; |
| 6951 | } |
| 6952 | |
| 6953 | // We need to have a complete type to look into. |
| 6954 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 6955 | diag::err_offsetof_incomplete_type)) |
| 6956 | return ExprError(); |
| 6957 | |
| 6958 | // Look for the designated field. |
| 6959 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 6960 | if (!RC) |
| 6961 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6962 | << CurrentType); |
| 6963 | RecordDecl *RD = RC->getDecl(); |
| 6964 | |
| 6965 | // C++ [lib.support.types]p5: |
| 6966 | // The macro offsetof accepts a restricted set of type arguments in this |
| 6967 | // International Standard. type shall be a POD structure or a POD union |
| 6968 | // (clause 9). |
| 6969 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 6970 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| 6971 | DiagRuntimeBehavior(BuiltinLoc, |
| 6972 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6973 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6974 | << CurrentType)) |
| 6975 | DidWarnAboutNonPOD = true; |
| 6976 | } |
| 6977 | |
| 6978 | // Look for the field. |
| 6979 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6980 | LookupQualifiedName(R, RD); |
| 6981 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
| 6982 | if (!MemberDecl) |
| 6983 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6984 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 6985 | OC.LocEnd)); |
| 6986 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 6987 | // C99 7.17p3: |
| 6988 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 6989 | // |
| 6990 | // We diagnose this as an error. |
| 6991 | if (MemberDecl->getBitWidth()) { |
| 6992 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 6993 | << MemberDecl->getDeclName() |
| 6994 | << SourceRange(BuiltinLoc, RParenLoc); |
| 6995 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 6996 | return ExprError(); |
| 6997 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 6998 | |
| 6999 | RecordDecl *Parent = MemberDecl->getParent(); |
| 7000 | bool AnonStructUnion = Parent->isAnonymousStructOrUnion(); |
| 7001 | if (AnonStructUnion) { |
| 7002 | do { |
| 7003 | Parent = cast<RecordDecl>(Parent->getParent()); |
| 7004 | } while (Parent->isAnonymousStructOrUnion()); |
| 7005 | } |
| 7006 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7007 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 7008 | // the base class indirections. |
| 7009 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 7010 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 7011 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 7012 | CXXBasePath &Path = Paths.front(); |
| 7013 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 7014 | B != BEnd; ++B) |
| 7015 | Comps.push_back(OffsetOfNode(B->Base)); |
| 7016 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 7017 | |
| 7018 | if (AnonStructUnion) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7019 | llvm::SmallVector<FieldDecl*, 4> Path; |
| 7020 | BuildAnonymousStructUnionMemberPath(MemberDecl, Path); |
| 7021 | unsigned n = Path.size(); |
| 7022 | for (int j = n - 1; j > -1; --j) |
| 7023 | Comps.push_back(OffsetOfNode(OC.LocStart, Path[j], OC.LocEnd)); |
| 7024 | } else { |
| 7025 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
| 7026 | } |
| 7027 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 7028 | } |
| 7029 | |
| 7030 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 7031 | TInfo, Comps.data(), Comps.size(), |
| 7032 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 7033 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7034 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7035 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7036 | SourceLocation BuiltinLoc, |
| 7037 | SourceLocation TypeLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7038 | ParsedType argty, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 7039 | OffsetOfComponent *CompPtr, |
| 7040 | unsigned NumComponents, |
| 7041 | SourceLocation RPLoc) { |
| 7042 | |
| 7043 | TypeSourceInfo *ArgTInfo; |
| 7044 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 7045 | if (ArgTy.isNull()) |
| 7046 | return ExprError(); |
| 7047 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 7048 | if (!ArgTInfo) |
| 7049 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 7050 | |
| 7051 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 7052 | RPLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7053 | } |
| 7054 | |
| 7055 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7056 | ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7057 | ParsedType arg1,ParsedType arg2, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7058 | SourceLocation RPLoc) { |
Abramo Bagnara | 092990a | 2010-08-10 08:50:03 +0000 | [diff] [blame] | 7059 | TypeSourceInfo *argTInfo1; |
| 7060 | QualType argT1 = GetTypeFromParser(arg1, &argTInfo1); |
| 7061 | TypeSourceInfo *argTInfo2; |
| 7062 | QualType argT2 = GetTypeFromParser(arg2, &argTInfo2); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7063 | |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 7064 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7065 | |
Abramo Bagnara | 092990a | 2010-08-10 08:50:03 +0000 | [diff] [blame] | 7066 | return BuildTypesCompatibleExpr(BuiltinLoc, argTInfo1, argTInfo2, RPLoc); |
| 7067 | } |
| 7068 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7069 | ExprResult |
Abramo Bagnara | 092990a | 2010-08-10 08:50:03 +0000 | [diff] [blame] | 7070 | Sema::BuildTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 7071 | TypeSourceInfo *argTInfo1, |
| 7072 | TypeSourceInfo *argTInfo2, |
| 7073 | SourceLocation RPLoc) { |
Douglas Gregor | f907cbf | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 7074 | if (getLangOptions().CPlusPlus) { |
| 7075 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 7076 | << SourceRange(BuiltinLoc, RPLoc); |
| 7077 | return ExprError(); |
| 7078 | } |
| 7079 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7080 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
Abramo Bagnara | 092990a | 2010-08-10 08:50:03 +0000 | [diff] [blame] | 7081 | argTInfo1, argTInfo2, RPLoc)); |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
Abramo Bagnara | 092990a | 2010-08-10 08:50:03 +0000 | [diff] [blame] | 7084 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7085 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7086 | Expr *CondExpr, |
| 7087 | Expr *LHSExpr, Expr *RHSExpr, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7088 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 7089 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 7090 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7091 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7092 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 7093 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7094 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7095 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7096 | } else { |
| 7097 | // The conditional expression is required to be a constant expression. |
| 7098 | llvm::APSInt condEval(32); |
| 7099 | SourceLocation ExpLoc; |
| 7100 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7101 | return ExprError(Diag(ExpLoc, |
| 7102 | diag::err_typecheck_choose_expr_requires_constant) |
| 7103 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 7104 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7105 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 7106 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7107 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 7108 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7109 | } |
| 7110 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7111 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7112 | resType, RPLoc, |
| 7113 | resType->isDependentType(), |
| 7114 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 7115 | } |
| 7116 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7117 | //===----------------------------------------------------------------------===// |
| 7118 | // Clang Extensions. |
| 7119 | //===----------------------------------------------------------------------===// |
| 7120 | |
| 7121 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7122 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7123 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 7124 | PushBlockScope(BlockScope, Block); |
| 7125 | CurContext->addDecl(Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 7126 | if (BlockScope) |
| 7127 | PushDeclContext(BlockScope, Block); |
| 7128 | else |
| 7129 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7130 | } |
| 7131 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 7132 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7133 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7134 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7135 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7136 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7137 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 7138 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 7139 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7140 | bool isVariadic; |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7141 | QualType RetTy; |
| 7142 | if (const FunctionType *Fn = T->getAs<FunctionType>()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7143 | CurBlock->FunctionType = T; |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7144 | RetTy = Fn->getResultType(); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7145 | isVariadic = |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7146 | !isa<FunctionProtoType>(Fn) || cast<FunctionProtoType>(Fn)->isVariadic(); |
| 7147 | } else { |
| 7148 | RetTy = T; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7149 | isVariadic = false; |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7150 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7151 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7152 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 7153 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7154 | // Don't allow returning an array by value. |
| 7155 | if (RetTy->isArrayType()) { |
| 7156 | Diag(ParamInfo.getSourceRange().getBegin(), diag::err_block_returns_array); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 7157 | return; |
| 7158 | } |
| 7159 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7160 | // Don't allow returning a objc interface by value. |
| 7161 | if (RetTy->isObjCObjectType()) { |
| 7162 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 7163 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 7164 | return; |
| 7165 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7166 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7167 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7168 | // return type. TODO: what should we do with declarators like: |
| 7169 | // ^ * { ... } |
| 7170 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7171 | if (RetTy != Context.DependentTy) |
| 7172 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7173 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7174 | // Push block parameters from the declarator if we had them. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7175 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7176 | if (isa<FunctionProtoType>(T)) { |
| 7177 | FunctionProtoTypeLoc TL = cast<FunctionProtoTypeLoc>(Sig->getTypeLoc()); |
| 7178 | for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) { |
| 7179 | ParmVarDecl *Param = TL.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 7180 | if (Param->getIdentifier() == 0 && |
| 7181 | !Param->isImplicit() && |
| 7182 | !Param->isInvalidDecl() && |
| 7183 | !getLangOptions().CPlusPlus) |
| 7184 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7185 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 7186 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7187 | |
| 7188 | // Fake up parameter variables if we have a typedef, like |
| 7189 | // ^ fntype { ... } |
| 7190 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 7191 | for (FunctionProtoType::arg_type_iterator |
| 7192 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 7193 | ParmVarDecl *Param = |
| 7194 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 7195 | ParamInfo.getSourceRange().getBegin(), |
| 7196 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7197 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7198 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7199 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7200 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7201 | // Set the parameters on the block decl. |
| 7202 | if (!Params.empty()) |
| 7203 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7204 | |
| 7205 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 7206 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 7207 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7208 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7209 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 7210 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 7211 | // FIXME: remove the attribute. |
| 7212 | } |
| 7213 | |
| 7214 | // Put the parameter variables in scope. We can bail out immediately |
| 7215 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7216 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 7217 | return; |
| 7218 | |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 7219 | bool ShouldCheckShadow = |
| 7220 | Diags.getDiagnosticLevel(diag::warn_decl_shadow) != Diagnostic::Ignored; |
| 7221 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7222 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 7223 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 7224 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 7225 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7226 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 7227 | if ((*AI)->getIdentifier()) { |
| 7228 | if (ShouldCheckShadow) |
| 7229 | CheckShadow(CurBlock->TheScope, *AI); |
| 7230 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7231 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 7232 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 7233 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7234 | } |
| 7235 | |
| 7236 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 7237 | /// is invoked to pop the information about the block from the action impl. |
| 7238 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7239 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 7240 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7241 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7242 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7243 | } |
| 7244 | |
| 7245 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 7246 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7247 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7248 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 7249 | // If blocks are disabled, emit an error. |
| 7250 | if (!LangOpts.Blocks) |
| 7251 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7252 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7253 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 7254 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 7255 | PopDeclContext(); |
| 7256 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7257 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 7258 | if (!BSI->ReturnType.isNull()) |
| 7259 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7260 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 7261 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7262 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7263 | |
| 7264 | // If the user wrote a function type in some form, try to use that. |
| 7265 | if (!BSI->FunctionType.isNull()) { |
| 7266 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 7267 | |
| 7268 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 7269 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 7270 | |
| 7271 | // Turn protoless block types into nullary block types. |
| 7272 | if (isa<FunctionNoProtoType>(FTy)) { |
| 7273 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, |
| 7274 | false, false, 0, 0, Ext); |
| 7275 | |
| 7276 | // Otherwise, if we don't need to change anything about the function type, |
| 7277 | // preserve its sugar structure. |
| 7278 | } else if (FTy->getResultType() == RetTy && |
| 7279 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 7280 | BlockTy = BSI->FunctionType; |
| 7281 | |
| 7282 | // Otherwise, make the minimal modifications to the function type. |
| 7283 | } else { |
| 7284 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
| 7285 | BlockTy = Context.getFunctionType(RetTy, |
| 7286 | FPT->arg_type_begin(), |
| 7287 | FPT->getNumArgs(), |
| 7288 | FPT->isVariadic(), |
| 7289 | /*quals*/ 0, |
| 7290 | FPT->hasExceptionSpec(), |
| 7291 | FPT->hasAnyExceptionSpec(), |
| 7292 | FPT->getNumExceptions(), |
| 7293 | FPT->exception_begin(), |
| 7294 | Ext); |
| 7295 | } |
| 7296 | |
| 7297 | // If we don't have a function type, just build one from nothing. |
| 7298 | } else { |
| 7299 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, |
| 7300 | false, false, 0, 0, |
| 7301 | FunctionType::ExtInfo(NoReturn, 0, CC_Default)); |
| 7302 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7303 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7304 | // FIXME: Check that return/parameter types are complete/non-abstract |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 7305 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 7306 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7307 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7308 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 7309 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 7310 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7311 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7312 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7313 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 7314 | |
| 7315 | bool Good = true; |
| 7316 | // Check goto/label use. |
| 7317 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 7318 | I = BSI->LabelMap.begin(), E = BSI->LabelMap.end(); I != E; ++I) { |
| 7319 | LabelStmt *L = I->second; |
| 7320 | |
| 7321 | // Verify that we have no forward references left. If so, there was a goto |
| 7322 | // or address of a label taken, but no definition of it. |
| 7323 | if (L->getSubStmt() != 0) |
| 7324 | continue; |
| 7325 | |
| 7326 | // Emit error. |
| 7327 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName(); |
| 7328 | Good = false; |
| 7329 | } |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7330 | if (!Good) { |
| 7331 | PopFunctionOrBlockScope(); |
Mike Stump | 314825b | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 7332 | return ExprError(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7333 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7334 | |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 7335 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 7336 | BSI->hasBlockDeclRefExprs); |
| 7337 | |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 7338 | // Issue any analysis-based warnings. |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 7339 | const sema::AnalysisBasedWarnings::Policy &WP = |
| 7340 | AnalysisWarnings.getDefaultPolicy(); |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 7341 | AnalysisWarnings.IssueWarnings(WP, Result); |
Ted Kremenek | 918fe84 | 2010-03-20 21:06:02 +0000 | [diff] [blame] | 7342 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7343 | PopFunctionOrBlockScope(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 7344 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 7345 | } |
| 7346 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7347 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 7348 | Expr *expr, ParsedType type, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7349 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 7350 | TypeSourceInfo *TInfo; |
| 7351 | QualType T = GetTypeFromParser(type, &TInfo); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7352 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 7353 | } |
| 7354 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7355 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 7356 | Expr *E, TypeSourceInfo *TInfo, |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 7357 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 7358 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7359 | |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 7360 | InitBuiltinVaListType(); |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 7361 | |
| 7362 | // Get the va_list type |
| 7363 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 7364 | if (VaListType->isArrayType()) { |
| 7365 | // Deal with implicit array decay; for example, on x86-64, |
| 7366 | // va_list is an array, but it's supposed to decay to |
| 7367 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 7368 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 7369 | // Make sure the input expression also decays appropriately. |
| 7370 | UsualUnaryConversions(E); |
| 7371 | } else { |
| 7372 | // Otherwise, the va_list argument must be an l-value because |
| 7373 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7374 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 7375 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 7376 | return ExprError(); |
| 7377 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 7378 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 7379 | if (!E->isTypeDependent() && |
| 7380 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7381 | return ExprError(Diag(E->getLocStart(), |
| 7382 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 7383 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 7384 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7385 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7386 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 7387 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7388 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 7389 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 7390 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 7391 | } |
| 7392 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7393 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 7394 | // The type of __null will be int or long, depending on the size of |
| 7395 | // pointers on the target. |
| 7396 | QualType Ty; |
| 7397 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 7398 | Ty = Context.IntTy; |
| 7399 | else |
| 7400 | Ty = Context.LongTy; |
| 7401 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 7402 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7405 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7406 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7407 | if (!SemaRef.getLangOptions().ObjC1) |
| 7408 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7409 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7410 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 7411 | if (!PT) |
| 7412 | return; |
| 7413 | |
| 7414 | // Check if the destination is of type 'id'. |
| 7415 | if (!PT->isObjCIdType()) { |
| 7416 | // Check if the destination is the 'NSString' interface. |
| 7417 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 7418 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 7419 | return; |
| 7420 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7421 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7422 | // Strip off any parens and casts. |
| 7423 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 7424 | if (!SL || SL->isWide()) |
| 7425 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7426 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7427 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7428 | } |
| 7429 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7430 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 7431 | SourceLocation Loc, |
| 7432 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7433 | Expr *SrcExpr, AssignmentAction Action, |
| 7434 | bool *Complained) { |
| 7435 | if (Complained) |
| 7436 | *Complained = false; |
| 7437 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7438 | // Decode the result (notice that AST's are still created for extensions). |
| 7439 | bool isInvalid = false; |
| 7440 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7441 | FixItHint Hint; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7442 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7443 | switch (ConvTy) { |
| 7444 | default: assert(0 && "Unknown conversion type"); |
| 7445 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 7446 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7447 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 7448 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 7449 | case IntToPointer: |
| 7450 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 7451 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7452 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7453 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7454 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 7455 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 7456 | case IncompatiblePointerSign: |
| 7457 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 7458 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7459 | case FunctionVoidPointer: |
| 7460 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 7461 | break; |
| 7462 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 7463 | // If the qualifiers lost were because we were applying the |
| 7464 | // (deprecated) C++ conversion from a string literal to a char* |
| 7465 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 7466 | // Ideally, this check would be performed in |
| 7467 | // CheckPointerTypesForAssignment. However, that would require a |
| 7468 | // bit of refactoring (so that the second argument is an |
| 7469 | // expression, rather than a type), which should be done as part |
| 7470 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 7471 | // C++ semantics. |
| 7472 | if (getLangOptions().CPlusPlus && |
| 7473 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 7474 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7475 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 7476 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 7477 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 7478 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 7479 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7480 | case IntToBlockPointer: |
| 7481 | DiagKind = diag::err_int_to_block_pointer; |
| 7482 | break; |
| 7483 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 7484 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7485 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7486 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7487 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7488 | // it can give a more specific diagnostic. |
| 7489 | DiagKind = diag::warn_incompatible_qualified_id; |
| 7490 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7491 | case IncompatibleVectors: |
| 7492 | DiagKind = diag::warn_incompatible_vectors; |
| 7493 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7494 | case Incompatible: |
| 7495 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 7496 | isInvalid = true; |
| 7497 | break; |
| 7498 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7499 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 7500 | QualType FirstType, SecondType; |
| 7501 | switch (Action) { |
| 7502 | case AA_Assigning: |
| 7503 | case AA_Initializing: |
| 7504 | // The destination type comes first. |
| 7505 | FirstType = DstType; |
| 7506 | SecondType = SrcType; |
| 7507 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7508 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 7509 | case AA_Returning: |
| 7510 | case AA_Passing: |
| 7511 | case AA_Converting: |
| 7512 | case AA_Sending: |
| 7513 | case AA_Casting: |
| 7514 | // The source type comes first. |
| 7515 | FirstType = SrcType; |
| 7516 | SecondType = DstType; |
| 7517 | break; |
| 7518 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7519 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 7520 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7521 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 7522 | if (Complained) |
| 7523 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7524 | return isInvalid; |
| 7525 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7526 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 7527 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7528 | llvm::APSInt ICEResult; |
| 7529 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 7530 | if (Result) |
| 7531 | *Result = ICEResult; |
| 7532 | return false; |
| 7533 | } |
| 7534 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7535 | Expr::EvalResult EvalResult; |
| 7536 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7537 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7538 | EvalResult.HasSideEffects) { |
| 7539 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 7540 | |
| 7541 | if (EvalResult.Diag) { |
| 7542 | // We only show the note if it's not the usual "invalid subexpression" |
| 7543 | // or if it's actually in a subexpression. |
| 7544 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 7545 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 7546 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 7547 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7548 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7549 | return true; |
| 7550 | } |
| 7551 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7552 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 7553 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7554 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7555 | if (EvalResult.Diag && |
| 7556 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 7557 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7558 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7559 | if (Result) |
| 7560 | *Result = EvalResult.Val.getInt(); |
| 7561 | return false; |
| 7562 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7563 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7564 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7565 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7566 | ExprEvalContexts.push_back( |
| 7567 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7568 | } |
| 7569 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7570 | void |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7571 | Sema::PopExpressionEvaluationContext() { |
| 7572 | // Pop the current expression evaluation context off the stack. |
| 7573 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 7574 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7575 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 7576 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 7577 | if (Rec.PotentiallyReferenced) { |
| 7578 | // Mark any remaining declarations in the current position of the stack |
| 7579 | // as "referenced". If they were not meant to be referenced, semantic |
| 7580 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7581 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 7582 | I = Rec.PotentiallyReferenced->begin(), |
| 7583 | IEnd = Rec.PotentiallyReferenced->end(); |
| 7584 | I != IEnd; ++I) |
| 7585 | MarkDeclarationReferenced(I->first, I->second); |
| 7586 | } |
| 7587 | |
| 7588 | if (Rec.PotentiallyDiagnosed) { |
| 7589 | // Emit any pending diagnostics. |
| 7590 | for (PotentiallyEmittedDiagnostics::iterator |
| 7591 | I = Rec.PotentiallyDiagnosed->begin(), |
| 7592 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 7593 | I != IEnd; ++I) |
| 7594 | Diag(I->first, I->second); |
| 7595 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7596 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7597 | |
| 7598 | // When are coming out of an unevaluated context, clear out any |
| 7599 | // temporaries that we may have created as part of the evaluation of |
| 7600 | // the expression in that context: they aren't relevant because they |
| 7601 | // will never be constructed. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7602 | if (Rec.Context == Unevaluated && |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7603 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 7604 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 7605 | ExprTemporaries.end()); |
| 7606 | |
| 7607 | // Destroy the popped expression evaluation record. |
| 7608 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7609 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7610 | |
| 7611 | /// \brief Note that the given declaration was referenced in the source code. |
| 7612 | /// |
| 7613 | /// This routine should be invoke whenever a given declaration is referenced |
| 7614 | /// in the source code, and where that reference occurred. If this declaration |
| 7615 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 7616 | /// C99 6.9p3), then the declaration will be marked as used. |
| 7617 | /// |
| 7618 | /// \param Loc the location where the declaration was referenced. |
| 7619 | /// |
| 7620 | /// \param D the declaration that has been referenced by the source code. |
| 7621 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 7622 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7623 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 7624 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7625 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7626 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 7627 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 7628 | // template or not. The reason for this is that unevaluated expressions |
| 7629 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 7630 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7631 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 7632 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7633 | D->setUsed(true); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 7634 | return; |
| 7635 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7636 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 7637 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 7638 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7639 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7640 | // Do not mark anything as "used" within a dependent context; wait for |
| 7641 | // an instantiation. |
| 7642 | if (CurContext->isDependentContext()) |
| 7643 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7644 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7645 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7646 | case Unevaluated: |
| 7647 | // We are in an expression that is not potentially evaluated; do nothing. |
| 7648 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7649 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7650 | case PotentiallyEvaluated: |
| 7651 | // We are in a potentially-evaluated expression, so this declaration is |
| 7652 | // "used"; handle this below. |
| 7653 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7654 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7655 | case PotentiallyPotentiallyEvaluated: |
| 7656 | // We are in an expression that may be potentially evaluated; queue this |
| 7657 | // declaration reference until we know whether the expression is |
| 7658 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7659 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7660 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7661 | |
| 7662 | case PotentiallyEvaluatedIfUsed: |
| 7663 | // Referenced declarations will only be used if the construct in the |
| 7664 | // containing expression is used. |
| 7665 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7666 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7667 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7668 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 7669 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7670 | unsigned TypeQuals; |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7671 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 7672 | if (Constructor->getParent()->hasTrivialConstructor()) |
| 7673 | return; |
| 7674 | if (!Constructor->isUsed(false)) |
| 7675 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7676 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 507eb87 | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 7677 | Constructor->isCopyConstructor(TypeQuals)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 7678 | if (!Constructor->isUsed(false)) |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7679 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7680 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7681 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7682 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7683 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 7684 | if (Destructor->isImplicit() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7685 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7686 | if (Destructor->isVirtual()) |
| 7687 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7688 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7689 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7690 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 7691 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 7692 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 7693 | } else if (MethodDecl->isVirtual()) |
| 7694 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7695 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7696 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7697 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7698 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 7699 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7700 | bool AlreadyInstantiated = false; |
| 7701 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7702 | = Function->getTemplateSpecializationInfo()) { |
| 7703 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7704 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7705 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7706 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7707 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7708 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7709 | = Function->getMemberSpecializationInfo()) { |
| 7710 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7711 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7712 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7713 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7714 | AlreadyInstantiated = true; |
| 7715 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7716 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 7717 | if (!AlreadyInstantiated) { |
| 7718 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 7719 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 7720 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 7721 | Loc)); |
| 7722 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 7723 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 7724 | } |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 7725 | } else // Walk redefinitions, as some of them may be instantiable. |
| 7726 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 7727 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 7728 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 7729 | MarkDeclarationReferenced(Loc, *i); |
| 7730 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7731 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7732 | // FIXME: keep track of references to static functions |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 7733 | |
| 7734 | // Recursive functions should be marked when used from another function. |
| 7735 | if (CurContext != Function) |
| 7736 | Function->setUsed(true); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7737 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7738 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7739 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7740 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7741 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7742 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7743 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7744 | Var->getInstantiatedFromStaticDataMember()) { |
| 7745 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7746 | assert(MSInfo && "Missing member specialization information?"); |
| 7747 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7748 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7749 | MSInfo->setPointOfInstantiation(Loc); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 7750 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7751 | } |
| 7752 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7753 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7754 | // FIXME: keep track of references to static data? |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7755 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7756 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7757 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7758 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7759 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7760 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7761 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7762 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7763 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7764 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7765 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 7766 | Sema &S; |
| 7767 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7768 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7769 | public: |
| 7770 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7771 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7772 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7773 | |
| 7774 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 7775 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7776 | }; |
| 7777 | } |
| 7778 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7779 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 7780 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7781 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 7782 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 7783 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7784 | |
| 7785 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7786 | } |
| 7787 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7788 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7789 | if (ClassTemplateSpecializationDecl *Spec |
| 7790 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 7791 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7792 | return TraverseTemplateArguments(Args.getFlatArgumentList(), |
| 7793 | Args.flat_size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7794 | } |
| 7795 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 7796 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7797 | } |
| 7798 | |
| 7799 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 7800 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 7801 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 7802 | } |
| 7803 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7804 | namespace { |
| 7805 | /// \brief Helper class that marks all of the declarations referenced by |
| 7806 | /// potentially-evaluated subexpressions as "referenced". |
| 7807 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 7808 | Sema &S; |
| 7809 | |
| 7810 | public: |
| 7811 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 7812 | |
| 7813 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 7814 | |
| 7815 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 7816 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 7817 | } |
| 7818 | |
| 7819 | void VisitMemberExpr(MemberExpr *E) { |
| 7820 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame^] | 7821 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7822 | } |
| 7823 | |
| 7824 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 7825 | if (E->getConstructor()) |
| 7826 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 7827 | if (E->getOperatorNew()) |
| 7828 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 7829 | if (E->getOperatorDelete()) |
| 7830 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame^] | 7831 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 7835 | if (E->getOperatorDelete()) |
| 7836 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame^] | 7837 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7838 | } |
| 7839 | |
| 7840 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 7841 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame^] | 7842 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7843 | } |
| 7844 | |
| 7845 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 7846 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 7847 | } |
| 7848 | }; |
| 7849 | } |
| 7850 | |
| 7851 | /// \brief Mark any declarations that appear within this expression or any |
| 7852 | /// potentially-evaluated subexpressions as "referenced". |
| 7853 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 7854 | EvaluatedExprMarker(*this).Visit(E); |
| 7855 | } |
| 7856 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7857 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 7858 | /// of the program being compiled. |
| 7859 | /// |
| 7860 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7861 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7862 | /// possibility that the code will actually be executable. Code in sizeof() |
| 7863 | /// expressions, code used only during overload resolution, etc., are not |
| 7864 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 7865 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7866 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7867 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7868 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7869 | /// This routine should be used for all diagnostics that describe the run-time |
| 7870 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 7871 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 7872 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7873 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7874 | const PartialDiagnostic &PD) { |
| 7875 | switch (ExprEvalContexts.back().Context ) { |
| 7876 | case Unevaluated: |
| 7877 | // The argument will never be evaluated, so don't complain. |
| 7878 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7879 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7880 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 7881 | case PotentiallyEvaluatedIfUsed: |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7882 | Diag(Loc, PD); |
| 7883 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7884 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7885 | case PotentiallyPotentiallyEvaluated: |
| 7886 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 7887 | break; |
| 7888 | } |
| 7889 | |
| 7890 | return false; |
| 7891 | } |
| 7892 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7893 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7894 | CallExpr *CE, FunctionDecl *FD) { |
| 7895 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7896 | return false; |
| 7897 | |
| 7898 | PartialDiagnostic Note = |
| 7899 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7900 | << FD->getDeclName() : PDiag(); |
| 7901 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7902 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7903 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7904 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7905 | PDiag(diag::err_call_function_incomplete_return) |
| 7906 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7907 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7908 | << CE->getSourceRange(), |
| 7909 | std::make_pair(NoteLoc, Note))) |
| 7910 | return true; |
| 7911 | |
| 7912 | return false; |
| 7913 | } |
| 7914 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7915 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7916 | // will prevent this condition from triggering, which is what we want. |
| 7917 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7918 | SourceLocation Loc; |
| 7919 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7920 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7921 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7922 | if (isa<BinaryOperator>(E)) { |
| 7923 | BinaryOperator *Op = cast<BinaryOperator>(E); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7924 | if (Op->getOpcode() != BO_Assign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7925 | return; |
| 7926 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7927 | // Greylist some idioms by putting them into a warning subcategory. |
| 7928 | if (ObjCMessageExpr *ME |
| 7929 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7930 | Selector Sel = ME->getSelector(); |
| 7931 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7932 | // self = [<foo> init...] |
| 7933 | if (isSelfExpr(Op->getLHS()) |
| 7934 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7935 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7936 | |
| 7937 | // <foo> = [<bar> nextObject] |
| 7938 | else if (Sel.isUnarySelector() && |
| 7939 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7940 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7941 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7942 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7943 | Loc = Op->getOperatorLoc(); |
| 7944 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7945 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7946 | if (Op->getOperator() != OO_Equal) |
| 7947 | return; |
| 7948 | |
| 7949 | Loc = Op->getOperatorLoc(); |
| 7950 | } else { |
| 7951 | // Not an assignment. |
| 7952 | return; |
| 7953 | } |
| 7954 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7955 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | e724ae9 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7956 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7957 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7958 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7959 | Diag(Loc, diag::note_condition_assign_to_comparison) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 7960 | << FixItHint::CreateReplacement(Loc, "=="); |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7961 | Diag(Loc, diag::note_condition_assign_silence) |
| 7962 | << FixItHint::CreateInsertion(Open, "(") |
| 7963 | << FixItHint::CreateInsertion(Close, ")"); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7964 | } |
| 7965 | |
| 7966 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7967 | DiagnoseAssignmentAsCondition(E); |
| 7968 | |
| 7969 | if (!E->isTypeDependent()) { |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 7970 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7971 | |
| 7972 | QualType T = E->getType(); |
| 7973 | |
| 7974 | if (getLangOptions().CPlusPlus) { |
| 7975 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7976 | return true; |
| 7977 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7978 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7979 | << T << E->getSourceRange(); |
| 7980 | return true; |
| 7981 | } |
| 7982 | } |
| 7983 | |
| 7984 | return false; |
| 7985 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 7986 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7987 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 7988 | Expr *Sub) { |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 7989 | if (!Sub) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 7990 | return ExprError(); |
| 7991 | |
Douglas Gregor | b412e17 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 7992 | if (CheckBooleanCondition(Sub, Loc)) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 7993 | return ExprError(); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 7994 | |
| 7995 | return Owned(Sub); |
| 7996 | } |