Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 15 | #include "SemaInit.h" |
John McCall | 7d384dd | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 16 | #include "Lookup.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/AnalysisContext.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 26 | #include "clang/Lex/LiteralSupport.h" |
| 27 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 28 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 29 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 30 | #include "clang/Parse/Scope.h" |
Douglas Gregor | 314b97f | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 31 | #include "clang/Parse/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 34 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 35 | /// \brief Determine whether the use of this declaration is valid, and |
| 36 | /// emit any corresponding diagnostics. |
| 37 | /// |
| 38 | /// This routine diagnoses various problems with referencing |
| 39 | /// declarations that can occur when using a declaration. For example, |
| 40 | /// it might warn if a deprecated or unavailable declaration is being |
| 41 | /// used, or produce an error (and return true) if a C++0x deleted |
| 42 | /// function is being used. |
| 43 | /// |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 44 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 45 | /// decls. |
| 46 | /// |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 47 | /// \returns true if there was an error (this declaration cannot be |
| 48 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 49 | /// |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 50 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 51 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 52 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 53 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Chris Lattner | ffb9368 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 56 | // See if the decl is unavailable |
| 57 | if (D->getAttr<UnavailableAttr>()) { |
| 58 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 59 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 60 | } |
| 61 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 62 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 63 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 64 | if (FD->isDeleted()) { |
| 65 | Diag(Loc, diag::err_deleted_function_use); |
| 66 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 67 | return true; |
| 68 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 69 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 70 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 71 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 74 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 76 | /// attribute. It warns if call does not have the sentinel argument. |
| 77 | /// |
| 78 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 80 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 82 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 83 | int sentinelPos = attr->getSentinel(); |
| 84 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 86 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 87 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 88 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 89 | bool warnNotEnoughArgs = false; |
| 90 | int isMethod = 0; |
| 91 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 92 | // skip over named parameters. |
| 93 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 94 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 95 | if (nullPos) |
| 96 | --nullPos; |
| 97 | else |
| 98 | ++i; |
| 99 | } |
| 100 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 101 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 102 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 103 | // skip over named parameters. |
| 104 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 105 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 106 | if (nullPos) |
| 107 | --nullPos; |
| 108 | else |
| 109 | ++i; |
| 110 | } |
| 111 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 112 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 113 | // block or function pointer call. |
| 114 | QualType Ty = V->getType(); |
| 115 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 117 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 118 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 119 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 120 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 121 | unsigned k; |
| 122 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 123 | if (nullPos) |
| 124 | --nullPos; |
| 125 | else |
| 126 | ++i; |
| 127 | } |
| 128 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 129 | } |
| 130 | if (Ty->isBlockPointerType()) |
| 131 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 132 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 133 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 134 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 135 | return; |
| 136 | |
| 137 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 138 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 139 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 140 | return; |
| 141 | } |
| 142 | int sentinel = i; |
| 143 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 144 | --sentinelPos; |
| 145 | ++i; |
| 146 | } |
| 147 | if (sentinelPos > 0) { |
| 148 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 149 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 150 | return; |
| 151 | } |
| 152 | while (i < NumArgs-1) { |
| 153 | ++i; |
| 154 | ++sentinel; |
| 155 | } |
| 156 | Expr *sentinelExpr = Args[sentinel]; |
Anders Carlsson | e4d2bdd | 2009-11-24 17:24:21 +0000 | [diff] [blame] | 157 | if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) && |
| 158 | (!sentinelExpr->getType()->isPointerType() || |
| 159 | !sentinelExpr->isNullPointerConstant(Context, |
| 160 | Expr::NPC_ValueDependentIsNull)))) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 161 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 162 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 163 | } |
| 164 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 167 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 168 | Expr *Ex = (Expr *)E; |
| 169 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 170 | } |
| 171 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 172 | //===----------------------------------------------------------------------===// |
| 173 | // Standard Promotions and Conversions |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 176 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 177 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 178 | QualType Ty = E->getType(); |
| 179 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 180 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 181 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 183 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 184 | else if (Ty->isArrayType()) { |
| 185 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 186 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 187 | // type 'array of type' is converted to an expression that has type 'pointer |
| 188 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 189 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 190 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 191 | // |
| 192 | // C++ 4.2p1: |
| 193 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 194 | // T" can be converted to an rvalue of type "pointer to T". |
| 195 | // |
| 196 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 197 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 198 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 199 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 203 | void Sema::DefaultFunctionArrayLvalueConversion(Expr *&E) { |
| 204 | DefaultFunctionArrayConversion(E); |
| 205 | |
| 206 | QualType Ty = E->getType(); |
| 207 | assert(!Ty.isNull() && "DefaultFunctionArrayLvalueConversion - missing type"); |
| 208 | if (!Ty->isDependentType() && Ty.hasQualifiers() && |
| 209 | (!getLangOptions().CPlusPlus || !Ty->isRecordType()) && |
| 210 | E->isLvalue(Context) == Expr::LV_Valid) { |
| 211 | // C++ [conv.lval]p1: |
| 212 | // [...] If T is a non-class type, the type of the rvalue is the |
| 213 | // cv-unqualified version of T. Otherwise, the type of the |
| 214 | // rvalue is T |
| 215 | // |
| 216 | // C99 6.3.2.1p2: |
| 217 | // If the lvalue has qualified type, the value has the unqualified |
| 218 | // version of the type of the lvalue; otherwise, the value has the |
| 219 | // type of the lvalue. |
| 220 | ImpCastExprToType(E, Ty.getUnqualifiedType(), CastExpr::CK_NoOp); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 225 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 227 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 228 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 229 | /// In these instances, this routine should *not* be called. |
| 230 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 231 | QualType Ty = Expr->getType(); |
| 232 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 233 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 234 | // C99 6.3.1.1p2: |
| 235 | // |
| 236 | // The following may be used in an expression wherever an int or |
| 237 | // unsigned int may be used: |
| 238 | // - an object or expression with an integer type whose integer |
| 239 | // conversion rank is less than or equal to the rank of int |
| 240 | // and unsigned int. |
| 241 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 242 | // |
| 243 | // If an int can represent all values of the original type, the |
| 244 | // value is converted to an int; otherwise, it is converted to an |
| 245 | // unsigned int. These are called the integer promotions. All |
| 246 | // other types are unchanged by the integer promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 247 | QualType PTy = Context.isPromotableBitField(Expr); |
| 248 | if (!PTy.isNull()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 249 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 250 | return Expr; |
| 251 | } |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 252 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 253 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 254 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 255 | return Expr; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 258 | DefaultFunctionArrayLvalueConversion(Expr); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | return Expr; |
| 260 | } |
| 261 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 262 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 264 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 265 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 266 | QualType Ty = Expr->getType(); |
| 267 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 269 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 270 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 271 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 272 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 273 | CastExpr::CK_FloatingCast); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 275 | UsualUnaryConversions(Expr); |
| 276 | } |
| 277 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 278 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 279 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 280 | /// interfaces passed by value. This returns true if the argument type is |
| 281 | /// completely illegal. |
| 282 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 283 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 285 | if (Expr->getType()->isObjCInterfaceType() && |
| 286 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 287 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 288 | << Expr->getType() << CT)) |
| 289 | return true; |
Douglas Gregor | 75b699a | 2009-12-12 07:25:49 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 291 | if (!Expr->getType()->isPODType() && |
| 292 | DiagRuntimeBehavior(Expr->getLocStart(), |
| 293 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 294 | << Expr->getType() << CT)) |
| 295 | return true; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 296 | |
| 297 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 301 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 302 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 304 | /// responsible for emitting appropriate error diagnostics. |
| 305 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 306 | /// GCC. |
| 307 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 308 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 309 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 310 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 311 | |
| 312 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 315 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 316 | QualType lhs = |
| 317 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 319 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 320 | |
| 321 | // If both types are identical, no conversion is needed. |
| 322 | if (lhs == rhs) |
| 323 | return lhs; |
| 324 | |
| 325 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 326 | // The caller can deal with this (e.g. pointer + int). |
| 327 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 328 | return lhs; |
| 329 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 330 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 331 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 332 | if (!LHSBitfieldPromoteTy.isNull()) |
| 333 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 334 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 335 | if (!RHSBitfieldPromoteTy.isNull()) |
| 336 | rhs = RHSBitfieldPromoteTy; |
| 337 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 338 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 339 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 340 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 341 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 342 | return destType; |
| 343 | } |
| 344 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 345 | //===----------------------------------------------------------------------===// |
| 346 | // Semantic Analysis for various Expression Types |
| 347 | //===----------------------------------------------------------------------===// |
| 348 | |
| 349 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 350 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 351 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 352 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 353 | /// multiple tokens. However, the common case is that StringToks points to one |
| 354 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 355 | /// |
| 356 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 357 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | assert(NumStringToks && "Must have at least one string!"); |
| 359 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 360 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 361 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 362 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | |
| 364 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 365 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 366 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 367 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 368 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 369 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 370 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 371 | |
| 372 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 373 | if (getLangOptions().CPlusPlus) |
| 374 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 375 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 376 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 377 | // the nul terminator character as well as the string length for pascal |
| 378 | // strings. |
| 379 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 380 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 381 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 383 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 385 | Literal.GetStringLength(), |
| 386 | Literal.AnyWide, StrTy, |
| 387 | &StringTokLocs[0], |
| 388 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 391 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 392 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 393 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 394 | /// for values inside the block or for globals). |
| 395 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 396 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 397 | /// up-to-date. |
| 398 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 399 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 400 | ValueDecl *VD) { |
| 401 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 402 | // we wanted to. |
| 403 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 404 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 406 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 407 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 408 | return false; |
| 409 | |
| 410 | // If this is a reference to an extern, static, or global variable, no need to |
| 411 | // snapshot it. |
| 412 | // FIXME: What about 'const' variables in C++? |
| 413 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 414 | if (!Var->hasLocalStorage()) |
| 415 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 417 | // Blocks that have these can't be constant. |
| 418 | CurBlock->hasBlockDeclRefExprs = true; |
| 419 | |
| 420 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 421 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 422 | // be defined outside all of the current blocks (in which case the blocks do |
| 423 | // all get the bit). Walk the nesting chain. |
| 424 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 425 | NextBlock = NextBlock->PrevBlockInfo) { |
| 426 | // If we found the defining block for the variable, don't mark the block as |
| 427 | // having a reference outside it. |
| 428 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 429 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 431 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 432 | // a snapshot as well. |
| 433 | NextBlock->hasBlockDeclRefExprs = true; |
| 434 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 436 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 439 | |
| 440 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 441 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 442 | Sema::OwningExprResult |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 443 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 444 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 445 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 446 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 448 | << D->getDeclName(); |
| 449 | return ExprError(); |
| 450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 452 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 453 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 454 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 455 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 457 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 459 | << D->getIdentifier(); |
| 460 | return ExprError(); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 466 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 468 | return Owned(DeclRefExpr::Create(Context, |
| 469 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 470 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 471 | D, Loc, Ty)); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 474 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 475 | /// variable corresponding to the anonymous union or struct whose type |
| 476 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 477 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 478 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 480 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 482 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 483 | // operation rather than a slow walk through DeclContext's vector (which |
| 484 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 485 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 486 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 487 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 488 | D != DEnd; ++D) { |
| 489 | if (*D == Record) { |
| 490 | // The object for the anonymous struct/union directly |
| 491 | // follows its type in the list of declarations. |
| 492 | ++D; |
| 493 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 494 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 495 | return *D; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | assert(false && "Missing object for anonymous record"); |
| 500 | return 0; |
| 501 | } |
| 502 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 503 | /// \brief Given a field that represents a member of an anonymous |
| 504 | /// struct/union, build the path from that field's context to the |
| 505 | /// actual member. |
| 506 | /// |
| 507 | /// Construct the sequence of field member references we'll have to |
| 508 | /// perform to get to the field in the anonymous union/struct. The |
| 509 | /// list of members is built from the field outward, so traverse it |
| 510 | /// backwards to go from an object in the current context to the field |
| 511 | /// we found. |
| 512 | /// |
| 513 | /// \returns The variable from which the field access should begin, |
| 514 | /// for an anonymous struct/union that is not a member of another |
| 515 | /// class. Otherwise, returns NULL. |
| 516 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 517 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 518 | assert(Field->getDeclContext()->isRecord() && |
| 519 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 520 | && "Field must be stored inside an anonymous struct or union"); |
| 521 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 522 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 523 | VarDecl *BaseObject = 0; |
| 524 | DeclContext *Ctx = Field->getDeclContext(); |
| 525 | do { |
| 526 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 527 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 528 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 529 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 530 | else { |
| 531 | BaseObject = cast<VarDecl>(AnonObject); |
| 532 | break; |
| 533 | } |
| 534 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 536 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 537 | |
| 538 | return BaseObject; |
| 539 | } |
| 540 | |
| 541 | Sema::OwningExprResult |
| 542 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 543 | FieldDecl *Field, |
| 544 | Expr *BaseObjectExpr, |
| 545 | SourceLocation OpLoc) { |
| 546 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 548 | AnonFields); |
| 549 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 550 | // Build the expression that refers to the base object, from |
| 551 | // which we will build a sequence of member references to each |
| 552 | // of the anonymous union objects and, eventually, the field we |
| 553 | // found via name lookup. |
| 554 | bool BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 555 | Qualifiers BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 556 | if (BaseObject) { |
| 557 | // BaseObject is an anonymous struct/union variable (and is, |
| 558 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 559 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 560 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 561 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 562 | SourceLocation()); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 563 | BaseQuals |
| 564 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 565 | } else if (BaseObjectExpr) { |
| 566 | // The caller provided the base object expression. Determine |
| 567 | // whether its a pointer and whether it adds any qualifiers to the |
| 568 | // anonymous struct/union fields we're looking into. |
| 569 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 570 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 571 | BaseObjectIsPointer = true; |
| 572 | ObjectType = ObjectPtr->getPointeeType(); |
| 573 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 574 | BaseQuals |
| 575 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 576 | } else { |
| 577 | // We've found a member of an anonymous struct/union that is |
| 578 | // inside a non-anonymous struct/union, so in a well-formed |
| 579 | // program our base object expression is "this". |
| 580 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 581 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 583 | = Context.getTagDeclType( |
| 584 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 585 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 587 | == Context.getCanonicalType(ThisType)) || |
| 588 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 589 | // Our base object expression is "this". |
Douglas Gregor | 8aa5f40 | 2009-12-24 20:23:34 +0000 | [diff] [blame] | 590 | BaseObjectExpr = new (Context) CXXThisExpr(Loc, |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 591 | MD->getThisType(Context), |
| 592 | /*isImplicit=*/true); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 593 | BaseObjectIsPointer = true; |
| 594 | } |
| 595 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 596 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 597 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 598 | } |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 599 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 602 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 603 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 604 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | // Build the implicit member references to the field of the |
| 608 | // anonymous struct/union. |
| 609 | Expr *Result = BaseObjectExpr; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 610 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 611 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 612 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 613 | FI != FIEnd; ++FI) { |
| 614 | QualType MemberType = (*FI)->getType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 615 | Qualifiers MemberTypeQuals = |
| 616 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 617 | |
| 618 | // CVR attributes from the base are picked up by members, |
| 619 | // except that 'mutable' members don't pick up 'const'. |
| 620 | if ((*FI)->isMutable()) |
| 621 | ResultQuals.removeConst(); |
| 622 | |
| 623 | // GC attributes are never picked up by members. |
| 624 | ResultQuals.removeObjCGCAttr(); |
| 625 | |
| 626 | // TR 18037 does not allow fields to be declared with address spaces. |
| 627 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 628 | |
| 629 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 630 | if (NewQuals != MemberTypeQuals) |
| 631 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 632 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 633 | MarkDeclarationReferenced(Loc, *FI); |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 634 | PerformObjectMemberConversion(Result, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 635 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 636 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 637 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 638 | BaseObjectIsPointer = false; |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 639 | ResultQuals = NewQuals; |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 642 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | } |
| 644 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 645 | /// Decomposes the given name into a DeclarationName, its location, and |
| 646 | /// possibly a list of template arguments. |
| 647 | /// |
| 648 | /// If this produces template arguments, it is permitted to call |
| 649 | /// DecomposeTemplateName. |
| 650 | /// |
| 651 | /// This actually loses a lot of source location information for |
| 652 | /// non-standard name kinds; we should consider preserving that in |
| 653 | /// some way. |
| 654 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 655 | const UnqualifiedId &Id, |
| 656 | TemplateArgumentListInfo &Buffer, |
| 657 | DeclarationName &Name, |
| 658 | SourceLocation &NameLoc, |
| 659 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 660 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 661 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 662 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 663 | |
| 664 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 665 | Id.TemplateId->getTemplateArgs(), |
| 666 | Id.TemplateId->NumArgs); |
| 667 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 668 | TemplateArgsPtr.release(); |
| 669 | |
| 670 | TemplateName TName = |
| 671 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 672 | |
| 673 | Name = SemaRef.Context.getNameForTemplate(TName); |
| 674 | NameLoc = Id.TemplateId->TemplateNameLoc; |
| 675 | TemplateArgs = &Buffer; |
| 676 | } else { |
| 677 | Name = SemaRef.GetNameFromUnqualifiedId(Id); |
| 678 | NameLoc = Id.StartLocation; |
| 679 | TemplateArgs = 0; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /// Decompose the given template name into a list of lookup results. |
| 684 | /// |
| 685 | /// The unqualified ID must name a non-dependent template, which can |
| 686 | /// be more easily tested by checking whether DecomposeUnqualifiedId |
| 687 | /// found template arguments. |
| 688 | static void DecomposeTemplateName(LookupResult &R, const UnqualifiedId &Id) { |
| 689 | assert(Id.getKind() == UnqualifiedId::IK_TemplateId); |
| 690 | TemplateName TName = |
| 691 | Sema::TemplateTy::make(Id.TemplateId->Template).getAsVal<TemplateName>(); |
| 692 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 693 | if (TemplateDecl *TD = TName.getAsTemplateDecl()) |
| 694 | R.addDecl(TD); |
John McCall | 0bd6feb | 2009-12-02 08:04:21 +0000 | [diff] [blame] | 695 | else if (OverloadedTemplateStorage *OT = TName.getAsOverloadedTemplate()) |
| 696 | for (OverloadedTemplateStorage::iterator I = OT->begin(), E = OT->end(); |
| 697 | I != E; ++I) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 698 | R.addDecl(*I); |
John McCall | b681b61 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 699 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 700 | R.resolveKind(); |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 701 | } |
| 702 | |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 703 | /// Determines whether the given record is "fully-formed" at the given |
| 704 | /// location, i.e. whether a qualified lookup into it is assured of |
| 705 | /// getting consistent results already. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 706 | static bool IsFullyFormedScope(Sema &SemaRef, CXXRecordDecl *Record) { |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 707 | if (!Record->hasDefinition()) |
| 708 | return false; |
| 709 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 710 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 711 | E = Record->bases_end(); I != E; ++I) { |
| 712 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 713 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 714 | if (!BaseRT) return false; |
| 715 | |
| 716 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 4c72d3e | 2010-02-08 19:26:07 +0000 | [diff] [blame] | 717 | if (!BaseRecord->hasDefinition() || |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 718 | !IsFullyFormedScope(SemaRef, BaseRecord)) |
| 719 | return false; |
| 720 | } |
| 721 | |
| 722 | return true; |
| 723 | } |
| 724 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 725 | /// Determines whether we can lookup this id-expression now or whether |
| 726 | /// we have to wait until template instantiation is complete. |
| 727 | static bool IsDependentIdExpression(Sema &SemaRef, const CXXScopeSpec &SS) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 728 | DeclContext *DC = SemaRef.computeDeclContext(SS, false); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 729 | |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 730 | // If the qualifier scope isn't computable, it's definitely dependent. |
| 731 | if (!DC) return true; |
| 732 | |
| 733 | // If the qualifier scope doesn't name a record, we can always look into it. |
| 734 | if (!isa<CXXRecordDecl>(DC)) return false; |
| 735 | |
| 736 | // We can't look into record types unless they're fully-formed. |
| 737 | if (!IsFullyFormedScope(SemaRef, cast<CXXRecordDecl>(DC))) return true; |
| 738 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
John McCall | e1599ce | 2009-11-30 23:50:49 +0000 | [diff] [blame] | 741 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 742 | /// Determines if the given class is provably not derived from all of |
| 743 | /// the prospective base classes. |
| 744 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 745 | CXXRecordDecl *Record, |
| 746 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 747 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 748 | return false; |
| 749 | |
Douglas Gregor | 952b017 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 750 | RecordDecl *RD = Record->getDefinition(); |
John McCall | b1b4256 | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 751 | if (!RD) return false; |
| 752 | Record = cast<CXXRecordDecl>(RD); |
| 753 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 754 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 755 | E = Record->bases_end(); I != E; ++I) { |
| 756 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 757 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 758 | if (!BaseRT) return false; |
| 759 | |
| 760 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 761 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 762 | return false; |
| 763 | } |
| 764 | |
| 765 | return true; |
| 766 | } |
| 767 | |
John McCall | 144238e | 2009-12-02 20:26:00 +0000 | [diff] [blame] | 768 | /// Determines if this is an instance member of a class. |
| 769 | static bool IsInstanceMember(NamedDecl *D) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 770 | assert(D->isCXXClassMember() && |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 771 | "checking whether non-member is instance member"); |
| 772 | |
| 773 | if (isa<FieldDecl>(D)) return true; |
| 774 | |
| 775 | if (isa<CXXMethodDecl>(D)) |
| 776 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 777 | |
| 778 | if (isa<FunctionTemplateDecl>(D)) { |
| 779 | D = cast<FunctionTemplateDecl>(D)->getTemplatedDecl(); |
| 780 | return !cast<CXXMethodDecl>(D)->isStatic(); |
| 781 | } |
| 782 | |
| 783 | return false; |
| 784 | } |
| 785 | |
| 786 | enum IMAKind { |
| 787 | /// The reference is definitely not an instance member access. |
| 788 | IMA_Static, |
| 789 | |
| 790 | /// The reference may be an implicit instance member access. |
| 791 | IMA_Mixed, |
| 792 | |
| 793 | /// The reference may be to an instance member, but it is invalid if |
| 794 | /// so, because the context is not an instance method. |
| 795 | IMA_Mixed_StaticContext, |
| 796 | |
| 797 | /// The reference may be to an instance member, but it is invalid if |
| 798 | /// so, because the context is from an unrelated class. |
| 799 | IMA_Mixed_Unrelated, |
| 800 | |
| 801 | /// The reference is definitely an implicit instance member access. |
| 802 | IMA_Instance, |
| 803 | |
| 804 | /// The reference may be to an unresolved using declaration. |
| 805 | IMA_Unresolved, |
| 806 | |
| 807 | /// The reference may be to an unresolved using declaration and the |
| 808 | /// context is not an instance method. |
| 809 | IMA_Unresolved_StaticContext, |
| 810 | |
| 811 | /// The reference is to a member of an anonymous structure in a |
| 812 | /// non-class context. |
| 813 | IMA_AnonymousMember, |
| 814 | |
| 815 | /// All possible referrents are instance members and the current |
| 816 | /// context is not an instance method. |
| 817 | IMA_Error_StaticContext, |
| 818 | |
| 819 | /// All possible referrents are instance members of an unrelated |
| 820 | /// class. |
| 821 | IMA_Error_Unrelated |
| 822 | }; |
| 823 | |
| 824 | /// The given lookup names class member(s) and is not being used for |
| 825 | /// an address-of-member expression. Classify the type of access |
| 826 | /// according to whether it's possible that this reference names an |
| 827 | /// instance member. This is best-effort; it is okay to |
| 828 | /// conservatively answer "yes", in which case some errors will simply |
| 829 | /// not be caught until template-instantiation. |
| 830 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| 831 | const LookupResult &R) { |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 832 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 833 | |
| 834 | bool isStaticContext = |
| 835 | (!isa<CXXMethodDecl>(SemaRef.CurContext) || |
| 836 | cast<CXXMethodDecl>(SemaRef.CurContext)->isStatic()); |
| 837 | |
| 838 | if (R.isUnresolvableResult()) |
| 839 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 840 | |
| 841 | // Collect all the declaring classes of instance members we find. |
| 842 | bool hasNonInstance = false; |
| 843 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 844 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 845 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 846 | if (IsInstanceMember(D)) { |
| 847 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| 848 | |
| 849 | // If this is a member of an anonymous record, move out to the |
| 850 | // innermost non-anonymous struct or union. If there isn't one, |
| 851 | // that's a special case. |
| 852 | while (R->isAnonymousStructOrUnion()) { |
| 853 | R = dyn_cast<CXXRecordDecl>(R->getParent()); |
| 854 | if (!R) return IMA_AnonymousMember; |
| 855 | } |
| 856 | Classes.insert(R->getCanonicalDecl()); |
| 857 | } |
| 858 | else |
| 859 | hasNonInstance = true; |
| 860 | } |
| 861 | |
| 862 | // If we didn't find any instance members, it can't be an implicit |
| 863 | // member reference. |
| 864 | if (Classes.empty()) |
| 865 | return IMA_Static; |
| 866 | |
| 867 | // If the current context is not an instance method, it can't be |
| 868 | // an implicit member reference. |
| 869 | if (isStaticContext) |
| 870 | return (hasNonInstance ? IMA_Mixed_StaticContext : IMA_Error_StaticContext); |
| 871 | |
| 872 | // If we can prove that the current context is unrelated to all the |
| 873 | // declaring classes, it can't be an implicit member reference (in |
| 874 | // which case it's an error if any of those members are selected). |
| 875 | if (IsProvablyNotDerivedFrom(SemaRef, |
| 876 | cast<CXXMethodDecl>(SemaRef.CurContext)->getParent(), |
| 877 | Classes)) |
| 878 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 879 | |
| 880 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 881 | } |
| 882 | |
| 883 | /// Diagnose a reference to a field with no object available. |
| 884 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 885 | const CXXScopeSpec &SS, |
| 886 | const LookupResult &R) { |
| 887 | SourceLocation Loc = R.getNameLoc(); |
| 888 | SourceRange Range(Loc); |
| 889 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 890 | |
| 891 | if (R.getAsSingle<FieldDecl>()) { |
| 892 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 893 | if (MD->isStatic()) { |
| 894 | // "invalid use of member 'x' in static member function" |
| 895 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| 896 | << Range << R.getLookupName(); |
| 897 | return; |
| 898 | } |
| 899 | } |
| 900 | |
| 901 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| 902 | << R.getLookupName() << Range; |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 907 | } |
| 908 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 909 | /// Diagnose an empty lookup. |
| 910 | /// |
| 911 | /// \return false if new lookup candidates were found |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 912 | bool Sema::DiagnoseEmptyLookup(Scope *S, const CXXScopeSpec &SS, |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 913 | LookupResult &R) { |
| 914 | DeclarationName Name = R.getLookupName(); |
| 915 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 916 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 917 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 918 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 919 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 920 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 921 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 922 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 923 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 924 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 925 | // If the original lookup was an unqualified lookup, fake an |
| 926 | // unqualified lookup. This is useful when (for example) the |
| 927 | // original lookup would not have found something because it was a |
| 928 | // dependent name. |
| 929 | for (DeclContext *DC = SS.isEmpty()? CurContext : 0; |
| 930 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 931 | if (isa<CXXRecordDecl>(DC)) { |
| 932 | LookupQualifiedName(R, DC); |
| 933 | |
| 934 | if (!R.empty()) { |
| 935 | // Don't give errors about ambiguities in this lookup. |
| 936 | R.suppressDiagnostics(); |
| 937 | |
| 938 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 939 | bool isInstance = CurMethod && |
| 940 | CurMethod->isInstance() && |
| 941 | DC == CurMethod->getParent(); |
| 942 | |
| 943 | // Give a code modification hint to insert 'this->'. |
| 944 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 945 | // Actually quite difficult! |
| 946 | if (isInstance) |
| 947 | Diag(R.getNameLoc(), diagnostic) << Name |
| 948 | << CodeModificationHint::CreateInsertion(R.getNameLoc(), |
| 949 | "this->"); |
| 950 | else |
| 951 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 952 | |
| 953 | // Do we really want to note all of these? |
| 954 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 955 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 956 | |
| 957 | // Tell the callee to try to recover. |
| 958 | return false; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 963 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 964 | if (S && CorrectTypo(R, S, &SS)) { |
| 965 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 966 | if (SS.isEmpty()) |
| 967 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 968 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 969 | R.getLookupName().getAsString()); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 970 | else |
| 971 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 972 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 973 | << SS.getRange() |
| 974 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 975 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 976 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 977 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 978 | << ND->getDeclName(); |
| 979 | |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 980 | // Tell the callee to try to recover. |
| 981 | return false; |
| 982 | } |
| 983 | |
| 984 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 985 | // FIXME: If we ended up with a typo for a type name or |
| 986 | // Objective-C class name, we're in trouble because the parser |
| 987 | // is in the wrong place to recover. Suggest the typo |
| 988 | // correction, but don't make it a fix-it since we're not going |
| 989 | // to recover well anyway. |
| 990 | if (SS.isEmpty()) |
| 991 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 992 | else |
| 993 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 994 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 995 | << SS.getRange(); |
| 996 | |
| 997 | // Don't try to recover; it won't work. |
| 998 | return true; |
| 999 | } |
| 1000 | |
| 1001 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | // Emit a special diagnostic for failed member lookups. |
| 1005 | // FIXME: computing the declaration context might fail here (?) |
| 1006 | if (!SS.isEmpty()) { |
| 1007 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1008 | << Name << computeDeclContext(SS, false) |
| 1009 | << SS.getRange(); |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1013 | // Give up, we can't recover. |
| 1014 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1015 | return true; |
| 1016 | } |
| 1017 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1018 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 1019 | const CXXScopeSpec &SS, |
| 1020 | UnqualifiedId &Id, |
| 1021 | bool HasTrailingLParen, |
| 1022 | bool isAddressOfOperand) { |
| 1023 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1024 | "cannot be direct & operand and have a trailing lparen"); |
| 1025 | |
| 1026 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1027 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1028 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1029 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1030 | |
| 1031 | // Decompose the UnqualifiedId into the following data. |
| 1032 | DeclarationName Name; |
| 1033 | SourceLocation NameLoc; |
| 1034 | const TemplateArgumentListInfo *TemplateArgs; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1035 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 1036 | Name, NameLoc, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1038 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1039 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1040 | // C++ [temp.dep.expr]p3: |
| 1041 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1042 | // -- an identifier that was declared with a dependent type, |
| 1043 | // (note: handled after lookup) |
| 1044 | // -- a template-id that is dependent, |
| 1045 | // (note: handled in BuildTemplateIdExpr) |
| 1046 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1047 | // -- a nested-name-specifier that contains a class-name that |
| 1048 | // names a dependent type. |
| 1049 | // Determine whether this is a member of an unknown specialization; |
| 1050 | // we need to handle these differently. |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1051 | if ((Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1052 | Name.getCXXNameType()->isDependentType()) || |
| 1053 | (SS.isSet() && IsDependentIdExpression(*this, SS))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1054 | return ActOnDependentIdExpression(SS, Name, NameLoc, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 1055 | isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1056 | TemplateArgs); |
| 1057 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1058 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1059 | // Perform the required lookup. |
| 1060 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1061 | if (TemplateArgs) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1062 | // Just re-use the lookup done by isTemplateName. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1063 | DecomposeTemplateName(R, Id); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1064 | } else { |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1065 | bool IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
| 1066 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1068 | // If this reference is in an Objective-C method, then we need to do |
| 1069 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1070 | if (IvarLookupFollowUp) { |
| 1071 | OwningExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1072 | if (E.isInvalid()) |
| 1073 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1074 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1075 | Expr *Ex = E.takeAs<Expr>(); |
| 1076 | if (Ex) return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1077 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1078 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1079 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1080 | if (R.isAmbiguous()) |
| 1081 | return ExprError(); |
| 1082 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1083 | // Determine whether this name might be a candidate for |
| 1084 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1085 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1086 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1087 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1088 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1089 | // in C90, extension in C99, forbidden in C++). |
| 1090 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1091 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1092 | if (D) R.addDecl(D); |
| 1093 | } |
| 1094 | |
| 1095 | // If this name wasn't predeclared and if this is not a function |
| 1096 | // call, diagnose the problem. |
| 1097 | if (R.empty()) { |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1098 | if (DiagnoseEmptyLookup(S, SS, R)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1099 | return ExprError(); |
| 1100 | |
| 1101 | assert(!R.empty() && |
| 1102 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1103 | |
| 1104 | // If we found an Objective-C instance variable, let |
| 1105 | // LookupInObjCMethod build the appropriate expression to |
| 1106 | // reference the ivar. |
| 1107 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1108 | R.clear(); |
| 1109 | OwningExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
| 1110 | assert(E.isInvalid() || E.get()); |
| 1111 | return move(E); |
| 1112 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1116 | // This is guaranteed from this point on. |
| 1117 | assert(!R.empty() || ADL); |
| 1118 | |
| 1119 | if (VarDecl *Var = R.getAsSingle<VarDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1120 | // Warn about constructs like: |
| 1121 | // if (void *X = foo()) { ... } else { X }. |
| 1122 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1124 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 1125 | Scope *CheckS = S; |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1126 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1128 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1129 | ExprError(Diag(NameLoc, diag::warn_value_always_zero) |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1130 | << Var->getDeclName() |
| 1131 | << (Var->getType()->isPointerType()? 2 : |
| 1132 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1135 | |
Douglas Gregor | 9c4b838 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 1136 | // Move to the parent of this scope. |
| 1137 | CheckS = CheckS->getParent(); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1140 | } else if (FunctionDecl *Func = R.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1141 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 1142 | // C99 DR 316 says that, if a function type comes from a |
| 1143 | // function definition (without a prototype), that type is only |
| 1144 | // used for checking compatibility. Therefore, when referencing |
| 1145 | // the function, we pretend that we don't have the full function |
| 1146 | // type. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1147 | if (DiagnoseUseOfDecl(Func, NameLoc)) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1148 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1150 | QualType T = Func->getType(); |
| 1151 | QualType NoProtoType = T; |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1152 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1153 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1154 | return BuildDeclRefExpr(Func, NoProtoType, NameLoc, &SS); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1158 | // Check whether this might be a C++ implicit instance member access. |
| 1159 | // C++ [expr.prim.general]p6: |
| 1160 | // Within the definition of a non-static member function, an |
| 1161 | // identifier that names a non-static member is transformed to a |
| 1162 | // class member access expression. |
| 1163 | // But note that &SomeClass::foo is grammatically distinct, even |
| 1164 | // though we don't parse it that way. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1165 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1166 | bool isAbstractMemberPointer = (isAddressOfOperand && !SS.isEmpty()); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1167 | if (!isAbstractMemberPointer) |
| 1168 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1171 | if (TemplateArgs) |
| 1172 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1173 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1174 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1175 | } |
| 1176 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1177 | /// Builds an expression which might be an implicit member expression. |
| 1178 | Sema::OwningExprResult |
| 1179 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1180 | LookupResult &R, |
| 1181 | const TemplateArgumentListInfo *TemplateArgs) { |
| 1182 | switch (ClassifyImplicitMemberAccess(*this, R)) { |
| 1183 | case IMA_Instance: |
| 1184 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 1185 | |
| 1186 | case IMA_AnonymousMember: |
| 1187 | assert(R.isSingleResult()); |
| 1188 | return BuildAnonymousStructUnionMemberReference(R.getNameLoc(), |
| 1189 | R.getAsSingle<FieldDecl>()); |
| 1190 | |
| 1191 | case IMA_Mixed: |
| 1192 | case IMA_Mixed_Unrelated: |
| 1193 | case IMA_Unresolved: |
| 1194 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 1195 | |
| 1196 | case IMA_Static: |
| 1197 | case IMA_Mixed_StaticContext: |
| 1198 | case IMA_Unresolved_StaticContext: |
| 1199 | if (TemplateArgs) |
| 1200 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 1201 | return BuildDeclarationNameExpr(SS, R, false); |
| 1202 | |
| 1203 | case IMA_Error_StaticContext: |
| 1204 | case IMA_Error_Unrelated: |
| 1205 | DiagnoseInstanceReference(*this, SS, R); |
| 1206 | return ExprError(); |
| 1207 | } |
| 1208 | |
| 1209 | llvm_unreachable("unexpected instance member access kind"); |
| 1210 | return ExprError(); |
| 1211 | } |
| 1212 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1213 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1214 | /// declaration name, generally during template instantiation. |
| 1215 | /// There's a large number of things which don't need to be done along |
| 1216 | /// this path. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1217 | Sema::OwningExprResult |
| 1218 | Sema::BuildQualifiedDeclarationNameExpr(const CXXScopeSpec &SS, |
| 1219 | DeclarationName Name, |
| 1220 | SourceLocation NameLoc) { |
| 1221 | DeclContext *DC; |
| 1222 | if (!(DC = computeDeclContext(SS, false)) || |
| 1223 | DC->isDependentContext() || |
| 1224 | RequireCompleteDeclContext(SS)) |
| 1225 | return BuildDependentDeclRefExpr(SS, Name, NameLoc, 0); |
| 1226 | |
| 1227 | LookupResult R(*this, Name, NameLoc, LookupOrdinaryName); |
| 1228 | LookupQualifiedName(R, DC); |
| 1229 | |
| 1230 | if (R.isAmbiguous()) |
| 1231 | return ExprError(); |
| 1232 | |
| 1233 | if (R.empty()) { |
| 1234 | Diag(NameLoc, diag::err_no_member) << Name << DC << SS.getRange(); |
| 1235 | return ExprError(); |
| 1236 | } |
| 1237 | |
| 1238 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1239 | } |
| 1240 | |
| 1241 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1242 | /// detected that we're currently inside an ObjC method. Perform some |
| 1243 | /// additional lookup. |
| 1244 | /// |
| 1245 | /// Ideally, most of this would be done by lookup, but there's |
| 1246 | /// actually quite a lot of extra work involved. |
| 1247 | /// |
| 1248 | /// Returns a null sentinel to indicate trivial success. |
| 1249 | Sema::OwningExprResult |
| 1250 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1251 | IdentifierInfo *II, |
| 1252 | bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1253 | SourceLocation Loc = Lookup.getNameLoc(); |
| 1254 | |
| 1255 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1256 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1257 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1258 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1259 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1260 | |
| 1261 | // If we're in a class method, we don't normally want to look for |
| 1262 | // ivars. But if we don't find anything else, and there's an |
| 1263 | // ivar, that's an error. |
| 1264 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 1265 | |
| 1266 | bool LookForIvars; |
| 1267 | if (Lookup.empty()) |
| 1268 | LookForIvars = true; |
| 1269 | else if (IsClassMethod) |
| 1270 | LookForIvars = false; |
| 1271 | else |
| 1272 | LookForIvars = (Lookup.isSingleResult() && |
| 1273 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1274 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1275 | if (LookForIvars) { |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1276 | IFace = getCurMethodDecl()->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1277 | ObjCInterfaceDecl *ClassDeclared; |
| 1278 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1279 | // Diagnose using an ivar in a class method. |
| 1280 | if (IsClassMethod) |
| 1281 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1282 | << IV->getDeclName()); |
| 1283 | |
| 1284 | // If we're referencing an invalid decl, just return this as a silent |
| 1285 | // error node. The error diagnostic was already emitted on the decl. |
| 1286 | if (IV->isInvalidDecl()) |
| 1287 | return ExprError(); |
| 1288 | |
| 1289 | // Check if referencing a field with __attribute__((deprecated)). |
| 1290 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1291 | return ExprError(); |
| 1292 | |
| 1293 | // Diagnose the use of an ivar outside of the declaring class. |
| 1294 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1295 | ClassDeclared != IFace) |
| 1296 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1297 | |
| 1298 | // FIXME: This should use a new expr for a direct reference, don't |
| 1299 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1300 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1301 | UnqualifiedId SelfName; |
| 1302 | SelfName.setIdentifier(&II, SourceLocation()); |
| 1303 | CXXScopeSpec SelfScopeSpec; |
| 1304 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 1305 | SelfName, false, false); |
| 1306 | MarkDeclarationReferenced(Loc, IV); |
| 1307 | return Owned(new (Context) |
| 1308 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 1309 | SelfExpr.takeAs<Expr>(), true, true)); |
| 1310 | } |
| 1311 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
| 1312 | // We should warn if a local variable hides an ivar. |
| 1313 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
| 1314 | ObjCInterfaceDecl *ClassDeclared; |
| 1315 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1316 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1317 | IFace == ClassDeclared) |
| 1318 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | // Needed to implement property "super.method" notation. |
| 1323 | if (Lookup.empty() && II->isStr("super")) { |
| 1324 | QualType T; |
| 1325 | |
| 1326 | if (getCurMethodDecl()->isInstanceMethod()) |
| 1327 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 1328 | getCurMethodDecl()->getClassInterface())); |
| 1329 | else |
| 1330 | T = Context.getObjCClassType(); |
| 1331 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
| 1332 | } |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1333 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1334 | // FIXME. Consolidate this with similar code in LookupName. |
| 1335 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1336 | if (!(getLangOptions().CPlusPlus && |
| 1337 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1338 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1339 | S, Lookup.isForRedeclaration(), |
| 1340 | Lookup.getNameLoc()); |
| 1341 | if (D) Lookup.addDecl(D); |
| 1342 | } |
| 1343 | } |
| 1344 | } |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1345 | if (LangOpts.ObjCNonFragileABI2 && LookForIvars && Lookup.empty()) { |
Fariborz Jahanian | 77e2dde | 2010-02-09 21:49:50 +0000 | [diff] [blame] | 1346 | ObjCIvarDecl *Ivar = SynthesizeNewPropertyIvar(IFace, II); |
| 1347 | if (Ivar) |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1348 | return LookupInObjCMethod(Lookup, S, II, AllowBuiltinCreation); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1349 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1350 | // Sentinel value saying that we didn't do anything special. |
| 1351 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1352 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1353 | |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1354 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1355 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1356 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 1357 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1358 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1359 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1361 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1362 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 1363 | return false; |
| 1364 | QualType FromRecordType = From->getType(); |
| 1365 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1366 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1367 | DestType = Context.getPointerType(DestType); |
| 1368 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1369 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 1370 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 1371 | CheckDerivedToBaseConversion(FromRecordType, |
| 1372 | DestRecordType, |
| 1373 | From->getSourceRange().getBegin(), |
| 1374 | From->getSourceRange())) |
| 1375 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 1376 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 1377 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1378 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1379 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1380 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1381 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1382 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1384 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1385 | SourceLocation Loc, QualType Ty, |
| 1386 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| 1387 | NestedNameSpecifier *Qualifier = 0; |
| 1388 | SourceRange QualifierRange; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1389 | if (SS.isSet()) { |
| 1390 | Qualifier = (NestedNameSpecifier *) SS.getScopeRep(); |
| 1391 | QualifierRange = SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1392 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1394 | return MemberExpr::Create(C, Base, isArrow, Qualifier, QualifierRange, |
| 1395 | Member, Loc, TemplateArgs, Ty); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1398 | /// Builds an implicit member access expression. The current context |
| 1399 | /// is known to be an instance method, and the given unqualified lookup |
| 1400 | /// set is known to contain only instance members, at least one of which |
| 1401 | /// is from an appropriate type. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1402 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1403 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 1404 | LookupResult &R, |
| 1405 | const TemplateArgumentListInfo *TemplateArgs, |
| 1406 | bool IsKnownInstance) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1407 | assert(!R.empty() && !R.isAmbiguous()); |
| 1408 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1409 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1411 | // We may have found a field within an anonymous union or struct |
| 1412 | // (C++ [class.union]). |
Douglas Gregor | e961afb | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 1413 | // FIXME: This needs to happen post-isImplicitMemberReference? |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1414 | // FIXME: template-ids inside anonymous structs? |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1415 | if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1416 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1417 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1418 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1419 | // If this is known to be an instance access, go ahead and build a |
| 1420 | // 'this' expression now. |
| 1421 | QualType ThisType = cast<CXXMethodDecl>(CurContext)->getThisType(Context); |
| 1422 | Expr *This = 0; // null signifies implicit access |
| 1423 | if (IsKnownInstance) { |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 1424 | SourceLocation Loc = R.getNameLoc(); |
| 1425 | if (SS.getRange().isValid()) |
| 1426 | Loc = SS.getRange().getBegin(); |
| 1427 | This = new (Context) CXXThisExpr(Loc, ThisType, /*isImplicit=*/true); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1430 | return BuildMemberReferenceExpr(ExprArg(*this, This), ThisType, |
| 1431 | /*OpLoc*/ SourceLocation(), |
| 1432 | /*IsArrow*/ true, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 1433 | SS, |
| 1434 | /*FirstQualifierInScope*/ 0, |
| 1435 | R, TemplateArgs); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1438 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1439 | const LookupResult &R, |
| 1440 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1441 | // Only when used directly as the postfix-expression of a call. |
| 1442 | if (!HasTrailingLParen) |
| 1443 | return false; |
| 1444 | |
| 1445 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1446 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1447 | return false; |
| 1448 | |
| 1449 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1450 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1451 | return false; |
| 1452 | |
| 1453 | // Turn off ADL when we find certain kinds of declarations during |
| 1454 | // normal lookup: |
| 1455 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 1456 | NamedDecl *D = *I; |
| 1457 | |
| 1458 | // C++0x [basic.lookup.argdep]p3: |
| 1459 | // -- a declaration of a class member |
| 1460 | // Since using decls preserve this property, we check this on the |
| 1461 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1462 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1463 | return false; |
| 1464 | |
| 1465 | // C++0x [basic.lookup.argdep]p3: |
| 1466 | // -- a block-scope function declaration that is not a |
| 1467 | // using-declaration |
| 1468 | // NOTE: we also trigger this for function templates (in fact, we |
| 1469 | // don't check the decl type at all, since all other decl types |
| 1470 | // turn off ADL anyway). |
| 1471 | if (isa<UsingShadowDecl>(D)) |
| 1472 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1473 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1474 | return false; |
| 1475 | |
| 1476 | // C++0x [basic.lookup.argdep]p3: |
| 1477 | // -- a declaration that is neither a function or a function |
| 1478 | // template |
| 1479 | // And also for builtin functions. |
| 1480 | if (isa<FunctionDecl>(D)) { |
| 1481 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1482 | |
| 1483 | // But also builtin functions. |
| 1484 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1485 | return false; |
| 1486 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1487 | return false; |
| 1488 | } |
| 1489 | |
| 1490 | return true; |
| 1491 | } |
| 1492 | |
| 1493 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1494 | /// Diagnoses obvious problems with the use of the given declaration |
| 1495 | /// as an expression. This is only actually called for lookups that |
| 1496 | /// were not overloaded, and it doesn't promise that the declaration |
| 1497 | /// will in fact be used. |
| 1498 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1499 | if (isa<TypedefDecl>(D)) { |
| 1500 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1501 | return true; |
| 1502 | } |
| 1503 | |
| 1504 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1505 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1506 | return true; |
| 1507 | } |
| 1508 | |
| 1509 | if (isa<NamespaceDecl>(D)) { |
| 1510 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1511 | return true; |
| 1512 | } |
| 1513 | |
| 1514 | return false; |
| 1515 | } |
| 1516 | |
| 1517 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1518 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1519 | LookupResult &R, |
| 1520 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 1521 | // If this is a single, fully-resolved result and we don't need ADL, |
| 1522 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 1523 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1524 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1525 | |
| 1526 | // We only need to check the declaration if there's exactly one |
| 1527 | // result, because in the overloaded case the results can only be |
| 1528 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1529 | if (R.isSingleResult() && |
| 1530 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1531 | return ExprError(); |
| 1532 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1533 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 1534 | // any lookup-related diagnostics; we'll hash these out later, when |
| 1535 | // we've picked a target. |
| 1536 | R.suppressDiagnostics(); |
| 1537 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1538 | bool Dependent |
| 1539 | = UnresolvedLookupExpr::ComputeDependence(R.begin(), R.end(), 0); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1540 | UnresolvedLookupExpr *ULE |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1541 | = UnresolvedLookupExpr::Create(Context, Dependent, R.getNamingClass(), |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1542 | (NestedNameSpecifier*) SS.getScopeRep(), |
| 1543 | SS.getRange(), |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1544 | R.getLookupName(), R.getNameLoc(), |
| 1545 | NeedsADL, R.isOverloadedResult()); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 1546 | ULE->addDecls(R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1547 | |
| 1548 | return Owned(ULE); |
| 1549 | } |
| 1550 | |
| 1551 | |
| 1552 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1553 | Sema::OwningExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1554 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1555 | SourceLocation Loc, NamedDecl *D) { |
| 1556 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1557 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1558 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1559 | |
| 1560 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1561 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1562 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1563 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 1564 | // Specifically diagnose references to class templates that are missing |
| 1565 | // a template argument list. |
| 1566 | Diag(Loc, diag::err_template_decl_ref) |
| 1567 | << Template << SS.getRange(); |
| 1568 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 1569 | return ExprError(); |
| 1570 | } |
| 1571 | |
| 1572 | // Make sure that we're referring to a value. |
| 1573 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1574 | if (!VD) { |
| 1575 | Diag(Loc, diag::err_ref_non_value) |
| 1576 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 1577 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 1578 | return ExprError(); |
| 1579 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1581 | // Check whether this declaration can be used. Note that we suppress |
| 1582 | // this check when we're going to perform argument-dependent lookup |
| 1583 | // on this function name, because this might not be the function |
| 1584 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1585 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1586 | return ExprError(); |
| 1587 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1588 | // Only create DeclRefExpr's for valid Decl's. |
| 1589 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1590 | return ExprError(); |
| 1591 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1592 | // If the identifier reference is inside a block, and it refers to a value |
| 1593 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1594 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1595 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1596 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1597 | // We do not do this for things like enum constants, global variables, etc, |
| 1598 | // as they do not get snapshotted. |
| 1599 | // |
| 1600 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 1601 | if (VD->getType().getTypePtr()->isVariablyModifiedType()) { |
| 1602 | Diag(Loc, diag::err_ref_vm_type); |
| 1603 | Diag(D->getLocation(), diag::note_declared_at); |
| 1604 | return ExprError(); |
| 1605 | } |
| 1606 | |
Mike Stump | 2849734 | 2010-01-05 03:10:36 +0000 | [diff] [blame] | 1607 | if (VD->getType()->isArrayType()) { |
| 1608 | Diag(Loc, diag::err_ref_array_type); |
| 1609 | Diag(D->getLocation(), diag::note_declared_at); |
| 1610 | return ExprError(); |
| 1611 | } |
| 1612 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1613 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1614 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1615 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1616 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1617 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1618 | // This is to record that a 'const' was actually synthesize and added. |
| 1619 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1620 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1622 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1624 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1625 | } |
| 1626 | // If this reference is not in a block or if the referenced variable is |
| 1627 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1628 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1629 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, &SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1632 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1633 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1634 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1637 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1638 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1639 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1640 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1641 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1642 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1643 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1644 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1645 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1646 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1647 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1648 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1649 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1650 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1651 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1652 | QualType ResTy; |
| 1653 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1654 | ResTy = Context.DependentTy; |
| 1655 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 1656 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1657 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1658 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1659 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1660 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1661 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1662 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1665 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1666 | llvm::SmallString<16> CharBuffer; |
| 1667 | CharBuffer.resize(Tok.getLength()); |
| 1668 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1669 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1670 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1671 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1672 | Tok.getLocation(), PP); |
| 1673 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1674 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1676 | QualType Ty; |
| 1677 | if (!getLangOptions().CPlusPlus) |
| 1678 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 1679 | else if (Literal.isWide()) |
| 1680 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 1681 | else if (Literal.isMultiChar()) |
| 1682 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1683 | else |
| 1684 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1685 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1686 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1687 | Literal.isWide(), |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 1688 | Ty, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1691 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1692 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1694 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1695 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1696 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1697 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1698 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1699 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1700 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1701 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1702 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1703 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1704 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1706 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1707 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1708 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1710 | Tok.getLocation(), PP); |
| 1711 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1712 | return ExprError(); |
| 1713 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1714 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1716 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1717 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1718 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1719 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1720 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1721 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1722 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1723 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1724 | |
| 1725 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1726 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1727 | using llvm::APFloat; |
| 1728 | APFloat Val(Format); |
| 1729 | |
| 1730 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Overflow is always an error, but underflow is only an error if |
| 1733 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 1734 | if ((result & APFloat::opOverflow) || |
| 1735 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 1736 | unsigned diagnostic; |
| 1737 | llvm::SmallVector<char, 20> buffer; |
| 1738 | if (result & APFloat::opOverflow) { |
| 1739 | diagnostic = diag::err_float_overflow; |
| 1740 | APFloat::getLargest(Format).toString(buffer); |
| 1741 | } else { |
| 1742 | diagnostic = diag::err_float_underflow; |
| 1743 | APFloat::getSmallest(Format).toString(buffer); |
| 1744 | } |
| 1745 | |
| 1746 | Diag(Tok.getLocation(), diagnostic) |
| 1747 | << Ty |
| 1748 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 1749 | } |
| 1750 | |
| 1751 | bool isExact = (result == APFloat::opOK); |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1752 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1753 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1754 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1755 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1756 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1757 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1759 | // long long is a C99 feature. |
| 1760 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1761 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1762 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1765 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1768 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1769 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1770 | Ty = Context.UnsignedLongLongTy; |
| 1771 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1772 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1773 | } else { |
| 1774 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1775 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1776 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1777 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1778 | // be an unsigned int. |
| 1779 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1780 | |
| 1781 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1782 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1783 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1784 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1785 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1787 | // Does it fit in a unsigned int? |
| 1788 | if (ResultVal.isIntN(IntSize)) { |
| 1789 | // Does it fit in a signed int? |
| 1790 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1791 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1792 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1793 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1794 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1795 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1796 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1797 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1798 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1799 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1800 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1801 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | // Does it fit in a unsigned long? |
| 1803 | if (ResultVal.isIntN(LongSize)) { |
| 1804 | // Does it fit in a signed long? |
| 1805 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1806 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1807 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1808 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1809 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1810 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1813 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1814 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1815 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1816 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1817 | // Does it fit in a unsigned long long? |
| 1818 | if (ResultVal.isIntN(LongLongSize)) { |
| 1819 | // Does it fit in a signed long long? |
| 1820 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1821 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1822 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1823 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1824 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1827 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1828 | // If we still couldn't decide a type, we probably have something that |
| 1829 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1830 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1831 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1832 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1833 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1834 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1836 | if (ResultVal.getBitWidth() != Width) |
| 1837 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1838 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1839 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1840 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1841 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1842 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1843 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1845 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1846 | |
| 1847 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1848 | } |
| 1849 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1850 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1851 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1852 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1853 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1854 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1858 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1859 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1860 | SourceLocation OpLoc, |
| 1861 | const SourceRange &ExprRange, |
| 1862 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1863 | if (exprType->isDependentType()) |
| 1864 | return false; |
| 1865 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1866 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1867 | // the result is the size of the referenced type." |
| 1868 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1869 | // result shall be the alignment of the referenced type." |
| 1870 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1871 | exprType = Ref->getPointeeType(); |
| 1872 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1873 | // C99 6.5.3.4p1: |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1874 | if (exprType->isFunctionType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1875 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1876 | if (isSizeof) |
| 1877 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1878 | return false; |
| 1879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1881 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1882 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1883 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1884 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1885 | return false; |
| 1886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1887 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1888 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 1889 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 1890 | << int(!isSizeof) << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1891 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1893 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1894 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1895 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1896 | << exprType << isSizeof << ExprRange; |
| 1897 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1898 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1899 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1900 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1903 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1904 | const SourceRange &ExprRange) { |
| 1905 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1907 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1908 | if (isa<DeclRefExpr>(E)) |
| 1909 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1910 | |
| 1911 | // Cannot know anything else if the expression is dependent. |
| 1912 | if (E->isTypeDependent()) |
| 1913 | return false; |
| 1914 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1915 | if (E->getBitField()) { |
| 1916 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1917 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1918 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1919 | |
| 1920 | // Alignment of a field access is always okay, so long as it isn't a |
| 1921 | // bit-field. |
| 1922 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1923 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1924 | return false; |
| 1925 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1926 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1927 | } |
| 1928 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1929 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | Action::OwningExprResult |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1931 | Sema::CreateSizeOfAlignOfExpr(TypeSourceInfo *TInfo, |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1932 | SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1933 | bool isSizeOf, SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1934 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1935 | return ExprError(); |
| 1936 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1937 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1939 | if (!T->isDependentType() && |
| 1940 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1941 | return ExprError(); |
| 1942 | |
| 1943 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1944 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, TInfo, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1945 | Context.getSizeType(), OpLoc, |
| 1946 | R.getEnd())); |
| 1947 | } |
| 1948 | |
| 1949 | /// \brief Build a sizeof or alignof expression given an expression |
| 1950 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | Action::OwningExprResult |
| 1952 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1953 | bool isSizeOf, SourceRange R) { |
| 1954 | // Verify that the operand is valid. |
| 1955 | bool isInvalid = false; |
| 1956 | if (E->isTypeDependent()) { |
| 1957 | // Delay type-checking for type-dependent expressions. |
| 1958 | } else if (!isSizeOf) { |
| 1959 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1960 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1961 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1962 | isInvalid = true; |
| 1963 | } else { |
| 1964 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1965 | } |
| 1966 | |
| 1967 | if (isInvalid) |
| 1968 | return ExprError(); |
| 1969 | |
| 1970 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1971 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1972 | Context.getSizeType(), OpLoc, |
| 1973 | R.getEnd())); |
| 1974 | } |
| 1975 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1976 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1977 | /// the same for @c alignof and @c __alignof |
| 1978 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1979 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1980 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1981 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1982 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1983 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1985 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1986 | TypeSourceInfo *TInfo; |
| 1987 | (void) GetTypeFromParser(TyOrEx, &TInfo); |
| 1988 | return CreateSizeOfAlignOfExpr(TInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1990 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1991 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1992 | Action::OwningExprResult Result |
| 1993 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1994 | |
| 1995 | if (Result.isInvalid()) |
| 1996 | DeleteExpr(ArgEx); |
| 1997 | |
| 1998 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2001 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2002 | if (V->isTypeDependent()) |
| 2003 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2004 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2005 | // These operators return the element type of a complex type. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2006 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2007 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2009 | // Otherwise they pass through real integer and floating point types here. |
| 2010 | if (V->getType()->isArithmeticType()) |
| 2011 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2013 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2014 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 2015 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2016 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2020 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2021 | Action::OwningExprResult |
| 2022 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 2023 | tok::TokenKind Kind, ExprArg Input) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2024 | UnaryOperator::Opcode Opc; |
| 2025 | switch (Kind) { |
| 2026 | default: assert(0 && "Unknown unary op!"); |
| 2027 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 2028 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 2029 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2030 | |
Eli Friedman | e4216e9 | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 2031 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2034 | Action::OwningExprResult |
| 2035 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 2036 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2037 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2038 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 2039 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2040 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 2041 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2043 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2044 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 2045 | Base.release(); |
| 2046 | Idx.release(); |
| 2047 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 2048 | Context.DependentTy, RLoc)); |
| 2049 | } |
| 2050 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2052 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2053 | LHSExp->getType()->isEnumeralType() || |
| 2054 | RHSExp->getType()->isRecordType() || |
| 2055 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2056 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 2059 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 2060 | } |
| 2061 | |
| 2062 | |
| 2063 | Action::OwningExprResult |
| 2064 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 2065 | ExprArg Idx, SourceLocation RLoc) { |
| 2066 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 2067 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 2068 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2069 | // Perform default conversions. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2070 | if (!LHSExp->getType()->getAs<VectorType>()) |
| 2071 | DefaultFunctionArrayLvalueConversion(LHSExp); |
| 2072 | DefaultFunctionArrayLvalueConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2073 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2074 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2076 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2077 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2078 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2079 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2080 | Expr *BaseExpr, *IndexExpr; |
| 2081 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2082 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 2083 | BaseExpr = LHSExp; |
| 2084 | IndexExpr = RHSExp; |
| 2085 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2086 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2087 | BaseExpr = LHSExp; |
| 2088 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2089 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2090 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 2091 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2092 | BaseExpr = RHSExp; |
| 2093 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2094 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2096 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2097 | BaseExpr = LHSExp; |
| 2098 | IndexExpr = RHSExp; |
| 2099 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2101 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2102 | // Handle the uncommon case of "123[Ptr]". |
| 2103 | BaseExpr = RHSExp; |
| 2104 | IndexExpr = LHSExp; |
| 2105 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2106 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 2107 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2108 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 2109 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 2110 | // FIXME: need to deal with const... |
| 2111 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2112 | } else if (LHSTy->isArrayType()) { |
| 2113 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 2114 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2115 | // wasn't promoted because of the C90 rule that doesn't |
| 2116 | // allow promoting non-lvalue arrays. Warn, then |
| 2117 | // force the promotion here. |
| 2118 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2119 | LHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2120 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 2121 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2122 | LHSTy = LHSExp->getType(); |
| 2123 | |
| 2124 | BaseExpr = LHSExp; |
| 2125 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2126 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2127 | } else if (RHSTy->isArrayType()) { |
| 2128 | // Same as previous, except for 123[f().a] case |
| 2129 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 2130 | RHSExp->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2131 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 2132 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 2133 | RHSTy = RHSExp->getType(); |
| 2134 | |
| 2135 | BaseExpr = RHSExp; |
| 2136 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2137 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2138 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2139 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 2140 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2141 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2142 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2143 | if (!(IndexExpr->getType()->isIntegerType() && |
| 2144 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 2145 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 2146 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2147 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2148 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 2149 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 2150 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 2151 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 2152 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2153 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2154 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 2155 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2156 | // incomplete types are not object types. |
| 2157 | if (ResultType->isFunctionType()) { |
| 2158 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 2159 | << ResultType << BaseExpr->getSourceRange(); |
| 2160 | return ExprError(); |
| 2161 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2163 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2165 | PDiag(diag::err_subscript_incomplete_type) |
| 2166 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 2167 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2169 | // Diagnose bad cases where we step over interface counts. |
| 2170 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 2171 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 2172 | << ResultType << BaseExpr->getSourceRange(); |
| 2173 | return ExprError(); |
| 2174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2176 | Base.release(); |
| 2177 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2178 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2179 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2182 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2183 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2184 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2185 | SourceLocation CompLoc) { |
Daniel Dunbar | 2ad3289 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 2186 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 2187 | // see FIXME there. |
| 2188 | // |
| 2189 | // FIXME: This logic can be greatly simplified by splitting it along |
| 2190 | // halving/not halving and reworking the component checking. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2191 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2192 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2193 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2194 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2195 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2196 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2197 | // special names that indicate a subset of exactly half the elements are |
| 2198 | // to be selected. |
| 2199 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2200 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2201 | // This flag determines whether or not CompName has an 's' char prefix, |
| 2202 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 131f465 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 2203 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2204 | |
| 2205 | // Check that we've found one of the special components, or that the component |
| 2206 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2207 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2208 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 2209 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2210 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2211 | do |
| 2212 | compStr++; |
| 2213 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2214 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2215 | do |
| 2216 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2217 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 2218 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2219 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2220 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2221 | // We didn't get to the end of the string. This means the component names |
| 2222 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2223 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 2224 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2225 | return QualType(); |
| 2226 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2227 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2228 | // Ensure no component accessor exceeds the width of the vector type it |
| 2229 | // operates on. |
| 2230 | if (!HalvingSwizzle) { |
Daniel Dunbar | e013d68 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 2231 | compStr = CompName->getNameStart(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2232 | |
| 2233 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2234 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2235 | |
| 2236 | while (*compStr) { |
| 2237 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 2238 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 2239 | << baseType << SourceRange(CompLoc); |
| 2240 | return QualType(); |
| 2241 | } |
| 2242 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2243 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2244 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2245 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2246 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2247 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2248 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2249 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 0479a0b | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 2250 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2251 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 2252 | if (HexSwizzle) |
| 2253 | CompSize--; |
| 2254 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2255 | if (CompSize == 1) |
| 2256 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2257 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2258 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2259 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2260 | // diagostics look bad. We want extended vector types to appear built-in. |
| 2261 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 2262 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 2263 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 2264 | } |
| 2265 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2268 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2269 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2270 | const Selector &Sel, |
| 2271 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2273 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2274 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2275 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2276 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2277 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2278 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 2279 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2280 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2281 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2282 | return D; |
| 2283 | } |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2287 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2288 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2289 | const Selector &Sel, |
| 2290 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2291 | // Check protocols on qualified interfaces. |
| 2292 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2293 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2294 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2295 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2296 | GDecl = PD; |
| 2297 | break; |
| 2298 | } |
| 2299 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2300 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2301 | GDecl = OMD; |
| 2302 | break; |
| 2303 | } |
| 2304 | } |
| 2305 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2306 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2307 | E = QIdTy->qual_end(); I != E; ++I) { |
| 2308 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2309 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 2310 | if (GDecl) |
| 2311 | return GDecl; |
| 2312 | } |
| 2313 | } |
| 2314 | return GDecl; |
| 2315 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 2316 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2317 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2318 | Sema::ActOnDependentMemberExpr(ExprArg Base, QualType BaseType, |
| 2319 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2320 | const CXXScopeSpec &SS, |
| 2321 | NamedDecl *FirstQualifierInScope, |
| 2322 | DeclarationName Name, SourceLocation NameLoc, |
| 2323 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2324 | Expr *BaseExpr = Base.takeAs<Expr>(); |
| 2325 | |
| 2326 | // Even in dependent contexts, try to diagnose base expressions with |
| 2327 | // obviously wrong types, e.g.: |
| 2328 | // |
| 2329 | // T* t; |
| 2330 | // t.f; |
| 2331 | // |
| 2332 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2333 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2334 | // allows this, while still reporting an error if T is a struct pointer. |
| 2335 | if (!IsArrow) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2336 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2337 | if (PT && (!getLangOptions().ObjC1 || |
| 2338 | PT->getPointeeType()->isRecordType())) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2339 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2340 | Diag(NameLoc, diag::err_typecheck_member_reference_struct_union) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2341 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2342 | return ExprError(); |
| 2343 | } |
| 2344 | } |
| 2345 | |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2346 | assert(BaseType->isDependentType() || Name.isDependentName()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2347 | |
| 2348 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2349 | // must have pointer type, and the accessed type is the pointee. |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2350 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2351 | IsArrow, OpLoc, |
| 2352 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()), |
| 2353 | SS.getRange(), |
| 2354 | FirstQualifierInScope, |
| 2355 | Name, NameLoc, |
| 2356 | TemplateArgs)); |
| 2357 | } |
| 2358 | |
| 2359 | /// We know that the given qualified member reference points only to |
| 2360 | /// declarations which do not belong to the static type of the base |
| 2361 | /// expression. Diagnose the problem. |
| 2362 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 2363 | Expr *BaseExpr, |
| 2364 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2365 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2366 | const LookupResult &R) { |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2367 | // If this is an implicit member access, use a different set of |
| 2368 | // diagnostics. |
| 2369 | if (!BaseExpr) |
| 2370 | return DiagnoseInstanceReference(SemaRef, SS, R); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2371 | |
| 2372 | // FIXME: this is an exceedingly lame diagnostic for some of the more |
| 2373 | // complicated cases here. |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2374 | DeclContext *DC = R.getRepresentativeDecl()->getDeclContext(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2375 | SemaRef.Diag(R.getNameLoc(), diag::err_not_direct_base_or_virtual) |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2376 | << SS.getRange() << DC << BaseType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | // Check whether the declarations we found through a nested-name |
| 2380 | // specifier in a member expression are actually members of the base |
| 2381 | // type. The restriction here is: |
| 2382 | // |
| 2383 | // C++ [expr.ref]p2: |
| 2384 | // ... In these cases, the id-expression shall name a |
| 2385 | // member of the class or of one of its base classes. |
| 2386 | // |
| 2387 | // So it's perfectly legitimate for the nested-name specifier to name |
| 2388 | // an unrelated class, and for us to find an overload set including |
| 2389 | // decls from classes which are not superclasses, as long as the decl |
| 2390 | // we actually pick through overload resolution is from a superclass. |
| 2391 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 2392 | QualType BaseType, |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2393 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2394 | const LookupResult &R) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2395 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 2396 | if (!BaseRT) { |
| 2397 | // We can't check this yet because the base type is still |
| 2398 | // dependent. |
| 2399 | assert(BaseType->isDependentType()); |
| 2400 | return false; |
| 2401 | } |
| 2402 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2403 | |
| 2404 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2405 | // If this is an implicit member reference and we find a |
| 2406 | // non-instance member, it's not an error. |
| 2407 | if (!BaseExpr && !IsInstanceMember((*I)->getUnderlyingDecl())) |
| 2408 | return false; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2409 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2410 | // Note that we use the DC of the decl, not the underlying decl. |
| 2411 | CXXRecordDecl *RecordD = cast<CXXRecordDecl>((*I)->getDeclContext()); |
| 2412 | while (RecordD->isAnonymousStructOrUnion()) |
| 2413 | RecordD = cast<CXXRecordDecl>(RecordD->getParent()); |
| 2414 | |
| 2415 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| 2416 | MemberRecord.insert(RecordD->getCanonicalDecl()); |
| 2417 | |
| 2418 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 2419 | return false; |
| 2420 | } |
| 2421 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2422 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, R); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2423 | return true; |
| 2424 | } |
| 2425 | |
| 2426 | static bool |
| 2427 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 2428 | SourceRange BaseRange, const RecordType *RTy, |
| 2429 | SourceLocation OpLoc, const CXXScopeSpec &SS) { |
| 2430 | RecordDecl *RDecl = RTy->getDecl(); |
| 2431 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| 2432 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2433 | << BaseRange)) |
| 2434 | return true; |
| 2435 | |
| 2436 | DeclContext *DC = RDecl; |
| 2437 | if (SS.isSet()) { |
| 2438 | // If the member name was a qualified-id, look into the |
| 2439 | // nested-name-specifier. |
| 2440 | DC = SemaRef.computeDeclContext(SS, false); |
| 2441 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2442 | if (SemaRef.RequireCompleteDeclContext(SS)) { |
| 2443 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 2444 | << SS.getRange() << DC; |
| 2445 | return true; |
| 2446 | } |
| 2447 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2448 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2449 | |
| 2450 | if (!isa<TypeDecl>(DC)) { |
| 2451 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 2452 | << DC << SS.getRange(); |
| 2453 | return true; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2454 | } |
| 2455 | } |
| 2456 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2457 | // The record definition is complete, now look up the member. |
| 2458 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2459 | |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2460 | if (!R.empty()) |
| 2461 | return false; |
| 2462 | |
| 2463 | // We didn't find anything with the given name, so try to correct |
| 2464 | // for typos. |
| 2465 | DeclarationName Name = R.getLookupName(); |
| 2466 | if (SemaRef.CorrectTypo(R, 0, &SS, DC) && |
| 2467 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 2468 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 2469 | << Name << DC << R.getLookupName() << SS.getRange() |
| 2470 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2471 | R.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2472 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 2473 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 2474 | << ND->getDeclName(); |
Douglas Gregor | 2dcc011 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 2475 | return false; |
| 2476 | } else { |
| 2477 | R.clear(); |
| 2478 | } |
| 2479 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2480 | return false; |
| 2481 | } |
| 2482 | |
| 2483 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2484 | Sema::BuildMemberReferenceExpr(ExprArg BaseArg, QualType BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2485 | SourceLocation OpLoc, bool IsArrow, |
| 2486 | const CXXScopeSpec &SS, |
| 2487 | NamedDecl *FirstQualifierInScope, |
| 2488 | DeclarationName Name, SourceLocation NameLoc, |
| 2489 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2490 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 2491 | |
John McCall | 2f841ba | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 2492 | if (BaseType->isDependentType() || |
| 2493 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2494 | return ActOnDependentMemberExpr(ExprArg(*this, Base), BaseType, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2495 | IsArrow, OpLoc, |
| 2496 | SS, FirstQualifierInScope, |
| 2497 | Name, NameLoc, |
| 2498 | TemplateArgs); |
| 2499 | |
| 2500 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2501 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2502 | // Implicit member accesses. |
| 2503 | if (!Base) { |
| 2504 | QualType RecordTy = BaseType; |
| 2505 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 2506 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 2507 | RecordTy->getAs<RecordType>(), |
| 2508 | OpLoc, SS)) |
| 2509 | return ExprError(); |
| 2510 | |
| 2511 | // Explicit member accesses. |
| 2512 | } else { |
| 2513 | OwningExprResult Result = |
| 2514 | LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2515 | SS, /*ObjCImpDecl*/ DeclPtrTy()); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2516 | |
| 2517 | if (Result.isInvalid()) { |
| 2518 | Owned(Base); |
| 2519 | return ExprError(); |
| 2520 | } |
| 2521 | |
| 2522 | if (Result.get()) |
| 2523 | return move(Result); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2526 | return BuildMemberReferenceExpr(ExprArg(*this, Base), BaseType, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2527 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 2528 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
| 2531 | Sema::OwningExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2532 | Sema::BuildMemberReferenceExpr(ExprArg Base, QualType BaseExprType, |
| 2533 | SourceLocation OpLoc, bool IsArrow, |
| 2534 | const CXXScopeSpec &SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2535 | NamedDecl *FirstQualifierInScope, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2536 | LookupResult &R, |
| 2537 | const TemplateArgumentListInfo *TemplateArgs) { |
| 2538 | Expr *BaseExpr = Base.takeAs<Expr>(); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2539 | QualType BaseType = BaseExprType; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2540 | if (IsArrow) { |
| 2541 | assert(BaseType->isPointerType()); |
| 2542 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2543 | } |
| 2544 | |
| 2545 | NestedNameSpecifier *Qualifier = |
| 2546 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()); |
| 2547 | DeclarationName MemberName = R.getLookupName(); |
| 2548 | SourceLocation MemberLoc = R.getNameLoc(); |
| 2549 | |
| 2550 | if (R.isAmbiguous()) |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2551 | return ExprError(); |
| 2552 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2553 | if (R.empty()) { |
| 2554 | // Rederive where we looked up. |
| 2555 | DeclContext *DC = (SS.isSet() |
| 2556 | ? computeDeclContext(SS, false) |
| 2557 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2558 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2559 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2560 | << MemberName << DC |
| 2561 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2562 | return ExprError(); |
| 2563 | } |
| 2564 | |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2565 | // Diagnose lookups that find only declarations from a non-base |
| 2566 | // type. This is possible for either qualified lookups (which may |
| 2567 | // have been qualified with an unrelated type) or implicit member |
| 2568 | // expressions (which were found with unqualified lookup and thus |
| 2569 | // may have come from an enclosing scope). Note that it's okay for |
| 2570 | // lookup to find declarations from a non-base type as long as those |
| 2571 | // aren't the ones picked by overload resolution. |
| 2572 | if ((SS.isSet() || !BaseExpr || |
| 2573 | (isa<CXXThisExpr>(BaseExpr) && |
| 2574 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
| 2575 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2576 | return ExprError(); |
| 2577 | |
| 2578 | // Construct an unresolved result if we in fact got an unresolved |
| 2579 | // result. |
| 2580 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2581 | bool Dependent = |
John McCall | 410a3f3 | 2009-12-19 02:05:44 +0000 | [diff] [blame] | 2582 | BaseExprType->isDependentType() || |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2583 | R.isUnresolvableResult() || |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2584 | OverloadExpr::ComputeDependence(R.begin(), R.end(), TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2585 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2586 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 2587 | // pick a member. |
| 2588 | R.suppressDiagnostics(); |
| 2589 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2590 | UnresolvedMemberExpr *MemExpr |
| 2591 | = UnresolvedMemberExpr::Create(Context, Dependent, |
| 2592 | R.isUnresolvableResult(), |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2593 | BaseExpr, BaseExprType, |
| 2594 | IsArrow, OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2595 | Qualifier, SS.getRange(), |
| 2596 | MemberName, MemberLoc, |
| 2597 | TemplateArgs); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2598 | MemExpr->addDecls(R.begin(), R.end()); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2599 | |
| 2600 | return Owned(MemExpr); |
| 2601 | } |
| 2602 | |
| 2603 | assert(R.isSingleResult()); |
| 2604 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 2605 | |
| 2606 | // FIXME: diagnose the presence of template arguments now. |
| 2607 | |
| 2608 | // If the decl being referenced had an error, return an error for this |
| 2609 | // sub-expr without emitting another error, in order to avoid cascading |
| 2610 | // error cases. |
| 2611 | if (MemberDecl->isInvalidDecl()) |
| 2612 | return ExprError(); |
| 2613 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2614 | // Handle the implicit-member-access case. |
| 2615 | if (!BaseExpr) { |
| 2616 | // If this is not an instance member, convert to a non-member access. |
| 2617 | if (!IsInstanceMember(MemberDecl)) |
| 2618 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), MemberDecl); |
| 2619 | |
Douglas Gregor | 828a197 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2620 | SourceLocation Loc = R.getNameLoc(); |
| 2621 | if (SS.getRange().isValid()) |
| 2622 | Loc = SS.getRange().getBegin(); |
| 2623 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2626 | bool ShouldCheckUse = true; |
| 2627 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2628 | // Don't diagnose the use of a virtual member function unless it's |
| 2629 | // explicitly qualified. |
| 2630 | if (MD->isVirtual() && !SS.isSet()) |
| 2631 | ShouldCheckUse = false; |
| 2632 | } |
| 2633 | |
| 2634 | // Check the use of this member. |
| 2635 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 2636 | Owned(BaseExpr); |
| 2637 | return ExprError(); |
| 2638 | } |
| 2639 | |
| 2640 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
| 2641 | // We may have found a field within an anonymous union or struct |
| 2642 | // (C++ [class.union]). |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 2643 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion() && |
| 2644 | !BaseType->getAs<RecordType>()->getDecl()->isAnonymousStructOrUnion()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2645 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
| 2646 | BaseExpr, OpLoc); |
| 2647 | |
| 2648 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2649 | QualType MemberType = FD->getType(); |
| 2650 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
| 2651 | MemberType = Ref->getPointeeType(); |
| 2652 | else { |
| 2653 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2654 | BaseQuals.removeObjCGCAttr(); |
| 2655 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2656 | |
| 2657 | Qualifiers MemberQuals |
| 2658 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2659 | |
| 2660 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2661 | if (Combined != MemberQuals) |
| 2662 | MemberType = Context.getQualifiedType(MemberType, Combined); |
| 2663 | } |
| 2664 | |
| 2665 | MarkDeclarationReferenced(MemberLoc, FD); |
| 2666 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2667 | return ExprError(); |
| 2668 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2669 | FD, MemberLoc, MemberType)); |
| 2670 | } |
| 2671 | |
| 2672 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2673 | MarkDeclarationReferenced(MemberLoc, Var); |
| 2674 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2675 | Var, MemberLoc, |
| 2676 | Var->getType().getNonReferenceType())); |
| 2677 | } |
| 2678 | |
| 2679 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2680 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2681 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2682 | MemberFn, MemberLoc, |
| 2683 | MemberFn->getType())); |
| 2684 | } |
| 2685 | |
| 2686 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2687 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 2688 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| 2689 | Enum, MemberLoc, Enum->getType())); |
| 2690 | } |
| 2691 | |
| 2692 | Owned(BaseExpr); |
| 2693 | |
| 2694 | if (isa<TypeDecl>(MemberDecl)) |
| 2695 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
| 2696 | << MemberName << int(IsArrow)); |
| 2697 | |
| 2698 | // We found a declaration kind that we didn't expect. This is a |
| 2699 | // generic error message that tells the user that she can't refer |
| 2700 | // to this member with '.' or '->'. |
| 2701 | return ExprError(Diag(MemberLoc, |
| 2702 | diag::err_typecheck_member_reference_unknown) |
| 2703 | << MemberName << int(IsArrow)); |
| 2704 | } |
| 2705 | |
| 2706 | /// Look up the given member of the given non-type-dependent |
| 2707 | /// expression. This can return in one of two ways: |
| 2708 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 2709 | /// assume that lookup was performed and the results written into |
| 2710 | /// the provided structure. It will take over from there. |
| 2711 | /// * Otherwise, the returned expression will be produced in place of |
| 2712 | /// an ordinary member expression. |
| 2713 | /// |
| 2714 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 2715 | /// fixed for ObjC++. |
| 2716 | Sema::OwningExprResult |
| 2717 | Sema::LookupMemberExpr(LookupResult &R, Expr *&BaseExpr, |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2718 | bool &IsArrow, SourceLocation OpLoc, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2719 | const CXXScopeSpec &SS, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2720 | DeclPtrTy ObjCImpDecl) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2721 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2723 | // Perform default conversions. |
| 2724 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2725 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2726 | QualType BaseType = BaseExpr->getType(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2727 | assert(!BaseType->isDependentType()); |
| 2728 | |
| 2729 | DeclarationName MemberName = R.getLookupName(); |
| 2730 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2731 | |
| 2732 | // If the user is trying to apply -> or . to a function pointer |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2733 | // type, it's probably because they forgot parentheses to call that |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2734 | // function. Suggest the addition of those parentheses, build the |
| 2735 | // call, and continue on. |
| 2736 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 2737 | if (const FunctionProtoType *Fun |
| 2738 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 2739 | QualType ResultTy = Fun->getResultType(); |
| 2740 | if (Fun->getNumArgs() == 0 && |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2741 | ((!IsArrow && ResultTy->isRecordType()) || |
| 2742 | (IsArrow && ResultTy->isPointerType() && |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2743 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 2744 | ->isRecordType()))) { |
| 2745 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2746 | Diag(Loc, diag::err_member_reference_needs_call) |
| 2747 | << QualType(Fun, 0) |
| 2748 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2749 | |
| 2750 | OwningExprResult NewBase |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2751 | = ActOnCallExpr(0, ExprArg(*this, BaseExpr), Loc, |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2752 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 2753 | if (NewBase.isInvalid()) |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2754 | return ExprError(); |
Douglas Gregor | 3f0b5fd | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 2755 | |
| 2756 | BaseExpr = NewBase.takeAs<Expr>(); |
| 2757 | DefaultFunctionArrayConversion(BaseExpr); |
| 2758 | BaseType = BaseExpr->getType(); |
| 2759 | } |
| 2760 | } |
| 2761 | } |
| 2762 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2763 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2764 | // use that. |
| 2765 | if (BaseType->isObjCIdType()) { |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2766 | if (IsArrow) { |
| 2767 | // Handle the following exceptional case PObj->isa. |
| 2768 | if (const ObjCObjectPointerType *OPT = |
| 2769 | BaseType->getAs<ObjCObjectPointerType>()) { |
| 2770 | if (OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCId) && |
| 2771 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 2772 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, true, MemberLoc, |
| 2773 | Context.getObjCClassType())); |
Fariborz Jahanian | 6d910f0 | 2009-12-07 20:09:25 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2776 | // We have an 'id' type. Rather than fall through, we check if this |
| 2777 | // is a reference to 'isa'. |
| 2778 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2779 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2780 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2781 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2782 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2783 | |
Fariborz Jahanian | 369a3bd | 2009-11-25 23:07:42 +0000 | [diff] [blame] | 2784 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2785 | // use that. |
| 2786 | if (Context.isObjCSelType(BaseType)) { |
| 2787 | // We have an 'SEL' type. Rather than fall through, we check if this |
| 2788 | // is a reference to 'sel_id'. |
| 2789 | if (BaseType != Context.ObjCSelRedefinitionType) { |
| 2790 | BaseType = Context.ObjCSelRedefinitionType; |
| 2791 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
| 2792 | } |
| 2793 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2794 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2795 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2796 | |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2797 | // Handle properties on ObjC 'Class' types. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2798 | if (!IsArrow && BaseType->isObjCClassType()) { |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2799 | // Also must look for a getter name which uses property syntax. |
| 2800 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2801 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 2802 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2803 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2804 | ObjCMethodDecl *Getter; |
| 2805 | // FIXME: need to also look locally in the implementation. |
| 2806 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2807 | // Check the use of this method. |
| 2808 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2809 | return ExprError(); |
| 2810 | } |
| 2811 | // If we found a getter then this may be a valid dot-reference, we |
| 2812 | // will look for the matching setter, in case it is needed. |
| 2813 | Selector SetterSel = |
| 2814 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 2815 | PP.getSelectorTable(), Member); |
| 2816 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2817 | if (!Setter) { |
| 2818 | // If this reference is in an @implementation, also check for 'private' |
| 2819 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2820 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2821 | } |
| 2822 | // Look through local category implementations associated with the class. |
| 2823 | if (!Setter) |
| 2824 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 2825 | |
| 2826 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2827 | return ExprError(); |
| 2828 | |
| 2829 | if (Getter || Setter) { |
| 2830 | QualType PType; |
| 2831 | |
| 2832 | if (Getter) |
| 2833 | PType = Getter->getResultType(); |
| 2834 | else |
| 2835 | // Get the expression type from Setter's incoming parameter. |
| 2836 | PType = (*(Setter->param_end() -1))->getType(); |
| 2837 | // FIXME: we must check that the setter has property type. |
| 2838 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 2839 | PType, |
| 2840 | Setter, MemberLoc, BaseExpr)); |
| 2841 | } |
| 2842 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 2843 | << MemberName << BaseType); |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | if (BaseType->isObjCClassType() && |
| 2848 | BaseType != Context.ObjCClassRedefinitionType) { |
| 2849 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 2850 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | b2ef1be | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2852 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2853 | if (IsArrow) { |
| 2854 | if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2855 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2856 | else if (BaseType->isObjCObjectPointerType()) |
| 2857 | ; |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2858 | else if (BaseType->isRecordType()) { |
| 2859 | // Recover from arrow accesses to records, e.g.: |
| 2860 | // struct MyRecord foo; |
| 2861 | // foo->bar |
| 2862 | // This is actually well-formed in C++ if MyRecord has an |
| 2863 | // overloaded operator->, but that should have been dealt with |
| 2864 | // by now. |
| 2865 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2866 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2867 | << CodeModificationHint::CreateReplacement(OpLoc, "."); |
| 2868 | IsArrow = false; |
| 2869 | } else { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2870 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| 2871 | << BaseType << BaseExpr->getSourceRange(); |
| 2872 | return ExprError(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2873 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2874 | } else { |
| 2875 | // Recover from dot accesses to pointers, e.g.: |
| 2876 | // type *foo; |
| 2877 | // foo.bar |
| 2878 | // This is actually well-formed in two cases: |
| 2879 | // - 'type' is an Objective C type |
| 2880 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 2881 | // the appropriate pointer type |
| 2882 | if (MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| 2883 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| 2884 | if (PT && PT->getPointeeType()->isRecordType()) { |
| 2885 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| 2886 | << BaseType << int(IsArrow) << BaseExpr->getSourceRange() |
| 2887 | << CodeModificationHint::CreateReplacement(OpLoc, "->"); |
| 2888 | BaseType = PT->getPointeeType(); |
| 2889 | IsArrow = true; |
| 2890 | } |
| 2891 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2892 | } |
John McCall | 812c154 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 2893 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2894 | // Handle field access to simple records. This also handles access |
| 2895 | // to fields of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2896 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2897 | if (LookupMemberExprInRecord(*this, R, BaseExpr->getSourceRange(), |
| 2898 | RTy, OpLoc, SS)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2899 | return ExprError(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2900 | return Owned((Expr*) 0); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2901 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2902 | |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2903 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2904 | // (*Obj).ivar. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2905 | if ((IsArrow && BaseType->isObjCObjectPointerType()) || |
| 2906 | (!IsArrow && BaseType->isObjCInterfaceType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2907 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | const ObjCInterfaceType *IFaceT = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2909 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2910 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2911 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2912 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2913 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2914 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2915 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2916 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2917 | if (!IV) { |
| 2918 | // Attempt to correct for typos in ivar names. |
| 2919 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 2920 | LookupMemberName); |
| 2921 | if (CorrectTypo(Res, 0, 0, IDecl) && |
| 2922 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 2923 | Diag(R.getNameLoc(), |
| 2924 | diag::err_typecheck_member_reference_ivar_suggest) |
| 2925 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 2926 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 2927 | IV->getNameAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 2928 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 2929 | << IV->getDeclName(); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2933 | if (IV) { |
| 2934 | // If the decl being referenced had an error, return an error for this |
| 2935 | // sub-expr without emitting another error, in order to avoid cascading |
| 2936 | // error cases. |
| 2937 | if (IV->isInvalidDecl()) |
| 2938 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2939 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2940 | // Check whether we can reference this field. |
| 2941 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2942 | return ExprError(); |
| 2943 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2944 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2945 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2946 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2947 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2948 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2949 | // Case of a c-function declared inside an objc implementation. |
| 2950 | // FIXME: For a c-style function nested inside an objc implementation |
| 2951 | // class, there is no implementation context available, so we pass |
| 2952 | // down the context as argument to this routine. Ideally, this context |
| 2953 | // need be passed down in the AST node and somehow calculated from the |
| 2954 | // AST for a function decl. |
| 2955 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2957 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2958 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2959 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2960 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2961 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2962 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2963 | |
| 2964 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2965 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2966 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2967 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2968 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2969 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2970 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2972 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2973 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2974 | |
| 2975 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2976 | MemberLoc, BaseExpr, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2977 | IsArrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2978 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2979 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2980 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2981 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2982 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2983 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2984 | // Handle properties on 'id' and qualified "id". |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2985 | if (!IsArrow && (BaseType->isObjCIdType() || |
| 2986 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2987 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2988 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2989 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2990 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2991 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2992 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2993 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2994 | // Check the use of this declaration |
| 2995 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2996 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2998 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2999 | MemberLoc, BaseExpr)); |
| 3000 | } |
| 3001 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 3002 | // Check the use of this method. |
| 3003 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 3004 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3005 | |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 3006 | return Owned(new (Context) ObjCMessageExpr(Context, BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | OMD->getResultType(), |
| 3008 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3009 | NULL, 0)); |
| 3010 | } |
| 3011 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3012 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3013 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3014 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3015 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 3016 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 3017 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3018 | const ObjCObjectPointerType *OPT; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3019 | if (!IsArrow && (OPT = BaseType->getAsObjCInterfacePointerType())) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3020 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 3021 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3022 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3023 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3024 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3025 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3026 | // Check whether we can reference this property. |
| 3027 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3028 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 3029 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3030 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3031 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 3032 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 3033 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 3034 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3035 | MemberLoc, BaseExpr)); |
| 3036 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3037 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3038 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 3039 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3040 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3041 | // Check whether we can reference this property. |
| 3042 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 3043 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3044 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3045 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 3046 | MemberLoc, BaseExpr)); |
| 3047 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3048 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 3049 | // selector is implemented. |
| 3050 | |
| 3051 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 3052 | // shared with the code in ActOnInstanceMessage. |
| 3053 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3054 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3055 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3056 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3057 | // If this reference is in an @implementation, check for 'private' methods. |
| 3058 | if (!Getter) |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3059 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3060 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 3061 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3062 | if (!Getter) |
| 3063 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 3064 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 3065 | // Check if we can reference this property. |
| 3066 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 3067 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3068 | } |
| 3069 | // If we found a getter then this may be a valid dot-reference, we |
| 3070 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3071 | Selector SetterSel = |
| 3072 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3073 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3074 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3075 | if (!Setter) { |
| 3076 | // If this reference is in an @implementation, also check for 'private' |
| 3077 | // methods. |
Steve Naroff | d789d3d | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 3078 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3079 | } |
| 3080 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 3081 | if (!Setter) |
| 3082 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3083 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3084 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 3085 | return ExprError(); |
| 3086 | |
Fariborz Jahanian | dd0cb90 | 2010-01-19 17:48:02 +0000 | [diff] [blame] | 3087 | if (Getter) { |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3088 | QualType PType; |
Fariborz Jahanian | dd0cb90 | 2010-01-19 17:48:02 +0000 | [diff] [blame] | 3089 | PType = Getter->getResultType(); |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 3090 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 3091 | Setter, MemberLoc, BaseExpr)); |
| 3092 | } |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3093 | |
| 3094 | // Attempt to correct for typos in property names. |
| 3095 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 3096 | LookupOrdinaryName); |
| 3097 | if (CorrectTypo(Res, 0, 0, IFace, false, OPT) && |
| 3098 | Res.getAsSingle<ObjCPropertyDecl>()) { |
| 3099 | Diag(R.getNameLoc(), diag::err_property_not_found_suggest) |
| 3100 | << MemberName << BaseType << Res.getLookupName() |
| 3101 | << CodeModificationHint::CreateReplacement(R.getNameLoc(), |
| 3102 | Res.getLookupName().getAsString()); |
Douglas Gregor | 67dd1d4 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3103 | ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>(); |
| 3104 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 3105 | << Property->getDeclName(); |
| 3106 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3107 | return LookupMemberExpr(Res, BaseExpr, IsArrow, OpLoc, SS, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3108 | ObjCImpDecl); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 3109 | } |
Fariborz Jahanian | 354095c | 2010-02-19 18:30:30 +0000 | [diff] [blame] | 3110 | Diag(MemberLoc, diag::err_property_not_found) |
| 3111 | << MemberName << BaseType; |
| 3112 | if (Setter && !Getter) |
| 3113 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
| 3114 | << MemberName << BaseExpr->getSourceRange(); |
| 3115 | return ExprError(); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 3116 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3118 | // Handle the following exceptional case (*Obj).isa. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3119 | if (!IsArrow && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3120 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3121 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3122 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
Fariborz Jahanian | 83dc325 | 2009-12-09 19:05:56 +0000 | [diff] [blame] | 3123 | Context.getObjCClassType())); |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 3124 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3125 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 3126 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3127 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3128 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 3129 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3130 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3131 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3132 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 3133 | } |
Fariborz Jahanian | 354095c | 2010-02-19 18:30:30 +0000 | [diff] [blame] | 3134 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3135 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 3136 | << BaseType << BaseExpr->getSourceRange(); |
| 3137 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 3138 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3141 | /// The main callback when the parser finds something like |
| 3142 | /// expression . [nested-name-specifier] identifier |
| 3143 | /// expression -> [nested-name-specifier] identifier |
| 3144 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 3145 | /// possibilities, including destructor and operator references. |
| 3146 | /// |
| 3147 | /// \param OpKind either tok::arrow or tok::period |
| 3148 | /// \param HasTrailingLParen whether the next token is '(', which |
| 3149 | /// is used to diagnose mis-uses of special members that can |
| 3150 | /// only be called |
| 3151 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 3152 | /// this is an ugly hack around the fact that ObjC @implementations |
| 3153 | /// aren't properly put in the context chain |
| 3154 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg BaseArg, |
| 3155 | SourceLocation OpLoc, |
| 3156 | tok::TokenKind OpKind, |
| 3157 | const CXXScopeSpec &SS, |
| 3158 | UnqualifiedId &Id, |
| 3159 | DeclPtrTy ObjCImpDecl, |
| 3160 | bool HasTrailingLParen) { |
| 3161 | if (SS.isSet() && SS.isInvalid()) |
| 3162 | return ExprError(); |
| 3163 | |
| 3164 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 3165 | |
| 3166 | // Decompose the name into its component parts. |
| 3167 | DeclarationName Name; |
| 3168 | SourceLocation NameLoc; |
| 3169 | const TemplateArgumentListInfo *TemplateArgs; |
| 3170 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| 3171 | Name, NameLoc, TemplateArgs); |
| 3172 | |
| 3173 | bool IsArrow = (OpKind == tok::arrow); |
| 3174 | |
| 3175 | NamedDecl *FirstQualifierInScope |
| 3176 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 3177 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 3178 | |
| 3179 | // This is a postfix expression, so get rid of ParenListExprs. |
| 3180 | BaseArg = MaybeConvertParenListExprToParenExpr(S, move(BaseArg)); |
| 3181 | |
| 3182 | Expr *Base = BaseArg.takeAs<Expr>(); |
| 3183 | OwningExprResult Result(*this); |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3184 | if (Base->getType()->isDependentType() || Name.isDependentName()) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3185 | Result = ActOnDependentMemberExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3186 | IsArrow, OpLoc, |
| 3187 | SS, FirstQualifierInScope, |
| 3188 | Name, NameLoc, |
| 3189 | TemplateArgs); |
| 3190 | } else { |
| 3191 | LookupResult R(*this, Name, NameLoc, LookupMemberName); |
| 3192 | if (TemplateArgs) { |
| 3193 | // Re-use the lookup done for the template name. |
| 3194 | DecomposeTemplateName(R, Id); |
| 3195 | } else { |
| 3196 | Result = LookupMemberExpr(R, Base, IsArrow, OpLoc, |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3197 | SS, ObjCImpDecl); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3198 | |
| 3199 | if (Result.isInvalid()) { |
| 3200 | Owned(Base); |
| 3201 | return ExprError(); |
| 3202 | } |
| 3203 | |
| 3204 | if (Result.get()) { |
| 3205 | // The only way a reference to a destructor can be used is to |
| 3206 | // immediately call it, which falls into this case. If the |
| 3207 | // next token is not a '(', produce a diagnostic and build the |
| 3208 | // call now. |
| 3209 | if (!HasTrailingLParen && |
| 3210 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
Douglas Gregor | 7754908 | 2010-02-24 21:29:12 +0000 | [diff] [blame] | 3211 | return DiagnoseDtorReference(NameLoc, move(Result)); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3212 | |
| 3213 | return move(Result); |
| 3214 | } |
| 3215 | } |
| 3216 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3217 | Result = BuildMemberReferenceExpr(ExprArg(*this, Base), Base->getType(), |
John McCall | c2233c5 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3218 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3219 | R, TemplateArgs); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | return move(Result); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3225 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 3226 | FunctionDecl *FD, |
| 3227 | ParmVarDecl *Param) { |
| 3228 | if (Param->hasUnparsedDefaultArg()) { |
| 3229 | Diag (CallLoc, |
| 3230 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 3231 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3233 | diag::note_default_argument_declared_here); |
| 3234 | } else { |
| 3235 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3236 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 3237 | |
| 3238 | // Instantiate the expression. |
Douglas Gregor | 525f96c | 2010-02-05 07:33:43 +0000 | [diff] [blame] | 3239 | MultiLevelTemplateArgumentList ArgList |
| 3240 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3241 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3242 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3243 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 3244 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3245 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 3246 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3247 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3248 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3249 | |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3250 | // Check the expression as an initializer for the parameter. |
| 3251 | InitializedEntity Entity |
| 3252 | = InitializedEntity::InitializeParameter(Param); |
| 3253 | InitializationKind Kind |
| 3254 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3255 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3256 | Expr *ResultE = Result.takeAs<Expr>(); |
| 3257 | |
| 3258 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3259 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3260 | MultiExprArg(*this, (void**)&ResultE, 1)); |
| 3261 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3262 | return ExprError(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3263 | |
| 3264 | // Build the default argument expression. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3265 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3266 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3267 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3268 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3269 | // If the default expression creates temporaries, we need to |
| 3270 | // push them to the current stack of expression temporaries so they'll |
| 3271 | // be properly destroyed. |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3272 | // FIXME: We should really be rebuilding the default argument with new |
| 3273 | // bound temporaries; see the comment in PR5810. |
Anders Carlsson | 337cba4 | 2009-12-15 19:16:31 +0000 | [diff] [blame] | 3274 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) |
| 3275 | ExprTemporaries.push_back(Param->getDefaultArgTemporary(i)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3279 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3280 | } |
| 3281 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3282 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3283 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3284 | /// function prototype Proto. Call is the call expression itself, and |
| 3285 | /// Fn is the function expression. For a C++ member function, this |
| 3286 | /// routine does not attempt to convert the object argument. Returns |
| 3287 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3288 | bool |
| 3289 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3290 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3291 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3292 | Expr **Args, unsigned NumArgs, |
| 3293 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3294 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3295 | // assignment, to the types of the corresponding parameter, ... |
| 3296 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3297 | bool Invalid = false; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3298 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3299 | // If too few arguments are available (and we don't have default |
| 3300 | // arguments for the remaining parameters), don't make the call. |
| 3301 | if (NumArgs < NumArgsInProto) { |
| 3302 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 3303 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 3304 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3305 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | // If too many are passed and not variadic, error on the extras and drop |
| 3309 | // them. |
| 3310 | if (NumArgs > NumArgsInProto) { |
| 3311 | if (!Proto->isVariadic()) { |
| 3312 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3313 | diag::err_typecheck_call_too_many_args) |
| 3314 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 3315 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3316 | Args[NumArgs-1]->getLocEnd()); |
| 3317 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3318 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3319 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3320 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3321 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3322 | llvm::SmallVector<Expr *, 8> AllArgs; |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3323 | VariadicCallType CallType = |
| 3324 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3325 | if (Fn->getType()->isBlockPointerType()) |
| 3326 | CallType = VariadicBlock; // Block |
| 3327 | else if (isa<MemberExpr>(Fn)) |
| 3328 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3329 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3330 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3331 | if (Invalid) |
| 3332 | return true; |
| 3333 | unsigned TotalNumArgs = AllArgs.size(); |
| 3334 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3335 | Call->setArg(i, AllArgs[i]); |
| 3336 | |
| 3337 | return false; |
| 3338 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3339 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3340 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3341 | FunctionDecl *FDecl, |
| 3342 | const FunctionProtoType *Proto, |
| 3343 | unsigned FirstProtoArg, |
| 3344 | Expr **Args, unsigned NumArgs, |
| 3345 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3346 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3347 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3348 | unsigned NumArgsToCheck = NumArgs; |
| 3349 | bool Invalid = false; |
| 3350 | if (NumArgs != NumArgsInProto) |
| 3351 | // Use default arguments for missing arguments |
| 3352 | NumArgsToCheck = NumArgsInProto; |
| 3353 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3354 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3355 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3356 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3357 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3358 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3359 | if (ArgIx < NumArgs) { |
| 3360 | Arg = Args[ArgIx++]; |
| 3361 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3362 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3363 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3364 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3365 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3366 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3367 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3368 | // Pass the argument |
| 3369 | ParmVarDecl *Param = 0; |
| 3370 | if (FDecl && i < FDecl->getNumParams()) |
| 3371 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3373 | |
| 3374 | InitializedEntity Entity = |
| 3375 | Param? InitializedEntity::InitializeParameter(Param) |
| 3376 | : InitializedEntity::InitializeParameter(ProtoArgType); |
| 3377 | OwningExprResult ArgE = PerformCopyInitialization(Entity, |
| 3378 | SourceLocation(), |
| 3379 | Owned(Arg)); |
| 3380 | if (ArgE.isInvalid()) |
| 3381 | return true; |
| 3382 | |
| 3383 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3384 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3385 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3386 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3388 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3389 | if (ArgExpr.isInvalid()) |
| 3390 | return true; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3391 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3392 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3393 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3394 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3395 | } |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3396 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3397 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3398 | if (CallType != VariadicDoesNotApply) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3399 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3400 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3401 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 3402 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3403 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3404 | } |
| 3405 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3406 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3409 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3410 | /// This provides the location of the left/right parens and a list of comma |
| 3411 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3412 | Action::OwningExprResult |
| 3413 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 3414 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3415 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3416 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3417 | |
| 3418 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 3419 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 3421 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3422 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 3423 | assert(Fn && "no function call expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3424 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3425 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3426 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3427 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3428 | if (NumArgs > 0) { |
| 3429 | // Pseudo-destructor calls should not have any arguments. |
| 3430 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 3431 | << CodeModificationHint::CreateRemoval( |
| 3432 | SourceRange(Args[0]->getLocStart(), |
| 3433 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3435 | for (unsigned I = 0; I != NumArgs; ++I) |
| 3436 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3438 | NumArgs = 0; |
| 3439 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3440 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3441 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 3442 | RParenLoc)); |
| 3443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3445 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3446 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3447 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3448 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3449 | bool Dependent = false; |
| 3450 | if (Fn->isTypeDependent()) |
| 3451 | Dependent = true; |
| 3452 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3453 | Dependent = true; |
| 3454 | |
| 3455 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3456 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3457 | Context.DependentTy, RParenLoc)); |
| 3458 | |
| 3459 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3460 | if (Fn->getType()->isRecordType()) |
| 3461 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 3462 | CommaLocs, RParenLoc)); |
| 3463 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3464 | Expr *NakedFn = Fn->IgnoreParens(); |
| 3465 | |
| 3466 | // Determine whether this is a call to an unresolved member function. |
| 3467 | if (UnresolvedMemberExpr *MemE = dyn_cast<UnresolvedMemberExpr>(NakedFn)) { |
| 3468 | // If lookup was unresolved but not dependent (i.e. didn't find |
| 3469 | // an unresolved using declaration), it has to be an overloaded |
| 3470 | // function set, which means it must contain either multiple |
| 3471 | // declarations (all methods or method templates) or a single |
| 3472 | // method template. |
| 3473 | assert((MemE->getNumDecls() > 1) || |
| 3474 | isa<FunctionTemplateDecl>(*MemE->decls_begin())); |
Douglas Gregor | 958aeb0 | 2009-12-01 03:34:29 +0000 | [diff] [blame] | 3475 | (void)MemE; |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3476 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3477 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3478 | CommaLocs, RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3481 | // Determine whether this is a call to a member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3482 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(NakedFn)) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3483 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3484 | if (isa<CXXMethodDecl>(MemDecl)) |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3485 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 3486 | CommaLocs, RParenLoc); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3487 | } |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3488 | |
| 3489 | // Determine whether this is a call to a pointer-to-member function. |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3490 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(NakedFn)) { |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3491 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 3492 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3493 | if (const FunctionProtoType *FPT = |
| 3494 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 3495 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3496 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3497 | ExprOwningPtr<CXXMemberCallExpr> |
| 3498 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 3499 | NumArgs, ResultTy, |
| 3500 | RParenLoc)); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3501 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3502 | if (CheckCallReturnType(FPT->getResultType(), |
| 3503 | BO->getRHS()->getSourceRange().getBegin(), |
| 3504 | TheCall.get(), 0)) |
| 3505 | return ExprError(); |
Anders Carlsson | 8d6d90d | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 3506 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3507 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 3508 | RParenLoc)) |
| 3509 | return ExprError(); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3510 | |
Fariborz Jahanian | 5de2450 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 3511 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 3512 | } |
| 3513 | return ExprError(Diag(Fn->getLocStart(), |
| 3514 | diag::err_typecheck_call_not_function) |
| 3515 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3516 | } |
| 3517 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3520 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3521 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3522 | // lookup and whether there were any explicitly-specified template arguments. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3523 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3524 | Expr *NakedFn = Fn->IgnoreParens(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3525 | if (isa<UnresolvedLookupExpr>(NakedFn)) { |
| 3526 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(NakedFn); |
| 3527 | return BuildOverloadedCallExpr(Fn, ULE, LParenLoc, Args, NumArgs, |
| 3528 | CommaLocs, RParenLoc); |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 3529 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3530 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3531 | NamedDecl *NDecl = 0; |
| 3532 | if (isa<DeclRefExpr>(NakedFn)) |
| 3533 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| 3534 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3535 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc); |
| 3536 | } |
| 3537 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3538 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3539 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3540 | /// unary-convert to an expression of function-pointer or |
| 3541 | /// block-pointer type. |
| 3542 | /// |
| 3543 | /// \param NDecl the declaration being called, if available |
| 3544 | Sema::OwningExprResult |
| 3545 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3546 | SourceLocation LParenLoc, |
| 3547 | Expr **Args, unsigned NumArgs, |
| 3548 | SourceLocation RParenLoc) { |
| 3549 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3550 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3551 | // Promote the function operand. |
| 3552 | UsualUnaryConversions(Fn); |
| 3553 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3554 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3555 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 3556 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 3557 | Args, NumArgs, |
| 3558 | Context.BoolTy, |
| 3559 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3560 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3561 | const FunctionType *FuncT; |
| 3562 | if (!Fn->getType()->isBlockPointerType()) { |
| 3563 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3564 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3565 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3566 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3567 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3568 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3569 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3570 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3571 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3572 | getAs<FunctionType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3573 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3574 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3575 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3576 | << Fn->getType() << Fn->getSourceRange()); |
| 3577 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3578 | // Check for a valid return type |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3579 | if (CheckCallReturnType(FuncT->getResultType(), |
| 3580 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 3581 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3582 | return ExprError(); |
| 3583 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3584 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 3585 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3586 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3587 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3588 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3589 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3590 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3591 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3592 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3594 | if (FDecl) { |
| 3595 | // Check if we have too few/too many template arguments, based |
| 3596 | // on our knowledge of the function definition. |
| 3597 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 3598 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3599 | const FunctionProtoType *Proto = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3600 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3601 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 3602 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3603 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 3604 | } |
| 3605 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3608 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3609 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3610 | Expr *Arg = Args[i]; |
| 3611 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3612 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3613 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3614 | PDiag(diag::err_call_incomplete_argument) |
| 3615 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3616 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3617 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3618 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3619 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3620 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3621 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3622 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3623 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3624 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3625 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3626 | // Check for sentinels |
| 3627 | if (NDecl) |
| 3628 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3629 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3630 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3631 | if (FDecl) { |
| 3632 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3633 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3635 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3636 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3637 | } else if (NDecl) { |
| 3638 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3639 | return ExprError(); |
| 3640 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3641 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3642 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3645 | Action::OwningExprResult |
| 3646 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3647 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3648 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3649 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3650 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3651 | |
| 3652 | TypeSourceInfo *TInfo; |
| 3653 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3654 | if (!TInfo) |
| 3655 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3656 | |
| 3657 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, move(InitExpr)); |
| 3658 | } |
| 3659 | |
| 3660 | Action::OwningExprResult |
| 3661 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
| 3662 | SourceLocation RParenLoc, ExprArg InitExpr) { |
| 3663 | QualType literalType = TInfo->getType(); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3664 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3665 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3666 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3667 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3668 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3669 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3670 | } else if (!literalType->isDependentType() && |
| 3671 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3672 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3674 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3675 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3676 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3677 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3678 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3679 | InitializationKind Kind |
| 3680 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| 3681 | /*IsCStyleCast=*/true); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3682 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
| 3683 | OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| 3684 | MultiExprArg(*this, (void**)&literalExpr, 1), |
| 3685 | &literalType); |
| 3686 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3687 | return ExprError(); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3688 | InitExpr.release(); |
| 3689 | literalExpr = static_cast<Expr*>(Result.get()); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3690 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3691 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3692 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3693 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3694 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3695 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3696 | |
| 3697 | Result.release(); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3698 | |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 3699 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3700 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3701 | } |
| 3702 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3703 | Action::OwningExprResult |
| 3704 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3705 | SourceLocation RBraceLoc) { |
| 3706 | unsigned NumInit = initlist.size(); |
| 3707 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3708 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3709 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3710 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3711 | |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 3712 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
| 3713 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3714 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3715 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3718 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3719 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3720 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3721 | return CastExpr::CK_NoOp; |
| 3722 | |
| 3723 | if (SrcTy->hasPointerRepresentation()) { |
| 3724 | if (DestTy->hasPointerRepresentation()) |
Fariborz Jahanian | a7fa7cd | 2009-12-15 21:34:52 +0000 | [diff] [blame] | 3725 | return DestTy->isObjCObjectPointerType() ? |
| 3726 | CastExpr::CK_AnyPointerToObjCPointerCast : |
| 3727 | CastExpr::CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3728 | if (DestTy->isIntegerType()) |
| 3729 | return CastExpr::CK_PointerToIntegral; |
| 3730 | } |
| 3731 | |
| 3732 | if (SrcTy->isIntegerType()) { |
| 3733 | if (DestTy->isIntegerType()) |
| 3734 | return CastExpr::CK_IntegralCast; |
| 3735 | if (DestTy->hasPointerRepresentation()) |
| 3736 | return CastExpr::CK_IntegralToPointer; |
| 3737 | if (DestTy->isRealFloatingType()) |
| 3738 | return CastExpr::CK_IntegralToFloating; |
| 3739 | } |
| 3740 | |
| 3741 | if (SrcTy->isRealFloatingType()) { |
| 3742 | if (DestTy->isRealFloatingType()) |
| 3743 | return CastExpr::CK_FloatingCast; |
| 3744 | if (DestTy->isIntegerType()) |
| 3745 | return CastExpr::CK_FloatingToIntegral; |
| 3746 | } |
| 3747 | |
| 3748 | // FIXME: Assert here. |
| 3749 | // assert(false && "Unhandled cast combination!"); |
| 3750 | return CastExpr::CK_Unknown; |
| 3751 | } |
| 3752 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3753 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3754 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3756 | CXXMethodDecl *& ConversionDecl, |
| 3757 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3758 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3759 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3760 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3762 | DefaultFunctionArrayLvalueConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3763 | |
| 3764 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3765 | // type needs to be scalar. |
| 3766 | if (castType->isVoidType()) { |
| 3767 | // Cast to void allows any expr type. |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3768 | Kind = CastExpr::CK_ToVoid; |
| 3769 | return false; |
| 3770 | } |
| 3771 | |
| 3772 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3773 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3774 | (castType->isStructureType() || castType->isUnionType())) { |
| 3775 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3776 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3777 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3778 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3779 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3780 | return false; |
| 3781 | } |
| 3782 | |
| 3783 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3784 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3785 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3786 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3787 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3788 | Field != FieldEnd; ++Field) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3789 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3790 | castExpr->getType())) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3791 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3792 | << castExpr->getSourceRange(); |
| 3793 | break; |
| 3794 | } |
| 3795 | } |
| 3796 | if (Field == FieldEnd) |
| 3797 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3798 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3799 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3800 | return false; |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3801 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3802 | |
| 3803 | // Reject any other conversions to non-scalar types. |
| 3804 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3805 | << castType << castExpr->getSourceRange(); |
| 3806 | } |
| 3807 | |
| 3808 | if (!castExpr->getType()->isScalarType() && |
| 3809 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3810 | return Diag(castExpr->getLocStart(), |
| 3811 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3812 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3813 | } |
| 3814 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3815 | if (castType->isExtVectorType()) |
| 3816 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3817 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3818 | if (castType->isVectorType()) |
| 3819 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3820 | if (castExpr->getType()->isVectorType()) |
| 3821 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3822 | |
| 3823 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3824 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3825 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3826 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3827 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3828 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3829 | if (!castType->isArithmeticType()) { |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3830 | QualType castExprType = castExpr->getType(); |
| 3831 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3832 | return Diag(castExpr->getLocStart(), |
| 3833 | diag::err_cast_pointer_from_non_pointer_int) |
| 3834 | << castExprType << castExpr->getSourceRange(); |
| 3835 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3836 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3837 | return Diag(castExpr->getLocStart(), |
| 3838 | diag::err_cast_pointer_to_non_pointer_int) |
| 3839 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3840 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3841 | |
| 3842 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3843 | return false; |
| 3844 | } |
| 3845 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3846 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3847 | CastExpr::CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3848 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3849 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3850 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3851 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3852 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3853 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3854 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3855 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3856 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3857 | } else |
| 3858 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3859 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3860 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3861 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3862 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3863 | return false; |
| 3864 | } |
| 3865 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3866 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3867 | CastExpr::CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3868 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3869 | |
| 3870 | QualType SrcTy = CastExpr->getType(); |
| 3871 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3872 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3873 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3874 | if (SrcTy->isVectorType()) { |
| 3875 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3876 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3877 | << DestTy << SrcTy << R; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3878 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3879 | return false; |
| 3880 | } |
| 3881 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3882 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3883 | // conversion will take place first from scalar to elt type, and then |
| 3884 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3885 | if (SrcTy->isPointerType()) |
| 3886 | return Diag(R.getBegin(), |
| 3887 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3888 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3889 | |
| 3890 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3891 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3892 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3893 | |
| 3894 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3895 | return false; |
| 3896 | } |
| 3897 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3898 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3899 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3900 | SourceLocation RParenLoc, ExprArg Op) { |
| 3901 | assert((Ty != 0) && (Op.get() != 0) && |
| 3902 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3903 | |
John McCall | 9d12503 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 3904 | TypeSourceInfo *castTInfo; |
| 3905 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 3906 | if (!castTInfo) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3907 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3908 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3909 | // If the Expr being casted is a ParenListExpr, handle it specially. |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3910 | Expr *castExpr = (Expr *)Op.get(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3911 | if (isa<ParenListExpr>(castExpr)) |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3912 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op), |
| 3913 | castTInfo); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3914 | |
| 3915 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, move(Op)); |
| 3916 | } |
| 3917 | |
| 3918 | Action::OwningExprResult |
| 3919 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
| 3920 | SourceLocation RParenLoc, ExprArg Op) { |
| 3921 | Expr *castExpr = static_cast<Expr*>(Op.get()); |
| 3922 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3923 | CXXMethodDecl *Method = 0; |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3924 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
| 3925 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3926 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3927 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3928 | |
| 3929 | if (Method) { |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3930 | // FIXME: preserve type source info here |
| 3931 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, Ty->getType(), |
| 3932 | Kind, Method, move(Op)); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3933 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3934 | if (CastArg.isInvalid()) |
| 3935 | return ExprError(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3936 | |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3937 | castExpr = CastArg.takeAs<Expr>(); |
| 3938 | } else { |
| 3939 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3940 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 3942 | return Owned(new (Context) CStyleCastExpr(Ty->getType().getNonReferenceType(), |
| 3943 | Kind, castExpr, Ty, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3944 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3947 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3948 | /// of comma binary operators. |
| 3949 | Action::OwningExprResult |
| 3950 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3951 | Expr *expr = EA.takeAs<Expr>(); |
| 3952 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3953 | if (!E) |
| 3954 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3955 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3956 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3958 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3959 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3960 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3962 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3963 | } |
| 3964 | |
| 3965 | Action::OwningExprResult |
| 3966 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3967 | SourceLocation RParenLoc, ExprArg Op, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3968 | TypeSourceInfo *TInfo) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3969 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3970 | QualType Ty = TInfo->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | |
| 3972 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3973 | // then handle it as such. |
| 3974 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3975 | if (PE->getNumExprs() == 0) { |
| 3976 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3977 | return ExprError(); |
| 3978 | } |
| 3979 | |
| 3980 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3981 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3982 | initExprs.push_back(PE->getExpr(i)); |
| 3983 | |
| 3984 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3985 | // braces instead of the original commas. |
| 3986 | Op.release(); |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 3987 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3988 | initExprs.size(), RParenLoc); |
| 3989 | E->setType(Ty); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3990 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, Owned(E)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3991 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3992 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3993 | // sequence of BinOp comma operators. |
| 3994 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3995 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, move(Op)); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3996 | } |
| 3997 | } |
| 3998 | |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 3999 | Action::OwningExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4000 | SourceLocation R, |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4001 | MultiExprArg Val, |
| 4002 | TypeTy *TypeOfCast) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4003 | unsigned nexprs = Val.size(); |
| 4004 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4005 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4006 | Expr *expr; |
| 4007 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 4008 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4009 | else |
| 4010 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4011 | return Owned(expr); |
| 4012 | } |
| 4013 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4014 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4015 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4016 | /// C99 6.5.15 |
| 4017 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 4018 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4019 | // C++ is sufficiently different to merit its own checker. |
| 4020 | if (getLangOptions().CPlusPlus) |
| 4021 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 4022 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4023 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 4024 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4025 | UsualUnaryConversions(Cond); |
| 4026 | UsualUnaryConversions(LHS); |
| 4027 | UsualUnaryConversions(RHS); |
| 4028 | QualType CondTy = Cond->getType(); |
| 4029 | QualType LHSTy = LHS->getType(); |
| 4030 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4031 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4032 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4033 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 4034 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4035 | << CondTy; |
| 4036 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4037 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4038 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4039 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4040 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 4041 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4043 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4044 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4045 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4046 | UsualArithmeticConversions(LHS, RHS); |
| 4047 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4048 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4049 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4050 | // If both operands are the same structure or union type, the result is that |
| 4051 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4052 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4053 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4054 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4055 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4056 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4057 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4058 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4059 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4060 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4061 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4062 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4063 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4064 | if (!LHSTy->isVoidType()) |
| 4065 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4066 | << RHS->getSourceRange(); |
| 4067 | if (!RHSTy->isVoidType()) |
| 4068 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4069 | << LHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4070 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 4071 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4072 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4073 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4074 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4075 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4076 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4077 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4078 | // promote the null to a pointer. |
| 4079 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4080 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4081 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4082 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4083 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4084 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4085 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4086 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4087 | |
| 4088 | // All objective-c pointer type analysis is done here. |
| 4089 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4090 | QuestionLoc); |
| 4091 | if (!compositeType.isNull()) |
| 4092 | return compositeType; |
| 4093 | |
| 4094 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4095 | // Handle block pointer types. |
| 4096 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4097 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4098 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4099 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4100 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4101 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4102 | return destType; |
| 4103 | } |
| 4104 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4105 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4106 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4107 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4108 | // We have 2 block pointer types. |
| 4109 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4110 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4111 | return LHSTy; |
| 4112 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4113 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4114 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4115 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4116 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4117 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4118 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4119 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4120 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4121 | // In this situation, we assume void* type. No especially good |
| 4122 | // reason, but this is what gcc does, and we do have to pick |
| 4123 | // to get a consistent AST. |
| 4124 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4125 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4126 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4127 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4128 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4129 | // The block pointer types are compatible. |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4130 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4131 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4132 | return LHSTy; |
| 4133 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4134 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4135 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4136 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4137 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4138 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4139 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4140 | |
| 4141 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4142 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4143 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4144 | QualType destPointee |
| 4145 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4146 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4147 | // Add qualifiers if necessary. |
| 4148 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4149 | // Promote to void*. |
| 4150 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4151 | return destType; |
| 4152 | } |
| 4153 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4154 | QualType destPointee |
| 4155 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4156 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4157 | // Add qualifiers if necessary. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4158 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4159 | // Promote to void*. |
Eli Friedman | 16fea9b | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 4160 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4161 | return destType; |
| 4162 | } |
| 4163 | |
| 4164 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4165 | // Two identical pointer types are always compatible. |
| 4166 | return LHSTy; |
| 4167 | } |
| 4168 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4169 | rhptee.getUnqualifiedType())) { |
| 4170 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4171 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4172 | // In this situation, we assume void* type. No especially good |
| 4173 | // reason, but this is what gcc does, and we do have to pick |
| 4174 | // to get a consistent AST. |
| 4175 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4176 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4177 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4178 | return incompatTy; |
| 4179 | } |
| 4180 | // The pointer types are compatible. |
| 4181 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4182 | // differently qualified versions of compatible types, the result type is |
| 4183 | // a pointer to an appropriately qualified version of the *composite* |
| 4184 | // type. |
| 4185 | // FIXME: Need to calculate the composite type. |
| 4186 | // FIXME: Need to add qualifiers |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4187 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 4188 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4189 | return LHSTy; |
| 4190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4191 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4192 | // GCC compatibility: soften pointer/integer mismatch. |
| 4193 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4194 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4195 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4196 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4197 | return RHSTy; |
| 4198 | } |
| 4199 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4200 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4201 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4202 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4203 | return LHSTy; |
| 4204 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4205 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4206 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4207 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 4208 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4209 | return QualType(); |
| 4210 | } |
| 4211 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4212 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4213 | /// two objective-c pointer types of the two input expressions. |
| 4214 | QualType Sema::FindCompositeObjCPointerType(Expr *&LHS, Expr *&RHS, |
| 4215 | SourceLocation QuestionLoc) { |
| 4216 | QualType LHSTy = LHS->getType(); |
| 4217 | QualType RHSTy = RHS->getType(); |
| 4218 | |
| 4219 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4220 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4221 | // redefinition type if an attempt is made to access its fields. |
| 4222 | if (LHSTy->isObjCClassType() && |
| 4223 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4224 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4225 | return LHSTy; |
| 4226 | } |
| 4227 | if (RHSTy->isObjCClassType() && |
| 4228 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 4229 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4230 | return RHSTy; |
| 4231 | } |
| 4232 | // And the same for struct objc_object* / id |
| 4233 | if (LHSTy->isObjCIdType() && |
| 4234 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4235 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4236 | return LHSTy; |
| 4237 | } |
| 4238 | if (RHSTy->isObjCIdType() && |
| 4239 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 4240 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4241 | return RHSTy; |
| 4242 | } |
| 4243 | // And the same for struct objc_selector* / SEL |
| 4244 | if (Context.isObjCSelType(LHSTy) && |
| 4245 | (RHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4246 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
| 4247 | return LHSTy; |
| 4248 | } |
| 4249 | if (Context.isObjCSelType(RHSTy) && |
| 4250 | (LHSTy.getDesugaredType() == Context.ObjCSelRedefinitionType)) { |
| 4251 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
| 4252 | return RHSTy; |
| 4253 | } |
| 4254 | // Check constraints for Objective-C object pointers types. |
| 4255 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4256 | |
| 4257 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4258 | // Two identical object pointer types are always compatible. |
| 4259 | return LHSTy; |
| 4260 | } |
| 4261 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4262 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4263 | QualType compositeType = LHSTy; |
| 4264 | |
| 4265 | // If both operands are interfaces and either operand can be |
| 4266 | // assigned to the other, use that type as the composite |
| 4267 | // type. This allows |
| 4268 | // xxx ? (A*) a : (B*) b |
| 4269 | // where B is a subclass of A. |
| 4270 | // |
| 4271 | // Additionally, as for assignment, if either type is 'id' |
| 4272 | // allow silent coercion. Finally, if the types are |
| 4273 | // incompatible then make sure to use 'id' as the composite |
| 4274 | // type so the result is acceptable for sending messages to. |
| 4275 | |
| 4276 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4277 | // It could return the composite type. |
| 4278 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4279 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4280 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4281 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4282 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4283 | RHSTy->isObjCQualifiedIdType()) && |
| 4284 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4285 | // Need to handle "id<xx>" explicitly. |
| 4286 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4287 | // id. Currently localizing to here until clear this should be |
| 4288 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4289 | compositeType = Context.getObjCIdType(); |
| 4290 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4291 | compositeType = Context.getObjCIdType(); |
| 4292 | } else if (!(compositeType = |
| 4293 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4294 | ; |
| 4295 | else { |
| 4296 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4297 | << LHSTy << RHSTy |
| 4298 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 4299 | QualType incompatTy = Context.getObjCIdType(); |
| 4300 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 4301 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
| 4302 | return incompatTy; |
| 4303 | } |
| 4304 | // The object pointer types are compatible. |
| 4305 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 4306 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
| 4307 | return compositeType; |
| 4308 | } |
| 4309 | // Check Objective-C object pointer types and 'void *' |
| 4310 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4311 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4312 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4313 | QualType destPointee |
| 4314 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4315 | QualType destType = Context.getPointerType(destPointee); |
| 4316 | // Add qualifiers if necessary. |
| 4317 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 4318 | // Promote to void*. |
| 4319 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
| 4320 | return destType; |
| 4321 | } |
| 4322 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4323 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4324 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4325 | QualType destPointee |
| 4326 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4327 | QualType destType = Context.getPointerType(destPointee); |
| 4328 | // Add qualifiers if necessary. |
| 4329 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 4330 | // Promote to void*. |
| 4331 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 4332 | return destType; |
| 4333 | } |
| 4334 | return QualType(); |
| 4335 | } |
| 4336 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4337 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4338 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4339 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 4340 | SourceLocation ColonLoc, |
| 4341 | ExprArg Cond, ExprArg LHS, |
| 4342 | ExprArg RHS) { |
| 4343 | Expr *CondExpr = (Expr *) Cond.get(); |
| 4344 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4345 | |
| 4346 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4347 | // was the condition. |
| 4348 | bool isLHSNull = LHSExpr == 0; |
| 4349 | if (isLHSNull) |
| 4350 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4351 | |
| 4352 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 4353 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4354 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4355 | return ExprError(); |
| 4356 | |
| 4357 | Cond.release(); |
| 4358 | LHS.release(); |
| 4359 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4360 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 4361 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 4362 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4363 | } |
| 4364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4365 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4366 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4367 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 4368 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 4369 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4370 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4371 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4372 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4373 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4374 | if ((lhsType->isObjCClassType() && |
| 4375 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4376 | (rhsType->isObjCClassType() && |
| 4377 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4378 | return Compatible; |
| 4379 | } |
| 4380 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4381 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4382 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 4383 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4385 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4386 | lhptee = Context.getCanonicalType(lhptee); |
| 4387 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4388 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4389 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4390 | |
| 4391 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 4392 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 4393 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 4394 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4395 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4396 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4397 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4398 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 4399 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4400 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4401 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4402 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4403 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4404 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4405 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4406 | assert(rhptee->isFunctionType()); |
| 4407 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4408 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4409 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4410 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4411 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4412 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4413 | |
| 4414 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 4415 | assert(lhptee->isFunctionType()); |
| 4416 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 4417 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4418 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4419 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4420 | lhptee = lhptee.getUnqualifiedType(); |
| 4421 | rhptee = rhptee.getUnqualifiedType(); |
| 4422 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 4423 | // Check if the pointee types are compatible ignoring the sign. |
| 4424 | // We explicitly check for char so that we catch "char" vs |
| 4425 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4426 | if (lhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4427 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4428 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4429 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4430 | |
| 4431 | if (rhptee->isCharType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4432 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4433 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4434 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 4435 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4436 | if (lhptee == rhptee) { |
| 4437 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 4438 | // takes priority over sign incompatibility because the sign |
| 4439 | // warning can be disabled. |
| 4440 | if (ConvTy != Compatible) |
| 4441 | return ConvTy; |
| 4442 | return IncompatiblePointerSign; |
| 4443 | } |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4444 | |
| 4445 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 4446 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 4447 | // the eventual target type is the same and the pointers have the same |
| 4448 | // level of indirection, this must be the issue. |
| 4449 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 4450 | do { |
| 4451 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 4452 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 4453 | |
| 4454 | lhptee = Context.getCanonicalType(lhptee); |
| 4455 | rhptee = Context.getCanonicalType(rhptee); |
| 4456 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 4457 | |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4458 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 4459 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 4460 | } |
| 4461 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4462 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 4464 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4465 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4466 | } |
| 4467 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4468 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 4469 | /// block pointer types are compatible or whether a block and normal pointer |
| 4470 | /// are compatible. It is more restrict than comparing two function pointer |
| 4471 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4472 | Sema::AssignConvertType |
| 4473 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4474 | QualType rhsType) { |
| 4475 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4476 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4477 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4478 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 4479 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4480 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4481 | // make sure we operate on the canonical type |
| 4482 | lhptee = Context.getCanonicalType(lhptee); |
| 4483 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4484 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4485 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4486 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4487 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4488 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4489 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4490 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4491 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4492 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4493 | return ConvTy; |
| 4494 | } |
| 4495 | |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4496 | /// CheckObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| 4497 | /// for assignment compatibility. |
| 4498 | Sema::AssignConvertType |
| 4499 | Sema::CheckObjCPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 4500 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 4501 | return Compatible; |
| 4502 | QualType lhptee = |
| 4503 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4504 | QualType rhptee = |
| 4505 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4506 | // make sure we operate on the canonical type |
| 4507 | lhptee = Context.getCanonicalType(lhptee); |
| 4508 | rhptee = Context.getCanonicalType(rhptee); |
| 4509 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 4510 | return CompatiblePointerDiscardsQualifiers; |
| 4511 | |
| 4512 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 4513 | return Compatible; |
| 4514 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 4515 | return IncompatibleObjCQualifiedId; |
| 4516 | return IncompatiblePointer; |
| 4517 | } |
| 4518 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4519 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 4520 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4521 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 4522 | /// |
| 4523 | /// int a, *pint; |
| 4524 | /// short *pshort; |
| 4525 | /// struct foo *pfoo; |
| 4526 | /// |
| 4527 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 4528 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 4529 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 4530 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 4531 | /// |
| 4532 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4533 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4534 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4535 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4536 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4537 | // Get canonical types. We're not formatting these types, just comparing |
| 4538 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4539 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 4540 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4541 | |
| 4542 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 4543 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 4544 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4545 | if ((lhsType->isObjCClassType() && |
| 4546 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 4547 | (rhsType->isObjCClassType() && |
| 4548 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 4549 | return Compatible; |
| 4550 | } |
| 4551 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4552 | // If the left-hand side is a reference type, then we are in a |
| 4553 | // (rare!) case where we've allowed the use of references in C, |
| 4554 | // e.g., as a parameter type in a built-in function. In this case, |
| 4555 | // just make sure that the type referenced is compatible with the |
| 4556 | // right-hand side type. The caller is responsible for adjusting |
| 4557 | // lhsType so that the resulting expression does not have reference |
| 4558 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4559 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4560 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 4561 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4562 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 4563 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4564 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 4565 | // to the same ExtVector type. |
| 4566 | if (lhsType->isExtVectorType()) { |
| 4567 | if (rhsType->isExtVectorType()) |
| 4568 | return lhsType == rhsType ? Compatible : Incompatible; |
| 4569 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 4570 | return Compatible; |
| 4571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4573 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4574 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4575 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4576 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4577 | if (getLangOptions().LaxVectorConversions && |
| 4578 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4579 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4580 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4581 | } |
| 4582 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4583 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4584 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 4585 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4586 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4587 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4588 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4589 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4590 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4591 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4592 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4593 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4594 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4595 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4596 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4597 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4598 | return Compatible; |
| 4599 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4600 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4601 | if (rhsType->getAs<BlockPointerType>()) { |
| 4602 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4603 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4604 | |
| 4605 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4606 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4607 | return Compatible; |
| 4608 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4609 | return Incompatible; |
| 4610 | } |
| 4611 | |
| 4612 | if (isa<BlockPointerType>(lhsType)) { |
| 4613 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 4614 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4615 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4616 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4617 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 4618 | return Compatible; |
| 4619 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4620 | if (rhsType->isBlockPointerType()) |
| 4621 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4622 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4623 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4624 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4625 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4626 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4627 | return Incompatible; |
| 4628 | } |
| 4629 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4630 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 4631 | if (rhsType->isIntegerType()) |
| 4632 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4633 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4634 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4635 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4636 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 4637 | return Compatible; |
| 4638 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4639 | } |
| 4640 | if (rhsType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 4641 | return CheckObjCPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4642 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4643 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4644 | if (RHSPT->getPointeeType()->isVoidType()) |
| 4645 | return Compatible; |
| 4646 | } |
| 4647 | // Treat block pointers as objects. |
| 4648 | if (rhsType->isBlockPointerType()) |
| 4649 | return Compatible; |
| 4650 | return Incompatible; |
| 4651 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4652 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4653 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4654 | if (lhsType == Context.BoolTy) |
| 4655 | return Compatible; |
| 4656 | |
| 4657 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 4658 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4659 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4660 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4661 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4662 | |
| 4663 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4664 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 4665 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4666 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4667 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4668 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 4669 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 4670 | if (lhsType == Context.BoolTy) |
| 4671 | return Compatible; |
| 4672 | |
| 4673 | if (lhsType->isIntegerType()) |
| 4674 | return PointerToInt; |
| 4675 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4676 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4677 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 4678 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 4679 | return Compatible; |
| 4680 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4681 | } |
| 4682 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4683 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4684 | return Compatible; |
| 4685 | return Incompatible; |
| 4686 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 4687 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 4688 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 4689 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4690 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4691 | } |
| 4692 | return Incompatible; |
| 4693 | } |
| 4694 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4695 | /// \brief Constructs a transparent union from an expression that is |
| 4696 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4697 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4698 | QualType UnionType, FieldDecl *Field) { |
| 4699 | // Build an initializer list that designates the appropriate member |
| 4700 | // of the transparent union. |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 4701 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 4702 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4703 | SourceLocation()); |
| 4704 | Initializer->setType(UnionType); |
| 4705 | Initializer->setInitializedFieldInUnion(Field); |
| 4706 | |
| 4707 | // Build a compound literal constructing a value of the transparent |
| 4708 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4709 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John McCall | 1d7d8d6 | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 4710 | E = new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4711 | Initializer, false); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4712 | } |
| 4713 | |
| 4714 | Sema::AssignConvertType |
| 4715 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 4716 | QualType FromType = rExpr->getType(); |
| 4717 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4718 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4719 | // transparent_union GCC extension. |
| 4720 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4721 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4722 | return Incompatible; |
| 4723 | |
| 4724 | // The field to initialize within the transparent union. |
| 4725 | RecordDecl *UD = UT->getDecl(); |
| 4726 | FieldDecl *InitField = 0; |
| 4727 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4728 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4729 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4730 | it != itend; ++it) { |
| 4731 | if (it->getType()->isPointerType()) { |
| 4732 | // If the transparent union contains a pointer type, we allow: |
| 4733 | // 1) void pointer |
| 4734 | // 2) null pointer constant |
| 4735 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4736 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4737 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4738 | InitField = *it; |
| 4739 | break; |
| 4740 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4742 | if (rExpr->isNullPointerConstant(Context, |
| 4743 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4744 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4745 | InitField = *it; |
| 4746 | break; |
| 4747 | } |
| 4748 | } |
| 4749 | |
| 4750 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4751 | == Compatible) { |
| 4752 | InitField = *it; |
| 4753 | break; |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | if (!InitField) |
| 4758 | return Incompatible; |
| 4759 | |
| 4760 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4761 | return Compatible; |
| 4762 | } |
| 4763 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4764 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4765 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4766 | if (getLangOptions().CPlusPlus) { |
| 4767 | if (!lhsType->isRecordType()) { |
| 4768 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4769 | // expression is implicitly converted (C++ 4) to the |
| 4770 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4771 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 4772 | AA_Assigning)) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4773 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4774 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
| 4777 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4778 | // structures. |
| 4779 | } |
| 4780 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4781 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4782 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4783 | if ((lhsType->isPointerType() || |
| 4784 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4785 | lhsType->isBlockPointerType()) |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4786 | && rExpr->isNullPointerConstant(Context, |
| 4787 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4788 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4789 | return Compatible; |
| 4790 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4791 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4792 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4793 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4794 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4795 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4796 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4797 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4798 | if (!lhsType->isReferenceType()) |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 4799 | DefaultFunctionArrayLvalueConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4800 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4801 | Sema::AssignConvertType result = |
| 4802 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4803 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4804 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4805 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4806 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4807 | // so that we can use references in built-in functions even in C. |
| 4808 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4809 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4810 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4811 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4812 | CastExpr::CK_Unknown); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4813 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4814 | } |
| 4815 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4816 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4817 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4818 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4819 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4820 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4821 | } |
| 4822 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4823 | QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4824 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4825 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4826 | QualType lhsType = |
| 4827 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4828 | QualType rhsType = |
| 4829 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4830 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4831 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4832 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4833 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4834 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4835 | // Handle the case of a vector & extvector type of the same size and element |
| 4836 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4837 | if (getLangOptions().LaxVectorConversions) { |
| 4838 | // FIXME: Should we warn here? |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4839 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4840 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4841 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4842 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4843 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4844 | } |
| 4845 | } |
| 4846 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4847 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4848 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4849 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4850 | bool swapped = false; |
| 4851 | if (rhsType->isExtVectorType()) { |
| 4852 | swapped = true; |
| 4853 | std::swap(rex, lex); |
| 4854 | std::swap(rhsType, lhsType); |
| 4855 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4856 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4857 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4858 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4859 | QualType EltTy = LV->getElementType(); |
| 4860 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4861 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4862 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4863 | if (swapped) std::swap(rex, lex); |
| 4864 | return lhsType; |
| 4865 | } |
| 4866 | } |
| 4867 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4868 | rhsType->isRealFloatingType()) { |
| 4869 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4870 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4871 | if (swapped) std::swap(rex, lex); |
| 4872 | return lhsType; |
| 4873 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4874 | } |
| 4875 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4876 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4877 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4878 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4879 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4880 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4881 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4884 | QualType Sema::CheckMultiplyDivideOperands( |
| 4885 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4886 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4887 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4888 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4889 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4891 | if (!lex->getType()->isArithmeticType() || |
| 4892 | !rex->getType()->isArithmeticType()) |
| 4893 | return InvalidOperands(Loc, lex, rex); |
| 4894 | |
| 4895 | // Check for division by zero. |
| 4896 | if (isDiv && |
| 4897 | rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 4898 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_division_by_zero) |
| 4899 | << rex->getSourceRange()); |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4900 | |
| 4901 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4902 | } |
| 4903 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4904 | QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4905 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4906 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4907 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4908 | return CheckVectorOperands(Loc, lex, rex); |
| 4909 | return InvalidOperands(Loc, lex, rex); |
| 4910 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4911 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4912 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4913 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4914 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 4915 | return InvalidOperands(Loc, lex, rex); |
| 4916 | |
| 4917 | // Check for remainder by zero. |
| 4918 | if (rex->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
Chris Lattner | cb329c5 | 2010-01-12 21:30:55 +0000 | [diff] [blame] | 4919 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_remainder_by_zero) |
| 4920 | << rex->getSourceRange()); |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4921 | |
| 4922 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 4925 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4927 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4928 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4929 | if (CompLHSTy) *CompLHSTy = compType; |
| 4930 | return compType; |
| 4931 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4932 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4933 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4934 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4935 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4936 | if (lex->getType()->isArithmeticType() && |
| 4937 | rex->getType()->isArithmeticType()) { |
| 4938 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4939 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4940 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4941 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4942 | // Put any potential pointer into PExp |
| 4943 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4944 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4945 | std::swap(PExp, IExp); |
| 4946 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4947 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4948 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4949 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4950 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4952 | // Check for arithmetic on pointers to incomplete types. |
| 4953 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4954 | if (getLangOptions().CPlusPlus) { |
| 4955 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4956 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4957 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4958 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4959 | |
| 4960 | // GNU extension: arithmetic on pointer to void |
| 4961 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4962 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4963 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4964 | if (getLangOptions().CPlusPlus) { |
| 4965 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4966 | << lex->getType() << lex->getSourceRange(); |
| 4967 | return QualType(); |
| 4968 | } |
| 4969 | |
| 4970 | // GNU extension: arithmetic on pointer to function |
| 4971 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4972 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4973 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4974 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4975 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4976 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4977 | PExp->getType()->isObjCObjectPointerType()) && |
| 4978 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4979 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4980 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4981 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4982 | return QualType(); |
| 4983 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4984 | // Diagnose bad cases where we step over interface counts. |
| 4985 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4986 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4987 | << PointeeTy << PExp->getSourceRange(); |
| 4988 | return QualType(); |
| 4989 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4990 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4991 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4992 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4993 | if (LHSTy.isNull()) { |
| 4994 | LHSTy = lex->getType(); |
| 4995 | if (LHSTy->isPromotableIntegerType()) |
| 4996 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4997 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4998 | *CompLHSTy = LHSTy; |
| 4999 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5000 | return PExp->getType(); |
| 5001 | } |
| 5002 | } |
| 5003 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5004 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5007 | // C99 6.5.6 |
| 5008 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5009 | SourceLocation Loc, QualType* CompLHSTy) { |
| 5010 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 5011 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 5012 | if (CompLHSTy) *CompLHSTy = compType; |
| 5013 | return compType; |
| 5014 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5015 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5016 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5017 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5018 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5019 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5020 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5021 | if (lex->getType()->isArithmeticType() |
| 5022 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5023 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5024 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5026 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5027 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5028 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 5029 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5030 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5031 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5032 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5033 | bool ComplainAboutVoid = false; |
| 5034 | Expr *ComplainAboutFunc = 0; |
| 5035 | if (lpointee->isVoidType()) { |
| 5036 | if (getLangOptions().CPlusPlus) { |
| 5037 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5038 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5039 | return QualType(); |
| 5040 | } |
| 5041 | |
| 5042 | // GNU C extension: arithmetic on pointer to void |
| 5043 | ComplainAboutVoid = true; |
| 5044 | } else if (lpointee->isFunctionType()) { |
| 5045 | if (getLangOptions().CPlusPlus) { |
| 5046 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5047 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5048 | return QualType(); |
| 5049 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5050 | |
| 5051 | // GNU C extension: arithmetic on pointer to function |
| 5052 | ComplainAboutFunc = lex; |
| 5053 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5054 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5055 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5056 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5057 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5058 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5059 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5060 | // Diagnose bad cases where we step over interface counts. |
| 5061 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5062 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5063 | << lpointee << lex->getSourceRange(); |
| 5064 | return QualType(); |
| 5065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5066 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5067 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5068 | if (rex->getType()->isIntegerType()) { |
| 5069 | if (ComplainAboutVoid) |
| 5070 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5071 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5072 | if (ComplainAboutFunc) |
| 5073 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5074 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5075 | << ComplainAboutFunc->getSourceRange(); |
| 5076 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5077 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5078 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5079 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5080 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5081 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5082 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 5083 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5085 | // RHS must be a completely-type object type. |
| 5086 | // Handle the GNU void* extension. |
| 5087 | if (rpointee->isVoidType()) { |
| 5088 | if (getLangOptions().CPlusPlus) { |
| 5089 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 5090 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5091 | return QualType(); |
| 5092 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5093 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5094 | ComplainAboutVoid = true; |
| 5095 | } else if (rpointee->isFunctionType()) { |
| 5096 | if (getLangOptions().CPlusPlus) { |
| 5097 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5098 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5099 | return QualType(); |
| 5100 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5101 | |
| 5102 | // GNU extension: arithmetic on pointer to function |
| 5103 | if (!ComplainAboutFunc) |
| 5104 | ComplainAboutFunc = rex; |
| 5105 | } else if (!rpointee->isDependentType() && |
| 5106 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5107 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 5108 | << rex->getSourceRange() |
| 5109 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5110 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5111 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 5112 | if (getLangOptions().CPlusPlus) { |
| 5113 | // Pointee types must be the same: C++ [expr.add] |
| 5114 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 5115 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5116 | << lex->getType() << rex->getType() |
| 5117 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5118 | return QualType(); |
| 5119 | } |
| 5120 | } else { |
| 5121 | // Pointee types must be compatible C99 6.5.6p3 |
| 5122 | if (!Context.typesAreCompatible( |
| 5123 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 5124 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 5125 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5126 | << lex->getType() << rex->getType() |
| 5127 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5128 | return QualType(); |
| 5129 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5130 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5131 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5132 | if (ComplainAboutVoid) |
| 5133 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 5134 | << lex->getSourceRange() << rex->getSourceRange(); |
| 5135 | if (ComplainAboutFunc) |
| 5136 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5137 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 5138 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5139 | |
| 5140 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5141 | return Context.getPointerDiffType(); |
| 5142 | } |
| 5143 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5144 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5145 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5146 | } |
| 5147 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5148 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5149 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5150 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5151 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 5152 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5153 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5154 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 5155 | // Vector shifts promote their scalar inputs to vector type. |
| 5156 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 5157 | return CheckVectorOperands(Loc, lex, rex); |
| 5158 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5159 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 5160 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5161 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 5162 | if (LHSTy.isNull()) { |
| 5163 | LHSTy = lex->getType(); |
| 5164 | if (LHSTy->isPromotableIntegerType()) |
| 5165 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5166 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 5167 | if (!isCompAssign) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5168 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5169 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5170 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5171 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5172 | // Sanity-check shift operands |
| 5173 | llvm::APSInt Right; |
| 5174 | // Check right/shifter operand |
Daniel Dunbar | 3f180c6 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 5175 | if (!rex->isValueDependent() && |
| 5176 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 5177 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 5178 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 5179 | else { |
| 5180 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 5181 | Context.getTypeSize(lex->getType())); |
| 5182 | if (Right.uge(LeftBits)) |
| 5183 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 5184 | } |
| 5185 | } |
| 5186 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5187 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5188 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5191 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5192 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5193 | unsigned OpaqueOpc, bool isRelational) { |
| 5194 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 5195 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5196 | // Handle vector comparisons separately. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5197 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5198 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5199 | |
John McCall | 5dbad3d | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 5200 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 5201 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 45aa455 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 5202 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5203 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 5204 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 5205 | UsualArithmeticConversions(lex, rex); |
| 5206 | else { |
| 5207 | UsualUnaryConversions(lex); |
| 5208 | UsualUnaryConversions(rex); |
| 5209 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5210 | QualType lType = lex->getType(); |
| 5211 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5212 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5213 | if (!lType->isFloatingType() |
| 5214 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5215 | // For non-floating point types, check for self-comparisons of the form |
| 5216 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5217 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 5219 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5220 | Expr *LHSStripped = lex->IgnoreParens(); |
| 5221 | Expr *RHSStripped = rex->IgnoreParens(); |
| 5222 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 5223 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 5224 | if (DRL->getDecl() == DRR->getDecl() && |
| 5225 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5226 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_selfcomparison)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5227 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5228 | if (isa<CastExpr>(LHSStripped)) |
| 5229 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 5230 | if (isa<CastExpr>(RHSStripped)) |
| 5231 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5232 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5233 | // Warn about comparisons against a string constant (unless the other |
| 5234 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5235 | Expr *literalString = 0; |
| 5236 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 5237 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5238 | !RHSStripped->isNullPointerConstant(Context, |
| 5239 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5240 | literalString = lex; |
| 5241 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 5242 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 5243 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5244 | !LHSStripped->isNullPointerConstant(Context, |
| 5245 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5246 | literalString = rex; |
| 5247 | literalStringStripped = RHSStripped; |
| 5248 | } |
| 5249 | |
| 5250 | if (literalString) { |
| 5251 | std::string resultComparison; |
| 5252 | switch (Opc) { |
| 5253 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 5254 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 5255 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 5256 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 5257 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 5258 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 5259 | default: assert(false && "Invalid comparison operator"); |
| 5260 | } |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5261 | |
| 5262 | DiagRuntimeBehavior(Loc, |
| 5263 | PDiag(diag::warn_stringcompare) |
| 5264 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 5265 | << literalString->getSourceRange() |
| 5266 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 5267 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 5268 | "strcmp(") |
| 5269 | << CodeModificationHint::CreateInsertion( |
| 5270 | PP.getLocForEndOfToken(rex->getLocEnd()), |
| 5271 | resultComparison)); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5272 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 5273 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5274 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5275 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5276 | QualType ResultTy = getLangOptions().CPlusPlus ? Context.BoolTy:Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5277 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5278 | if (isRelational) { |
| 5279 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5280 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5281 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 5282 | // Check for comparisons of floating point operands using != and ==. |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 5283 | if (lType->isFloatingType() && rType->isFloatingType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5284 | CheckFloatComparison(Loc,lex,rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5285 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5286 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5287 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5288 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5289 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5290 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 5291 | Expr::NPC_ValueDependentIsNull); |
| 5292 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 5293 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5294 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 5295 | // All of the following pointer related warnings are GCC extensions, except |
| 5296 | // when handling null pointer constants. One day, we can consider making them |
| 5297 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 5298 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5299 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5300 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 5301 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5302 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5303 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5304 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5305 | if (LCanPointeeTy == RCanPointeeTy) |
| 5306 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 5307 | if (!isRelational && |
| 5308 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5309 | // Valid unless comparison between non-null pointer and function pointer |
| 5310 | // This is a gcc extension compatibility comparison. |
| 5311 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5312 | && !LHSIsNull && !RHSIsNull) { |
| 5313 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5314 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5315 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
| 5316 | return ResultTy; |
| 5317 | } |
| 5318 | } |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5319 | // C++ [expr.rel]p2: |
| 5320 | // [...] Pointer conversions (4.10) and qualification |
| 5321 | // conversions (4.4) are performed on pointer operands (or on |
| 5322 | // a pointer operand and a null pointer constant) to bring |
| 5323 | // them to their composite pointer type. [...] |
| 5324 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5325 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5326 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 5327 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5328 | if (T.isNull()) { |
| 5329 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5330 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5331 | return QualType(); |
| 5332 | } |
| 5333 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5334 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5335 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 5336 | return ResultTy; |
| 5337 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5338 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 5339 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 5340 | RCanPointeeTy.getUnqualifiedType())) { |
| 5341 | // Valid unless a relational comparison of function pointers |
| 5342 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 5343 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 5344 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5345 | } |
| 5346 | } else if (!isRelational && |
| 5347 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 5348 | // Valid unless comparison between non-null pointer and function pointer |
| 5349 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 5350 | && !LHSIsNull && !RHSIsNull) { |
| 5351 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 5352 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5353 | } |
| 5354 | } else { |
| 5355 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5356 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5357 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5358 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 5359 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5360 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5361 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5364 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5365 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5366 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5367 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5368 | (lType->isPointerType() || |
| 5369 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5370 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5371 | return ResultTy; |
| 5372 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5373 | if (LHSIsNull && |
| 5374 | (rType->isPointerType() || |
| 5375 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 5376 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5377 | return ResultTy; |
| 5378 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5379 | |
| 5380 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5381 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5382 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 5383 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | // In addition, pointers to members can be compared, or a pointer to |
| 5385 | // member and a null pointer constant. Pointer to member conversions |
| 5386 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 5387 | // them to a common type. If one operand is a null pointer constant, |
| 5388 | // the common type is the type of the other operand. Otherwise, the |
| 5389 | // common type is a pointer to member type similar (4.4) to the type |
| 5390 | // of one of the operands, with a cv-qualification signature (4.4) |
| 5391 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5392 | // types. |
| 5393 | QualType T = FindCompositePointerType(lex, rex); |
| 5394 | if (T.isNull()) { |
| 5395 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 5396 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 5397 | return QualType(); |
| 5398 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5399 | |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5400 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 5401 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5402 | return ResultTy; |
| 5403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 5405 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 5406 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 5407 | return ResultTy; |
| 5408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5410 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5411 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5412 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 5413 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5414 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5415 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 5416 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5417 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5418 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5419 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5420 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5421 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5422 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5423 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5424 | if (!isRelational |
| 5425 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 5426 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5427 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5428 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5429 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5430 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5431 | ->getPointeeType()->isVoidType()))) |
| 5432 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 5433 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5434 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5435 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5436 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 5437 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5438 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5439 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5440 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5441 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 5442 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5443 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5444 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5445 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5446 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5447 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 5448 | if (!LPtrToVoid && !RPtrToVoid && |
| 5449 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5450 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5451 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 5452 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5453 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5454 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 5455 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5456 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5457 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5458 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 5459 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5460 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5461 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 5462 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 5463 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5464 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5465 | unsigned DiagID = 0; |
| 5466 | if (RHSIsNull) { |
| 5467 | if (isRelational) |
| 5468 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5469 | } else if (isRelational) |
| 5470 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5471 | else |
| 5472 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5473 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5474 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5475 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5476 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5477 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5478 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5479 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 5480 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5481 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5482 | unsigned DiagID = 0; |
| 5483 | if (LHSIsNull) { |
| 5484 | if (isRelational) |
| 5485 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 5486 | } else if (isRelational) |
| 5487 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 5488 | else |
| 5489 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5490 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 5491 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5492 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 5493 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 5494 | } |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5495 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5496 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5497 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5498 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5499 | if (!isRelational && RHSIsNull |
| 5500 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5501 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5502 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5503 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5504 | if (!isRelational && LHSIsNull |
| 5505 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5506 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 5507 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 5508 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5509 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5512 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5513 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5514 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 5515 | /// types. |
| 5516 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5517 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5518 | bool isRelational) { |
| 5519 | // Check to make sure we're operating on vectors of the same type and width, |
| 5520 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5521 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5522 | if (vType.isNull()) |
| 5523 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5524 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5525 | QualType lType = lex->getType(); |
| 5526 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5527 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5528 | // For non-floating point types, check for self-comparisons of the form |
| 5529 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 5530 | // often indicate logic errors in the program. |
| 5531 | if (!lType->isFloatingType()) { |
| 5532 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 5533 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 5534 | if (DRL->getDecl() == DRR->getDecl()) |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 5535 | DiagRuntimeBehavior(Loc, PDiag(diag::warn_selfcomparison)); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5536 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5537 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5538 | // Check for comparisons of floating point operands using != and ==. |
| 5539 | if (!isRelational && lType->isFloatingType()) { |
| 5540 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5541 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5542 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5543 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5544 | // Return the type for the comparison, which is the same as vector type for |
| 5545 | // integer vectors, or an integer type of identical size and number of |
| 5546 | // elements for floating point vectors. |
| 5547 | if (lType->isIntegerType()) |
| 5548 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5549 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5550 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5551 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5552 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5553 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 5554 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5555 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 5556 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5557 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 5558 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5559 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 5560 | } |
| 5561 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5562 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5563 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 5564 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5565 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5566 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5567 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5568 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5569 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5570 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5571 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
| 5574 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5575 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5576 | if (!Context.getLangOptions().CPlusPlus) { |
| 5577 | UsualUnaryConversions(lex); |
| 5578 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5579 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5580 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 5581 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5582 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5583 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5584 | } |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5585 | |
| 5586 | // C++ [expr.log.and]p1 |
| 5587 | // C++ [expr.log.or]p1 |
| 5588 | // The operands are both implicitly converted to type bool (clause 4). |
| 5589 | StandardConversionSequence LHS; |
| 5590 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 5591 | /*InOverloadResolution=*/false, LHS)) |
| 5592 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 5593 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5594 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5595 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5596 | return InvalidOperands(Loc, lex, rex); |
| 5597 | |
| 5598 | StandardConversionSequence RHS; |
| 5599 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 5600 | /*InOverloadResolution=*/false, RHS)) |
| 5601 | return InvalidOperands(Loc, lex, rex); |
| 5602 | |
| 5603 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5604 | AA_Passing, /*IgnoreBaseAccess=*/false)) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 5605 | return InvalidOperands(Loc, lex, rex); |
| 5606 | |
| 5607 | // C++ [expr.log.and]p2 |
| 5608 | // C++ [expr.log.or]p2 |
| 5609 | // The result is a bool. |
| 5610 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5613 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 5614 | /// is a read-only property; return true if so. A readonly property expression |
| 5615 | /// depends on various declarations and thus must be treated specially. |
| 5616 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5617 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5618 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 5619 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 5620 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 5621 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5623 | BaseType->getAsObjCInterfacePointerType()) |
| 5624 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 5625 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 5626 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5627 | } |
| 5628 | } |
| 5629 | return false; |
| 5630 | } |
| 5631 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5632 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 5633 | /// emit an error and return true. If so, return false. |
| 5634 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5635 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5636 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5637 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 5638 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 5639 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5640 | if (IsLV == Expr::MLV_Valid) |
| 5641 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5642 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5643 | unsigned Diag = 0; |
| 5644 | bool NeedType = false; |
| 5645 | switch (IsLV) { // C99 6.5.16p2 |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5646 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5647 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5648 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 5649 | NeedType = true; |
| 5650 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5651 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5652 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 5653 | NeedType = true; |
| 5654 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 5655 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5656 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 5657 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 5658 | case Expr::MLV_Valid: |
| 5659 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5660 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 5661 | case Expr::MLV_MemberFunction: |
| 5662 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5663 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 5664 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5665 | case Expr::MLV_IncompleteType: |
| 5666 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 5667 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5668 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 5669 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5670 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5671 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 5672 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 5673 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5674 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 5675 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 5676 | case Expr::MLV_ReadonlyProperty: |
| 5677 | Diag = diag::error_readonly_property_assignment; |
| 5678 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 5679 | case Expr::MLV_NoSetterProperty: |
| 5680 | Diag = diag::error_nosetter_property_assignment; |
| 5681 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 5682 | case Expr::MLV_SubObjCPropertySetting: |
| 5683 | Diag = diag::error_no_subobject_property_setting; |
| 5684 | break; |
Fariborz Jahanian | e9ff443 | 2010-02-11 01:11:34 +0000 | [diff] [blame] | 5685 | case Expr::MLV_SubObjCPropertyGetterSetting: |
| 5686 | Diag = diag::error_no_subobject_property_getter_setting; |
| 5687 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5688 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5689 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5690 | SourceRange Assign; |
| 5691 | if (Loc != OrigLoc) |
| 5692 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5693 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 5694 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5695 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5696 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5697 | return true; |
| 5698 | } |
| 5699 | |
| 5700 | |
| 5701 | |
| 5702 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5703 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5704 | SourceLocation Loc, |
| 5705 | QualType CompoundType) { |
| 5706 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5707 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5708 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5709 | |
| 5710 | QualType LHSType = LHS->getType(); |
| 5711 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5712 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5713 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5714 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5715 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5716 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5717 | // Special case of NSObject attributes on c-style pointer types. |
| 5718 | if (ConvTy == IncompatiblePointer && |
| 5719 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5720 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5721 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5722 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5723 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5724 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5725 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5726 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5727 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5728 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5729 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5730 | RHSCheck = ICE->getSubExpr(); |
| 5731 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5732 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5733 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5734 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5735 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5736 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5737 | // And there is a space or other character before the subexpr of the |
| 5738 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5739 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5740 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5741 | Diag(Loc, diag::warn_not_compound_assign) |
| 5742 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5743 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5744 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5745 | } |
| 5746 | } else { |
| 5747 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5748 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5749 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5750 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5751 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 5752 | RHS, AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5753 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5754 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5755 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5756 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5757 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5758 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5759 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5760 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5761 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5762 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5765 | // C99 6.5.17 |
| 5766 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5767 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 5768 | // C++ does not perform this conversion (C++ [expr.comma]p1). |
| 5769 | if (!getLangOptions().CPlusPlus) |
| 5770 | DefaultFunctionArrayLvalueConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5771 | |
| 5772 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5773 | // incomplete in C++). |
| 5774 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5775 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5778 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5779 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5780 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5781 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5782 | if (Op->isTypeDependent()) |
| 5783 | return Context.DependentTy; |
| 5784 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5785 | QualType ResType = Op->getType(); |
| 5786 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5787 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5788 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5789 | // Decrement of bool is not allowed. |
| 5790 | if (!isInc) { |
| 5791 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5792 | return QualType(); |
| 5793 | } |
| 5794 | // Increment of bool sets it to true, but is deprecated. |
| 5795 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5796 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5797 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5798 | } else if (ResType->isAnyPointerType()) { |
| 5799 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5800 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5801 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5802 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5803 | if (getLangOptions().CPlusPlus) { |
| 5804 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5805 | << Op->getSourceRange(); |
| 5806 | return QualType(); |
| 5807 | } |
| 5808 | |
| 5809 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5810 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5811 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5812 | if (getLangOptions().CPlusPlus) { |
| 5813 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5814 | << Op->getType() << Op->getSourceRange(); |
| 5815 | return QualType(); |
| 5816 | } |
| 5817 | |
| 5818 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5819 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5820 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5821 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5822 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5823 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5824 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5825 | // Diagnose bad cases where we step over interface counts. |
| 5826 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5827 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5828 | << PointeeTy << Op->getSourceRange(); |
| 5829 | return QualType(); |
| 5830 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 5831 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5832 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5833 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5834 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5835 | } else { |
| 5836 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 5837 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5838 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5839 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5840 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 5841 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5842 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5843 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5844 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5845 | } |
| 5846 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5847 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5848 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5849 | /// where the declaration is needed for type checking. We only need to |
| 5850 | /// handle cases when the expression references a function designator |
| 5851 | /// or is an lvalue. Here are some examples: |
| 5852 | /// - &(x) => x |
| 5853 | /// - &*****f => f for f a function designator. |
| 5854 | /// - &s.xx => s |
| 5855 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5856 | /// - *(x + 1) -> x, if x is an array |
| 5857 | /// - &"123"[2] -> 0 |
| 5858 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5859 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5860 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5861 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5862 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5863 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5864 | // If this is an arrow operator, the address is an offset from |
| 5865 | // the base's value, so the object the base refers to is |
| 5866 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5867 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5868 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5869 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5870 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5871 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5872 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5873 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5874 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5875 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5876 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5877 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5878 | } |
| 5879 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5880 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5881 | case Stmt::UnaryOperatorClass: { |
| 5882 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5883 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5884 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5885 | case UnaryOperator::Real: |
| 5886 | case UnaryOperator::Imag: |
| 5887 | case UnaryOperator::Extension: |
| 5888 | return getPrimaryDecl(UO->getSubExpr()); |
| 5889 | default: |
| 5890 | return 0; |
| 5891 | } |
| 5892 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5893 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5894 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5895 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5896 | // If the result of an implicit cast is an l-value, we care about |
| 5897 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5898 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5899 | default: |
| 5900 | return 0; |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5905 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5906 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5907 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5908 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5909 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5910 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5911 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5912 | // Make sure to ignore parentheses in subsequent checks |
| 5913 | op = op->IgnoreParens(); |
| 5914 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5915 | if (op->isTypeDependent()) |
| 5916 | return Context.DependentTy; |
| 5917 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5918 | if (getLangOptions().C99) { |
| 5919 | // Implement C99-only parts of addressof rules. |
| 5920 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5921 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5922 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5923 | // (assuming the deref expression is valid). |
| 5924 | return uOp->getSubExpr()->getType(); |
| 5925 | } |
| 5926 | // Technically, there should be a check for array subscript |
| 5927 | // expressions here, but the result of one is always an lvalue anyway. |
| 5928 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5929 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5930 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5931 | |
Sebastian Redl | e27d87f | 2010-01-11 15:56:56 +0000 | [diff] [blame] | 5932 | MemberExpr *ME = dyn_cast<MemberExpr>(op); |
| 5933 | if (lval == Expr::LV_MemberFunction && ME && |
| 5934 | isa<CXXMethodDecl>(ME->getMemberDecl())) { |
| 5935 | ValueDecl *dcl = cast<MemberExpr>(op)->getMemberDecl(); |
| 5936 | // &f where f is a member of the current object, or &o.f, or &p->f |
| 5937 | // All these are not allowed, and we need to catch them before the dcl |
| 5938 | // branch of the if, below. |
| 5939 | Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| 5940 | << dcl; |
| 5941 | // FIXME: Improve this diagnostic and provide a fixit. |
| 5942 | |
| 5943 | // Now recover by acting as if the function had been accessed qualified. |
| 5944 | return Context.getMemberPointerType(op->getType(), |
| 5945 | Context.getTypeDeclType(cast<RecordDecl>(dcl->getDeclContext())) |
| 5946 | .getTypePtr()); |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 5947 | } else if (lval == Expr::LV_ClassTemporary) { |
| 5948 | Diag(OpLoc, isSFINAEContext()? diag::err_typecheck_addrof_class_temporary |
| 5949 | : diag::ext_typecheck_addrof_class_temporary) |
| 5950 | << op->getType() << op->getSourceRange(); |
| 5951 | if (isSFINAEContext()) |
| 5952 | return QualType(); |
Sebastian Redl | e27d87f | 2010-01-11 15:56:56 +0000 | [diff] [blame] | 5953 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5954 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5955 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5956 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5957 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5958 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5959 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5960 | return QualType(); |
| 5961 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5962 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5963 | // The operand cannot be a bit-field |
| 5964 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5965 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5966 | return QualType(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5967 | } else if (op->refersToVectorElement()) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5968 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5969 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5970 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5971 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5972 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5973 | // cannot take address of a property expression. |
| 5974 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5975 | << "property expression" << op->getSourceRange(); |
| 5976 | return QualType(); |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5977 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5978 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 474e102 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5979 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5980 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5981 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5982 | return Context.OverloadTy; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5983 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5984 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5985 | // with the register storage-class specifier. |
| 5986 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5987 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5988 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5989 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5990 | return QualType(); |
| 5991 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5992 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5993 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5994 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5995 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5996 | // Could be a pointer to member, though, if there is an explicit |
| 5997 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5998 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5999 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6000 | if (Ctx && Ctx->isRecord()) { |
| 6001 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6002 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6003 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 6004 | << FD->getDeclName() << FD->getType(); |
| 6005 | return QualType(); |
| 6006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6007 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6008 | return Context.getMemberPointerType(op->getType(), |
| 6009 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 6010 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 6011 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 6012 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 6013 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6014 | // As above. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 6015 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 6016 | MD->isInstance()) |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 6017 | return Context.getMemberPointerType(op->getType(), |
| 6018 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 6019 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6020 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6021 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 6022 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 6023 | if (lval == Expr::LV_IncompleteVoidType) { |
| 6024 | // Taking the address of a void variable is technically illegal, but we |
| 6025 | // allow it in cases which are otherwise valid. |
| 6026 | // Example: "extern void x; void* y = &x;". |
| 6027 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 6028 | } |
| 6029 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6030 | // If the operand has type "type", the result has type "pointer to type". |
| 6031 | return Context.getPointerType(op->getType()); |
| 6032 | } |
| 6033 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6034 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6035 | if (Op->isTypeDependent()) |
| 6036 | return Context.DependentTy; |
| 6037 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6038 | UsualUnaryConversions(Op); |
| 6039 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6040 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6041 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 6042 | // incomplete type or void. It would be possible to warn about dereferencing |
| 6043 | // a void pointer, but it's completely well-defined, and such a warning is |
| 6044 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6045 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 6046 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6047 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6048 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 6049 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6050 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6051 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 6052 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6053 | return QualType(); |
| 6054 | } |
| 6055 | |
| 6056 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 6057 | tok::TokenKind Kind) { |
| 6058 | BinaryOperator::Opcode Opc; |
| 6059 | switch (Kind) { |
| 6060 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6061 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 6062 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6063 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 6064 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 6065 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 6066 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 6067 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 6068 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 6069 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 6070 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 6071 | case tok::less: Opc = BinaryOperator::LT; break; |
| 6072 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 6073 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 6074 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 6075 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 6076 | case tok::amp: Opc = BinaryOperator::And; break; |
| 6077 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 6078 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 6079 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 6080 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 6081 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 6082 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 6083 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 6084 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 6085 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 6086 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 6087 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 6088 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 6089 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 6090 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 6091 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 6092 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 6093 | } |
| 6094 | return Opc; |
| 6095 | } |
| 6096 | |
| 6097 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 6098 | tok::TokenKind Kind) { |
| 6099 | UnaryOperator::Opcode Opc; |
| 6100 | switch (Kind) { |
| 6101 | default: assert(0 && "Unknown unary op!"); |
| 6102 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 6103 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 6104 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 6105 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 6106 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 6107 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 6108 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 6109 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6110 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 6111 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 6112 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 6113 | } |
| 6114 | return Opc; |
| 6115 | } |
| 6116 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6117 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 6118 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 6119 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6120 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 6121 | unsigned Op, |
| 6122 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6123 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6124 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6125 | // The following two variables are used for compound assignment operators |
| 6126 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 6127 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6128 | |
| 6129 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6130 | case BinaryOperator::Assign: |
| 6131 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 6132 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6133 | case BinaryOperator::PtrMemD: |
| 6134 | case BinaryOperator::PtrMemI: |
| 6135 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 6136 | Opc == BinaryOperator::PtrMemI); |
| 6137 | break; |
| 6138 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6139 | case BinaryOperator::Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6140 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
| 6141 | Opc == BinaryOperator::Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6142 | break; |
| 6143 | case BinaryOperator::Rem: |
| 6144 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 6145 | break; |
| 6146 | case BinaryOperator::Add: |
| 6147 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 6148 | break; |
| 6149 | case BinaryOperator::Sub: |
| 6150 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 6151 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6152 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6153 | case BinaryOperator::Shr: |
| 6154 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 6155 | break; |
| 6156 | case BinaryOperator::LE: |
| 6157 | case BinaryOperator::LT: |
| 6158 | case BinaryOperator::GE: |
| 6159 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6160 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6161 | break; |
| 6162 | case BinaryOperator::EQ: |
| 6163 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6164 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6165 | break; |
| 6166 | case BinaryOperator::And: |
| 6167 | case BinaryOperator::Xor: |
| 6168 | case BinaryOperator::Or: |
| 6169 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 6170 | break; |
| 6171 | case BinaryOperator::LAnd: |
| 6172 | case BinaryOperator::LOr: |
| 6173 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 6174 | break; |
| 6175 | case BinaryOperator::MulAssign: |
| 6176 | case BinaryOperator::DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6177 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
| 6178 | Opc == BinaryOperator::DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6179 | CompLHSTy = CompResultTy; |
| 6180 | if (!CompResultTy.isNull()) |
| 6181 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6182 | break; |
| 6183 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6184 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 6185 | CompLHSTy = CompResultTy; |
| 6186 | if (!CompResultTy.isNull()) |
| 6187 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6188 | break; |
| 6189 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6190 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6191 | if (!CompResultTy.isNull()) |
| 6192 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6193 | break; |
| 6194 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6195 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 6196 | if (!CompResultTy.isNull()) |
| 6197 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6198 | break; |
| 6199 | case BinaryOperator::ShlAssign: |
| 6200 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6201 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 6202 | CompLHSTy = CompResultTy; |
| 6203 | if (!CompResultTy.isNull()) |
| 6204 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6205 | break; |
| 6206 | case BinaryOperator::AndAssign: |
| 6207 | case BinaryOperator::XorAssign: |
| 6208 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6209 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 6210 | CompLHSTy = CompResultTy; |
| 6211 | if (!CompResultTy.isNull()) |
| 6212 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6213 | break; |
| 6214 | case BinaryOperator::Comma: |
| 6215 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 6216 | break; |
| 6217 | } |
| 6218 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6219 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6220 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6221 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 6222 | else |
| 6223 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6224 | CompLHSTy, CompResultTy, |
| 6225 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6226 | } |
| 6227 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6228 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6229 | /// ParenRange in parentheses. |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6230 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6231 | const PartialDiagnostic &PD, |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6232 | SourceRange ParenRange, |
| 6233 | const PartialDiagnostic &SecondPD = PartialDiagnostic(0), |
| 6234 | SourceRange SecondParenRange = SourceRange()) { |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6235 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6236 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6237 | // We can't display the parentheses, so just dig the |
| 6238 | // warning/error and return. |
| 6239 | Self.Diag(Loc, PD); |
| 6240 | return; |
| 6241 | } |
| 6242 | |
| 6243 | Self.Diag(Loc, PD) |
| 6244 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6245 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6246 | |
| 6247 | if (!SecondPD.getDiagID()) |
| 6248 | return; |
| 6249 | |
| 6250 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6251 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6252 | // We can't display the parentheses, so just dig the |
| 6253 | // warning/error and return. |
| 6254 | Self.Diag(Loc, SecondPD); |
| 6255 | return; |
| 6256 | } |
| 6257 | |
| 6258 | Self.Diag(Loc, SecondPD) |
| 6259 | << CodeModificationHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6260 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6261 | } |
| 6262 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6263 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 6264 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 6265 | /// comparison operators have higher precedence. The most typical example of |
| 6266 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6267 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6268 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6269 | typedef BinaryOperator BinOp; |
| 6270 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 6271 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 6272 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6273 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6274 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6275 | rhsopc = BO->getOpcode(); |
| 6276 | |
| 6277 | // Subs are not binary operators. |
| 6278 | if (lhsopc == -1 && rhsopc == -1) |
| 6279 | return; |
| 6280 | |
| 6281 | // Bitwise operations are sometimes used as eager logical ops. |
| 6282 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6283 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 6284 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6285 | return; |
| 6286 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6287 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6288 | SuggestParentheses(Self, OpLoc, |
| 6289 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6290 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 6291 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6292 | lhs->getSourceRange(), |
| 6293 | PDiag(diag::note_precedence_bitwise_first) |
| 6294 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6295 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 6296 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 6297 | SuggestParentheses(Self, OpLoc, |
| 6298 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6299 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 6300 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 6301 | rhs->getSourceRange(), |
| 6302 | PDiag(diag::note_precedence_bitwise_first) |
| 6303 | << BinOp::getOpcodeStr(Opc), |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6304 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 6308 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 6309 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 6310 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 6311 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 6312 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6313 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 6314 | } |
| 6315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6316 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6317 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 6318 | tok::TokenKind Kind, |
| 6319 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6320 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6321 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6322 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6323 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 6324 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6325 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 6326 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 6327 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 6328 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6329 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 6330 | } |
| 6331 | |
| 6332 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 6333 | BinaryOperator::Opcode Opc, |
| 6334 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6335 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6337 | rhs->getType()->isOverloadableType())) { |
| 6338 | // Find all of the overloaded operators visible from this |
| 6339 | // point. We perform both an operator-name lookup from the local |
| 6340 | // scope and an argument-dependent lookup based on the types of |
| 6341 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6342 | UnresolvedSet<16> Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6343 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6344 | if (S && OverOp != OO_None) |
| 6345 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 6346 | Functions); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6347 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 6348 | // Build the (potentially-overloaded, potentially-dependent) |
| 6349 | // binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6350 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6351 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 6353 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6354 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6357 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6358 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6359 | ExprArg InputArg) { |
| 6360 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6361 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 6362 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6363 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6364 | QualType resultType; |
| 6365 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6366 | case UnaryOperator::OffsetOf: |
| 6367 | assert(false && "Invalid unary operator"); |
| 6368 | break; |
| 6369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6370 | case UnaryOperator::PreInc: |
| 6371 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6372 | case UnaryOperator::PostInc: |
| 6373 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 6374 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 6375 | Opc == UnaryOperator::PreInc || |
| 6376 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6377 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6378 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6379 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 6380 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6381 | case UnaryOperator::Deref: |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6382 | DefaultFunctionArrayLvalueConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6383 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 6384 | break; |
| 6385 | case UnaryOperator::Plus: |
| 6386 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6387 | UsualUnaryConversions(Input); |
| 6388 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6389 | if (resultType->isDependentType()) |
| 6390 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 6391 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 6392 | break; |
| 6393 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 6394 | resultType->isEnumeralType()) |
| 6395 | break; |
| 6396 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 6397 | Opc == UnaryOperator::Plus && |
| 6398 | resultType->isPointerType()) |
| 6399 | break; |
| 6400 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6401 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6402 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6403 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6404 | UsualUnaryConversions(Input); |
| 6405 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6406 | if (resultType->isDependentType()) |
| 6407 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6408 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 6409 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 6410 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6411 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 6412 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 6413 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6414 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6415 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6416 | break; |
| 6417 | case UnaryOperator::LNot: // logical negation |
| 6418 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6419 | DefaultFunctionArrayLvalueConversion(Input); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 6420 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6421 | if (resultType->isDependentType()) |
| 6422 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6423 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6424 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 6425 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6426 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6427 | // In C++, it's bool. C++ 5.3.1p8 |
| 6428 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6429 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6430 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6431 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 6432 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 6433 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6434 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6435 | resultType = Input->getType(); |
| 6436 | break; |
| 6437 | } |
| 6438 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 6439 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6440 | |
| 6441 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6442 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6443 | } |
| 6444 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6445 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6446 | UnaryOperator::Opcode Opc, |
| 6447 | ExprArg input) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6448 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 6449 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 6450 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6451 | // Find all of the overloaded operators visible from this |
| 6452 | // point. We perform both an operator-name lookup from the local |
| 6453 | // scope and an argument-dependent lookup based on the types of |
| 6454 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6455 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6456 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 6457 | if (S && OverOp != OO_None) |
| 6458 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 6459 | Functions); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6460 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6461 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 6462 | } |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6463 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 6464 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 6465 | } |
| 6466 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 6467 | // Unary Operators. 'Tok' is the token for the operator. |
| 6468 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 6469 | tok::TokenKind Op, ExprArg input) { |
| 6470 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 6471 | } |
| 6472 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 6473 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6474 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 6475 | SourceLocation LabLoc, |
| 6476 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6477 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 6478 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6479 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 6480 | // If we haven't seen this label yet, create a forward reference. It |
| 6481 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 6482 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 6483 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6485 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6486 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 6487 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6488 | } |
| 6489 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6490 | Sema::OwningExprResult |
| 6491 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 6492 | SourceLocation RPLoc) { // "({..})" |
| 6493 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6494 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 6495 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 6496 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6497 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 6498 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6499 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 6500 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6501 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 6502 | // example, it is not possible to goto into a stmt expression apparently. |
| 6503 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6504 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6505 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 6506 | // as the type of the stmtexpr. |
| 6507 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6508 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6509 | if (!Compound->body_empty()) { |
| 6510 | Stmt *LastStmt = Compound->body_back(); |
| 6511 | // If LastStmt is a label, skip down through into the body. |
| 6512 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 6513 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6514 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6515 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6516 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 6517 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6518 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6519 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 6520 | // expressions are not lvalues. |
| 6521 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6522 | substmt.release(); |
| 6523 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 6524 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6525 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6526 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 6527 | SourceLocation BuiltinLoc, |
| 6528 | SourceLocation TypeLoc, |
| 6529 | TypeTy *argty, |
| 6530 | OffsetOfComponent *CompPtr, |
| 6531 | unsigned NumComponents, |
| 6532 | SourceLocation RPLoc) { |
| 6533 | // FIXME: This function leaks all expressions in the offset components on |
| 6534 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6535 | // FIXME: Preserve type source info. |
| 6536 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6537 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6538 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6539 | bool Dependent = ArgTy->isDependentType(); |
| 6540 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6541 | // We must have at least one component that refers to the type, and the first |
| 6542 | // one is known to be a field designator. Verify that the ArgTy represents |
| 6543 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6544 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6545 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6546 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6547 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 6548 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 6549 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6550 | // Otherwise, create a null pointer as the base, and iteratively process |
| 6551 | // the offsetof designators. |
| 6552 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 6553 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6554 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6555 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 6556 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6557 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 6558 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6559 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 6560 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 6561 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 6562 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 6563 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6564 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6565 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 6566 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6567 | |
John McCall | d00f200 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 6568 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 6569 | diag::err_offsetof_incomplete_type)) |
| 6570 | return ExprError(); |
| 6571 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6572 | // FIXME: Dependent case loses a lot of information here. And probably |
| 6573 | // leaks like a sieve. |
| 6574 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 6575 | const OffsetOfComponent &OC = CompPtr[i]; |
| 6576 | if (OC.isBrackets) { |
| 6577 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 6578 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 6579 | if (!AT) { |
| 6580 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6581 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 6582 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 6586 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6587 | // Promote the array so it looks more like a normal array subscript |
| 6588 | // expression. |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6589 | DefaultFunctionArrayLvalueConversion(Res); |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 6590 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6591 | // C99 6.5.2.1p1 |
| 6592 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6593 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6594 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6595 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 6596 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6597 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6598 | |
| 6599 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 6600 | OC.LocEnd); |
| 6601 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6602 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6603 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6604 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6605 | if (!RC) { |
| 6606 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6607 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 6608 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6609 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 6610 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6611 | // Get the decl corresponding to this. |
| 6612 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6613 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 6614 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| 6615 | DiagRuntimeBehavior(BuiltinLoc, |
| 6616 | PDiag(diag::warn_offsetof_non_pod_type) |
| 6617 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 6618 | << Res->getType())) |
| 6619 | DidWarnAboutNonPOD = true; |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 6620 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6621 | |
John McCall | a24dc2e | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 6622 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 6623 | LookupQualifiedName(R, RD); |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 6624 | |
John McCall | 1bcee0a | 2009-12-02 08:25:40 +0000 | [diff] [blame] | 6625 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6626 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6627 | if (!MemberDecl) |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 6628 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 6629 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6630 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6631 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 6632 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6633 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 6634 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 09b6d0e | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 6635 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6636 | } else { |
Eli Friedman | 16c5378 | 2009-12-04 07:18:51 +0000 | [diff] [blame] | 6637 | PerformObjectMemberConversion(Res, MemberDecl); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 6638 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 6639 | // doesn't matter here. |
| 6640 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 6641 | MemberDecl->getType().getNonReferenceType()); |
| 6642 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6643 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6644 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6645 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6646 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 6647 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 6648 | } |
| 6649 | |
| 6650 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6651 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 6652 | TypeTy *arg1,TypeTy *arg2, |
| 6653 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6654 | // FIXME: Preserve type source info. |
| 6655 | QualType argT1 = GetTypeFromParser(arg1); |
| 6656 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6657 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6658 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6659 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 6660 | if (getLangOptions().CPlusPlus) { |
| 6661 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 6662 | << SourceRange(BuiltinLoc, RPLoc); |
| 6663 | return ExprError(); |
| 6664 | } |
| 6665 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6666 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 6667 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 6668 | } |
| 6669 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6670 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 6671 | ExprArg cond, |
| 6672 | ExprArg expr1, ExprArg expr2, |
| 6673 | SourceLocation RPLoc) { |
| 6674 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 6675 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 6676 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6677 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6678 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 6679 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6680 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6681 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 6682 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6683 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6684 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6685 | } else { |
| 6686 | // The conditional expression is required to be a constant expression. |
| 6687 | llvm::APSInt condEval(32); |
| 6688 | SourceLocation ExpLoc; |
| 6689 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6690 | return ExprError(Diag(ExpLoc, |
| 6691 | diag::err_typecheck_choose_expr_requires_constant) |
| 6692 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6693 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6694 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 6695 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6696 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 6697 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 6698 | } |
| 6699 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6700 | cond.release(); expr1.release(); expr2.release(); |
| 6701 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6702 | resType, RPLoc, |
| 6703 | resType->isDependentType(), |
| 6704 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 6705 | } |
| 6706 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6707 | //===----------------------------------------------------------------------===// |
| 6708 | // Clang Extensions. |
| 6709 | //===----------------------------------------------------------------------===// |
| 6710 | |
| 6711 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6712 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6713 | // Analyze block parameters. |
| 6714 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6715 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6716 | // Add BSI to CurBlock. |
| 6717 | BSI->PrevBlockInfo = CurBlock; |
| 6718 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6719 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6720 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6721 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 6722 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 6723 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6724 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 6725 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6726 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6727 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Ted Kremenek | 3cdff23 | 2009-12-07 22:01:30 +0000 | [diff] [blame] | 6728 | CurContext->addDecl(BSI->TheDecl); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 6729 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6730 | } |
| 6731 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6732 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6733 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6734 | |
| 6735 | if (ParamInfo.getNumTypeObjects() == 0 |
| 6736 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6737 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6738 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 6739 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6740 | if (T->isArrayType()) { |
| 6741 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6742 | diag::err_block_returns_array); |
| 6743 | return; |
| 6744 | } |
| 6745 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6746 | // The parameter list is optional, if there was none, assume (). |
| 6747 | if (!T->isFunctionType()) |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6748 | T = Context.getFunctionType(T, 0, 0, false, 0, false, false, 0, 0, false, |
| 6749 | CC_Default); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6750 | |
| 6751 | CurBlock->hasPrototype = true; |
| 6752 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6753 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6754 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6755 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6756 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6757 | // FIXME: remove the attribute. |
| 6758 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6759 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6760 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6761 | // Do not allow returning a objc interface by-value. |
| 6762 | if (RetTy->isObjCInterfaceType()) { |
| 6763 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6764 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6765 | return; |
| 6766 | } |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 6767 | |
| 6768 | CurBlock->ReturnType = RetTy; |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6769 | return; |
| 6770 | } |
| 6771 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6772 | // Analyze arguments to block. |
| 6773 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6774 | "Not a function declarator!"); |
| 6775 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6776 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6777 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6778 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6779 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6780 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6781 | // no arguments, not a function that takes a single void argument. |
| 6782 | if (FTI.hasPrototype && |
| 6783 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6784 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6785 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6786 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6787 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6788 | } else if (FTI.hasPrototype) { |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 6789 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) { |
| 6790 | ParmVarDecl *Param = FTI.ArgInfo[i].Param.getAs<ParmVarDecl>(); |
| 6791 | if (Param->getIdentifier() == 0 && |
| 6792 | !Param->isImplicit() && |
| 6793 | !Param->isInvalidDecl() && |
| 6794 | !getLangOptions().CPlusPlus) |
| 6795 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
| 6796 | CurBlock->Params.push_back(Param); |
| 6797 | } |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6798 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6799 | } |
Douglas Gregor | 838db38 | 2010-02-11 01:19:42 +0000 | [diff] [blame] | 6800 | CurBlock->TheDecl->setParams(CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6801 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6802 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6803 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6804 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 6805 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 6806 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 6807 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6808 | // If this has an identifier, add it to the scope stack. |
| 6809 | if ((*AI)->getIdentifier()) |
| 6810 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 6811 | } |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6812 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6813 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6814 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6815 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6816 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6817 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6818 | // FIXME: remove the attribute. |
| 6819 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6820 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6821 | // Analyze the return type. |
| 6822 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6823 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6824 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6825 | // Do not allow returning a objc interface by-value. |
| 6826 | if (RetTy->isObjCInterfaceType()) { |
| 6827 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6828 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6829 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6830 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6834 | /// is invoked to pop the information about the block from the action impl. |
| 6835 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6836 | // Ensure that CurBlock is deleted. |
| 6837 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6838 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6839 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6840 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6841 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6842 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6843 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6844 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6848 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6849 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6850 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6851 | // If blocks are disabled, emit an error. |
| 6852 | if (!LangOpts.Blocks) |
| 6853 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6854 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6855 | // Ensure that CurBlock is deleted. |
| 6856 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6857 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6858 | PopDeclContext(); |
| 6859 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6860 | // Pop off CurBlock, handle nested blocks. |
| 6861 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6862 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6863 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6864 | if (!BSI->ReturnType.isNull()) |
| 6865 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6866 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6867 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6868 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6869 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6870 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6871 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6872 | QualType BlockTy; |
| 6873 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6874 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6875 | NoReturn, CC_Default); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6876 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6877 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6878 | BSI->isVariadic, 0, false, false, 0, 0, |
Douglas Gregor | ce056bc | 2010-02-21 22:15:06 +0000 | [diff] [blame] | 6879 | NoReturn, CC_Default); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6880 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6881 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6882 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6883 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6884 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6885 | // If needed, diagnose invalid gotos and switches in the block. |
| 6886 | if (CurFunctionNeedsScopeChecking) |
| 6887 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6888 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6889 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6890 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | a3899eb | 2010-01-19 23:08:01 +0000 | [diff] [blame] | 6891 | |
| 6892 | bool Good = true; |
| 6893 | // Check goto/label use. |
| 6894 | for (llvm::DenseMap<IdentifierInfo*, LabelStmt*>::iterator |
| 6895 | I = BSI->LabelMap.begin(), E = BSI->LabelMap.end(); I != E; ++I) { |
| 6896 | LabelStmt *L = I->second; |
| 6897 | |
| 6898 | // Verify that we have no forward references left. If so, there was a goto |
| 6899 | // or address of a label taken, but no definition of it. |
| 6900 | if (L->getSubStmt() != 0) |
| 6901 | continue; |
| 6902 | |
| 6903 | // Emit error. |
| 6904 | Diag(L->getIdentLoc(), diag::err_undeclared_label_use) << L->getName(); |
| 6905 | Good = false; |
| 6906 | } |
| 6907 | BSI->LabelMap.clear(); |
| 6908 | if (!Good) |
| 6909 | return ExprError(); |
| 6910 | |
Mike Stump | fa6ef18 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 6911 | AnalysisContext AC(BSI->TheDecl); |
| 6912 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody(), AC); |
| 6913 | CheckUnreachable(AC); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6914 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6915 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6916 | } |
| 6917 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6918 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6919 | ExprArg expr, TypeTy *type, |
| 6920 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6921 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6922 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6923 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6924 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6925 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6926 | |
| 6927 | // Get the va_list type |
| 6928 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6929 | if (VaListType->isArrayType()) { |
| 6930 | // Deal with implicit array decay; for example, on x86-64, |
| 6931 | // va_list is an array, but it's supposed to decay to |
| 6932 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6933 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6934 | // Make sure the input expression also decays appropriately. |
| 6935 | UsualUnaryConversions(E); |
| 6936 | } else { |
| 6937 | // Otherwise, the va_list argument must be an l-value because |
| 6938 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6939 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6940 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6941 | return ExprError(); |
| 6942 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6943 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6944 | if (!E->isTypeDependent() && |
| 6945 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6946 | return ExprError(Diag(E->getLocStart(), |
| 6947 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6948 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6949 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6950 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6951 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6952 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6953 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6954 | expr.release(); |
| 6955 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6956 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6959 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6960 | // The type of __null will be int or long, depending on the size of |
| 6961 | // pointers on the target. |
| 6962 | QualType Ty; |
| 6963 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6964 | Ty = Context.IntTy; |
| 6965 | else |
| 6966 | Ty = Context.LongTy; |
| 6967 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6968 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6969 | } |
| 6970 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6971 | static void |
| 6972 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6973 | QualType DstType, |
| 6974 | Expr *SrcExpr, |
| 6975 | CodeModificationHint &Hint) { |
| 6976 | if (!SemaRef.getLangOptions().ObjC1) |
| 6977 | return; |
| 6978 | |
| 6979 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6980 | if (!PT) |
| 6981 | return; |
| 6982 | |
| 6983 | // Check if the destination is of type 'id'. |
| 6984 | if (!PT->isObjCIdType()) { |
| 6985 | // Check if the destination is the 'NSString' interface. |
| 6986 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6987 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6988 | return; |
| 6989 | } |
| 6990 | |
| 6991 | // Strip off any parens and casts. |
| 6992 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6993 | if (!SL || SL->isWide()) |
| 6994 | return; |
| 6995 | |
| 6996 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6997 | } |
| 6998 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6999 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 7000 | SourceLocation Loc, |
| 7001 | QualType DstType, QualType SrcType, |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7002 | Expr *SrcExpr, AssignmentAction Action) { |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7003 | // Decode the result (notice that AST's are still created for extensions). |
| 7004 | bool isInvalid = false; |
| 7005 | unsigned DiagKind; |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7006 | CodeModificationHint Hint; |
| 7007 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7008 | switch (ConvTy) { |
| 7009 | default: assert(0 && "Unknown conversion type"); |
| 7010 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 7011 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7012 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 7013 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 7014 | case IntToPointer: |
| 7015 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 7016 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7017 | case IncompatiblePointer: |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7018 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7019 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 7020 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 7021 | case IncompatiblePointerSign: |
| 7022 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 7023 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7024 | case FunctionVoidPointer: |
| 7025 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 7026 | break; |
| 7027 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 7028 | // If the qualifiers lost were because we were applying the |
| 7029 | // (deprecated) C++ conversion from a string literal to a char* |
| 7030 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 7031 | // Ideally, this check would be performed in |
| 7032 | // CheckPointerTypesForAssignment. However, that would require a |
| 7033 | // bit of refactoring (so that the second argument is an |
| 7034 | // expression, rather than a type), which should be done as part |
| 7035 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 7036 | // C++ semantics. |
| 7037 | if (getLangOptions().CPlusPlus && |
| 7038 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 7039 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7040 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 7041 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 7042 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 7043 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 7044 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7045 | case IntToBlockPointer: |
| 7046 | DiagKind = diag::err_int_to_block_pointer; |
| 7047 | break; |
| 7048 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 7049 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7050 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7051 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7052 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 7053 | // it can give a more specific diagnostic. |
| 7054 | DiagKind = diag::warn_incompatible_qualified_id; |
| 7055 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7056 | case IncompatibleVectors: |
| 7057 | DiagKind = diag::warn_incompatible_vectors; |
| 7058 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7059 | case Incompatible: |
| 7060 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 7061 | isInvalid = true; |
| 7062 | break; |
| 7063 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7064 | |
Douglas Gregor | 6864748 | 2009-12-16 03:45:30 +0000 | [diff] [blame] | 7065 | Diag(Loc, DiagKind) << DstType << SrcType << Action |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 7066 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7067 | return isInvalid; |
| 7068 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7069 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 7070 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7071 | llvm::APSInt ICEResult; |
| 7072 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 7073 | if (Result) |
| 7074 | *Result = ICEResult; |
| 7075 | return false; |
| 7076 | } |
| 7077 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7078 | Expr::EvalResult EvalResult; |
| 7079 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7080 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7081 | EvalResult.HasSideEffects) { |
| 7082 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 7083 | |
| 7084 | if (EvalResult.Diag) { |
| 7085 | // We only show the note if it's not the usual "invalid subexpression" |
| 7086 | // or if it's actually in a subexpression. |
| 7087 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 7088 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 7089 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 7090 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7091 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7092 | return true; |
| 7093 | } |
| 7094 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7095 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 7096 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7097 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 7098 | if (EvalResult.Diag && |
| 7099 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 7100 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7101 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 7102 | if (Result) |
| 7103 | *Result = EvalResult.Val.getInt(); |
| 7104 | return false; |
| 7105 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7106 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7107 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7108 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7109 | ExprEvalContexts.push_back( |
| 7110 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7113 | void |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7114 | Sema::PopExpressionEvaluationContext() { |
| 7115 | // Pop the current expression evaluation context off the stack. |
| 7116 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 7117 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7118 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 7119 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 7120 | if (Rec.PotentiallyReferenced) { |
| 7121 | // Mark any remaining declarations in the current position of the stack |
| 7122 | // as "referenced". If they were not meant to be referenced, semantic |
| 7123 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 7124 | for (PotentiallyReferencedDecls::iterator |
| 7125 | I = Rec.PotentiallyReferenced->begin(), |
| 7126 | IEnd = Rec.PotentiallyReferenced->end(); |
| 7127 | I != IEnd; ++I) |
| 7128 | MarkDeclarationReferenced(I->first, I->second); |
| 7129 | } |
| 7130 | |
| 7131 | if (Rec.PotentiallyDiagnosed) { |
| 7132 | // Emit any pending diagnostics. |
| 7133 | for (PotentiallyEmittedDiagnostics::iterator |
| 7134 | I = Rec.PotentiallyDiagnosed->begin(), |
| 7135 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 7136 | I != IEnd; ++I) |
| 7137 | Diag(I->first, I->second); |
| 7138 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | // When are coming out of an unevaluated context, clear out any |
| 7142 | // temporaries that we may have created as part of the evaluation of |
| 7143 | // the expression in that context: they aren't relevant because they |
| 7144 | // will never be constructed. |
| 7145 | if (Rec.Context == Unevaluated && |
| 7146 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 7147 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 7148 | ExprTemporaries.end()); |
| 7149 | |
| 7150 | // Destroy the popped expression evaluation record. |
| 7151 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7152 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7153 | |
| 7154 | /// \brief Note that the given declaration was referenced in the source code. |
| 7155 | /// |
| 7156 | /// This routine should be invoke whenever a given declaration is referenced |
| 7157 | /// in the source code, and where that reference occurred. If this declaration |
| 7158 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 7159 | /// C99 6.9p3), then the declaration will be marked as used. |
| 7160 | /// |
| 7161 | /// \param Loc the location where the declaration was referenced. |
| 7162 | /// |
| 7163 | /// \param D the declaration that has been referenced by the source code. |
| 7164 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 7165 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7166 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7167 | if (D->isUsed()) |
| 7168 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7169 | |
Douglas Gregor | b5352cf | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 7170 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 7171 | // template or not. The reason for this is that unevaluated expressions |
| 7172 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 7173 | // -Wunused-parameters) |
| 7174 | if (isa<ParmVarDecl>(D) || |
| 7175 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7176 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7177 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7178 | // Do not mark anything as "used" within a dependent context; wait for |
| 7179 | // an instantiation. |
| 7180 | if (CurContext->isDependentContext()) |
| 7181 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7182 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7183 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7184 | case Unevaluated: |
| 7185 | // We are in an expression that is not potentially evaluated; do nothing. |
| 7186 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7187 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7188 | case PotentiallyEvaluated: |
| 7189 | // We are in a potentially-evaluated expression, so this declaration is |
| 7190 | // "used"; handle this below. |
| 7191 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7192 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7193 | case PotentiallyPotentiallyEvaluated: |
| 7194 | // We are in an expression that may be potentially evaluated; queue this |
| 7195 | // declaration reference until we know whether the expression is |
| 7196 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 7197 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 7198 | return; |
| 7199 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7200 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7201 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 7202 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7203 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 7204 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 7205 | if (!Constructor->isUsed()) |
| 7206 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7207 | } else if (Constructor->isImplicit() && |
Douglas Gregor | 9e9199d | 2009-12-22 00:34:07 +0000 | [diff] [blame] | 7208 | Constructor->isCopyConstructor(TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 7209 | if (!Constructor->isUsed()) |
| 7210 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 7211 | } |
Anders Carlsson | d6a637f | 2009-12-07 08:24:59 +0000 | [diff] [blame] | 7212 | |
| 7213 | MaybeMarkVirtualMembersReferenced(Loc, Constructor); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 7214 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 7215 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 7216 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7217 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 7218 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 7219 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 7220 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 7221 | if (!MethodDecl->isUsed()) |
| 7222 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 7223 | } |
| 7224 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 7225 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7226 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 7227 | // class templates. |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7228 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7229 | bool AlreadyInstantiated = false; |
| 7230 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 7231 | = Function->getTemplateSpecializationInfo()) { |
| 7232 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 7233 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7234 | else if (SpecInfo->getTemplateSpecializationKind() |
| 7235 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7236 | AlreadyInstantiated = true; |
| 7237 | } else if (MemberSpecializationInfo *MSInfo |
| 7238 | = Function->getMemberSpecializationInfo()) { |
| 7239 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 7240 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 7241 | else if (MSInfo->getTemplateSpecializationKind() |
| 7242 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7243 | AlreadyInstantiated = true; |
| 7244 | } |
| 7245 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 7246 | if (!AlreadyInstantiated) { |
| 7247 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 7248 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 7249 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 7250 | Loc)); |
| 7251 | else |
| 7252 | PendingImplicitInstantiations.push_back(std::make_pair(Function, |
| 7253 | Loc)); |
| 7254 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7255 | } |
| 7256 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7257 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7258 | Function->setUsed(true); |
Tanya Lattner | e6bbc01 | 2010-02-12 00:07:30 +0000 | [diff] [blame] | 7259 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7260 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 7261 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7262 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7263 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7264 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7265 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 7266 | Var->getInstantiatedFromStaticDataMember()) { |
| 7267 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 7268 | assert(MSInfo && "Missing member specialization information?"); |
| 7269 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 7270 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 7271 | MSInfo->setPointOfInstantiation(Loc); |
| 7272 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 7273 | } |
| 7274 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7275 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7276 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7277 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7278 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 7279 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 7280 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 7281 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7282 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 7283 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 7284 | /// of the program being compiled. |
| 7285 | /// |
| 7286 | /// This routine emits the given diagnostic when the code currently being |
| 7287 | /// type-checked is "potentially evaluated", meaning that there is a |
| 7288 | /// possibility that the code will actually be executable. Code in sizeof() |
| 7289 | /// expressions, code used only during overload resolution, etc., are not |
| 7290 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 7291 | /// in the absolutely nutty case of potentially potentially evaluated |
| 7292 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
| 7293 | /// later. |
| 7294 | /// |
| 7295 | /// This routine should be used for all diagnostics that describe the run-time |
| 7296 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 7297 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 7298 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
| 7299 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, |
| 7300 | const PartialDiagnostic &PD) { |
| 7301 | switch (ExprEvalContexts.back().Context ) { |
| 7302 | case Unevaluated: |
| 7303 | // The argument will never be evaluated, so don't complain. |
| 7304 | break; |
| 7305 | |
| 7306 | case PotentiallyEvaluated: |
| 7307 | Diag(Loc, PD); |
| 7308 | return true; |
| 7309 | |
| 7310 | case PotentiallyPotentiallyEvaluated: |
| 7311 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 7312 | break; |
| 7313 | } |
| 7314 | |
| 7315 | return false; |
| 7316 | } |
| 7317 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 7318 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 7319 | CallExpr *CE, FunctionDecl *FD) { |
| 7320 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 7321 | return false; |
| 7322 | |
| 7323 | PartialDiagnostic Note = |
| 7324 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 7325 | << FD->getDeclName() : PDiag(); |
| 7326 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 7327 | |
| 7328 | if (RequireCompleteType(Loc, ReturnType, |
| 7329 | FD ? |
| 7330 | PDiag(diag::err_call_function_incomplete_return) |
| 7331 | << CE->getSourceRange() << FD->getDeclName() : |
| 7332 | PDiag(diag::err_call_incomplete_return) |
| 7333 | << CE->getSourceRange(), |
| 7334 | std::make_pair(NoteLoc, Note))) |
| 7335 | return true; |
| 7336 | |
| 7337 | return false; |
| 7338 | } |
| 7339 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7340 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 7341 | // will prevent this condition from triggering, which is what we want. |
| 7342 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 7343 | SourceLocation Loc; |
| 7344 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7345 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 7346 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7347 | if (isa<BinaryOperator>(E)) { |
| 7348 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 7349 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 7350 | return; |
| 7351 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7352 | // Greylist some idioms by putting them into a warning subcategory. |
| 7353 | if (ObjCMessageExpr *ME |
| 7354 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 7355 | Selector Sel = ME->getSelector(); |
| 7356 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 7357 | // self = [<foo> init...] |
| 7358 | if (isSelfExpr(Op->getLHS()) |
| 7359 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 7360 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7361 | |
| 7362 | // <foo> = [<bar> nextObject] |
| 7363 | else if (Sel.isUnarySelector() && |
| 7364 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 7365 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 7366 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7367 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7368 | Loc = Op->getOperatorLoc(); |
| 7369 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 7370 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 7371 | if (Op->getOperator() != OO_Equal) |
| 7372 | return; |
| 7373 | |
| 7374 | Loc = Op->getOperatorLoc(); |
| 7375 | } else { |
| 7376 | // Not an assignment. |
| 7377 | return; |
| 7378 | } |
| 7379 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7380 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | 2d15215 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 7381 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7382 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 7383 | Diag(Loc, diagnostic) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7384 | << E->getSourceRange() |
| 7385 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 7386 | << CodeModificationHint::CreateInsertion(Close, ")"); |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7387 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 7388 | << CodeModificationHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7389 | } |
| 7390 | |
| 7391 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 7392 | DiagnoseAssignmentAsCondition(E); |
| 7393 | |
| 7394 | if (!E->isTypeDependent()) { |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 7395 | DefaultFunctionArrayLvalueConversion(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 7396 | |
| 7397 | QualType T = E->getType(); |
| 7398 | |
| 7399 | if (getLangOptions().CPlusPlus) { |
| 7400 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 7401 | return true; |
| 7402 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 7403 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 7404 | << T << E->getSourceRange(); |
| 7405 | return true; |
| 7406 | } |
| 7407 | } |
| 7408 | |
| 7409 | return false; |
| 7410 | } |