Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Lex/LiteralSupport.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 07d754a | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Designator.h" |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 27 | #include "clang/Parse/Scope.h" |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 28 | #include "clang/Parse/Template.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 31 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 32 | /// \brief Determine whether the use of this declaration is valid, and |
| 33 | /// emit any corresponding diagnostics. |
| 34 | /// |
| 35 | /// This routine diagnoses various problems with referencing |
| 36 | /// declarations that can occur when using a declaration. For example, |
| 37 | /// it might warn if a deprecated or unavailable declaration is being |
| 38 | /// used, or produce an error (and return true) if a C++0x deleted |
| 39 | /// function is being used. |
| 40 | /// |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 41 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 42 | /// decls. |
| 43 | /// |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 44 | /// \returns true if there was an error (this declaration cannot be |
| 45 | /// referenced), false otherwise. |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 46 | /// |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 47 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 48 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 49 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 50 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 53 | // See if the decl is unavailable |
| 54 | if (D->getAttr<UnavailableAttr>()) { |
| 55 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 56 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 57 | } |
| 58 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 59 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 60 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 61 | if (FD->isDeleted()) { |
| 62 | Diag(Loc, diag::err_deleted_function_use); |
| 63 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 64 | return true; |
| 65 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 66 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 68 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 71 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 73 | /// attribute. It warns if call does not have the sentinel argument. |
| 74 | /// |
| 75 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 76 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 77 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 79 | return; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 80 | int sentinelPos = attr->getSentinel(); |
| 81 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 83 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 84 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 85 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 86 | bool warnNotEnoughArgs = false; |
| 87 | int isMethod = 0; |
| 88 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 89 | // skip over named parameters. |
| 90 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 91 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 92 | if (nullPos) |
| 93 | --nullPos; |
| 94 | else |
| 95 | ++i; |
| 96 | } |
| 97 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 98 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 99 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 100 | // skip over named parameters. |
| 101 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 102 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 103 | if (nullPos) |
| 104 | --nullPos; |
| 105 | else |
| 106 | ++i; |
| 107 | } |
| 108 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 109 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 110 | // block or function pointer call. |
| 111 | QualType Ty = V->getType(); |
| 112 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 114 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 115 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 116 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 117 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 118 | unsigned k; |
| 119 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 120 | if (nullPos) |
| 121 | --nullPos; |
| 122 | else |
| 123 | ++i; |
| 124 | } |
| 125 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 126 | } |
| 127 | if (Ty->isBlockPointerType()) |
| 128 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 129 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 130 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 131 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 132 | return; |
| 133 | |
| 134 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 135 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 136 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | int sentinel = i; |
| 140 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 141 | --sentinelPos; |
| 142 | ++i; |
| 143 | } |
| 144 | if (sentinelPos > 0) { |
| 145 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 146 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | while (i < NumArgs-1) { |
| 150 | ++i; |
| 151 | ++sentinel; |
| 152 | } |
| 153 | Expr *sentinelExpr = Args[sentinel]; |
| 154 | if (sentinelExpr && (!sentinelExpr->getType()->isPointerType() || |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 155 | !sentinelExpr->isNullPointerConstant(Context, |
| 156 | Expr::NPC_ValueDependentIsNull))) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 157 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 158 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 159 | } |
| 160 | return; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 163 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 164 | Expr *Ex = (Expr *)E; |
| 165 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 166 | } |
| 167 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 168 | //===----------------------------------------------------------------------===// |
| 169 | // Standard Promotions and Conversions |
| 170 | //===----------------------------------------------------------------------===// |
| 171 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 172 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 173 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 174 | QualType Ty = E->getType(); |
| 175 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 176 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 177 | if (Ty->isFunctionType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | 6904f64 | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 179 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 180 | else if (Ty->isArrayType()) { |
| 181 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 182 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 183 | // type 'array of type' is converted to an expression that has type 'pointer |
| 184 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 185 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 186 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 187 | // |
| 188 | // C++ 4.2p1: |
| 189 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 190 | // T" can be converted to an rvalue of type "pointer to T". |
| 191 | // |
| 192 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 193 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 8fc489d | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 194 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 195 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 196 | } |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 200 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 201 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 202 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 203 | /// In these instances, this routine should *not* be called. |
| 204 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 205 | QualType Ty = Expr->getType(); |
| 206 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 208 | // C99 6.3.1.1p2: |
| 209 | // |
| 210 | // The following may be used in an expression wherever an int or |
| 211 | // unsigned int may be used: |
| 212 | // - an object or expression with an integer type whose integer |
| 213 | // conversion rank is less than or equal to the rank of int |
| 214 | // and unsigned int. |
| 215 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 216 | // |
| 217 | // If an int can represent all values of the original type, the |
| 218 | // value is converted to an int; otherwise, it is converted to an |
| 219 | // unsigned int. These are called the integer promotions. All |
| 220 | // other types are unchanged by the integer promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 221 | QualType PTy = Context.isPromotableBitField(Expr); |
| 222 | if (!PTy.isNull()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 223 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 224 | return Expr; |
| 225 | } |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 226 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 227 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 228 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 229 | return Expr; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 232 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 233 | return Expr; |
| 234 | } |
| 235 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 236 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 238 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 239 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 240 | QualType Ty = Expr->getType(); |
| 241 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 243 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 244 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 245 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 246 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 247 | CastExpr::CK_FloatingCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 249 | UsualUnaryConversions(Expr); |
| 250 | } |
| 251 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 252 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 253 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 254 | /// interfaces passed by value. This returns true if the argument type is |
| 255 | /// completely illegal. |
| 256 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 257 | DefaultArgumentPromotion(Expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 259 | if (Expr->getType()->isObjCInterfaceType()) { |
| 260 | Diag(Expr->getLocStart(), |
| 261 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 262 | << Expr->getType() << CT; |
| 263 | return true; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 264 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 266 | if (!Expr->getType()->isPODType()) |
| 267 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 268 | << Expr->getType() << CT; |
| 269 | |
| 270 | return false; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 274 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 275 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 277 | /// responsible for emitting appropriate error diagnostics. |
| 278 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 279 | /// GCC. |
| 280 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 281 | bool isCompAssign) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 282 | if (!isCompAssign) |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 283 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 284 | |
| 285 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 286 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 288 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 289 | QualType lhs = |
| 290 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | QualType rhs = |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 292 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 293 | |
| 294 | // If both types are identical, no conversion is needed. |
| 295 | if (lhs == rhs) |
| 296 | return lhs; |
| 297 | |
| 298 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 299 | // The caller can deal with this (e.g. pointer + int). |
| 300 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 301 | return lhs; |
| 302 | |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 303 | // Perform bitfield promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 304 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 305 | if (!LHSBitfieldPromoteTy.isNull()) |
| 306 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 307 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 308 | if (!RHSBitfieldPromoteTy.isNull()) |
| 309 | rhs = RHSBitfieldPromoteTy; |
| 310 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 311 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 312 | if (!isCompAssign) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 313 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 314 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 315 | return destType; |
| 316 | } |
| 317 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 318 | //===----------------------------------------------------------------------===// |
| 319 | // Semantic Analysis for various Expression Types |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | |
| 322 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 323 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 324 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 325 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 326 | /// multiple tokens. However, the common case is that StringToks points to one |
| 327 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 328 | /// |
| 329 | Action::OwningExprResult |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 330 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 331 | assert(NumStringToks && "Must have at least one string!"); |
| 332 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 333 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 334 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 335 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 337 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 338 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 339 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 341 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | cbad725 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 342 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 343 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 344 | |
| 345 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 346 | if (getLangOptions().CPlusPlus) |
| 347 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 349 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 350 | // the nul terminator character as well as the string length for pascal |
| 351 | // strings. |
| 352 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 353 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 354 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 356 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 358 | Literal.GetStringLength(), |
| 359 | Literal.AnyWide, StrTy, |
| 360 | &StringTokLocs[0], |
| 361 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 364 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 365 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 366 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 367 | /// for values inside the block or for globals). |
| 368 | /// |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 369 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 370 | /// up-to-date. |
| 371 | /// |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 372 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 373 | ValueDecl *VD) { |
| 374 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 375 | // we wanted to. |
| 376 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 377 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 379 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 380 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 381 | return false; |
| 382 | |
| 383 | // If this is a reference to an extern, static, or global variable, no need to |
| 384 | // snapshot it. |
| 385 | // FIXME: What about 'const' variables in C++? |
| 386 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 387 | if (!Var->hasLocalStorage()) |
| 388 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 390 | // Blocks that have these can't be constant. |
| 391 | CurBlock->hasBlockDeclRefExprs = true; |
| 392 | |
| 393 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 394 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 395 | // be defined outside all of the current blocks (in which case the blocks do |
| 396 | // all get the bit). Walk the nesting chain. |
| 397 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 398 | NextBlock = NextBlock->PrevBlockInfo) { |
| 399 | // If we found the defining block for the variable, don't mark the block as |
| 400 | // having a reference outside it. |
| 401 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 402 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 403 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 404 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 405 | // a snapshot as well. |
| 406 | NextBlock->hasBlockDeclRefExprs = true; |
| 407 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 409 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 412 | |
| 413 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 414 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 415 | Sema::OwningExprResult |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 416 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 417 | bool TypeDependent, bool ValueDependent, |
| 418 | const CXXScopeSpec *SS) { |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 419 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 420 | Diag(Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 421 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 422 | << D->getDeclName(); |
| 423 | return ExprError(); |
| 424 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 426 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 427 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 428 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 429 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 431 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 433 | << D->getIdentifier(); |
| 434 | return ExprError(); |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 440 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 442 | return Owned(DeclRefExpr::Create(Context, |
| 443 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 444 | SS? SS->getRange() : SourceRange(), |
| 445 | D, Loc, |
| 446 | Ty, TypeDependent, ValueDependent)); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 449 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 450 | /// variable corresponding to the anonymous union or struct whose type |
| 451 | /// is Record. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 452 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 453 | RecordDecl *Record) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 455 | "Record must be an anonymous struct or union!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 457 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 458 | // operation rather than a slow walk through DeclContext's vector (which |
| 459 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 460 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 462 | DEnd = Ctx->decls_end(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 463 | D != DEnd; ++D) { |
| 464 | if (*D == Record) { |
| 465 | // The object for the anonymous struct/union directly |
| 466 | // follows its type in the list of declarations. |
| 467 | ++D; |
| 468 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 469 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 470 | return *D; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | assert(false && "Missing object for anonymous record"); |
| 475 | return 0; |
| 476 | } |
| 477 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 478 | /// \brief Given a field that represents a member of an anonymous |
| 479 | /// struct/union, build the path from that field's context to the |
| 480 | /// actual member. |
| 481 | /// |
| 482 | /// Construct the sequence of field member references we'll have to |
| 483 | /// perform to get to the field in the anonymous union/struct. The |
| 484 | /// list of members is built from the field outward, so traverse it |
| 485 | /// backwards to go from an object in the current context to the field |
| 486 | /// we found. |
| 487 | /// |
| 488 | /// \returns The variable from which the field access should begin, |
| 489 | /// for an anonymous struct/union that is not a member of another |
| 490 | /// class. Otherwise, returns NULL. |
| 491 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 492 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 493 | assert(Field->getDeclContext()->isRecord() && |
| 494 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 495 | && "Field must be stored inside an anonymous struct or union"); |
| 496 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 497 | Path.push_back(Field); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 498 | VarDecl *BaseObject = 0; |
| 499 | DeclContext *Ctx = Field->getDeclContext(); |
| 500 | do { |
| 501 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 502 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 503 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 504 | Path.push_back(AnonField); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 505 | else { |
| 506 | BaseObject = cast<VarDecl>(AnonObject); |
| 507 | break; |
| 508 | } |
| 509 | Ctx = Ctx->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | } while (Ctx->isRecord() && |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 511 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 512 | |
| 513 | return BaseObject; |
| 514 | } |
| 515 | |
| 516 | Sema::OwningExprResult |
| 517 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 518 | FieldDecl *Field, |
| 519 | Expr *BaseObjectExpr, |
| 520 | SourceLocation OpLoc) { |
| 521 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 522 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 523 | AnonFields); |
| 524 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 525 | // Build the expression that refers to the base object, from |
| 526 | // which we will build a sequence of member references to each |
| 527 | // of the anonymous union objects and, eventually, the field we |
| 528 | // found via name lookup. |
| 529 | bool BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 530 | Qualifiers BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 531 | if (BaseObject) { |
| 532 | // BaseObject is an anonymous struct/union variable (and is, |
| 533 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 534 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 535 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 536 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 537 | SourceLocation()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 538 | BaseQuals |
| 539 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 540 | } else if (BaseObjectExpr) { |
| 541 | // The caller provided the base object expression. Determine |
| 542 | // whether its a pointer and whether it adds any qualifiers to the |
| 543 | // anonymous struct/union fields we're looking into. |
| 544 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 545 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 546 | BaseObjectIsPointer = true; |
| 547 | ObjectType = ObjectPtr->getPointeeType(); |
| 548 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 549 | BaseQuals |
| 550 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 551 | } else { |
| 552 | // We've found a member of an anonymous struct/union that is |
| 553 | // inside a non-anonymous struct/union, so in a well-formed |
| 554 | // program our base object expression is "this". |
| 555 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 556 | if (!MD->isStatic()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 557 | QualType AnonFieldType |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 558 | = Context.getTagDeclType( |
| 559 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 560 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 562 | == Context.getCanonicalType(ThisType)) || |
| 563 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 564 | // Our base object expression is "this". |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 565 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 566 | MD->getThisType(Context)); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 567 | BaseObjectIsPointer = true; |
| 568 | } |
| 569 | } else { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 570 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 571 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 572 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 573 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | if (!BaseObjectExpr) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 577 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 578 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | // Build the implicit member references to the field of the |
| 582 | // anonymous struct/union. |
| 583 | Expr *Result = BaseObjectExpr; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 584 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 585 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 586 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 587 | FI != FIEnd; ++FI) { |
| 588 | QualType MemberType = (*FI)->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 589 | Qualifiers MemberTypeQuals = |
| 590 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 591 | |
| 592 | // CVR attributes from the base are picked up by members, |
| 593 | // except that 'mutable' members don't pick up 'const'. |
| 594 | if ((*FI)->isMutable()) |
| 595 | ResultQuals.removeConst(); |
| 596 | |
| 597 | // GC attributes are never picked up by members. |
| 598 | ResultQuals.removeObjCGCAttr(); |
| 599 | |
| 600 | // TR 18037 does not allow fields to be declared with address spaces. |
| 601 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 602 | |
| 603 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 604 | if (NewQuals != MemberTypeQuals) |
| 605 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 606 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 607 | MarkDeclarationReferenced(Loc, *FI); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 608 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 609 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 610 | OpLoc, MemberType); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 611 | BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 612 | ResultQuals = NewQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 615 | return Owned(Result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 618 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 619 | const CXXScopeSpec &SS, |
| 620 | UnqualifiedId &Name, |
| 621 | bool HasTrailingLParen, |
| 622 | bool IsAddressOfOperand) { |
| 623 | if (Name.getKind() == UnqualifiedId::IK_TemplateId) { |
| 624 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 625 | Name.TemplateId->getTemplateArgs(), |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 626 | Name.TemplateId->NumArgs); |
| 627 | return ActOnTemplateIdExpr(SS, |
| 628 | TemplateTy::make(Name.TemplateId->Template), |
| 629 | Name.TemplateId->TemplateNameLoc, |
| 630 | Name.TemplateId->LAngleLoc, |
| 631 | TemplateArgsPtr, |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 632 | Name.TemplateId->RAngleLoc); |
| 633 | } |
| 634 | |
| 635 | // FIXME: We lose a bunch of source information by doing this. Later, |
| 636 | // we'll want to merge ActOnDeclarationNameExpr's logic into |
| 637 | // ActOnIdExpression. |
| 638 | return ActOnDeclarationNameExpr(S, |
| 639 | Name.StartLocation, |
| 640 | GetNameFromUnqualifiedId(Name), |
| 641 | HasTrailingLParen, |
| 642 | &SS, |
| 643 | IsAddressOfOperand); |
| 644 | } |
| 645 | |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 646 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 647 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 648 | /// performs lookup on that name and returns an expression that refers |
| 649 | /// to that name. This routine isn't directly called from the parser, |
| 650 | /// because the parser doesn't know about DeclarationName. Rather, |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 651 | /// this routine is called by ActOnIdExpression, which contains a |
| 652 | /// parsed UnqualifiedId. |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 653 | /// |
| 654 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 655 | /// function call context. LookupCtx is only used for a C++ |
| 656 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 657 | /// the identifier must be a member of. |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 658 | /// |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 659 | /// isAddressOfOperand means that this expression is the direct operand |
| 660 | /// of an address-of operator. This matters because this is the only |
| 661 | /// situation where a qualified name referencing a non-static member may |
| 662 | /// appear outside a member function of this class. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 663 | Sema::OwningExprResult |
| 664 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 665 | DeclarationName Name, bool HasTrailingLParen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 666 | const CXXScopeSpec *SS, |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 667 | bool isAddressOfOperand) { |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 668 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 669 | if (SS && SS->isInvalid()) |
| 670 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 671 | |
| 672 | // C++ [temp.dep.expr]p3: |
| 673 | // An id-expression is type-dependent if it contains: |
| 674 | // -- a nested-name-specifier that contains a class-name that |
| 675 | // names a dependent type. |
Douglas Gregor | 82dbbd7 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 676 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 677 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | f21eb49 | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 678 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | Loc, SS->getRange(), |
Anders Carlsson | 03f89b1 | 2009-07-09 00:05:08 +0000 | [diff] [blame] | 680 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()), |
| 681 | isAddressOfOperand)); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 682 | } |
| 683 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 684 | LookupResult Lookup(*this, Name, Loc, LookupOrdinaryName); |
| 685 | LookupParsedName(Lookup, S, SS, true); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 686 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 687 | if (Lookup.isAmbiguous()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 688 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 689 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 690 | NamedDecl *D = Lookup.getAsSingleDecl(Context); |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 691 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 692 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 693 | // well. |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 694 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 695 | if (II && getCurMethodDecl()) { |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 696 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 697 | // in which case we should look for an ivar. 2) scoped lookup could have |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | // found a decl, but that decl is outside the current instance method (i.e. |
| 699 | // a global variable). In these two cases, we do a lookup for an ivar with |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 700 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 701 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 702 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 703 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 704 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Chris Lattner | 50afe31 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 705 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 706 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 707 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 709 | // If we're referencing an invalid decl, just return this as a silent |
| 710 | // error node. The error diagnostic was already emitted on the decl. |
| 711 | if (IV->isInvalidDecl()) |
| 712 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 714 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 715 | // If a class method attemps to use a free standing ivar, this is |
| 716 | // an error. |
| 717 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 718 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 719 | << IV->getDeclName()); |
| 720 | // If a class method uses a global variable, even if an ivar with |
| 721 | // same name exists, use the global. |
| 722 | if (!IsClsMethod) { |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 723 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 724 | ClassDeclared != IFace) |
| 725 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 726 | // FIXME: This should use a new expr for a direct reference, don't |
| 727 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 728 | IdentifierInfo &II = Context.Idents.get("self"); |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 729 | UnqualifiedId SelfName; |
| 730 | SelfName.setIdentifier(&II, SourceLocation()); |
| 731 | CXXScopeSpec SelfScopeSpec; |
| 732 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 733 | SelfName, false, false); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 734 | MarkDeclarationReferenced(Loc, IV); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 735 | return Owned(new (Context) |
| 736 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 737 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 739 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 740 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 741 | // We should warn if a local variable hides an ivar. |
| 742 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 743 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 744 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 745 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 746 | IFace == ClassDeclared) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 747 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 748 | } |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 749 | } |
Steve Naroff | 0d7c6db | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 750 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 751 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 752 | QualType T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 754 | if (getCurMethodDecl()->isInstanceMethod()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 755 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 756 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 757 | else |
| 758 | T = Context.getObjCClassType(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 759 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 760 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 761 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 763 | // Determine whether this name might be a candidate for |
| 764 | // argument-dependent lookup. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 765 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 766 | HasTrailingLParen; |
| 767 | |
| 768 | if (ADL && D == 0) { |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 769 | // We've seen something of the form |
| 770 | // |
| 771 | // identifier( |
| 772 | // |
| 773 | // and we did not find any entity by the name |
| 774 | // "identifier". However, this identifier is still subject to |
| 775 | // argument-dependent lookup, so keep track of the name. |
| 776 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 777 | Context.OverloadTy, |
| 778 | Loc)); |
| 779 | } |
| 780 | |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 781 | if (D == 0) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 782 | // Otherwise, this could be an implicitly declared function reference (legal |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 783 | // in C90, extension in C99). |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 784 | if (HasTrailingLParen && II && |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 785 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 786 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 787 | else { |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 788 | // If this name wasn't predeclared and if this is not a function call, |
| 789 | // diagnose the problem. |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 790 | if (SS && !SS->isEmpty()) |
| 791 | return ExprError(Diag(Loc, diag::err_no_member) |
| 792 | << Name << computeDeclContext(*SS, false) |
| 793 | << SS->getRange()); |
| 794 | else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 795 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 796 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 797 | << Name.getAsString()); |
Argyrios Kyrtzidis | 16ac9be | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 798 | else |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 799 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 800 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 801 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 803 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 804 | // Warn about constructs like: |
| 805 | // if (void *X = foo()) { ... } else { X }. |
| 806 | // In the else block, the pointer is always false. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 808 | // FIXME: In a template instantiation, we don't have scope |
| 809 | // information to check this property. |
| 810 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 811 | Scope *CheckS = S; |
Douglas Gregor | 13a2c03 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 812 | while (CheckS && CheckS->getControlParent()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | if (CheckS->isWithinElse() && |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 814 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
Douglas Gregor | 13a2c03 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 815 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 816 | << Var->getDeclName() |
| 817 | << (Var->getType()->isPointerType()? 2 : |
| 818 | Var->getType()->isBooleanType()? 1 : 0)); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 819 | break; |
| 820 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Douglas Gregor | 13a2c03 | 2009-11-05 17:49:26 +0000 | [diff] [blame] | 822 | // Move to the parent of this scope. |
| 823 | CheckS = CheckS->getParent(); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 827 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 828 | // C99 DR 316 says that, if a function type comes from a |
| 829 | // function definition (without a prototype), that type is only |
| 830 | // used for checking compatibility. Therefore, when referencing |
| 831 | // the function, we pretend that we don't have the full function |
| 832 | // type. |
| 833 | if (DiagnoseUseOfDecl(Func, Loc)) |
| 834 | return ExprError(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 835 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 836 | QualType T = Func->getType(); |
| 837 | QualType NoProtoType = T; |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 838 | if (const FunctionProtoType *Proto = T->getAs<FunctionProtoType>()) |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 839 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
| 840 | return BuildDeclRefExpr(Func, NoProtoType, Loc, false, false, SS); |
| 841 | } |
| 842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 844 | return BuildDeclarationNameExpr(Loc, D, HasTrailingLParen, SS, isAddressOfOperand); |
| 845 | } |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 846 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 847 | bool |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 848 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 849 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 851 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | QualType DestType = |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 853 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 854 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 855 | return false; |
| 856 | QualType FromRecordType = From->getType(); |
| 857 | QualType DestRecordType = DestType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 858 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 859 | DestType = Context.getPointerType(DestType); |
| 860 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 861 | } |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 862 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 863 | CheckDerivedToBaseConversion(FromRecordType, |
| 864 | DestRecordType, |
| 865 | From->getSourceRange().getBegin(), |
| 866 | From->getSourceRange())) |
| 867 | return true; |
Anders Carlsson | a076d14 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 868 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 869 | /*isLvalue=*/true); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 870 | } |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 871 | return false; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 872 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 873 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 874 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
| 876 | const CXXScopeSpec *SS, NamedDecl *Member, |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 877 | SourceLocation Loc, QualType Ty) { |
| 878 | if (SS && SS->isSet()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 879 | return MemberExpr::Create(C, Base, isArrow, |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 880 | (NestedNameSpecifier *)SS->getScopeRep(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | SS->getRange(), Member, Loc, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 882 | // FIXME: Explicit template argument lists |
| 883 | false, SourceLocation(), 0, 0, SourceLocation(), |
| 884 | Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 886 | return new (C) MemberExpr(Base, isArrow, Member, Loc, Ty); |
| 887 | } |
| 888 | |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 889 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 890 | Sema::OwningExprResult |
| 891 | Sema::BuildDeclarationNameExpr(SourceLocation Loc, NamedDecl *D, |
| 892 | bool HasTrailingLParen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | const CXXScopeSpec *SS, |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 894 | bool isAddressOfOperand) { |
| 895 | assert(D && "Cannot refer to a NULL declaration"); |
| 896 | DeclarationName Name = D->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 898 | // If this is an expression of the form &Class::member, don't build an |
| 899 | // implicit member ref, because we want a pointer to the member in general, |
| 900 | // not any specific instance's member. |
| 901 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 902 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 2ada048 | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 903 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 904 | QualType DType; |
| 905 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 906 | DType = FD->getType().getNonReferenceType(); |
| 907 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 908 | DType = Method->getType(); |
| 909 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 910 | DType = Context.OverloadTy; |
| 911 | } |
| 912 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 913 | if (!DType.isNull()) { |
| 914 | // The pointer is type- and value-dependent if it points into something |
| 915 | // dependent. |
Douglas Gregor | 82dbbd7 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 916 | bool Dependent = DC->isDependentContext(); |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 917 | return BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 922 | // We may have found a field within an anonymous union or struct |
| 923 | // (C++ [class.union]). |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 924 | // FIXME: This needs to happen post-isImplicitMemberReference? |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 925 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 926 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 927 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 929 | // Cope with an implicit member access in a C++ non-static member function. |
| 930 | QualType ThisType, MemberType; |
| 931 | if (isImplicitMemberReference(SS, D, Loc, ThisType, MemberType)) { |
| 932 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), ThisType); |
| 933 | MarkDeclarationReferenced(Loc, D); |
| 934 | if (PerformObjectMemberConversion(This, D)) |
| 935 | return ExprError(); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 937 | bool ShouldCheckUse = true; |
| 938 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 939 | // Don't diagnose the use of a virtual member function unless it's |
| 940 | // explicitly qualified. |
| 941 | if (MD->isVirtual() && (!SS || !SS->isSet())) |
| 942 | ShouldCheckUse = false; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 943 | } |
Douglas Gregor | 6493d9c | 2009-10-22 07:08:30 +0000 | [diff] [blame] | 944 | |
| 945 | if (ShouldCheckUse && DiagnoseUseOfDecl(D, Loc)) |
| 946 | return ExprError(); |
| 947 | return Owned(BuildMemberExpr(Context, This, true, SS, D, |
| 948 | Loc, MemberType)); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 951 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 952 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 953 | if (MD->isStatic()) |
| 954 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 955 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 956 | << FD->getDeclName()); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 959 | // Any other ways we could have found the field in a well-formed |
| 960 | // program would have been turned into implicit member expressions |
| 961 | // above. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 962 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 963 | << FD->getDeclName()); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 964 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 965 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 966 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 967 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 1b0ea82 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 968 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 969 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 0811489 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 970 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 971 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 972 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 973 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 974 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 975 | return BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 976 | false, false, SS); |
Douglas Gregor | d32e028 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 977 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 978 | return BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 979 | false, false, SS); |
Anders Carlsson | 938b100 | 2009-08-29 01:06:32 +0000 | [diff] [blame] | 980 | else if (UnresolvedUsingDecl *UD = dyn_cast<UnresolvedUsingDecl>(D)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | return BuildDeclRefExpr(UD, Context.DependentTy, Loc, |
| 982 | /*TypeDependent=*/true, |
Anders Carlsson | 938b100 | 2009-08-29 01:06:32 +0000 | [diff] [blame] | 983 | /*ValueDependent=*/true, SS); |
| 984 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 985 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 986 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 987 | // Check whether this declaration can be used. Note that we suppress |
| 988 | // this check when we're going to perform argument-dependent lookup |
| 989 | // on this function name, because this might not be the function |
| 990 | // that overload resolution actually selects. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 991 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 992 | HasTrailingLParen; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 993 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 994 | return ExprError(); |
| 995 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 996 | // Only create DeclRefExpr's for valid Decl's. |
| 997 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 998 | return ExprError(); |
| 999 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1000 | // If the identifier reference is inside a block, and it refers to a value |
| 1001 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1002 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1003 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1004 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1005 | // We do not do this for things like enum constants, global variables, etc, |
| 1006 | // as they do not get snapshotted. |
| 1007 | // |
| 1008 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1009 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1010 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1011 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1012 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1013 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1014 | // This is to record that a 'const' was actually synthesize and added. |
| 1015 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1016 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1017 | |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1018 | ExprTy.addConst(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1019 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1020 | constAdded)); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1021 | } |
| 1022 | // If this reference is not in a block or if the referenced variable is |
| 1023 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1025 | bool TypeDependent = false; |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1026 | bool ValueDependent = false; |
| 1027 | if (getLangOptions().CPlusPlus) { |
| 1028 | // C++ [temp.dep.expr]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1029 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1030 | // - an identifier that was declared with a dependent type, |
| 1031 | if (VD->getType()->isDependentType()) |
| 1032 | TypeDependent = true; |
| 1033 | // - FIXME: a template-id that is dependent, |
| 1034 | // - a conversion-function-id that specifies a dependent type, |
| 1035 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1036 | Name.getCXXNameType()->isDependentType()) |
| 1037 | TypeDependent = true; |
| 1038 | // - a nested-name-specifier that contains a class-name that |
| 1039 | // names a dependent type. |
| 1040 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | 5253768 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1041 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1042 | DC; DC = DC->getParent()) { |
| 1043 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1044 | if (DC->isRecord()) { |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1045 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1046 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1047 | TypeDependent = true; |
| 1048 | break; |
| 1049 | } |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1050 | } |
| 1051 | } |
| 1052 | } |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1054 | // C++ [temp.dep.constexpr]p2: |
| 1055 | // |
| 1056 | // An identifier is value-dependent if it is: |
| 1057 | // - a name declared with a dependent type, |
| 1058 | if (TypeDependent) |
| 1059 | ValueDependent = true; |
| 1060 | // - the name of a non-type template parameter, |
| 1061 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1062 | ValueDependent = true; |
| 1063 | // - a constant with integral or enumeration type and is |
| 1064 | // initialized with an expression that is value-dependent |
Eli Friedman | dd49ee3 | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1065 | else if (const VarDecl *Dcl = dyn_cast<VarDecl>(VD)) { |
Mike Stump | 96638af | 2009-11-03 22:20:01 +0000 | [diff] [blame] | 1066 | if (Context.getCanonicalType(Dcl->getType()).getCVRQualifiers() |
| 1067 | == Qualifiers::Const && |
Eli Friedman | dd49ee3 | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1068 | Dcl->getInit()) { |
| 1069 | ValueDependent = Dcl->getInit()->isValueDependent(); |
| 1070 | } |
| 1071 | } |
Douglas Gregor | 872ffce | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1072 | } |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1073 | |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1074 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1075 | TypeDependent, ValueDependent, SS); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1076 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1077 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1078 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1079 | tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1080 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1081 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1082 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1083 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1084 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1085 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1086 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1087 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1088 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1089 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1090 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1092 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1093 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1094 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1095 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1096 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1097 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1098 | QualType ResTy; |
| 1099 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1100 | ResTy = Context.DependentTy; |
| 1101 | } else { |
| 1102 | unsigned Length = |
| 1103 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1104 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1105 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1106 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1107 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1108 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1109 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1112 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1113 | llvm::SmallString<16> CharBuffer; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1114 | CharBuffer.resize(Tok.getLength()); |
| 1115 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1116 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1117 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1118 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1119 | Tok.getLocation(), PP); |
| 1120 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1121 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1122 | |
| 1123 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1124 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1125 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1126 | Literal.isWide(), |
| 1127 | type, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1130 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1131 | // Fast path for a single digit (which is quite common). A single digit |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1132 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1133 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1134 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1135 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1136 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1137 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1138 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1139 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1140 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1141 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1142 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 1143 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1145 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 1146 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1147 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 1149 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1150 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1151 | return ExprError(); |
| 1152 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1153 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1154 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1155 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1156 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1157 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1158 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1159 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1160 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1161 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1162 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1163 | |
| 1164 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1165 | |
Ted Kremenek | 3a2c950 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1166 | // isExact will be set by GetFloatValue(). |
| 1167 | bool isExact = false; |
Chris Lattner | e4edb8e | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1168 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1169 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1170 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1171 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1172 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1173 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1174 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1175 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1176 | // long long is a C99 feature. |
| 1177 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1178 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1179 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1180 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1181 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1182 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1183 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1184 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1185 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1186 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1187 | Ty = Context.UnsignedLongLongTy; |
| 1188 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1189 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1190 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1191 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1192 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1193 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1194 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1195 | // be an unsigned int. |
| 1196 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1197 | |
| 1198 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1199 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1200 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1201 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1202 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1203 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1204 | // Does it fit in a unsigned int? |
| 1205 | if (ResultVal.isIntN(IntSize)) { |
| 1206 | // Does it fit in a signed int? |
| 1207 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1208 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1209 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1210 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1211 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1212 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1213 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1214 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1215 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1216 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1217 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1219 | // Does it fit in a unsigned long? |
| 1220 | if (ResultVal.isIntN(LongSize)) { |
| 1221 | // Does it fit in a signed long? |
| 1222 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1223 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1224 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1225 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1226 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1227 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1230 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1231 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1232 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1233 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1234 | // Does it fit in a unsigned long long? |
| 1235 | if (ResultVal.isIntN(LongLongSize)) { |
| 1236 | // Does it fit in a signed long long? |
| 1237 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1238 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1239 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1240 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1241 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1244 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1245 | // If we still couldn't decide a type, we probably have something that |
| 1246 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1247 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1248 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1249 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1250 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1251 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1252 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1253 | if (ResultVal.getBitWidth() != Width) |
| 1254 | ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1255 | } |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1256 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1257 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1259 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1260 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1262 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1263 | |
| 1264 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1267 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1268 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1269 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1270 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1271 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 1274 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 1275 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1276 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1277 | SourceLocation OpLoc, |
| 1278 | const SourceRange &ExprRange, |
| 1279 | bool isSizeof) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1280 | if (exprType->isDependentType()) |
| 1281 | return false; |
| 1282 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1283 | // C99 6.5.3.4p1: |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1284 | if (exprType->isFunctionType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1285 | // alignof(function) is allowed as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1286 | if (isSizeof) |
| 1287 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1288 | return false; |
| 1289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1291 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1292 | if (exprType->isVoidType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1293 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1294 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1295 | return false; |
| 1296 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1298 | if (RequireCompleteType(OpLoc, exprType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1299 | isSizeof ? diag::err_sizeof_incomplete_type : |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1300 | PDiag(diag::err_alignof_incomplete_type) |
| 1301 | << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1302 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1304 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | 1dcb322 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1305 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1306 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1307 | << exprType << isSizeof << ExprRange; |
| 1308 | return true; |
Chris Lattner | 37920f5 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1309 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1311 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1314 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1315 | const SourceRange &ExprRange) { |
| 1316 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1317 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1319 | if (isa<DeclRefExpr>(E)) |
| 1320 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1321 | |
| 1322 | // Cannot know anything else if the expression is dependent. |
| 1323 | if (E->isTypeDependent()) |
| 1324 | return false; |
| 1325 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1326 | if (E->getBitField()) { |
| 1327 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1328 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1329 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1330 | |
| 1331 | // Alignment of a field access is always okay, so long as it isn't a |
| 1332 | // bit-field. |
| 1333 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1334 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1335 | return false; |
| 1336 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1337 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1338 | } |
| 1339 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1340 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | Action::OwningExprResult |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1342 | Sema::CreateSizeOfAlignOfExpr(DeclaratorInfo *DInfo, |
| 1343 | SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1344 | bool isSizeOf, SourceRange R) { |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1345 | if (!DInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1346 | return ExprError(); |
| 1347 | |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1348 | QualType T = DInfo->getType(); |
| 1349 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1350 | if (!T->isDependentType() && |
| 1351 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1352 | return ExprError(); |
| 1353 | |
| 1354 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1355 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, DInfo, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1356 | Context.getSizeType(), OpLoc, |
| 1357 | R.getEnd())); |
| 1358 | } |
| 1359 | |
| 1360 | /// \brief Build a sizeof or alignof expression given an expression |
| 1361 | /// operand. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | Action::OwningExprResult |
| 1363 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1364 | bool isSizeOf, SourceRange R) { |
| 1365 | // Verify that the operand is valid. |
| 1366 | bool isInvalid = false; |
| 1367 | if (E->isTypeDependent()) { |
| 1368 | // Delay type-checking for type-dependent expressions. |
| 1369 | } else if (!isSizeOf) { |
| 1370 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1371 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1372 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1373 | isInvalid = true; |
| 1374 | } else { |
| 1375 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1376 | } |
| 1377 | |
| 1378 | if (isInvalid) |
| 1379 | return ExprError(); |
| 1380 | |
| 1381 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1382 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1383 | Context.getSizeType(), OpLoc, |
| 1384 | R.getEnd())); |
| 1385 | } |
| 1386 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1387 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1388 | /// the same for @c alignof and @c __alignof |
| 1389 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1390 | Action::OwningExprResult |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1391 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1392 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 1393 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1394 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1395 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1396 | if (isType) { |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1397 | DeclaratorInfo *DInfo; |
| 1398 | (void) GetTypeFromParser(TyOrEx, &DInfo); |
| 1399 | return CreateSizeOfAlignOfExpr(DInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1401 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1402 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1403 | Action::OwningExprResult Result |
| 1404 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1405 | |
| 1406 | if (Result.isInvalid()) |
| 1407 | DeleteExpr(ArgEx); |
| 1408 | |
| 1409 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1412 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1413 | if (V->isTypeDependent()) |
| 1414 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1416 | // These operators return the element type of a complex type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1417 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1418 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1420 | // Otherwise they pass through real integer and floating point types here. |
| 1421 | if (V->getType()->isArithmeticType()) |
| 1422 | return V->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1424 | // Reject anything else. |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1425 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1426 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1427 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1431 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1432 | Action::OwningExprResult |
| 1433 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1434 | tok::TokenKind Kind, ExprArg Input) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1435 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1436 | Input = MaybeConvertParenListExprToParenExpr(S, move(Input)); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1437 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1439 | UnaryOperator::Opcode Opc; |
| 1440 | switch (Kind) { |
| 1441 | default: assert(0 && "Unknown unary op!"); |
| 1442 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1443 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1444 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1445 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1446 | if (getLangOptions().CPlusPlus && |
| 1447 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1448 | // Which overloaded operator? |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1449 | OverloadedOperatorKind OverOp = |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1450 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1451 | |
| 1452 | // C++ [over.inc]p1: |
| 1453 | // |
| 1454 | // [...] If the function is a member function with one |
| 1455 | // parameter (which shall be of type int) or a non-member |
| 1456 | // function with two parameters (the second of which shall be |
| 1457 | // of type int), it defines the postfix increment operator ++ |
| 1458 | // for objects of that type. When the postfix increment is |
| 1459 | // called as a result of using the ++ operator, the int |
| 1460 | // argument will have value zero. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | Expr *Args[2] = { |
| 1462 | Arg, |
| 1463 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1464 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1465 | }; |
| 1466 | |
| 1467 | // Build the candidate set for overloading |
| 1468 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1469 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1470 | |
| 1471 | // Perform overload resolution. |
| 1472 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1473 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1474 | case OR_Success: { |
| 1475 | // We found a built-in operator or an overloaded operator. |
| 1476 | FunctionDecl *FnDecl = Best->Function; |
| 1477 | |
| 1478 | if (FnDecl) { |
| 1479 | // We matched an overloaded operator. Build a call to that |
| 1480 | // operator. |
| 1481 | |
| 1482 | // Convert the arguments. |
| 1483 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1484 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1485 | return ExprError(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1486 | } else { |
| 1487 | // Convert the arguments. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1488 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1489 | FnDecl->getParamDecl(0)->getType(), |
| 1490 | "passing")) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1491 | return ExprError(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | // Determine the result type |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 1495 | QualType ResultTy = FnDecl->getResultType().getNonReferenceType(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1497 | // Build the actual expression node. |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1498 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 82191d0 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1499 | SourceLocation()); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1500 | UsualUnaryConversions(FnExpr); |
| 1501 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1502 | Input.release(); |
Douglas Gregor | 2517f33 | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1503 | Args[0] = Arg; |
Anders Carlsson | 3d5829c | 2009-10-13 21:49:31 +0000 | [diff] [blame] | 1504 | |
| 1505 | ExprOwningPtr<CXXOperatorCallExpr> |
| 1506 | TheCall(this, new (Context) CXXOperatorCallExpr(Context, OverOp, |
| 1507 | FnExpr, Args, 2, |
| 1508 | ResultTy, OpLoc)); |
| 1509 | |
| 1510 | if (CheckCallReturnType(FnDecl->getResultType(), OpLoc, TheCall.get(), |
| 1511 | FnDecl)) |
| 1512 | return ExprError(); |
Anders Carlsson | 834facc | 2009-10-13 22:22:09 +0000 | [diff] [blame] | 1513 | return Owned(TheCall.release()); |
| 1514 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1515 | } else { |
| 1516 | // We matched a built-in operator. Convert the arguments, then |
| 1517 | // break out so that we will build the appropriate built-in |
| 1518 | // operator node. |
| 1519 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1520 | "passing")) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1521 | return ExprError(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1522 | |
| 1523 | break; |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1524 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Douglas Gregor | 66950a3 | 2009-09-30 21:46:01 +0000 | [diff] [blame] | 1527 | case OR_No_Viable_Function: { |
| 1528 | // No viable function; try checking this as a built-in operator, which |
| 1529 | // will fail and provide a diagnostic. Then, print the overload |
| 1530 | // candidates. |
| 1531 | OwningExprResult Result = CreateBuiltinUnaryOp(OpLoc, Opc, move(Input)); |
| 1532 | assert(Result.isInvalid() && |
| 1533 | "C++ postfix-unary operator overloading is missing candidates!"); |
| 1534 | if (Result.isInvalid()) |
| 1535 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/false); |
| 1536 | |
| 1537 | return move(Result); |
| 1538 | } |
| 1539 | |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1540 | case OR_Ambiguous: |
| 1541 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1542 | << UnaryOperator::getOpcodeStr(Opc) |
| 1543 | << Arg->getSourceRange(); |
| 1544 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1545 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1546 | |
| 1547 | case OR_Deleted: |
| 1548 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1549 | << Best->Function->isDeleted() |
| 1550 | << UnaryOperator::getOpcodeStr(Opc) |
| 1551 | << Arg->getSourceRange(); |
| 1552 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1553 | return ExprError(); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | // Either we found no viable overloaded operator or we matched a |
| 1557 | // built-in operator. In either case, fall through to trying to |
| 1558 | // build a built-in operation. |
| 1559 | } |
| 1560 | |
Eli Friedman | f32f0a7 | 2009-07-22 23:24:42 +0000 | [diff] [blame] | 1561 | Input.release(); |
| 1562 | Input = Arg; |
Eli Friedman | 6aea575 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 1563 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(Input)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1566 | Action::OwningExprResult |
| 1567 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1568 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1569 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1570 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1571 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1572 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1573 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1575 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1576 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1577 | Base.release(); |
| 1578 | Idx.release(); |
| 1579 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1580 | Context.DependentTy, RLoc)); |
| 1581 | } |
| 1582 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1583 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1584 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1585 | LHSExp->getType()->isEnumeralType() || |
| 1586 | RHSExp->getType()->isRecordType() || |
| 1587 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1588 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1591 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 1592 | } |
| 1593 | |
| 1594 | |
| 1595 | Action::OwningExprResult |
| 1596 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 1597 | ExprArg Idx, SourceLocation RLoc) { |
| 1598 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 1599 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 1600 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1601 | // Perform default conversions. |
| 1602 | DefaultFunctionArrayConversion(LHSExp); |
| 1603 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1604 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1605 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1606 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 1607 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1608 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1609 | // in the subscript position. As a result, we need to derive the array base |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1610 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1611 | Expr *BaseExpr, *IndexExpr; |
| 1612 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1613 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1614 | BaseExpr = LHSExp; |
| 1615 | IndexExpr = RHSExp; |
| 1616 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1617 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1618 | BaseExpr = LHSExp; |
| 1619 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1620 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1621 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1622 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1623 | BaseExpr = RHSExp; |
| 1624 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1625 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1627 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1628 | BaseExpr = LHSExp; |
| 1629 | IndexExpr = RHSExp; |
| 1630 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1632 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1633 | // Handle the uncommon case of "123[Ptr]". |
| 1634 | BaseExpr = RHSExp; |
| 1635 | IndexExpr = LHSExp; |
| 1636 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1637 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1638 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1639 | IndexExpr = RHSExp; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1640 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1641 | // FIXME: need to deal with const... |
| 1642 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1643 | } else if (LHSTy->isArrayType()) { |
| 1644 | // If we see an array that wasn't promoted by |
| 1645 | // DefaultFunctionArrayConversion, it must be an array that |
| 1646 | // wasn't promoted because of the C90 rule that doesn't |
| 1647 | // allow promoting non-lvalue arrays. Warn, then |
| 1648 | // force the promotion here. |
| 1649 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1650 | LHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1651 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 1652 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1653 | LHSTy = LHSExp->getType(); |
| 1654 | |
| 1655 | BaseExpr = LHSExp; |
| 1656 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1657 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1658 | } else if (RHSTy->isArrayType()) { |
| 1659 | // Same as previous, except for 123[f().a] case |
| 1660 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1661 | RHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1662 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 1663 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1664 | RHSTy = RHSExp->getType(); |
| 1665 | |
| 1666 | BaseExpr = RHSExp; |
| 1667 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1668 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 1669 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1670 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1671 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1672 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 1673 | // C99 6.5.2.1p1 |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1674 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1675 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1676 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1677 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 1678 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1679 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 1680 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 1681 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 1682 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 1683 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1684 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1686 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1687 | // incomplete types are not object types. |
| 1688 | if (ResultType->isFunctionType()) { |
| 1689 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1690 | << ResultType << BaseExpr->getSourceRange(); |
| 1691 | return ExprError(); |
| 1692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1694 | if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1696 | PDiag(diag::err_subscript_incomplete_type) |
| 1697 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1698 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1700 | // Diagnose bad cases where we step over interface counts. |
| 1701 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1702 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1703 | << ResultType << BaseExpr->getSourceRange(); |
| 1704 | return ExprError(); |
| 1705 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1707 | Base.release(); |
| 1708 | Idx.release(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1709 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1710 | ResultType, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1713 | QualType Sema:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1714 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1716 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 1717 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 1718 | // see FIXME there. |
| 1719 | // |
| 1720 | // FIXME: This logic can be greatly simplified by splitting it along |
| 1721 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1722 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1723 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1724 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1725 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1726 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1727 | // This flag determines whether or not the component is one of the four |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1728 | // special names that indicate a subset of exactly half the elements are |
| 1729 | // to be selected. |
| 1730 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1731 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1732 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1733 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 0359e12 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 1734 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1735 | |
| 1736 | // Check that we've found one of the special components, or that the component |
| 1737 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1738 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1739 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1740 | HalvingSwizzle = true; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1741 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1742 | do |
| 1743 | compStr++; |
| 1744 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1745 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1746 | do |
| 1747 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1748 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1749 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1750 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1751 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1752 | // We didn't get to the end of the string. This means the component names |
| 1753 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1754 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1755 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1756 | return QualType(); |
| 1757 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1758 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1759 | // Ensure no component accessor exceeds the width of the vector type it |
| 1760 | // operates on. |
| 1761 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1762 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1763 | |
| 1764 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1765 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1766 | |
| 1767 | while (*compStr) { |
| 1768 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1769 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1770 | << baseType << SourceRange(CompLoc); |
| 1771 | return QualType(); |
| 1772 | } |
| 1773 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1774 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1775 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1776 | // If this is a halving swizzle, verify that the base type has an even |
| 1777 | // number of elements. |
| 1778 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1779 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1780 | << baseType << SourceRange(CompLoc); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1781 | return QualType(); |
| 1782 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1783 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1784 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1785 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1786 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1787 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1788 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1789 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1790 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1791 | if (HexSwizzle) |
| 1792 | CompSize--; |
| 1793 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1794 | if (CompSize == 1) |
| 1795 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1796 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1797 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1798 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1799 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1800 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1801 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1802 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1803 | } |
| 1804 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1807 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1808 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1809 | const Selector &Sel, |
| 1810 | ASTContext &Context) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1812 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1813 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1814 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1815 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1816 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1817 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1818 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1820 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1821 | return D; |
| 1822 | } |
| 1823 | return 0; |
| 1824 | } |
| 1825 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1826 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1827 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1828 | const Selector &Sel, |
| 1829 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1830 | // Check protocols on qualified interfaces. |
| 1831 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1832 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1833 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1834 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1835 | GDecl = PD; |
| 1836 | break; |
| 1837 | } |
| 1838 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1839 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1840 | GDecl = OMD; |
| 1841 | break; |
| 1842 | } |
| 1843 | } |
| 1844 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1845 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1846 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1847 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1848 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1849 | if (GDecl) |
| 1850 | return GDecl; |
| 1851 | } |
| 1852 | } |
| 1853 | return GDecl; |
| 1854 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1855 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | Action::OwningExprResult |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1857 | Sema::BuildMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1858 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | DeclarationName MemberName, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 1860 | bool HasExplicitTemplateArgs, |
| 1861 | SourceLocation LAngleLoc, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1862 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 1863 | unsigned NumExplicitTemplateArgs, |
| 1864 | SourceLocation RAngleLoc, |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1865 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS, |
| 1866 | NamedDecl *FirstQualifierInScope) { |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1867 | if (SS && SS->isInvalid()) |
| 1868 | return ExprError(); |
| 1869 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1870 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1871 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1872 | |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1873 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1874 | assert(BaseExpr && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1876 | // Perform default conversions. |
| 1877 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1878 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1879 | QualType BaseType = BaseExpr->getType(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 1880 | |
| 1881 | // If the user is trying to apply -> or . to a function pointer |
| 1882 | // type, it's probably because the forgot parentheses to call that |
| 1883 | // function. Suggest the addition of those parentheses, build the |
| 1884 | // call, and continue on. |
| 1885 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 1886 | if (const FunctionProtoType *Fun |
| 1887 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 1888 | QualType ResultTy = Fun->getResultType(); |
| 1889 | if (Fun->getNumArgs() == 0 && |
| 1890 | ((OpKind == tok::period && ResultTy->isRecordType()) || |
| 1891 | (OpKind == tok::arrow && ResultTy->isPointerType() && |
| 1892 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 1893 | ->isRecordType()))) { |
| 1894 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 1895 | Diag(Loc, diag::err_member_reference_needs_call) |
| 1896 | << QualType(Fun, 0) |
| 1897 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 1898 | |
| 1899 | OwningExprResult NewBase |
| 1900 | = ActOnCallExpr(S, ExprArg(*this, BaseExpr), Loc, |
| 1901 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 1902 | if (NewBase.isInvalid()) |
| 1903 | return move(NewBase); |
| 1904 | |
| 1905 | BaseExpr = NewBase.takeAs<Expr>(); |
| 1906 | DefaultFunctionArrayConversion(BaseExpr); |
| 1907 | BaseType = BaseExpr->getType(); |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1912 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 1913 | // use that. |
| 1914 | if (BaseType->isObjCIdType()) { |
| 1915 | // We have an 'id' type. Rather than fall through, we check if this |
| 1916 | // is a reference to 'isa'. |
| 1917 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 1918 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1919 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1920 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1921 | } |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1922 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1923 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1924 | // Handle properties on ObjC 'Class' types. |
| 1925 | if (OpKind == tok::period && BaseType->isObjCClassType()) { |
| 1926 | // Also must look for a getter name which uses property syntax. |
| 1927 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1928 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1929 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 1930 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 1931 | ObjCMethodDecl *Getter; |
| 1932 | // FIXME: need to also look locally in the implementation. |
| 1933 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 1934 | // Check the use of this method. |
| 1935 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1936 | return ExprError(); |
| 1937 | } |
| 1938 | // If we found a getter then this may be a valid dot-reference, we |
| 1939 | // will look for the matching setter, in case it is needed. |
| 1940 | Selector SetterSel = |
| 1941 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 1942 | PP.getSelectorTable(), Member); |
| 1943 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 1944 | if (!Setter) { |
| 1945 | // If this reference is in an @implementation, also check for 'private' |
| 1946 | // methods. |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 1947 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1948 | } |
| 1949 | // Look through local category implementations associated with the class. |
| 1950 | if (!Setter) |
| 1951 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 1952 | |
| 1953 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1954 | return ExprError(); |
| 1955 | |
| 1956 | if (Getter || Setter) { |
| 1957 | QualType PType; |
| 1958 | |
| 1959 | if (Getter) |
| 1960 | PType = Getter->getResultType(); |
| 1961 | else |
| 1962 | // Get the expression type from Setter's incoming parameter. |
| 1963 | PType = (*(Setter->param_end() -1))->getType(); |
| 1964 | // FIXME: we must check that the setter has property type. |
| 1965 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 1966 | PType, |
| 1967 | Setter, MemberLoc, BaseExpr)); |
| 1968 | } |
| 1969 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1970 | << MemberName << BaseType); |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | if (BaseType->isObjCClassType() && |
| 1975 | BaseType != Context.ObjCClassRedefinitionType) { |
| 1976 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1977 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Chris Lattner | 4befd73 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1980 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1981 | // must have pointer type, and the accessed type is the pointee. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1982 | if (OpKind == tok::arrow) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1983 | if (BaseType->isDependentType()) { |
| 1984 | NestedNameSpecifier *Qualifier = 0; |
| 1985 | if (SS) { |
| 1986 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 1987 | if (!FirstQualifierInScope) |
| 1988 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 1989 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
| 1991 | return Owned(CXXUnresolvedMemberExpr::Create(Context, BaseExpr, true, |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1992 | OpLoc, Qualifier, |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 1993 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1994 | FirstQualifierInScope, |
| 1995 | MemberName, |
| 1996 | MemberLoc, |
| 1997 | HasExplicitTemplateArgs, |
| 1998 | LAngleLoc, |
| 1999 | ExplicitTemplateArgs, |
| 2000 | NumExplicitTemplateArgs, |
| 2001 | RAngleLoc)); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2002 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2003 | else if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2004 | BaseType = PT->getPointeeType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2005 | else if (BaseType->isObjCObjectPointerType()) |
| 2006 | ; |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2007 | else |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2008 | return ExprError(Diag(MemberLoc, |
| 2009 | diag::err_typecheck_member_reference_arrow) |
| 2010 | << BaseType << BaseExpr->getSourceRange()); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 2011 | } else if (BaseType->isDependentType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2012 | // Require that the base type isn't a pointer type |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2013 | // (so we'll report an error for) |
| 2014 | // T* t; |
| 2015 | // t.f; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2016 | // |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2017 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2018 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2019 | // allows this, while still reporting an error if T is a struct pointer. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2020 | const PointerType *PT = BaseType->getAs<PointerType>(); |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2021 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | if (!PT || (getLangOptions().ObjC1 && |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2023 | !PT->getPointeeType()->isRecordType())) { |
| 2024 | NestedNameSpecifier *Qualifier = 0; |
| 2025 | if (SS) { |
| 2026 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 2027 | if (!FirstQualifierInScope) |
| 2028 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 2029 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2031 | return Owned(CXXUnresolvedMemberExpr::Create(Context, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | BaseExpr, false, |
| 2033 | OpLoc, |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2034 | Qualifier, |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2035 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2036 | FirstQualifierInScope, |
| 2037 | MemberName, |
| 2038 | MemberLoc, |
| 2039 | HasExplicitTemplateArgs, |
| 2040 | LAngleLoc, |
| 2041 | ExplicitTemplateArgs, |
| 2042 | NumExplicitTemplateArgs, |
| 2043 | RAngleLoc)); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2044 | } |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2045 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2046 | |
Chris Lattner | 4befd73 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2047 | // Handle field access to simple records. This also handles access to fields |
| 2048 | // of the ObjC 'id' struct. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2049 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2050 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2051 | if (RequireCompleteType(OpLoc, BaseType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2052 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2053 | << BaseExpr->getSourceRange())) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2054 | return ExprError(); |
| 2055 | |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2056 | DeclContext *DC = RDecl; |
| 2057 | if (SS && SS->isSet()) { |
| 2058 | // If the member name was a qualified-id, look into the |
| 2059 | // nested-name-specifier. |
| 2060 | DC = computeDeclContext(*SS, false); |
Douglas Gregor | 0b3d95a | 2009-10-17 22:37:54 +0000 | [diff] [blame] | 2061 | |
| 2062 | if (!isa<TypeDecl>(DC)) { |
| 2063 | Diag(MemberLoc, diag::err_qualified_member_nonclass) |
| 2064 | << DC << SS->getRange(); |
| 2065 | return ExprError(); |
| 2066 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
| 2068 | // FIXME: If DC is not computable, we should build a |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2069 | // CXXUnresolvedMemberExpr. |
| 2070 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2071 | } |
| 2072 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2073 | // The record definition is complete, now make sure the member is valid. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 2074 | LookupResult Result(*this, MemberName, MemberLoc, LookupMemberName); |
| 2075 | LookupQualifiedName(Result, DC); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2076 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2077 | if (Result.empty()) |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 2078 | return ExprError(Diag(MemberLoc, diag::err_no_member) |
| 2079 | << MemberName << DC << BaseExpr->getSourceRange()); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 2080 | if (Result.isAmbiguous()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2081 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2083 | NamedDecl *MemberDecl = Result.getAsSingleDecl(Context); |
| 2084 | |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2085 | if (SS && SS->isSet()) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2086 | TypeDecl* TyD = cast<TypeDecl>(MemberDecl->getDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | QualType BaseTypeCanon |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2088 | = Context.getCanonicalType(BaseType).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2089 | QualType MemberTypeCanon |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2090 | = Context.getCanonicalType(Context.getTypeDeclType(TyD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2091 | |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2092 | if (BaseTypeCanon != MemberTypeCanon && |
| 2093 | !IsDerivedFrom(BaseTypeCanon, MemberTypeCanon)) |
| 2094 | return ExprError(Diag(SS->getBeginLoc(), |
| 2095 | diag::err_not_direct_base_or_virtual) |
| 2096 | << MemberTypeCanon << BaseTypeCanon); |
| 2097 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
Chris Lattner | 303284a | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2099 | // If the decl being referenced had an error, return an error for this |
| 2100 | // sub-expr without emitting another error, in order to avoid cascading |
| 2101 | // error cases. |
| 2102 | if (MemberDecl->isInvalidDecl()) |
| 2103 | return ExprError(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2104 | |
Anders Carlsson | 04e1e22 | 2009-09-10 20:48:14 +0000 | [diff] [blame] | 2105 | bool ShouldCheckUse = true; |
| 2106 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2107 | // Don't diagnose the use of a virtual member function unless it's |
| 2108 | // explicitly qualified. |
| 2109 | if (MD->isVirtual() && (!SS || !SS->isSet())) |
| 2110 | ShouldCheckUse = false; |
| 2111 | } |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2113 | // Check the use of this field |
Anders Carlsson | 04e1e22 | 2009-09-10 20:48:14 +0000 | [diff] [blame] | 2114 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2115 | return ExprError(); |
Chris Lattner | 303284a | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2117 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2118 | // We may have found a field within an anonymous union or struct |
| 2119 | // (C++ [class.union]). |
| 2120 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2121 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2122 | BaseExpr, OpLoc); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2124 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2125 | QualType MemberType = FD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2126 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2127 | MemberType = Ref->getPointeeType(); |
| 2128 | else { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2129 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2130 | BaseQuals.removeObjCGCAttr(); |
| 2131 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2132 | |
| 2133 | Qualifiers MemberQuals |
| 2134 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2135 | |
| 2136 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2137 | if (Combined != MemberQuals) |
| 2138 | MemberType = Context.getQualifiedType(MemberType, Combined); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2139 | } |
Eli Friedman | 1242fff | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2141 | MarkDeclarationReferenced(MemberLoc, FD); |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2142 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2143 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2145 | FD, MemberLoc, MemberType)); |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2146 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2148 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2149 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2150 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2151 | Var, MemberLoc, |
| 2152 | Var->getType().getNonReferenceType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2153 | } |
| 2154 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2155 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2156 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2157 | MemberFn, MemberLoc, |
| 2158 | MemberFn->getType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2159 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2161 | = dyn_cast<FunctionTemplateDecl>(MemberDecl)) { |
| 2162 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2164 | if (HasExplicitTemplateArgs) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2166 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2167 | SS? SS->getRange() : SourceRange(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | FunTmpl, MemberLoc, true, |
| 2169 | LAngleLoc, ExplicitTemplateArgs, |
| 2170 | NumExplicitTemplateArgs, RAngleLoc, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2171 | Context.OverloadTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2173 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2174 | FunTmpl, MemberLoc, |
| 2175 | Context.OverloadTy)); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2176 | } |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2177 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2178 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) { |
| 2179 | if (HasExplicitTemplateArgs) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2181 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2182 | SS? SS->getRange() : SourceRange(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | Ovl, MemberLoc, true, |
| 2184 | LAngleLoc, ExplicitTemplateArgs, |
| 2185 | NumExplicitTemplateArgs, RAngleLoc, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2186 | Context.OverloadTy)); |
| 2187 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2188 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2189 | Ovl, MemberLoc, Context.OverloadTy)); |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2190 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2191 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2192 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2193 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2194 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2195 | } |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2196 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2197 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2198 | << MemberName << int(OpKind == tok::arrow)); |
Eli Friedman | 1242fff | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2200 | // We found a declaration kind that we didn't expect. This is a |
| 2201 | // generic error message that tells the user that she can't refer |
| 2202 | // to this member with '.' or '->'. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2203 | return ExprError(Diag(MemberLoc, |
| 2204 | diag::err_typecheck_member_reference_unknown) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2205 | << MemberName << int(OpKind == tok::arrow)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2206 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2208 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2209 | // into a record type was handled above, any destructor we see here is a |
| 2210 | // pseudo-destructor. |
| 2211 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2212 | // C++ [expr.pseudo]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | // The left hand side of the dot operator shall be of scalar type. The |
| 2214 | // left hand side of the arrow operator shall be of pointer to scalar |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2215 | // type. |
| 2216 | if (!BaseType->isScalarType()) |
| 2217 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2218 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2220 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2221 | // same as the object type. |
| 2222 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2223 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2224 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2225 | << BaseType << MemberName.getCXXNameType() |
| 2226 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2227 | |
| 2228 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2229 | // the form |
| 2230 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2232 | // |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2233 | // shall designate the same scalar type. |
| 2234 | // |
| 2235 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2236 | // isn't checked here. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2238 | // FIXME: We've lost the precise spelling of the type by going through |
| 2239 | // DeclarationName. Can we do better? |
| 2240 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2241 | OpKind == tok::arrow, |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2242 | OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2244 | SS? SS->getRange() : SourceRange(), |
| 2245 | MemberName.getCXXNameType(), |
| 2246 | MemberLoc)); |
| 2247 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2249 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2250 | // (*Obj).ivar. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2251 | if ((OpKind == tok::arrow && BaseType->isObjCObjectPointerType()) || |
| 2252 | (OpKind == tok::period && BaseType->isObjCInterfaceType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2253 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | const ObjCInterfaceType *IFaceT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2255 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2256 | if (IFaceT) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2257 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2258 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2259 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2260 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2261 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2262 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2263 | if (IV) { |
| 2264 | // If the decl being referenced had an error, return an error for this |
| 2265 | // sub-expr without emitting another error, in order to avoid cascading |
| 2266 | // error cases. |
| 2267 | if (IV->isInvalidDecl()) |
| 2268 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2269 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2270 | // Check whether we can reference this field. |
| 2271 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2272 | return ExprError(); |
| 2273 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2274 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2275 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2276 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2277 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2278 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2279 | // Case of a c-function declared inside an objc implementation. |
| 2280 | // FIXME: For a c-style function nested inside an objc implementation |
| 2281 | // class, there is no implementation context available, so we pass |
| 2282 | // down the context as argument to this routine. Ideally, this context |
| 2283 | // need be passed down in the AST node and somehow calculated from the |
| 2284 | // AST for a function decl. |
| 2285 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2287 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2288 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2289 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2290 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2291 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2292 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2293 | |
| 2294 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2295 | if (ClassDeclared != IDecl || |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2296 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2297 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2298 | << IV->getDeclName(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2299 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2300 | // @protected |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2301 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2302 | << IV->getDeclName(); |
Steve Naroff | d1b64be | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2303 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2304 | |
| 2305 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2306 | MemberLoc, BaseExpr, |
| 2307 | OpKind == tok::arrow)); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2308 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2309 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2310 | << IDecl->getDeclName() << MemberName |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2311 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | b1378f9 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2312 | } |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2313 | } |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2314 | // Handle properties on 'id' and qualified "id". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | if (OpKind == tok::period && (BaseType->isObjCIdType() || |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2316 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2317 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2318 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2319 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2320 | // Check protocols on qualified interfaces. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2321 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2322 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2323 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2324 | // Check the use of this declaration |
| 2325 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2326 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2328 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2329 | MemberLoc, BaseExpr)); |
| 2330 | } |
| 2331 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2332 | // Check the use of this method. |
| 2333 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2334 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2336 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2337 | OMD->getResultType(), |
| 2338 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2339 | NULL, 0)); |
| 2340 | } |
| 2341 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2342 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2343 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2344 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2345 | } |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2346 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2347 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2348 | const ObjCObjectPointerType *OPT; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2349 | if (OpKind == tok::period && |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2350 | (OPT = BaseType->getAsObjCInterfacePointerType())) { |
| 2351 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2352 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2353 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2355 | // Search for a declared property first. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2356 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2357 | // Check whether we can reference this property. |
| 2358 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2359 | return ExprError(); |
Fariborz Jahanian | b2ab73d | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2360 | QualType ResTy = PD->getType(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2361 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2362 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | fe9e394 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2363 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2364 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | b2ab73d | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2365 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2366 | MemberLoc, BaseExpr)); |
| 2367 | } |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2368 | // Check protocols on qualified interfaces. |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2369 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2370 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2371 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2372 | // Check whether we can reference this property. |
| 2373 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2374 | return ExprError(); |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2375 | |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2376 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2377 | MemberLoc, BaseExpr)); |
| 2378 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2379 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2380 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2381 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2382 | // Check whether we can reference this property. |
| 2383 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2384 | return ExprError(); |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2385 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2386 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2387 | MemberLoc, BaseExpr)); |
| 2388 | } |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2389 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2390 | // selector is implemented. |
| 2391 | |
| 2392 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2393 | // shared with the code in ActOnInstanceMessage. |
| 2394 | |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2395 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2396 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2397 | |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2398 | // If this reference is in an @implementation, check for 'private' methods. |
| 2399 | if (!Getter) |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2400 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2401 | |
Steve Naroff | 1df6269 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2402 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2403 | if (!Getter) |
| 2404 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2405 | if (Getter) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2406 | // Check if we can reference this property. |
| 2407 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2408 | return ExprError(); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2409 | } |
| 2410 | // If we found a getter then this may be a valid dot-reference, we |
| 2411 | // will look for the matching setter, in case it is needed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2412 | Selector SetterSel = |
| 2413 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2414 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2415 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2416 | if (!Setter) { |
| 2417 | // If this reference is in an @implementation, also check for 'private' |
| 2418 | // methods. |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2419 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2420 | } |
| 2421 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2422 | if (!Setter) |
| 2423 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2424 | |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2425 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2426 | return ExprError(); |
| 2427 | |
| 2428 | if (Getter || Setter) { |
| 2429 | QualType PType; |
| 2430 | |
| 2431 | if (Getter) |
| 2432 | PType = Getter->getResultType(); |
Fariborz Jahanian | 88cc234 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2433 | else |
| 2434 | // Get the expression type from Setter's incoming parameter. |
| 2435 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2436 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 9a84665 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2437 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2438 | Setter, MemberLoc, BaseExpr)); |
| 2439 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2440 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2441 | << MemberName << BaseType); |
Fariborz Jahanian | 21f54ee | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2442 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2444 | // Handle the following exceptional case (*Obj).isa. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2445 | if (OpKind == tok::period && |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2446 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2447 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2448 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
| 2449 | Context.getObjCIdType())); |
| 2450 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2451 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2452 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2453 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2454 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2455 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2456 | return ExprError(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2457 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2458 | MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2459 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2460 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2461 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2462 | << BaseType << BaseExpr->getSourceRange(); |
| 2463 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2464 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2467 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg Base, |
| 2468 | SourceLocation OpLoc, |
| 2469 | tok::TokenKind OpKind, |
| 2470 | const CXXScopeSpec &SS, |
| 2471 | UnqualifiedId &Member, |
| 2472 | DeclPtrTy ObjCImpDecl, |
| 2473 | bool HasTrailingLParen) { |
| 2474 | if (Member.getKind() == UnqualifiedId::IK_TemplateId) { |
| 2475 | TemplateName Template |
| 2476 | = TemplateName::getFromVoidPointer(Member.TemplateId->Template); |
| 2477 | |
| 2478 | // FIXME: We're going to end up looking up the template based on its name, |
| 2479 | // twice! |
| 2480 | DeclarationName Name; |
| 2481 | if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl()) |
| 2482 | Name = ActualTemplate->getDeclName(); |
| 2483 | else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl()) |
| 2484 | Name = Ovl->getDeclName(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2485 | else { |
| 2486 | DependentTemplateName *DTN = Template.getAsDependentTemplateName(); |
| 2487 | if (DTN->isIdentifier()) |
| 2488 | Name = DTN->getIdentifier(); |
| 2489 | else |
| 2490 | Name = Context.DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2491 | } |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Translate the parser's template argument list in our AST format. |
| 2494 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 2495 | Member.TemplateId->getTemplateArgs(), |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2496 | Member.TemplateId->NumArgs); |
| 2497 | |
| 2498 | llvm::SmallVector<TemplateArgumentLoc, 16> TemplateArgs; |
| 2499 | translateTemplateArguments(TemplateArgsPtr, |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2500 | TemplateArgs); |
| 2501 | TemplateArgsPtr.release(); |
| 2502 | |
| 2503 | // Do we have the save the actual template name? We might need it... |
| 2504 | return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, |
| 2505 | Member.TemplateId->TemplateNameLoc, |
| 2506 | Name, true, Member.TemplateId->LAngleLoc, |
| 2507 | TemplateArgs.data(), TemplateArgs.size(), |
| 2508 | Member.TemplateId->RAngleLoc, DeclPtrTy(), |
| 2509 | &SS); |
| 2510 | } |
| 2511 | |
| 2512 | // FIXME: We lose a lot of source information by mapping directly to the |
| 2513 | // DeclarationName. |
| 2514 | OwningExprResult Result |
| 2515 | = BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, |
| 2516 | Member.getSourceRange().getBegin(), |
| 2517 | GetNameFromUnqualifiedId(Member), |
| 2518 | ObjCImpDecl, &SS); |
| 2519 | |
| 2520 | if (Result.isInvalid() || HasTrailingLParen || |
| 2521 | Member.getKind() != UnqualifiedId::IK_DestructorName) |
| 2522 | return move(Result); |
| 2523 | |
| 2524 | // The only way a reference to a destructor can be used is to |
| 2525 | // immediately call them. Since the next token is not a '(', produce a |
| 2526 | // diagnostic and build the call now. |
| 2527 | Expr *E = (Expr *)Result.get(); |
| 2528 | SourceLocation ExpectedLParenLoc |
| 2529 | = PP.getLocForEndOfToken(Member.getSourceRange().getEnd()); |
| 2530 | Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
| 2531 | << isa<CXXPseudoDestructorExpr>(E) |
| 2532 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 2533 | |
| 2534 | return ActOnCallExpr(0, move(Result), ExpectedLParenLoc, |
| 2535 | MultiExprArg(*this, 0, 0), 0, ExpectedLParenLoc); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2538 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 2539 | FunctionDecl *FD, |
| 2540 | ParmVarDecl *Param) { |
| 2541 | if (Param->hasUnparsedDefaultArg()) { |
| 2542 | Diag (CallLoc, |
| 2543 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2544 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2546 | diag::note_default_argument_declared_here); |
| 2547 | } else { |
| 2548 | if (Param->hasUninstantiatedDefaultArg()) { |
| 2549 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 2550 | |
| 2551 | // Instantiate the expression. |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2552 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 2553 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 2555 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2556 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2557 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2558 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | if (Result.isInvalid()) |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2560 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | |
| 2562 | if (SetParamDefaultArgument(Param, move(Result), |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2563 | /*FIXME:EqualLoc*/ |
| 2564 | UninstExpr->getSourceRange().getBegin())) |
| 2565 | return ExprError(); |
| 2566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2568 | Expr *DefaultExpr = Param->getDefaultArg(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2570 | // If the default expression creates temporaries, we need to |
| 2571 | // push them to the current stack of expression temporaries so they'll |
| 2572 | // be properly destroyed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | if (CXXExprWithTemporaries *E |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2574 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2575 | assert(!E->shouldDestroyTemporaries() && |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2576 | "Can't destroy temporaries in a default argument expr!"); |
| 2577 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2578 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | // We already type-checked the argument, so we know it works. |
| 2583 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 2584 | } |
| 2585 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2586 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2587 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2588 | /// function prototype Proto. Call is the call expression itself, and |
| 2589 | /// Fn is the function expression. For a C++ member function, this |
| 2590 | /// routine does not attempt to convert the object argument. Returns |
| 2591 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2592 | bool |
| 2593 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2594 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2595 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2596 | Expr **Args, unsigned NumArgs, |
| 2597 | SourceLocation RParenLoc) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2598 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2599 | // assignment, to the types of the corresponding parameter, ... |
| 2600 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2601 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2602 | bool Invalid = false; |
| 2603 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2604 | // If too few arguments are available (and we don't have default |
| 2605 | // arguments for the remaining parameters), don't make the call. |
| 2606 | if (NumArgs < NumArgsInProto) { |
| 2607 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2608 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2609 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2610 | // Use default arguments for missing arguments |
| 2611 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2612 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
| 2615 | // If too many are passed and not variadic, error on the extras and drop |
| 2616 | // them. |
| 2617 | if (NumArgs > NumArgsInProto) { |
| 2618 | if (!Proto->isVariadic()) { |
| 2619 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2620 | diag::err_typecheck_call_too_many_args) |
| 2621 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2622 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2623 | Args[NumArgs-1]->getLocEnd()); |
| 2624 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2625 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2626 | Invalid = true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2627 | } |
| 2628 | NumArgsToCheck = NumArgsInProto; |
| 2629 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2630 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2631 | // Continue to check argument types (even if we have too few/many args). |
| 2632 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2633 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2635 | Expr *Arg; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2636 | if (i < NumArgs) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2637 | Arg = Args[i]; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2638 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2639 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2640 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2641 | PDiag(diag::err_call_incomplete_argument) |
| 2642 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2643 | return true; |
| 2644 | |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2645 | // Pass the argument. |
| 2646 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2647 | return true; |
Anders Carlsson | 78cfaa9 | 2009-11-13 04:34:45 +0000 | [diff] [blame] | 2648 | |
Anders Carlsson | 97df0b4 | 2009-11-13 17:04:35 +0000 | [diff] [blame] | 2649 | if (!ProtoArgType->isReferenceType()) |
| 2650 | Arg = MaybeBindToTemporary(Arg).takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2651 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 2652 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
| 2654 | OwningExprResult ArgExpr = |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2655 | BuildCXXDefaultArgExpr(Call->getSourceRange().getBegin(), |
| 2656 | FDecl, Param); |
| 2657 | if (ArgExpr.isInvalid()) |
| 2658 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2660 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2663 | Call->setArg(i, Arg); |
| 2664 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2665 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2666 | // If this is a variadic call, handle args passed through "...". |
| 2667 | if (Proto->isVariadic()) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2668 | VariadicCallType CallType = VariadicFunction; |
| 2669 | if (Fn->getType()->isBlockPointerType()) |
| 2670 | CallType = VariadicBlock; // Block |
| 2671 | else if (isa<MemberExpr>(Fn)) |
| 2672 | CallType = VariadicMethod; |
| 2673 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2674 | // Promote the arguments (C99 6.5.2.2p7). |
Eli Friedman | a9ea959 | 2009-11-14 04:43:10 +0000 | [diff] [blame] | 2675 | for (unsigned i = NumArgsInProto; i < NumArgs; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2676 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2677 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2678 | Call->setArg(i, Arg); |
| 2679 | } |
| 2680 | } |
| 2681 | |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2682 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2685 | /// \brief "Deconstruct" the function argument of a call expression to find |
| 2686 | /// the underlying declaration (if any), the name of the called function, |
| 2687 | /// whether argument-dependent lookup is available, whether it has explicit |
| 2688 | /// template arguments, etc. |
| 2689 | void Sema::DeconstructCallFunction(Expr *FnExpr, |
| 2690 | NamedDecl *&Function, |
| 2691 | DeclarationName &Name, |
| 2692 | NestedNameSpecifier *&Qualifier, |
| 2693 | SourceRange &QualifierRange, |
| 2694 | bool &ArgumentDependentLookup, |
| 2695 | bool &HasExplicitTemplateArguments, |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2696 | const TemplateArgumentLoc *&ExplicitTemplateArgs, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2697 | unsigned &NumExplicitTemplateArgs) { |
| 2698 | // Set defaults for all of the output parameters. |
| 2699 | Function = 0; |
| 2700 | Name = DeclarationName(); |
| 2701 | Qualifier = 0; |
| 2702 | QualifierRange = SourceRange(); |
| 2703 | ArgumentDependentLookup = getLangOptions().CPlusPlus; |
| 2704 | HasExplicitTemplateArguments = false; |
| 2705 | |
| 2706 | // If we're directly calling a function, get the appropriate declaration. |
| 2707 | // Also, in C++, keep track of whether we should perform argument-dependent |
| 2708 | // lookup and whether there were any explicitly-specified template arguments. |
| 2709 | while (true) { |
| 2710 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2711 | FnExpr = IcExpr->getSubExpr(); |
| 2712 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
| 2713 | // Parentheses around a function disable ADL |
| 2714 | // (C++0x [basic.lookup.argdep]p1). |
| 2715 | ArgumentDependentLookup = false; |
| 2716 | FnExpr = PExpr->getSubExpr(); |
| 2717 | } else if (isa<UnaryOperator>(FnExpr) && |
| 2718 | cast<UnaryOperator>(FnExpr)->getOpcode() |
| 2719 | == UnaryOperator::AddrOf) { |
| 2720 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2721 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
| 2722 | Function = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2723 | if ((Qualifier = DRExpr->getQualifier())) { |
| 2724 | ArgumentDependentLookup = false; |
| 2725 | QualifierRange = DRExpr->getQualifierRange(); |
| 2726 | } |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2727 | break; |
| 2728 | } else if (UnresolvedFunctionNameExpr *DepName |
| 2729 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2730 | Name = DepName->getName(); |
| 2731 | break; |
| 2732 | } else if (TemplateIdRefExpr *TemplateIdRef |
| 2733 | = dyn_cast<TemplateIdRefExpr>(FnExpr)) { |
| 2734 | Function = TemplateIdRef->getTemplateName().getAsTemplateDecl(); |
| 2735 | if (!Function) |
| 2736 | Function = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl(); |
| 2737 | HasExplicitTemplateArguments = true; |
| 2738 | ExplicitTemplateArgs = TemplateIdRef->getTemplateArgs(); |
| 2739 | NumExplicitTemplateArgs = TemplateIdRef->getNumTemplateArgs(); |
| 2740 | |
| 2741 | // C++ [temp.arg.explicit]p6: |
| 2742 | // [Note: For simple function names, argument dependent lookup (3.4.2) |
| 2743 | // applies even when the function name is not visible within the |
| 2744 | // scope of the call. This is because the call still has the syntactic |
| 2745 | // form of a function call (3.4.1). But when a function template with |
| 2746 | // explicit template arguments is used, the call does not have the |
| 2747 | // correct syntactic form unless there is a function template with |
| 2748 | // that name visible at the point of the call. If no such name is |
| 2749 | // visible, the call is not syntactically well-formed and |
| 2750 | // argument-dependent lookup does not apply. If some such name is |
| 2751 | // visible, argument dependent lookup applies and additional function |
| 2752 | // templates may be found in other namespaces. |
| 2753 | // |
| 2754 | // The summary of this paragraph is that, if we get to this point and the |
| 2755 | // template-id was not a qualified name, then argument-dependent lookup |
| 2756 | // is still possible. |
| 2757 | if ((Qualifier = TemplateIdRef->getQualifier())) { |
| 2758 | ArgumentDependentLookup = false; |
| 2759 | QualifierRange = TemplateIdRef->getQualifierRange(); |
| 2760 | } |
| 2761 | break; |
| 2762 | } else { |
| 2763 | // Any kind of name that does not refer to a declaration (or |
| 2764 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2765 | ArgumentDependentLookup = false; |
| 2766 | break; |
| 2767 | } |
| 2768 | } |
| 2769 | } |
| 2770 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2771 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2772 | /// This provides the location of the left/right parens and a list of comma |
| 2773 | /// locations. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2774 | Action::OwningExprResult |
| 2775 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2776 | MultiExprArg args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2777 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2778 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2779 | |
| 2780 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2781 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2783 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2784 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 2785 | assert(Fn && "no function call expression"); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2786 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2787 | NamedDecl *NDecl = NULL; |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2788 | DeclarationName UnqualifiedName; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2790 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2791 | // If this is a pseudo-destructor expression, build the call immediately. |
| 2792 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 2793 | if (NumArgs > 0) { |
| 2794 | // Pseudo-destructor calls should not have any arguments. |
| 2795 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 2796 | << CodeModificationHint::CreateRemoval( |
| 2797 | SourceRange(Args[0]->getLocStart(), |
| 2798 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2800 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2801 | Args[I]->Destroy(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2802 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2803 | NumArgs = 0; |
| 2804 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2806 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 2807 | RParenLoc)); |
| 2808 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2810 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2811 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2812 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2813 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2814 | bool Dependent = false; |
| 2815 | if (Fn->isTypeDependent()) |
| 2816 | Dependent = true; |
| 2817 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2818 | Dependent = true; |
| 2819 | |
| 2820 | if (Dependent) |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2821 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2822 | Context.DependentTy, RParenLoc)); |
| 2823 | |
| 2824 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2825 | if (Fn->getType()->isRecordType()) |
| 2826 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2827 | CommaLocs, RParenLoc)); |
| 2828 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2829 | // Determine whether this is a call to a member function. |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2830 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) { |
| 2831 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
| 2832 | if (isa<OverloadedFunctionDecl>(MemDecl) || |
| 2833 | isa<CXXMethodDecl>(MemDecl) || |
| 2834 | (isa<FunctionTemplateDecl>(MemDecl) && |
| 2835 | isa<CXXMethodDecl>( |
| 2836 | cast<FunctionTemplateDecl>(MemDecl)->getTemplatedDecl()))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2837 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2838 | CommaLocs, RParenLoc)); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2839 | } |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2840 | |
| 2841 | // Determine whether this is a call to a pointer-to-member function. |
| 2842 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Fn->IgnoreParens())) { |
| 2843 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 2844 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2845 | if (const FunctionProtoType *FPT = |
| 2846 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 2847 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2848 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2849 | ExprOwningPtr<CXXMemberCallExpr> |
| 2850 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 2851 | NumArgs, ResultTy, |
| 2852 | RParenLoc)); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2853 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2854 | if (CheckCallReturnType(FPT->getResultType(), |
| 2855 | BO->getRHS()->getSourceRange().getBegin(), |
| 2856 | TheCall.get(), 0)) |
| 2857 | return ExprError(); |
Anders Carlsson | 63dce02 | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 2858 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2859 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 2860 | RParenLoc)) |
| 2861 | return ExprError(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2862 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2863 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 2864 | } |
| 2865 | return ExprError(Diag(Fn->getLocStart(), |
| 2866 | diag::err_typecheck_call_not_function) |
| 2867 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2868 | } |
| 2869 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2872 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2873 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2874 | // lookup and whether there were any explicitly-specified template arguments. |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2875 | bool ADL = true; |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2876 | bool HasExplicitTemplateArgs = 0; |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 2877 | const TemplateArgumentLoc *ExplicitTemplateArgs = 0; |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2878 | unsigned NumExplicitTemplateArgs = 0; |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2879 | NestedNameSpecifier *Qualifier = 0; |
| 2880 | SourceRange QualifierRange; |
| 2881 | DeconstructCallFunction(Fn, NDecl, UnqualifiedName, Qualifier, QualifierRange, |
| 2882 | ADL,HasExplicitTemplateArgs, ExplicitTemplateArgs, |
| 2883 | NumExplicitTemplateArgs); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2884 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2885 | OverloadedFunctionDecl *Ovl = 0; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2886 | FunctionTemplateDecl *FunctionTemplate = 0; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2887 | if (NDecl) { |
| 2888 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2889 | if ((FunctionTemplate = dyn_cast<FunctionTemplateDecl>(NDecl))) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2890 | FDecl = FunctionTemplate->getTemplatedDecl(); |
| 2891 | else |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2892 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2893 | Ovl = dyn_cast<OverloadedFunctionDecl>(NDecl); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2894 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2895 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2896 | if (Ovl || FunctionTemplate || |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2897 | (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | b9063fc | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2898 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2899 | if (FDecl && FDecl->getBuiltinID() && FDecl->isImplicit()) |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2900 | ADL = false; |
| 2901 | |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2902 | // We don't perform ADL in C. |
| 2903 | if (!getLangOptions().CPlusPlus) |
| 2904 | ADL = false; |
| 2905 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2906 | if (Ovl || FunctionTemplate || ADL) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | FDecl = ResolveOverloadedCallFn(Fn, NDecl, UnqualifiedName, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2908 | HasExplicitTemplateArgs, |
| 2909 | ExplicitTemplateArgs, |
| 2910 | NumExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2911 | LParenLoc, Args, NumArgs, CommaLocs, |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2912 | RParenLoc, ADL); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2913 | if (!FDecl) |
| 2914 | return ExprError(); |
| 2915 | |
Douglas Gregor | 091f042 | 2009-10-23 22:18:25 +0000 | [diff] [blame] | 2916 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2917 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2918 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2919 | |
| 2920 | // Promote the function operand. |
| 2921 | UsualUnaryConversions(Fn); |
| 2922 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2923 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2924 | // of arguments and function on error. |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2925 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2926 | Args, NumArgs, |
| 2927 | Context.BoolTy, |
| 2928 | RParenLoc)); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2929 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2930 | const FunctionType *FuncT; |
| 2931 | if (!Fn->getType()->isBlockPointerType()) { |
| 2932 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2933 | // have type pointer to function". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2934 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2935 | if (PT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2936 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2937 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2938 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2939 | } else { // This is a block call. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2940 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2941 | getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2942 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2943 | if (FuncT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2944 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2945 | << Fn->getType() << Fn->getSourceRange()); |
| 2946 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2947 | // Check for a valid return type |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2948 | if (CheckCallReturnType(FuncT->getResultType(), |
| 2949 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 2950 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2951 | return ExprError(); |
| 2952 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2953 | // We know the result type of the call, set it. |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2954 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2955 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2956 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2957 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2958 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2959 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2960 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2961 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2962 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2963 | if (FDecl) { |
| 2964 | // Check if we have too few/too many template arguments, based |
| 2965 | // on our knowledge of the function definition. |
| 2966 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2967 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2968 | const FunctionProtoType *Proto = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2969 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2970 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2971 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2972 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2973 | } |
| 2974 | } |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2977 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2978 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2979 | Expr *Arg = Args[i]; |
| 2980 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2981 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2982 | Arg->getType(), |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2983 | PDiag(diag::err_call_incomplete_argument) |
| 2984 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2985 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2986 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2987 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2988 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2989 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2990 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2991 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2992 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2993 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2994 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2995 | // Check for sentinels |
| 2996 | if (NDecl) |
| 2997 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2998 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2999 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3000 | if (FDecl) { |
| 3001 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 3002 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 3004 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3005 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 3006 | } else if (NDecl) { |
| 3007 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 3008 | return ExprError(); |
| 3009 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3010 | |
Anders Carlsson | f898401 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 3011 | return MaybeBindToTemporary(TheCall.take()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3012 | } |
| 3013 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3014 | Action::OwningExprResult |
| 3015 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 3016 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3017 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3018 | //FIXME: Preserve type source info. |
| 3019 | QualType literalType = GetTypeFromParser(Ty); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3020 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3021 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3022 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3023 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3024 | if (literalType->isArrayType()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3025 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3026 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3027 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3028 | } else if (!literalType->isDependentType() && |
| 3029 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3030 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3032 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3033 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3034 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3035 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3036 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3037 | return ExprError(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3038 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3039 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3040 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3041 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3042 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3043 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3044 | InitExpr.release(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3045 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3046 | literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3049 | Action::OwningExprResult |
| 3050 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3051 | SourceLocation RBraceLoc) { |
| 3052 | unsigned NumInit = initlist.size(); |
| 3053 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3054 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3055 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3056 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3057 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3058 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3059 | RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3060 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3061 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3064 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3065 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3066 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3067 | return CastExpr::CK_NoOp; |
| 3068 | |
| 3069 | if (SrcTy->hasPointerRepresentation()) { |
| 3070 | if (DestTy->hasPointerRepresentation()) |
| 3071 | return CastExpr::CK_BitCast; |
| 3072 | if (DestTy->isIntegerType()) |
| 3073 | return CastExpr::CK_PointerToIntegral; |
| 3074 | } |
| 3075 | |
| 3076 | if (SrcTy->isIntegerType()) { |
| 3077 | if (DestTy->isIntegerType()) |
| 3078 | return CastExpr::CK_IntegralCast; |
| 3079 | if (DestTy->hasPointerRepresentation()) |
| 3080 | return CastExpr::CK_IntegralToPointer; |
| 3081 | if (DestTy->isRealFloatingType()) |
| 3082 | return CastExpr::CK_IntegralToFloating; |
| 3083 | } |
| 3084 | |
| 3085 | if (SrcTy->isRealFloatingType()) { |
| 3086 | if (DestTy->isRealFloatingType()) |
| 3087 | return CastExpr::CK_FloatingCast; |
| 3088 | if (DestTy->isIntegerType()) |
| 3089 | return CastExpr::CK_FloatingToIntegral; |
| 3090 | } |
| 3091 | |
| 3092 | // FIXME: Assert here. |
| 3093 | // assert(false && "Unhandled cast combination!"); |
| 3094 | return CastExpr::CK_Unknown; |
| 3095 | } |
| 3096 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3097 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | 955a067 | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3098 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3099 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3100 | CXXMethodDecl *& ConversionDecl, |
| 3101 | bool FunctionalStyle) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3102 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3103 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3104 | ConversionDecl); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3105 | |
Eli Friedman | da8d4de | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3106 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3107 | |
| 3108 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3109 | // type needs to be scalar. |
| 3110 | if (castType->isVoidType()) { |
| 3111 | // Cast to void allows any expr type. |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3112 | Kind = CastExpr::CK_ToVoid; |
| 3113 | return false; |
| 3114 | } |
| 3115 | |
| 3116 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3117 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3118 | (castType->isStructureType() || castType->isUnionType())) { |
| 3119 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3120 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3121 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3122 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | ec14377 | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3123 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3124 | return false; |
| 3125 | } |
| 3126 | |
| 3127 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3128 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3129 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3130 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3131 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3132 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3133 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3134 | castExpr->getType())) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3135 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3136 | << castExpr->getSourceRange(); |
| 3137 | break; |
| 3138 | } |
| 3139 | } |
| 3140 | if (Field == FieldEnd) |
| 3141 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3142 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | ec14377 | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3143 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3144 | return false; |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3145 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3146 | |
| 3147 | // Reject any other conversions to non-scalar types. |
| 3148 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3149 | << castType << castExpr->getSourceRange(); |
| 3150 | } |
| 3151 | |
| 3152 | if (!castExpr->getType()->isScalarType() && |
| 3153 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3154 | return Diag(castExpr->getLocStart(), |
| 3155 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3156 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3159 | if (castType->isExtVectorType()) |
| 3160 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3161 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3162 | if (castType->isVectorType()) |
| 3163 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3164 | if (castExpr->getType()->isVectorType()) |
| 3165 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3166 | |
| 3167 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | b47acdb | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3168 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3169 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3170 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3171 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3172 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3173 | if (!castType->isArithmeticType()) { |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3174 | QualType castExprType = castExpr->getType(); |
| 3175 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3176 | return Diag(castExpr->getLocStart(), |
| 3177 | diag::err_cast_pointer_from_non_pointer_int) |
| 3178 | << castExprType << castExpr->getSourceRange(); |
| 3179 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3180 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3181 | return Diag(castExpr->getLocStart(), |
| 3182 | diag::err_cast_pointer_to_non_pointer_int) |
| 3183 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3184 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3185 | |
| 3186 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3187 | return false; |
| 3188 | } |
| 3189 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3190 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3191 | CastExpr::CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3192 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3193 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3194 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3195 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3196 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3197 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3198 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3199 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3200 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3201 | } else |
| 3202 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3203 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3204 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3205 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3206 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3207 | return false; |
| 3208 | } |
| 3209 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3210 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3211 | CastExpr::CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3212 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3213 | |
| 3214 | QualType SrcTy = CastExpr->getType(); |
| 3215 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3216 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3217 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3218 | if (SrcTy->isVectorType()) { |
| 3219 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3220 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3221 | << DestTy << SrcTy << R; |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3222 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3223 | return false; |
| 3224 | } |
| 3225 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3226 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3227 | // conversion will take place first from scalar to elt type, and then |
| 3228 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3229 | if (SrcTy->isPointerType()) |
| 3230 | return Diag(R.getBegin(), |
| 3231 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3232 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3233 | |
| 3234 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3235 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3236 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3237 | |
| 3238 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3239 | return false; |
| 3240 | } |
| 3241 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3242 | Action::OwningExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3243 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3244 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3245 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3247 | assert((Ty != 0) && (Op.get() != 0) && |
| 3248 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3249 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3250 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3251 | //FIXME: Preserve type source info. |
| 3252 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3254 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3255 | if (isa<ParenListExpr>(castExpr)) |
| 3256 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3257 | CXXMethodDecl *Method = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3259 | Kind, Method)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3260 | return ExprError(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3261 | |
| 3262 | if (Method) { |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3263 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3264 | Method, move(Op)); |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3265 | |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3266 | if (CastArg.isInvalid()) |
| 3267 | return ExprError(); |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3268 | |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3269 | castExpr = CastArg.takeAs<Expr>(); |
| 3270 | } else { |
| 3271 | Op.release(); |
Fariborz Jahanian | 3df8767 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3272 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3274 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3275 | Kind, castExpr, castType, |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3276 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3279 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3280 | /// of comma binary operators. |
| 3281 | Action::OwningExprResult |
| 3282 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3283 | Expr *expr = EA.takeAs<Expr>(); |
| 3284 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3285 | if (!E) |
| 3286 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3288 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3290 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3291 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3292 | Owned(E->getExpr(i))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3294 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3295 | } |
| 3296 | |
| 3297 | Action::OwningExprResult |
| 3298 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3299 | SourceLocation RParenLoc, ExprArg Op, |
| 3300 | QualType Ty) { |
| 3301 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | |
| 3303 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3304 | // then handle it as such. |
| 3305 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3306 | if (PE->getNumExprs() == 0) { |
| 3307 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3308 | return ExprError(); |
| 3309 | } |
| 3310 | |
| 3311 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3312 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3313 | initExprs.push_back(PE->getExpr(i)); |
| 3314 | |
| 3315 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3316 | // braces instead of the original commas. |
| 3317 | Op.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3319 | initExprs.size(), RParenLoc); |
| 3320 | E->setType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3322 | Owned(E)); |
| 3323 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3325 | // sequence of BinOp comma operators. |
| 3326 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3327 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3328 | } |
| 3329 | } |
| 3330 | |
| 3331 | Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 3332 | SourceLocation R, |
| 3333 | MultiExprArg Val) { |
| 3334 | unsigned nexprs = Val.size(); |
| 3335 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| 3336 | assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); |
| 3337 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| 3338 | return Owned(expr); |
| 3339 | } |
| 3340 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3341 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3342 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3343 | /// C99 6.5.15 |
| 3344 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3345 | SourceLocation QuestionLoc) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3346 | // C++ is sufficiently different to merit its own checker. |
| 3347 | if (getLangOptions().CPlusPlus) |
| 3348 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3349 | |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 3350 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 3351 | |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3352 | UsualUnaryConversions(Cond); |
| 3353 | UsualUnaryConversions(LHS); |
| 3354 | UsualUnaryConversions(RHS); |
| 3355 | QualType CondTy = Cond->getType(); |
| 3356 | QualType LHSTy = LHS->getType(); |
| 3357 | QualType RHSTy = RHS->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3358 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3359 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3360 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3361 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3362 | << CondTy; |
| 3363 | return QualType(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3364 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3365 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3366 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3367 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3368 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3369 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3370 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3371 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3372 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3373 | UsualArithmeticConversions(LHS, RHS); |
| 3374 | return LHS->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3375 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3376 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3377 | // If both operands are the same structure or union type, the result is that |
| 3378 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3379 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3380 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3381 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3382 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3383 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3384 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3385 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3386 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3387 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3388 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3389 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3390 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3391 | if (!LHSTy->isVoidType()) |
| 3392 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3393 | << RHS->getSourceRange(); |
| 3394 | if (!RHSTy->isVoidType()) |
| 3395 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3396 | << LHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3397 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 3398 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3399 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3400 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3401 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3402 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3403 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3404 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3405 | // promote the null to a pointer. |
| 3406 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3407 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3408 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3409 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3410 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3411 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3412 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3413 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3414 | // Handle things like Class and struct objc_class*. Here we case the result |
| 3415 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 3416 | // redefinition type if an attempt is made to access its fields. |
| 3417 | if (LHSTy->isObjCClassType() && |
| 3418 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3419 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3420 | return LHSTy; |
| 3421 | } |
| 3422 | if (RHSTy->isObjCClassType() && |
| 3423 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3424 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3425 | return RHSTy; |
| 3426 | } |
| 3427 | // And the same for struct objc_object* / id |
| 3428 | if (LHSTy->isObjCIdType() && |
| 3429 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3430 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3431 | return LHSTy; |
| 3432 | } |
| 3433 | if (RHSTy->isObjCIdType() && |
| 3434 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3435 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3436 | return RHSTy; |
| 3437 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3438 | // Handle block pointer types. |
| 3439 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3440 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3441 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3442 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3443 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 3444 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3445 | return destType; |
| 3446 | } |
| 3447 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3448 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3449 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3450 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3451 | // We have 2 block pointer types. |
| 3452 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3453 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3454 | return LHSTy; |
| 3455 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3456 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3457 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3458 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3459 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3460 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3461 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3462 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3463 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3464 | // In this situation, we assume void* type. No especially good |
| 3465 | // reason, but this is what gcc does, and we do have to pick |
| 3466 | // to get a consistent AST. |
| 3467 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3468 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3469 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3470 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3471 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3472 | // The block pointer types are compatible. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3473 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3474 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3475 | return LHSTy; |
| 3476 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3477 | // Check constraints for Objective-C object pointers types. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3478 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3480 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3481 | // Two identical object pointer types are always compatible. |
| 3482 | return LHSTy; |
| 3483 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3484 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 3485 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3486 | QualType compositeType = LHSTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3488 | // If both operands are interfaces and either operand can be |
| 3489 | // assigned to the other, use that type as the composite |
| 3490 | // type. This allows |
| 3491 | // xxx ? (A*) a : (B*) b |
| 3492 | // where B is a subclass of A. |
| 3493 | // |
| 3494 | // Additionally, as for assignment, if either type is 'id' |
| 3495 | // allow silent coercion. Finally, if the types are |
| 3496 | // incompatible then make sure to use 'id' as the composite |
| 3497 | // type so the result is acceptable for sending messages to. |
| 3498 | |
| 3499 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3500 | // It could return the composite type. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3501 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
Fariborz Jahanian | a83c016 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3502 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3503 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
Fariborz Jahanian | a83c016 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3504 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3505 | } else if ((LHSTy->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3506 | RHSTy->isObjCQualifiedIdType()) && |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3507 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | // Need to handle "id<xx>" explicitly. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3509 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3510 | // id. Currently localizing to here until clear this should be |
| 3511 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 3512 | compositeType = Context.getObjCIdType(); |
| 3513 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3514 | compositeType = Context.getObjCIdType(); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 3515 | } else if (!(compositeType = |
| 3516 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 3517 | ; |
| 3518 | else { |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3519 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3520 | << LHSTy << RHSTy |
| 3521 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3522 | QualType incompatTy = Context.getObjCIdType(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3523 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3524 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3525 | return incompatTy; |
| 3526 | } |
| 3527 | // The object pointer types are compatible. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3528 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 3529 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3530 | return compositeType; |
| 3531 | } |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3532 | // Check Objective-C object pointer types and 'void *' |
| 3533 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3534 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3535 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3536 | QualType destPointee |
| 3537 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3538 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3539 | // Add qualifiers if necessary. |
| 3540 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 3541 | // Promote to void*. |
| 3542 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3543 | return destType; |
| 3544 | } |
| 3545 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3546 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3547 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3548 | QualType destPointee |
| 3549 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3550 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3551 | // Add qualifiers if necessary. |
| 3552 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 3553 | // Promote to void*. |
| 3554 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3555 | return destType; |
| 3556 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3557 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3558 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3559 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3560 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3561 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3562 | |
| 3563 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3564 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3565 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3566 | QualType destPointee |
| 3567 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3568 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3569 | // Add qualifiers if necessary. |
| 3570 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 3571 | // Promote to void*. |
| 3572 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3573 | return destType; |
| 3574 | } |
| 3575 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3576 | QualType destPointee |
| 3577 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3578 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3579 | // Add qualifiers if necessary. |
Eli Friedman | b0bc559 | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3580 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3581 | // Promote to void*. |
Eli Friedman | b0bc559 | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3582 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3583 | return destType; |
| 3584 | } |
| 3585 | |
| 3586 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3587 | // Two identical pointer types are always compatible. |
| 3588 | return LHSTy; |
| 3589 | } |
| 3590 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3591 | rhptee.getUnqualifiedType())) { |
| 3592 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3593 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3594 | // In this situation, we assume void* type. No especially good |
| 3595 | // reason, but this is what gcc does, and we do have to pick |
| 3596 | // to get a consistent AST. |
| 3597 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3598 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3599 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3600 | return incompatTy; |
| 3601 | } |
| 3602 | // The pointer types are compatible. |
| 3603 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3604 | // differently qualified versions of compatible types, the result type is |
| 3605 | // a pointer to an appropriately qualified version of the *composite* |
| 3606 | // type. |
| 3607 | // FIXME: Need to calculate the composite type. |
| 3608 | // FIXME: Need to add qualifiers |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3609 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3610 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3611 | return LHSTy; |
| 3612 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3613 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3614 | // GCC compatibility: soften pointer/integer mismatch. |
| 3615 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3616 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3617 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3618 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3619 | return RHSTy; |
| 3620 | } |
| 3621 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3622 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3623 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3624 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3625 | return LHSTy; |
| 3626 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3627 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3628 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3629 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3630 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3631 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3634 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3635 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3636 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3637 | SourceLocation ColonLoc, |
| 3638 | ExprArg Cond, ExprArg LHS, |
| 3639 | ExprArg RHS) { |
| 3640 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3641 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3642 | |
| 3643 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3644 | // was the condition. |
| 3645 | bool isLHSNull = LHSExpr == 0; |
| 3646 | if (isLHSNull) |
| 3647 | LHSExpr = CondExpr; |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3648 | |
| 3649 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | daaa9f2 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 3650 | RHSExpr, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 3651 | if (result.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3652 | return ExprError(); |
| 3653 | |
| 3654 | Cond.release(); |
| 3655 | LHS.release(); |
| 3656 | RHS.release(); |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3657 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3658 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3659 | ColonLoc, RHSExpr, result)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3662 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3663 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3664 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3665 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3666 | // FIXME: add a couple examples in this comment. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3667 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3668 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3669 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3670 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3671 | if ((lhsType->isObjCClassType() && |
| 3672 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3673 | (rhsType->isObjCClassType() && |
| 3674 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3675 | return Compatible; |
| 3676 | } |
| 3677 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3678 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3679 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 3680 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3681 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3682 | // make sure we operate on the canonical type |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3683 | lhptee = Context.getCanonicalType(lhptee); |
| 3684 | rhptee = Context.getCanonicalType(rhptee); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3685 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3686 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3687 | |
| 3688 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3689 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3690 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | ece8582 | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3691 | // FIXME: Handle ExtQualType |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3692 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3693 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3694 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3695 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3696 | // incomplete type and the other is a pointer to a qualified or unqualified |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3697 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3698 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3699 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3700 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3701 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3702 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3703 | assert(rhptee->isFunctionType()); |
| 3704 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3705 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3706 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3707 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3708 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3709 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3710 | |
| 3711 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3712 | assert(lhptee->isFunctionType()); |
| 3713 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3714 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3715 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3716 | // unqualified versions of compatible types, ... |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3717 | lhptee = lhptee.getUnqualifiedType(); |
| 3718 | rhptee = rhptee.getUnqualifiedType(); |
| 3719 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3720 | // Check if the pointee types are compatible ignoring the sign. |
| 3721 | // We explicitly check for char so that we catch "char" vs |
| 3722 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3723 | if (lhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3724 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3725 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3726 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3727 | |
| 3728 | if (rhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3729 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3730 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3731 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3732 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3733 | if (lhptee == rhptee) { |
| 3734 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3735 | // takes priority over sign incompatibility because the sign |
| 3736 | // warning can be disabled. |
| 3737 | if (ConvTy != Compatible) |
| 3738 | return ConvTy; |
| 3739 | return IncompatiblePointerSign; |
| 3740 | } |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 3741 | |
| 3742 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 3743 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 3744 | // the eventual target type is the same and the pointers have the same |
| 3745 | // level of indirection, this must be the issue. |
| 3746 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 3747 | do { |
| 3748 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 3749 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 3750 | |
| 3751 | lhptee = Context.getCanonicalType(lhptee); |
| 3752 | rhptee = Context.getCanonicalType(rhptee); |
| 3753 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 3754 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3755 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 3756 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3759 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | return IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3761 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3762 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3765 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3766 | /// block pointer types are compatible or whether a block and normal pointer |
| 3767 | /// are compatible. It is more restrict than comparing two function pointer |
| 3768 | // types. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3769 | Sema::AssignConvertType |
| 3770 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3771 | QualType rhsType) { |
| 3772 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3773 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3774 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3775 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 3776 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3777 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3778 | // make sure we operate on the canonical type |
| 3779 | lhptee = Context.getCanonicalType(lhptee); |
| 3780 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3781 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3782 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3783 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3784 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3785 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3786 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3787 | |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3788 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3789 | return IncompatibleBlockPointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3790 | return ConvTy; |
| 3791 | } |
| 3792 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3793 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3794 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 3795 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3796 | /// |
| 3797 | /// int a, *pint; |
| 3798 | /// short *pshort; |
| 3799 | /// struct foo *pfoo; |
| 3800 | /// |
| 3801 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3802 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3803 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3804 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3805 | /// |
| 3806 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3807 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 3808 | /// |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3809 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3810 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3811 | // Get canonical types. We're not formatting these types, just comparing |
| 3812 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3813 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3814 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3815 | |
| 3816 | if (lhsType == rhsType) |
Chris Lattner | f5c973d | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3817 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 3818 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3819 | if ((lhsType->isObjCClassType() && |
| 3820 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3821 | (rhsType->isObjCClassType() && |
| 3822 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3823 | return Compatible; |
| 3824 | } |
| 3825 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3826 | // If the left-hand side is a reference type, then we are in a |
| 3827 | // (rare!) case where we've allowed the use of references in C, |
| 3828 | // e.g., as a parameter type in a built-in function. In this case, |
| 3829 | // just make sure that the type referenced is compatible with the |
| 3830 | // right-hand side type. The caller is responsible for adjusting |
| 3831 | // lhsType so that the resulting expression does not have reference |
| 3832 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3833 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3834 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3835 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3836 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3837 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3838 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 3839 | // to the same ExtVector type. |
| 3840 | if (lhsType->isExtVectorType()) { |
| 3841 | if (rhsType->isExtVectorType()) |
| 3842 | return lhsType == rhsType ? Compatible : Incompatible; |
| 3843 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 3844 | return Compatible; |
| 3845 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3847 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3848 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3849 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3850 | // no bits are changed but the result type is different. |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3851 | if (getLangOptions().LaxVectorConversions && |
| 3852 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3853 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3854 | return IncompatibleVectors; |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3855 | } |
| 3856 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3857 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3858 | |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3859 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3860 | return Compatible; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3861 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3862 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3863 | if (rhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3864 | return IntToPointer; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3865 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3866 | if (isa<PointerType>(rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3867 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3868 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3869 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3870 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3871 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3872 | return Compatible; |
| 3873 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3874 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3875 | if (rhsType->getAs<BlockPointerType>()) { |
| 3876 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3877 | return Compatible; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3878 | |
| 3879 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3880 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3881 | return Compatible; |
| 3882 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3883 | return Incompatible; |
| 3884 | } |
| 3885 | |
| 3886 | if (isa<BlockPointerType>(lhsType)) { |
| 3887 | if (rhsType->isIntegerType()) |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3888 | return IntToBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3889 | |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3890 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3891 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3892 | return Compatible; |
| 3893 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3894 | if (rhsType->isBlockPointerType()) |
| 3895 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3896 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3897 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3898 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3899 | return Compatible; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3900 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3901 | return Incompatible; |
| 3902 | } |
| 3903 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3904 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 3905 | if (rhsType->isIntegerType()) |
| 3906 | return IntToPointer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3907 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3908 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3909 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3910 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 3911 | return Compatible; |
| 3912 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3913 | } |
| 3914 | if (rhsType->isObjCObjectPointerType()) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3915 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 3916 | return Compatible; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3917 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 3918 | return Compatible; |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3919 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 3920 | return IncompatibleObjCQualifiedId; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3921 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3922 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3923 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3924 | if (RHSPT->getPointeeType()->isVoidType()) |
| 3925 | return Compatible; |
| 3926 | } |
| 3927 | // Treat block pointers as objects. |
| 3928 | if (rhsType->isBlockPointerType()) |
| 3929 | return Compatible; |
| 3930 | return Incompatible; |
| 3931 | } |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3932 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3933 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3934 | if (lhsType == Context.BoolTy) |
| 3935 | return Compatible; |
| 3936 | |
| 3937 | if (lhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3938 | return PointerToInt; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3939 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3940 | if (isa<PointerType>(lhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3941 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3942 | |
| 3943 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3944 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3945 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3946 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3947 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3948 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 3949 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 3950 | if (lhsType == Context.BoolTy) |
| 3951 | return Compatible; |
| 3952 | |
| 3953 | if (lhsType->isIntegerType()) |
| 3954 | return PointerToInt; |
| 3955 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3956 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3957 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3958 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3959 | return Compatible; |
| 3960 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3961 | } |
| 3962 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3963 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3964 | return Compatible; |
| 3965 | return Incompatible; |
| 3966 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3967 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3968 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3969 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3970 | return Compatible; |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 3971 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3972 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3975 | /// \brief Constructs a transparent union from an expression that is |
| 3976 | /// used to initialize the transparent union. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3978 | QualType UnionType, FieldDecl *Field) { |
| 3979 | // Build an initializer list that designates the appropriate member |
| 3980 | // of the transparent union. |
| 3981 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3982 | &E, 1, |
| 3983 | SourceLocation()); |
| 3984 | Initializer->setType(UnionType); |
| 3985 | Initializer->setInitializedFieldInUnion(Field); |
| 3986 | |
| 3987 | // Build a compound literal constructing a value of the transparent |
| 3988 | // union type from this initializer list. |
| 3989 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3990 | false); |
| 3991 | } |
| 3992 | |
| 3993 | Sema::AssignConvertType |
| 3994 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3995 | QualType FromType = rExpr->getType(); |
| 3996 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3997 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3998 | // transparent_union GCC extension. |
| 3999 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 4000 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4001 | return Incompatible; |
| 4002 | |
| 4003 | // The field to initialize within the transparent union. |
| 4004 | RecordDecl *UD = UT->getDecl(); |
| 4005 | FieldDecl *InitField = 0; |
| 4006 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4007 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 4008 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4009 | it != itend; ++it) { |
| 4010 | if (it->getType()->isPointerType()) { |
| 4011 | // If the transparent union contains a pointer type, we allow: |
| 4012 | // 1) void pointer |
| 4013 | // 2) null pointer constant |
| 4014 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4015 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4016 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4017 | InitField = *it; |
| 4018 | break; |
| 4019 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4021 | if (rExpr->isNullPointerConstant(Context, |
| 4022 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4023 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4024 | InitField = *it; |
| 4025 | break; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4030 | == Compatible) { |
| 4031 | InitField = *it; |
| 4032 | break; |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | if (!InitField) |
| 4037 | return Incompatible; |
| 4038 | |
| 4039 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4040 | return Compatible; |
| 4041 | } |
| 4042 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4043 | Sema::AssignConvertType |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4044 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4045 | if (getLangOptions().CPlusPlus) { |
| 4046 | if (!lhsType->isRecordType()) { |
| 4047 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4048 | // expression is implicitly converted (C++ 4) to the |
| 4049 | // cv-unqualified type of the left operand. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4050 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 4051 | "assigning")) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4052 | return Incompatible; |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4053 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
| 4056 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4057 | // structures. |
| 4058 | } |
| 4059 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4060 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4061 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4062 | if ((lhsType->isPointerType() || |
| 4063 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4064 | lhsType->isBlockPointerType()) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4065 | && rExpr->isNullPointerConstant(Context, |
| 4066 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4067 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4068 | return Compatible; |
| 4069 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4070 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4071 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4072 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4073 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4074 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4075 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4076 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4077 | if (!lhsType->isReferenceType()) |
| 4078 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4079 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4080 | Sema::AssignConvertType result = |
| 4081 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4082 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4083 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4084 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4085 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4086 | // so that we can use references in built-in functions even in C. |
| 4087 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4088 | // does not have reference type. |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4089 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4090 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4091 | CastExpr::CK_Unknown); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4092 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4095 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4096 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4097 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4098 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4099 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4102 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4103 | Expr *&rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4104 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4105 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4106 | QualType lhsType = |
| 4107 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4108 | QualType rhsType = |
| 4109 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4110 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4111 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4112 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 4113 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4114 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4115 | // Handle the case of a vector & extvector type of the same size and element |
| 4116 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4117 | if (getLangOptions().LaxVectorConversions) { |
| 4118 | // FIXME: Should we warn here? |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4119 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4120 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4121 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4122 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4123 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4127 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4128 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4129 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4130 | bool swapped = false; |
| 4131 | if (rhsType->isExtVectorType()) { |
| 4132 | swapped = true; |
| 4133 | std::swap(rex, lex); |
| 4134 | std::swap(rhsType, lhsType); |
| 4135 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4136 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4137 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4138 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4139 | QualType EltTy = LV->getElementType(); |
| 4140 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4141 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4142 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4143 | if (swapped) std::swap(rex, lex); |
| 4144 | return lhsType; |
| 4145 | } |
| 4146 | } |
| 4147 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4148 | rhsType->isRealFloatingType()) { |
| 4149 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4150 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4151 | if (swapped) std::swap(rex, lex); |
| 4152 | return lhsType; |
| 4153 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4154 | } |
| 4155 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4156 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4157 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4158 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4159 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4160 | << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 4161 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4164 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 060d5e2 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4166 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4167 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4168 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4169 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4170 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4171 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4172 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4173 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4176 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4178 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4179 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4180 | return CheckVectorOperands(Loc, lex, rex); |
| 4181 | return InvalidOperands(Loc, lex, rex); |
| 4182 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4183 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4184 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4185 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4186 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4187 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4188 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4193 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4194 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4195 | if (CompLHSTy) *CompLHSTy = compType; |
| 4196 | return compType; |
| 4197 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4198 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4199 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4200 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4201 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4202 | if (lex->getType()->isArithmeticType() && |
| 4203 | rex->getType()->isArithmeticType()) { |
| 4204 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4205 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4206 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4207 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4208 | // Put any potential pointer into PExp |
| 4209 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4210 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4211 | std::swap(PExp, IExp); |
| 4212 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4213 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4214 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4215 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4216 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4218 | // Check for arithmetic on pointers to incomplete types. |
| 4219 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4220 | if (getLangOptions().CPlusPlus) { |
| 4221 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4222 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4223 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4224 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4225 | |
| 4226 | // GNU extension: arithmetic on pointer to void |
| 4227 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4228 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4229 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4230 | if (getLangOptions().CPlusPlus) { |
| 4231 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4232 | << lex->getType() << lex->getSourceRange(); |
| 4233 | return QualType(); |
| 4234 | } |
| 4235 | |
| 4236 | // GNU extension: arithmetic on pointer to function |
| 4237 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4238 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4239 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4240 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4241 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4242 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4243 | PExp->getType()->isObjCObjectPointerType()) && |
| 4244 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4245 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4246 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4247 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4248 | return QualType(); |
| 4249 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4250 | // Diagnose bad cases where we step over interface counts. |
| 4251 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4252 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4253 | << PointeeTy << PExp->getSourceRange(); |
| 4254 | return QualType(); |
| 4255 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4257 | if (CompLHSTy) { |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4258 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4259 | if (LHSTy.isNull()) { |
| 4260 | LHSTy = lex->getType(); |
| 4261 | if (LHSTy->isPromotableIntegerType()) |
| 4262 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4263 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4264 | *CompLHSTy = LHSTy; |
| 4265 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4266 | return PExp->getType(); |
| 4267 | } |
| 4268 | } |
| 4269 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4270 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4273 | // C99 6.5.6 |
| 4274 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4275 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4276 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4277 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4278 | if (CompLHSTy) *CompLHSTy = compType; |
| 4279 | return compType; |
| 4280 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4281 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4282 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4283 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4284 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4285 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4286 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4287 | if (lex->getType()->isArithmeticType() |
| 4288 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4289 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4290 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4291 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4292 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4293 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4294 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 4eed7a1 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4295 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4297 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4298 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4299 | bool ComplainAboutVoid = false; |
| 4300 | Expr *ComplainAboutFunc = 0; |
| 4301 | if (lpointee->isVoidType()) { |
| 4302 | if (getLangOptions().CPlusPlus) { |
| 4303 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4304 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4305 | return QualType(); |
| 4306 | } |
| 4307 | |
| 4308 | // GNU C extension: arithmetic on pointer to void |
| 4309 | ComplainAboutVoid = true; |
| 4310 | } else if (lpointee->isFunctionType()) { |
| 4311 | if (getLangOptions().CPlusPlus) { |
| 4312 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4313 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4314 | return QualType(); |
| 4315 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4316 | |
| 4317 | // GNU C extension: arithmetic on pointer to function |
| 4318 | ComplainAboutFunc = lex; |
| 4319 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4321 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4322 | << lex->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4323 | << lex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4324 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4325 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4326 | // Diagnose bad cases where we step over interface counts. |
| 4327 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4328 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4329 | << lpointee << lex->getSourceRange(); |
| 4330 | return QualType(); |
| 4331 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4332 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4333 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4334 | if (rex->getType()->isIntegerType()) { |
| 4335 | if (ComplainAboutVoid) |
| 4336 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4337 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4338 | if (ComplainAboutFunc) |
| 4339 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4340 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4341 | << ComplainAboutFunc->getSourceRange(); |
| 4342 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4343 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4344 | return lex->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4345 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4346 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4347 | // Handle pointer-pointer subtractions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4348 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4349 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4350 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4351 | // RHS must be a completely-type object type. |
| 4352 | // Handle the GNU void* extension. |
| 4353 | if (rpointee->isVoidType()) { |
| 4354 | if (getLangOptions().CPlusPlus) { |
| 4355 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4356 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4357 | return QualType(); |
| 4358 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4360 | ComplainAboutVoid = true; |
| 4361 | } else if (rpointee->isFunctionType()) { |
| 4362 | if (getLangOptions().CPlusPlus) { |
| 4363 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4364 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4365 | return QualType(); |
| 4366 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4367 | |
| 4368 | // GNU extension: arithmetic on pointer to function |
| 4369 | if (!ComplainAboutFunc) |
| 4370 | ComplainAboutFunc = rex; |
| 4371 | } else if (!rpointee->isDependentType() && |
| 4372 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4373 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4374 | << rex->getSourceRange() |
| 4375 | << rex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4376 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4377 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4378 | if (getLangOptions().CPlusPlus) { |
| 4379 | // Pointee types must be the same: C++ [expr.add] |
| 4380 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4381 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4382 | << lex->getType() << rex->getType() |
| 4383 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4384 | return QualType(); |
| 4385 | } |
| 4386 | } else { |
| 4387 | // Pointee types must be compatible C99 6.5.6p3 |
| 4388 | if (!Context.typesAreCompatible( |
| 4389 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4390 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4391 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4392 | << lex->getType() << rex->getType() |
| 4393 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4394 | return QualType(); |
| 4395 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4396 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4397 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4398 | if (ComplainAboutVoid) |
| 4399 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4400 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4401 | if (ComplainAboutFunc) |
| 4402 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4403 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4404 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4405 | |
| 4406 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4407 | return Context.getPointerDiffType(); |
| 4408 | } |
| 4409 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4410 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4411 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4414 | // C99 6.5.7 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4415 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4416 | bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4417 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4418 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4419 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4420 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 4421 | // Vector shifts promote their scalar inputs to vector type. |
| 4422 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 4423 | return CheckVectorOperands(Loc, lex, rex); |
| 4424 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4425 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4426 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4427 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4428 | if (LHSTy.isNull()) { |
| 4429 | LHSTy = lex->getType(); |
| 4430 | if (LHSTy->isPromotableIntegerType()) |
| 4431 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4432 | } |
Chris Lattner | 3c13340 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4433 | if (!isCompAssign) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4434 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4435 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4436 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4437 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4438 | // Sanity-check shift operands |
| 4439 | llvm::APSInt Right; |
| 4440 | // Check right/shifter operand |
Daniel Dunbar | 687fa86 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 4441 | if (!rex->isValueDependent() && |
| 4442 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 2f08571 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4443 | if (Right.isNegative()) |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4444 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4445 | else { |
| 4446 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4447 | Context.getTypeSize(lex->getType())); |
| 4448 | if (Right.uge(LeftBits)) |
| 4449 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4450 | } |
| 4451 | } |
| 4452 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4453 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4454 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4457 | /// \brief Implements -Wsign-compare. |
| 4458 | /// |
| 4459 | /// \param lex the left-hand expression |
| 4460 | /// \param rex the right-hand expression |
| 4461 | /// \param OpLoc the location of the joining operator |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4462 | /// \param Equality whether this is an "equality-like" join, which |
| 4463 | /// suppresses the warning in some cases |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4464 | void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc, |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4465 | const PartialDiagnostic &PD, bool Equality) { |
John McCall | e2c91e6 | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 4466 | // Don't warn if we're in an unevaluated context. |
| 4467 | if (ExprEvalContext == Unevaluated) |
| 4468 | return; |
| 4469 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4470 | QualType lt = lex->getType(), rt = rex->getType(); |
| 4471 | |
| 4472 | // Only warn if both operands are integral. |
| 4473 | if (!lt->isIntegerType() || !rt->isIntegerType()) |
| 4474 | return; |
| 4475 | |
Sebastian Redl | 0b7c85f | 2009-11-05 21:09:23 +0000 | [diff] [blame] | 4476 | // If either expression is value-dependent, don't warn. We'll get another |
| 4477 | // chance at instantiation time. |
| 4478 | if (lex->isValueDependent() || rex->isValueDependent()) |
| 4479 | return; |
| 4480 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4481 | // The rule is that the signed operand becomes unsigned, so isolate the |
| 4482 | // signed operand. |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4483 | Expr *signedOperand, *unsignedOperand; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4484 | if (lt->isSignedIntegerType()) { |
| 4485 | if (rt->isSignedIntegerType()) return; |
| 4486 | signedOperand = lex; |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4487 | unsignedOperand = rex; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4488 | } else { |
| 4489 | if (!rt->isSignedIntegerType()) return; |
| 4490 | signedOperand = rex; |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4491 | unsignedOperand = lex; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4494 | // If the unsigned type is strictly smaller than the signed type, |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4495 | // then (1) the result type will be signed and (2) the unsigned |
| 4496 | // value will fit fully within the signed type, and thus the result |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4497 | // of the comparison will be exact. |
| 4498 | if (Context.getIntWidth(signedOperand->getType()) > |
| 4499 | Context.getIntWidth(unsignedOperand->getType())) |
| 4500 | return; |
| 4501 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4502 | // If the value is a non-negative integer constant, then the |
| 4503 | // signed->unsigned conversion won't change it. |
| 4504 | llvm::APSInt value; |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4505 | if (signedOperand->isIntegerConstantExpr(value, Context)) { |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4506 | assert(value.isSigned() && "result of signed expression not signed"); |
| 4507 | |
| 4508 | if (value.isNonNegative()) |
| 4509 | return; |
| 4510 | } |
| 4511 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4512 | if (Equality) { |
| 4513 | // For (in)equality comparisons, if the unsigned operand is a |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4514 | // constant which cannot collide with a overflowed signed operand, |
| 4515 | // then reinterpreting the signed operand as unsigned will not |
| 4516 | // change the result of the comparison. |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4517 | if (unsignedOperand->isIntegerConstantExpr(value, Context)) { |
| 4518 | assert(!value.isSigned() && "result of unsigned expression is signed"); |
| 4519 | |
| 4520 | // 2's complement: test the top bit. |
| 4521 | if (value.isNonNegative()) |
| 4522 | return; |
| 4523 | } |
| 4524 | } |
| 4525 | |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4526 | Diag(OpLoc, PD) |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4527 | << lex->getType() << rex->getType() |
| 4528 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4529 | } |
| 4530 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4531 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4532 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4533 | unsigned OpaqueOpc, bool isRelational) { |
| 4534 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 4535 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4536 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4537 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4538 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4539 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 4540 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4541 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4542 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 47fea35 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 4543 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 4544 | UsualArithmeticConversions(lex, rex); |
| 4545 | else { |
| 4546 | UsualUnaryConversions(lex); |
| 4547 | UsualUnaryConversions(rex); |
| 4548 | } |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4549 | QualType lType = lex->getType(); |
| 4550 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4551 | |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4552 | if (!lType->isFloatingType() |
| 4553 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4554 | // For non-floating point types, check for self-comparisons of the form |
| 4555 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4556 | // often indicate logic errors in the program. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4557 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | de9e968 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 4558 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4559 | Expr *LHSStripped = lex->IgnoreParens(); |
| 4560 | Expr *RHSStripped = rex->IgnoreParens(); |
| 4561 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 4562 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | 9ffbe41 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 4563 | if (DRL->getDecl() == DRR->getDecl() && |
| 4564 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4565 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4566 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4567 | if (isa<CastExpr>(LHSStripped)) |
| 4568 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 4569 | if (isa<CastExpr>(RHSStripped)) |
| 4570 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4572 | // Warn about comparisons against a string constant (unless the other |
| 4573 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4574 | Expr *literalString = 0; |
| 4575 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4576 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4577 | !RHSStripped->isNullPointerConstant(Context, |
| 4578 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4579 | literalString = lex; |
| 4580 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4581 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 4582 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4583 | !LHSStripped->isNullPointerConstant(Context, |
| 4584 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4585 | literalString = rex; |
| 4586 | literalStringStripped = RHSStripped; |
| 4587 | } |
| 4588 | |
| 4589 | if (literalString) { |
| 4590 | std::string resultComparison; |
| 4591 | switch (Opc) { |
| 4592 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 4593 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 4594 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 4595 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 4596 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 4597 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 4598 | default: assert(false && "Invalid comparison operator"); |
| 4599 | } |
| 4600 | Diag(Loc, diag::warn_stringcompare) |
| 4601 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 4602 | << literalString->getSourceRange() |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4603 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 4604 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 4605 | "strcmp(") |
| 4606 | << CodeModificationHint::CreateInsertion( |
| 4607 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4608 | resultComparison); |
| 4609 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 4610 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4611 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4612 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4613 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4614 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4615 | if (isRelational) { |
| 4616 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4617 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4618 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4619 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4620 | if (lType->isFloatingType()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4621 | assert(rType->isFloatingType()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4622 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | d4ecc6d | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 4623 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4624 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4625 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4626 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4627 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4628 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4629 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 4630 | Expr::NPC_ValueDependentIsNull); |
| 4631 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 4632 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4633 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4634 | // All of the following pointer related warnings are GCC extensions, except |
| 4635 | // when handling null pointer constants. One day, we can consider making them |
| 4636 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 4637 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4638 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4639 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4640 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4641 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4642 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4643 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4644 | if (LCanPointeeTy == RCanPointeeTy) |
| 4645 | return ResultTy; |
| 4646 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4647 | // C++ [expr.rel]p2: |
| 4648 | // [...] Pointer conversions (4.10) and qualification |
| 4649 | // conversions (4.4) are performed on pointer operands (or on |
| 4650 | // a pointer operand and a null pointer constant) to bring |
| 4651 | // them to their composite pointer type. [...] |
| 4652 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4653 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4654 | // comparisons of pointers. |
Douglas Gregor | b842046 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 4655 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4656 | if (T.isNull()) { |
| 4657 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4658 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4659 | return QualType(); |
| 4660 | } |
| 4661 | |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4662 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 4663 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4664 | return ResultTy; |
| 4665 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4666 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 4667 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 4668 | RCanPointeeTy.getUnqualifiedType())) { |
| 4669 | // Valid unless a relational comparison of function pointers |
| 4670 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 4671 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 4672 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4673 | } |
| 4674 | } else if (!isRelational && |
| 4675 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 4676 | // Valid unless comparison between non-null pointer and function pointer |
| 4677 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 4678 | && !LHSIsNull && !RHSIsNull) { |
| 4679 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 4680 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4681 | } |
| 4682 | } else { |
| 4683 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4684 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4685 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 4686 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4687 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4688 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4689 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4690 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4691 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4692 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4693 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4694 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | if (RHSIsNull && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4696 | (lType->isPointerType() || |
| 4697 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 83133d9 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4698 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4699 | return ResultTy; |
| 4700 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4701 | if (LHSIsNull && |
| 4702 | (rType->isPointerType() || |
| 4703 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 83133d9 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4704 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4705 | return ResultTy; |
| 4706 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4707 | |
| 4708 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4709 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4710 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 4711 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4712 | // In addition, pointers to members can be compared, or a pointer to |
| 4713 | // member and a null pointer constant. Pointer to member conversions |
| 4714 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 4715 | // them to a common type. If one operand is a null pointer constant, |
| 4716 | // the common type is the type of the other operand. Otherwise, the |
| 4717 | // common type is a pointer to member type similar (4.4) to the type |
| 4718 | // of one of the operands, with a cv-qualification signature (4.4) |
| 4719 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4720 | // types. |
| 4721 | QualType T = FindCompositePointerType(lex, rex); |
| 4722 | if (T.isNull()) { |
| 4723 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4724 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4725 | return QualType(); |
| 4726 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4728 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 4729 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4730 | return ResultTy; |
| 4731 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4733 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4734 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4735 | return ResultTy; |
| 4736 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4737 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4738 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4739 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4740 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 4741 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4742 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4743 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4744 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4745 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4746 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4747 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4748 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4749 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4750 | } |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4751 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4752 | if (!isRelational |
| 4753 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4754 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4755 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4756 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4757 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4758 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4759 | ->getPointeeType()->isVoidType()))) |
| 4760 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4761 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4762 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4763 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4764 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4765 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4766 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4767 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4768 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4769 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 4770 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4771 | bool LPtrToVoid = LPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4772 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4773 | bool RPtrToVoid = RPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4774 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4775 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4776 | if (!LPtrToVoid && !RPtrToVoid && |
| 4777 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4778 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4779 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4780 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4781 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4782 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4783 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4784 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4785 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4786 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 4787 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4788 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4789 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4790 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4791 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4792 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4793 | unsigned DiagID = 0; |
| 4794 | if (RHSIsNull) { |
| 4795 | if (isRelational) |
| 4796 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4797 | } else if (isRelational) |
| 4798 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4799 | else |
| 4800 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4801 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4802 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4803 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4804 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4805 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4806 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4807 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4808 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4809 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4810 | unsigned DiagID = 0; |
| 4811 | if (LHSIsNull) { |
| 4812 | if (isRelational) |
| 4813 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4814 | } else if (isRelational) |
| 4815 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4816 | else |
| 4817 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4818 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4819 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4820 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4821 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4822 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4823 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4824 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 4825 | } |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4826 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4827 | if (!isRelational && RHSIsNull |
| 4828 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4829 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4830 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4831 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4832 | if (!isRelational && LHSIsNull |
| 4833 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4834 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4835 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4836 | } |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4837 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4838 | } |
| 4839 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4840 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4841 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4842 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4843 | /// types. |
| 4844 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4845 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4846 | bool isRelational) { |
| 4847 | // Check to make sure we're operating on vectors of the same type and width, |
| 4848 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4849 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4850 | if (vType.isNull()) |
| 4851 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4852 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4853 | QualType lType = lex->getType(); |
| 4854 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4855 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4856 | // For non-floating point types, check for self-comparisons of the form |
| 4857 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4858 | // often indicate logic errors in the program. |
| 4859 | if (!lType->isFloatingType()) { |
| 4860 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4861 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4862 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4863 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4864 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4865 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4866 | // Check for comparisons of floating point operands using != and ==. |
| 4867 | if (!isRelational && lType->isFloatingType()) { |
| 4868 | assert (rType->isFloatingType()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4869 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4870 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4871 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4872 | // Return the type for the comparison, which is the same as vector type for |
| 4873 | // integer vectors, or an integer type of identical size and number of |
| 4874 | // elements for floating point vectors. |
| 4875 | if (lType->isIntegerType()) |
| 4876 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4877 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4878 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4879 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4880 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4881 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4882 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4883 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4884 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4885 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4886 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4887 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4888 | } |
| 4889 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4890 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 4892 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4893 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4894 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4895 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4896 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4897 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4898 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4899 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4900 | } |
| 4901 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4902 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4903 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4904 | UsualUnaryConversions(lex); |
| 4905 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4906 | |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 4907 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 4908 | return InvalidOperands(Loc, lex, rex); |
| 4909 | |
| 4910 | if (Context.getLangOptions().CPlusPlus) { |
| 4911 | // C++ [expr.log.and]p2 |
| 4912 | // C++ [expr.log.or]p2 |
| 4913 | return Context.BoolTy; |
| 4914 | } |
| 4915 | |
| 4916 | return Context.IntTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4919 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4920 | /// is a read-only property; return true if so. A readonly property expression |
| 4921 | /// depends on various declarations and thus must be treated specially. |
| 4922 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4923 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4924 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4925 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4926 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4927 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4929 | BaseType->getAsObjCInterfacePointerType()) |
| 4930 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 4931 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4932 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4933 | } |
| 4934 | } |
| 4935 | return false; |
| 4936 | } |
| 4937 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4938 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4939 | /// emit an error and return true. If so, return false. |
| 4940 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4941 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4942 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4943 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4944 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4945 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4946 | if (IsLV == Expr::MLV_Valid) |
| 4947 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4948 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4949 | unsigned Diag = 0; |
| 4950 | bool NeedType = false; |
| 4951 | switch (IsLV) { // C99 6.5.16p2 |
| 4952 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4953 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4954 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4955 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4956 | NeedType = true; |
| 4957 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4958 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4959 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4960 | NeedType = true; |
| 4961 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4962 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4963 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4964 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4965 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4966 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4967 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4968 | case Expr::MLV_IncompleteType: |
| 4969 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4970 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4971 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 4972 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4973 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4974 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4975 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4976 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4977 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4978 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4979 | case Expr::MLV_ReadonlyProperty: |
| 4980 | Diag = diag::error_readonly_property_assignment; |
| 4981 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4982 | case Expr::MLV_NoSetterProperty: |
| 4983 | Diag = diag::error_nosetter_property_assignment; |
| 4984 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4985 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4986 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4987 | SourceRange Assign; |
| 4988 | if (Loc != OrigLoc) |
| 4989 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4990 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4991 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4992 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4993 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4994 | return true; |
| 4995 | } |
| 4996 | |
| 4997 | |
| 4998 | |
| 4999 | // C99 6.5.16.1 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5000 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 5001 | SourceLocation Loc, |
| 5002 | QualType CompoundType) { |
| 5003 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 5004 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 5005 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5006 | |
| 5007 | QualType LHSType = LHS->getType(); |
| 5008 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5009 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5010 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5011 | if (CompoundType.isNull()) { |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5012 | // Simple assignment "x = y". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5013 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5014 | // Special case of NSObject attributes on c-style pointer types. |
| 5015 | if (ConvTy == IncompatiblePointer && |
| 5016 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5017 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5018 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5019 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5020 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5021 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5022 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5023 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5024 | // instead of "x += 4". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5025 | Expr *RHSCheck = RHS; |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5026 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5027 | RHSCheck = ICE->getSubExpr(); |
| 5028 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5029 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5030 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5031 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5032 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5033 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5034 | // And there is a space or other character before the subexpr of the |
| 5035 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5036 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5037 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5038 | Diag(Loc, diag::warn_not_compound_assign) |
| 5039 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5040 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5041 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5042 | } |
| 5043 | } else { |
| 5044 | // Compound assignment "x += y" |
Eli Friedman | b05c41e | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5045 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5046 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5047 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5048 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 5049 | RHS, "assigning")) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5050 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5051 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5052 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5053 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5054 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5055 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5056 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5057 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5058 | // operand. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5059 | return LHSType.getUnqualifiedType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5062 | // C99 6.5.17 |
| 5063 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | f6e1e30 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5064 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5065 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5066 | |
| 5067 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5068 | // incomplete in C++). |
| 5069 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5070 | return RHS->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 5071 | } |
| 5072 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5073 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5074 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5075 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5076 | bool isInc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5077 | if (Op->isTypeDependent()) |
| 5078 | return Context.DependentTy; |
| 5079 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5080 | QualType ResType = Op->getType(); |
| 5081 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 5082 | |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5083 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5084 | // Decrement of bool is not allowed. |
| 5085 | if (!isInc) { |
| 5086 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5087 | return QualType(); |
| 5088 | } |
| 5089 | // Increment of bool sets it to true, but is deprecated. |
| 5090 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5091 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5092 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5093 | } else if (ResType->isAnyPointerType()) { |
| 5094 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5095 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5096 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5097 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5098 | if (getLangOptions().CPlusPlus) { |
| 5099 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5100 | << Op->getSourceRange(); |
| 5101 | return QualType(); |
| 5102 | } |
| 5103 | |
| 5104 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5105 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5106 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5107 | if (getLangOptions().CPlusPlus) { |
| 5108 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5109 | << Op->getType() << Op->getSourceRange(); |
| 5110 | return QualType(); |
| 5111 | } |
| 5112 | |
| 5113 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5114 | << ResType << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5115 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5116 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5117 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5118 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5119 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5120 | // Diagnose bad cases where we step over interface counts. |
| 5121 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5122 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5123 | << PointeeTy << Op->getSourceRange(); |
| 5124 | return QualType(); |
| 5125 | } |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5126 | } else if (ResType->isComplexType()) { |
| 5127 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5128 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5129 | << ResType << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5130 | } else { |
| 5131 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5132 | << ResType << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5133 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 5134 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5135 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 9e1e551 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 5136 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5137 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5138 | return QualType(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5139 | return ResType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5140 | } |
| 5141 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5142 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5143 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5144 | /// where the declaration is needed for type checking. We only need to |
| 5145 | /// handle cases when the expression references a function designator |
| 5146 | /// or is an lvalue. Here are some examples: |
| 5147 | /// - &(x) => x |
| 5148 | /// - &*****f => f for f a function designator. |
| 5149 | /// - &s.xx => s |
| 5150 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5151 | /// - *(x + 1) -> x, if x is an array |
| 5152 | /// - &"123"[2] -> 0 |
| 5153 | /// - & __real__ x -> x |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5154 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5155 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5156 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5157 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5158 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5159 | // If this is an arrow operator, the address is an offset from |
| 5160 | // the base's value, so the object the base refers to is |
| 5161 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5162 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5163 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5164 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5165 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5166 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5167 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5168 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5169 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5170 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5171 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5172 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5173 | } |
| 5174 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5175 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5176 | case Stmt::UnaryOperatorClass: { |
| 5177 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5178 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5179 | switch(UO->getOpcode()) { |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5180 | case UnaryOperator::Real: |
| 5181 | case UnaryOperator::Imag: |
| 5182 | case UnaryOperator::Extension: |
| 5183 | return getPrimaryDecl(UO->getSubExpr()); |
| 5184 | default: |
| 5185 | return 0; |
| 5186 | } |
| 5187 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5188 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5189 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5190 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5191 | // If the result of an implicit cast is an l-value, we care about |
| 5192 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5193 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5194 | default: |
| 5195 | return 0; |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5200 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5201 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5202 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5203 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5204 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5205 | /// we allow the '&' but retain the overloaded-function type. |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5206 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5207 | // Make sure to ignore parentheses in subsequent checks |
| 5208 | op = op->IgnoreParens(); |
| 5209 | |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5210 | if (op->isTypeDependent()) |
| 5211 | return Context.DependentTy; |
| 5212 | |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5213 | if (getLangOptions().C99) { |
| 5214 | // Implement C99-only parts of addressof rules. |
| 5215 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5216 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5217 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5218 | // (assuming the deref expression is valid). |
| 5219 | return uOp->getSubExpr()->getType(); |
| 5220 | } |
| 5221 | // Technically, there should be a check for array subscript |
| 5222 | // expressions here, but the result of one is always an lvalue anyway. |
| 5223 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5224 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 6731544 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5225 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5226 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5227 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5228 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5229 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5230 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5231 | // FIXME: emit more specific diag... |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5232 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5233 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5234 | return QualType(); |
| 5235 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5236 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5237 | // The operand cannot be a bit-field |
| 5238 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5239 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5240 | return QualType(); |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5241 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5242 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5243 | // The operand cannot be an element of a vector |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5244 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5245 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5246 | return QualType(); |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5247 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5248 | // cannot take address of a property expression. |
| 5249 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5250 | << "property expression" << op->getSourceRange(); |
| 5251 | return QualType(); |
Anders Carlsson | 3fa58d1 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5252 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5253 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 01ccf99 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5254 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5255 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5256 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5257 | // We have an lvalue with a decl. Make sure the decl is not declared |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5258 | // with the register storage-class specifier. |
| 5259 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5260 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5261 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5262 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5263 | return QualType(); |
| 5264 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5265 | } else if (isa<OverloadedFunctionDecl>(dcl) || |
| 5266 | isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5267 | return Context.OverloadTy; |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5268 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5269 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5270 | // Could be a pointer to member, though, if there is an explicit |
| 5271 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5272 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5273 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5274 | if (Ctx && Ctx->isRecord()) { |
| 5275 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5276 | Diag(OpLoc, |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5277 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5278 | << FD->getDeclName() << FD->getType(); |
| 5279 | return QualType(); |
| 5280 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5281 | |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5282 | return Context.getMemberPointerType(op->getType(), |
| 5283 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5284 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5285 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5286 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 5773a1b | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5287 | // Okay: we can take the address of a function. |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5288 | // As above. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5289 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 5290 | MD->isInstance()) |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5291 | return Context.getMemberPointerType(op->getType(), |
| 5292 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5293 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 5294 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5295 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5296 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5297 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5298 | // Taking the address of a void variable is technically illegal, but we |
| 5299 | // allow it in cases which are otherwise valid. |
| 5300 | // Example: "extern void x; void* y = &x;". |
| 5301 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5302 | } |
| 5303 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5304 | // If the operand has type "type", the result has type "pointer to type". |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5305 | return Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5306 | } |
| 5307 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5308 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5309 | if (Op->isTypeDependent()) |
| 5310 | return Context.DependentTy; |
| 5311 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5312 | UsualUnaryConversions(Op); |
| 5313 | QualType Ty = Op->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5314 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5315 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5316 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5317 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5318 | // unlikely to catch any mistakes. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5319 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5320 | return PT->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5321 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5322 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | f15d4b6 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5323 | return OPT->getPointeeType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5324 | |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5325 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5326 | << Ty << Op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5327 | return QualType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 5328 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5329 | |
| 5330 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5331 | tok::TokenKind Kind) { |
| 5332 | BinaryOperator::Opcode Opc; |
| 5333 | switch (Kind) { |
| 5334 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5335 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5336 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5337 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5338 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5339 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5340 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5341 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5342 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5343 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5344 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5345 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5346 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5347 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5348 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5349 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5350 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5351 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5352 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5353 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5354 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5355 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5356 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5357 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5358 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5359 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5360 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5361 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5362 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5363 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5364 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5365 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5366 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5367 | } |
| 5368 | return Opc; |
| 5369 | } |
| 5370 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5371 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5372 | tok::TokenKind Kind) { |
| 5373 | UnaryOperator::Opcode Opc; |
| 5374 | switch (Kind) { |
| 5375 | default: assert(0 && "Unknown unary op!"); |
| 5376 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5377 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5378 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5379 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5380 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5381 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5382 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5383 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5384 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5385 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
Chris Lattner | d0f7651 | 2007-06-08 22:16:53 +0000 | [diff] [blame] | 5386 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5387 | } |
| 5388 | return Opc; |
| 5389 | } |
| 5390 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5391 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5392 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5393 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5394 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5395 | unsigned Op, |
| 5396 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5397 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5398 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5399 | // The following two variables are used for compound assignment operators |
| 5400 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5401 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5402 | |
| 5403 | switch (Opc) { |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5404 | case BinaryOperator::Assign: |
| 5405 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5406 | break; |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5407 | case BinaryOperator::PtrMemD: |
| 5408 | case BinaryOperator::PtrMemI: |
| 5409 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5410 | Opc == BinaryOperator::PtrMemI); |
| 5411 | break; |
| 5412 | case BinaryOperator::Mul: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5413 | case BinaryOperator::Div: |
| 5414 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5415 | break; |
| 5416 | case BinaryOperator::Rem: |
| 5417 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5418 | break; |
| 5419 | case BinaryOperator::Add: |
| 5420 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5421 | break; |
| 5422 | case BinaryOperator::Sub: |
| 5423 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5424 | break; |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5425 | case BinaryOperator::Shl: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5426 | case BinaryOperator::Shr: |
| 5427 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5428 | break; |
| 5429 | case BinaryOperator::LE: |
| 5430 | case BinaryOperator::LT: |
| 5431 | case BinaryOperator::GE: |
| 5432 | case BinaryOperator::GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5433 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5434 | break; |
| 5435 | case BinaryOperator::EQ: |
| 5436 | case BinaryOperator::NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5437 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5438 | break; |
| 5439 | case BinaryOperator::And: |
| 5440 | case BinaryOperator::Xor: |
| 5441 | case BinaryOperator::Or: |
| 5442 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5443 | break; |
| 5444 | case BinaryOperator::LAnd: |
| 5445 | case BinaryOperator::LOr: |
| 5446 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5447 | break; |
| 5448 | case BinaryOperator::MulAssign: |
| 5449 | case BinaryOperator::DivAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5450 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 5451 | CompLHSTy = CompResultTy; |
| 5452 | if (!CompResultTy.isNull()) |
| 5453 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5454 | break; |
| 5455 | case BinaryOperator::RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5456 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 5457 | CompLHSTy = CompResultTy; |
| 5458 | if (!CompResultTy.isNull()) |
| 5459 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5460 | break; |
| 5461 | case BinaryOperator::AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5462 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5463 | if (!CompResultTy.isNull()) |
| 5464 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5465 | break; |
| 5466 | case BinaryOperator::SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5467 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5468 | if (!CompResultTy.isNull()) |
| 5469 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5470 | break; |
| 5471 | case BinaryOperator::ShlAssign: |
| 5472 | case BinaryOperator::ShrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5473 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 5474 | CompLHSTy = CompResultTy; |
| 5475 | if (!CompResultTy.isNull()) |
| 5476 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5477 | break; |
| 5478 | case BinaryOperator::AndAssign: |
| 5479 | case BinaryOperator::XorAssign: |
| 5480 | case BinaryOperator::OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5481 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 5482 | CompLHSTy = CompResultTy; |
| 5483 | if (!CompResultTy.isNull()) |
| 5484 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5485 | break; |
| 5486 | case BinaryOperator::Comma: |
| 5487 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 5488 | break; |
| 5489 | } |
| 5490 | if (ResultTy.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5491 | return ExprError(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5492 | if (CompResultTy.isNull()) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5493 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 5494 | else |
| 5495 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5496 | CompLHSTy, CompResultTy, |
| 5497 | OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5498 | } |
| 5499 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5500 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 5501 | /// ParenRange in parentheses. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 5502 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 5503 | const PartialDiagnostic &PD, |
| 5504 | SourceRange ParenRange) |
| 5505 | { |
| 5506 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 5507 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 5508 | // We can't display the parentheses, so just dig the |
| 5509 | // warning/error and return. |
| 5510 | Self.Diag(Loc, PD); |
| 5511 | return; |
| 5512 | } |
| 5513 | |
| 5514 | Self.Diag(Loc, PD) |
| 5515 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 5516 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
| 5517 | } |
| 5518 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5519 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 5520 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 5521 | /// comparison operators have higher precedence. The most typical example of |
| 5522 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5523 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 5524 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5525 | typedef BinaryOperator BinOp; |
| 5526 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 5527 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 5528 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5529 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5530 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5531 | rhsopc = BO->getOpcode(); |
| 5532 | |
| 5533 | // Subs are not binary operators. |
| 5534 | if (lhsopc == -1 && rhsopc == -1) |
| 5535 | return; |
| 5536 | |
| 5537 | // Bitwise operations are sometimes used as eager logical ops. |
| 5538 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5539 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 5540 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5541 | return; |
| 5542 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5543 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 5544 | SuggestParentheses(Self, OpLoc, |
| 5545 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5546 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 5547 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
| 5548 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 5549 | else if (BinOp::isComparisonOp(rhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 5550 | SuggestParentheses(Self, OpLoc, |
| 5551 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5552 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 5553 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
| 5554 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5555 | } |
| 5556 | |
| 5557 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 5558 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 5559 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 5560 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 5561 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5562 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5563 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 5564 | } |
| 5565 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5566 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5567 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 5568 | tok::TokenKind Kind, |
| 5569 | ExprArg LHS, ExprArg RHS) { |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5570 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5571 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5572 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5573 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 5574 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5575 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5576 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 5577 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 5578 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5579 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 5580 | } |
| 5581 | |
| 5582 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 5583 | BinaryOperator::Opcode Opc, |
| 5584 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5585 | if (getLangOptions().CPlusPlus && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5586 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5587 | rhs->getType()->isOverloadableType())) { |
| 5588 | // Find all of the overloaded operators visible from this |
| 5589 | // point. We perform both an operator-name lookup from the local |
| 5590 | // scope and an argument-dependent lookup based on the types of |
| 5591 | // the arguments. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5592 | FunctionSet Functions; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5593 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 5594 | if (OverOp != OO_None) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5595 | if (S) |
| 5596 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 5597 | Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5598 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5599 | DeclarationName OpName |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5600 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 5601 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5602 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5603 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5604 | // Build the (potentially-overloaded, potentially-dependent) |
| 5605 | // binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5606 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5607 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5609 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5610 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5611 | } |
| 5612 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5613 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | unsigned OpcIn, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5615 | ExprArg InputArg) { |
| 5616 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5617 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5618 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5619 | Expr *Input = (Expr *)InputArg.get(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5620 | QualType resultType; |
| 5621 | switch (Opc) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5622 | case UnaryOperator::OffsetOf: |
| 5623 | assert(false && "Invalid unary operator"); |
| 5624 | break; |
| 5625 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5626 | case UnaryOperator::PreInc: |
| 5627 | case UnaryOperator::PreDec: |
Eli Friedman | 6aea575 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5628 | case UnaryOperator::PostInc: |
| 5629 | case UnaryOperator::PostDec: |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5630 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | 6aea575 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5631 | Opc == UnaryOperator::PreInc || |
| 5632 | Opc == UnaryOperator::PostInc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5633 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5634 | case UnaryOperator::AddrOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5635 | resultType = CheckAddressOfOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5636 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5637 | case UnaryOperator::Deref: |
Steve Naroff | b723564 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 5638 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5639 | resultType = CheckIndirectionOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5640 | break; |
| 5641 | case UnaryOperator::Plus: |
| 5642 | case UnaryOperator::Minus: |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5643 | UsualUnaryConversions(Input); |
| 5644 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5645 | if (resultType->isDependentType()) |
| 5646 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5647 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 5648 | break; |
| 5649 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 5650 | resultType->isEnumeralType()) |
| 5651 | break; |
| 5652 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 5653 | Opc == UnaryOperator::Plus && |
| 5654 | resultType->isPointerType()) |
| 5655 | break; |
| 5656 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5657 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5658 | << resultType << Input->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5659 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5660 | UsualUnaryConversions(Input); |
| 5661 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5662 | if (resultType->isDependentType()) |
| 5663 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5664 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 5665 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 5666 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5667 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5668 | << resultType << Input->getSourceRange(); |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5669 | else if (!resultType->isIntegerType()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5670 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5671 | << resultType << Input->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5672 | break; |
| 5673 | case UnaryOperator::LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 5674 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5675 | DefaultFunctionArrayConversion(Input); |
| 5676 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5677 | if (resultType->isDependentType()) |
| 5678 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5679 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5680 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5681 | << resultType << Input->getSourceRange()); |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 5682 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5683 | // In C++, it's bool. C++ 5.3.1p8 |
| 5684 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5685 | break; |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5686 | case UnaryOperator::Real: |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5687 | case UnaryOperator::Imag: |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 5688 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5689 | break; |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5690 | case UnaryOperator::Extension: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5691 | resultType = Input->getType(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 5692 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5693 | } |
| 5694 | if (resultType.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5695 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5696 | |
| 5697 | InputArg.release(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5698 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5699 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5701 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5702 | UnaryOperator::Opcode Opc, |
| 5703 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5704 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 5705 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 5706 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5707 | // Find all of the overloaded operators visible from this |
| 5708 | // point. We perform both an operator-name lookup from the local |
| 5709 | // scope and an argument-dependent lookup based on the types of |
| 5710 | // the arguments. |
| 5711 | FunctionSet Functions; |
| 5712 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 5713 | if (OverOp != OO_None) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5714 | if (S) |
| 5715 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 5716 | Functions); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5717 | DeclarationName OpName |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5718 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 5719 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5720 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5721 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5722 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 5723 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5724 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5725 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 5726 | } |
| 5727 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5728 | // Unary Operators. 'Tok' is the token for the operator. |
| 5729 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5730 | tok::TokenKind Op, ExprArg input) { |
| 5731 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 5732 | } |
| 5733 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 5734 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5735 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 5736 | SourceLocation LabLoc, |
| 5737 | IdentifierInfo *LabelII) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5738 | // Look up the record for this label identifier. |
Chris Lattner | 3318e86 | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 5739 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5740 | |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 5741 | // If we haven't seen this label yet, create a forward reference. It |
| 5742 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | 846b1ec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 5743 | if (LabelDecl == 0) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5744 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5745 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5746 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5747 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 5748 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5749 | } |
| 5750 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5751 | Sema::OwningExprResult |
| 5752 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 5753 | SourceLocation RPLoc) { // "({..})" |
| 5754 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5755 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 5756 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 5757 | |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5758 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 5759 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5760 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5761 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5762 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 5763 | // example, it is not possible to goto into a stmt expression apparently. |
| 5764 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5765 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5766 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 5767 | // as the type of the stmtexpr. |
| 5768 | QualType Ty = Context.VoidTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5769 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5770 | if (!Compound->body_empty()) { |
| 5771 | Stmt *LastStmt = Compound->body_back(); |
| 5772 | // If LastStmt is a label, skip down through into the body. |
| 5773 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 5774 | LastStmt = Label->getSubStmt(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5775 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5776 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5777 | Ty = LastExpr->getType(); |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5778 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5779 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5780 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 5781 | // expressions are not lvalues. |
| 5782 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5783 | substmt.release(); |
| 5784 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5785 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5786 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5787 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 5788 | SourceLocation BuiltinLoc, |
| 5789 | SourceLocation TypeLoc, |
| 5790 | TypeTy *argty, |
| 5791 | OffsetOfComponent *CompPtr, |
| 5792 | unsigned NumComponents, |
| 5793 | SourceLocation RPLoc) { |
| 5794 | // FIXME: This function leaks all expressions in the offset components on |
| 5795 | // error. |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5796 | // FIXME: Preserve type source info. |
| 5797 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5798 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5799 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5800 | bool Dependent = ArgTy->isDependentType(); |
| 5801 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5802 | // We must have at least one component that refers to the type, and the first |
| 5803 | // one is known to be a field designator. Verify that the ArgTy represents |
| 5804 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5805 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5806 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5807 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5808 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 5809 | // with an incomplete type would be illegal. |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 5810 | |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5811 | // Otherwise, create a null pointer as the base, and iteratively process |
| 5812 | // the offsetof designators. |
| 5813 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 5814 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5815 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5816 | ArgTy, SourceLocation()); |
Eli Friedman | 16c88df | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 5817 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5818 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 5819 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5820 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 5821 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5822 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5823 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 5824 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5825 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5826 | if (!Dependent) { |
Eli Friedman | 8469bc7 | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 5827 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5828 | |
John McCall | 9eff4e6 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 5829 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 5830 | diag::err_offsetof_incomplete_type)) |
| 5831 | return ExprError(); |
| 5832 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5833 | // FIXME: Dependent case loses a lot of information here. And probably |
| 5834 | // leaks like a sieve. |
| 5835 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 5836 | const OffsetOfComponent &OC = CompPtr[i]; |
| 5837 | if (OC.isBrackets) { |
| 5838 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 5839 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 5840 | if (!AT) { |
| 5841 | Res->Destroy(Context); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5842 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 5843 | << Res->getType()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 5847 | |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5848 | // Promote the array so it looks more like a normal array subscript |
| 5849 | // expression. |
| 5850 | DefaultFunctionArrayConversion(Res); |
| 5851 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5852 | // C99 6.5.2.1p1 |
| 5853 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5854 | // FIXME: Leaks Res |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5855 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5856 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5857 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5858 | << Idx->getSourceRange()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5859 | |
| 5860 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5861 | OC.LocEnd); |
| 5862 | continue; |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5863 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5864 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5865 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5866 | if (!RC) { |
| 5867 | Res->Destroy(Context); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5868 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5869 | << Res->getType()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5870 | } |
Chris Lattner | 98dbf0a | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5871 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5872 | // Get the decl corresponding to this. |
| 5873 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 2bbb86b | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5874 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 38ebcaa | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5875 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | 8b98d02 | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5876 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5877 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5878 | << Res->getType()); |
Anders Carlsson | 38ebcaa | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5879 | DidWarnAboutNonPOD = true; |
| 5880 | } |
Anders Carlsson | 2bbb86b | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5881 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5882 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame^] | 5883 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 5884 | LookupQualifiedName(R, RD); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5885 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5886 | FieldDecl *MemberDecl |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5887 | = dyn_cast_or_null<FieldDecl>(R.getAsSingleDecl(Context)); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5888 | // FIXME: Leaks Res |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5889 | if (!MemberDecl) |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5890 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 5891 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5892 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5893 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5894 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 64fc3c6 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5895 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5896 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 7e1d6d7 | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 5897 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | 64fc3c6 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5898 | } else { |
| 5899 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5900 | // doesn't matter here. |
| 5901 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5902 | MemberDecl->getType().getNonReferenceType()); |
| 5903 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5904 | } |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5905 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5906 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5907 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5908 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5909 | } |
| 5910 | |
| 5911 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5912 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5913 | TypeTy *arg1,TypeTy *arg2, |
| 5914 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5915 | // FIXME: Preserve type source info. |
| 5916 | QualType argT1 = GetTypeFromParser(arg1); |
| 5917 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5918 | |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5919 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5920 | |
Douglas Gregor | f907cbf | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5921 | if (getLangOptions().CPlusPlus) { |
| 5922 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5923 | << SourceRange(BuiltinLoc, RPLoc); |
| 5924 | return ExprError(); |
| 5925 | } |
| 5926 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5927 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5928 | argT1, argT2, RPLoc)); |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5931 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5932 | ExprArg cond, |
| 5933 | ExprArg expr1, ExprArg expr2, |
| 5934 | SourceLocation RPLoc) { |
| 5935 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5936 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5937 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5938 | |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5939 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5940 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5941 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5942 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5943 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5944 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5945 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5946 | } else { |
| 5947 | // The conditional expression is required to be a constant expression. |
| 5948 | llvm::APSInt condEval(32); |
| 5949 | SourceLocation ExpLoc; |
| 5950 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5951 | return ExprError(Diag(ExpLoc, |
| 5952 | diag::err_typecheck_choose_expr_requires_constant) |
| 5953 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5954 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5955 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5956 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5957 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 5958 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5961 | cond.release(); expr1.release(); expr2.release(); |
| 5962 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5963 | resType, RPLoc, |
| 5964 | resType->isDependentType(), |
| 5965 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5966 | } |
| 5967 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5968 | //===----------------------------------------------------------------------===// |
| 5969 | // Clang Extensions. |
| 5970 | //===----------------------------------------------------------------------===// |
| 5971 | |
| 5972 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5973 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5974 | // Analyze block parameters. |
| 5975 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5976 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5977 | // Add BSI to CurBlock. |
| 5978 | BSI->PrevBlockInfo = CurBlock; |
| 5979 | CurBlock = BSI; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5980 | |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5981 | BSI->ReturnType = QualType(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5982 | BSI->TheScope = BlockScope; |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5983 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | b9a6861 | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 5984 | BSI->hasPrototype = false; |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5985 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5986 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5987 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5988 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5989 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5992 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5993 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5994 | |
| 5995 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5996 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5997 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5998 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5999 | |
Mike Stump | d456c48 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 6000 | if (T->isArrayType()) { |
| 6001 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6002 | diag::err_block_returns_array); |
| 6003 | return; |
| 6004 | } |
| 6005 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6006 | // The parameter list is optional, if there was none, assume (). |
| 6007 | if (!T->isFunctionType()) |
| 6008 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6009 | |
| 6010 | CurBlock->hasPrototype = true; |
| 6011 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6012 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6013 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6014 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6015 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6016 | // FIXME: remove the attribute. |
| 6017 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6018 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6019 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6020 | // Do not allow returning a objc interface by-value. |
| 6021 | if (RetTy->isObjCInterfaceType()) { |
| 6022 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6023 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6024 | return; |
| 6025 | } |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6026 | return; |
| 6027 | } |
| 6028 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6029 | // Analyze arguments to block. |
| 6030 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6031 | "Not a function declarator!"); |
| 6032 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6033 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6034 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6035 | CurBlock->isVariadic = true; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6036 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6037 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6038 | // no arguments, not a function that takes a single void argument. |
| 6039 | if (FTI.hasPrototype && |
| 6040 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6041 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6042 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6043 | // empty arg list, don't push any params. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6044 | CurBlock->isVariadic = false; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6045 | } else if (FTI.hasPrototype) { |
| 6046 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6047 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6048 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6049 | } |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6050 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6051 | CurBlock->Params.size()); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6052 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6053 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6054 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6055 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6056 | // If this has an identifier, add it to the scope stack. |
| 6057 | if ((*AI)->getIdentifier()) |
| 6058 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6059 | |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6060 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6061 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6062 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6063 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6064 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6065 | // FIXME: remove the attribute. |
| 6066 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6067 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6068 | // Analyze the return type. |
| 6069 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6070 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6071 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6072 | // Do not allow returning a objc interface by-value. |
| 6073 | if (RetTy->isObjCInterfaceType()) { |
| 6074 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6075 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6076 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6077 | CurBlock->ReturnType = RetTy; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6078 | } |
| 6079 | |
| 6080 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6081 | /// is invoked to pop the information about the block from the action impl. |
| 6082 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6083 | // Ensure that CurBlock is deleted. |
| 6084 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6085 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6086 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6087 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6088 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6089 | PopDeclContext(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6090 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6091 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6092 | } |
| 6093 | |
| 6094 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6095 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6096 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6097 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6098 | // If blocks are disabled, emit an error. |
| 6099 | if (!LangOpts.Blocks) |
| 6100 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6101 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6102 | // Ensure that CurBlock is deleted. |
| 6103 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6104 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6105 | PopDeclContext(); |
| 6106 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6107 | // Pop off CurBlock, handle nested blocks. |
| 6108 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6109 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6110 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6111 | if (!BSI->ReturnType.isNull()) |
| 6112 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6113 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6114 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6115 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6116 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6117 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6118 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6119 | QualType BlockTy; |
| 6120 | if (!BSI->hasPrototype) |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6121 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6122 | NoReturn); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6123 | else |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6124 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6125 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6126 | NoReturn); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6127 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6128 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6129 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6130 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6131 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6132 | // If needed, diagnose invalid gotos and switches in the block. |
| 6133 | if (CurFunctionNeedsScopeChecking) |
| 6134 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6135 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6136 | |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6137 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6138 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6139 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6140 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6143 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6144 | ExprArg expr, TypeTy *type, |
| 6145 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6146 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6147 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6148 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6149 | |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6150 | InitBuiltinVaListType(); |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6151 | |
| 6152 | // Get the va_list type |
| 6153 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6154 | if (VaListType->isArrayType()) { |
| 6155 | // Deal with implicit array decay; for example, on x86-64, |
| 6156 | // va_list is an array, but it's supposed to decay to |
| 6157 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6158 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6159 | // Make sure the input expression also decays appropriately. |
| 6160 | UsualUnaryConversions(E); |
| 6161 | } else { |
| 6162 | // Otherwise, the va_list argument must be an l-value because |
| 6163 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6164 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6165 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6166 | return ExprError(); |
| 6167 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6168 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6169 | if (!E->isTypeDependent() && |
| 6170 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6171 | return ExprError(Diag(E->getLocStart(), |
| 6172 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6173 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6174 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6175 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6176 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6177 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6178 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6179 | expr.release(); |
| 6180 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6181 | RPLoc)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6182 | } |
| 6183 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6184 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6185 | // The type of __null will be int or long, depending on the size of |
| 6186 | // pointers on the target. |
| 6187 | QualType Ty; |
| 6188 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6189 | Ty = Context.IntTy; |
| 6190 | else |
| 6191 | Ty = Context.LongTy; |
| 6192 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6193 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6194 | } |
| 6195 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6196 | static void |
| 6197 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6198 | QualType DstType, |
| 6199 | Expr *SrcExpr, |
| 6200 | CodeModificationHint &Hint) { |
| 6201 | if (!SemaRef.getLangOptions().ObjC1) |
| 6202 | return; |
| 6203 | |
| 6204 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6205 | if (!PT) |
| 6206 | return; |
| 6207 | |
| 6208 | // Check if the destination is of type 'id'. |
| 6209 | if (!PT->isObjCIdType()) { |
| 6210 | // Check if the destination is the 'NSString' interface. |
| 6211 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6212 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6213 | return; |
| 6214 | } |
| 6215 | |
| 6216 | // Strip off any parens and casts. |
| 6217 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6218 | if (!SL || SL->isWide()) |
| 6219 | return; |
| 6220 | |
| 6221 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6222 | } |
| 6223 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6224 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6225 | SourceLocation Loc, |
| 6226 | QualType DstType, QualType SrcType, |
| 6227 | Expr *SrcExpr, const char *Flavor) { |
| 6228 | // Decode the result (notice that AST's are still created for extensions). |
| 6229 | bool isInvalid = false; |
| 6230 | unsigned DiagKind; |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6231 | CodeModificationHint Hint; |
| 6232 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6233 | switch (ConvTy) { |
| 6234 | default: assert(0 && "Unknown conversion type"); |
| 6235 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6236 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6237 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6238 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6239 | case IntToPointer: |
| 6240 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6241 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6242 | case IncompatiblePointer: |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6243 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6244 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6245 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6246 | case IncompatiblePointerSign: |
| 6247 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6248 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6249 | case FunctionVoidPointer: |
| 6250 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6251 | break; |
| 6252 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 6253 | // If the qualifiers lost were because we were applying the |
| 6254 | // (deprecated) C++ conversion from a string literal to a char* |
| 6255 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 6256 | // Ideally, this check would be performed in |
| 6257 | // CheckPointerTypesForAssignment. However, that would require a |
| 6258 | // bit of refactoring (so that the second argument is an |
| 6259 | // expression, rather than a type), which should be done as part |
| 6260 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 6261 | // C++ semantics. |
| 6262 | if (getLangOptions().CPlusPlus && |
| 6263 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 6264 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6265 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 6266 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 6267 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 6268 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6269 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6270 | case IntToBlockPointer: |
| 6271 | DiagKind = diag::err_int_to_block_pointer; |
| 6272 | break; |
| 6273 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 6274 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6275 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6276 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6277 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6278 | // it can give a more specific diagnostic. |
| 6279 | DiagKind = diag::warn_incompatible_qualified_id; |
| 6280 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6281 | case IncompatibleVectors: |
| 6282 | DiagKind = diag::warn_incompatible_vectors; |
| 6283 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6284 | case Incompatible: |
| 6285 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 6286 | isInvalid = true; |
| 6287 | break; |
| 6288 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6289 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6290 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6291 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6292 | return isInvalid; |
| 6293 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6294 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 6295 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6296 | llvm::APSInt ICEResult; |
| 6297 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 6298 | if (Result) |
| 6299 | *Result = ICEResult; |
| 6300 | return false; |
| 6301 | } |
| 6302 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6303 | Expr::EvalResult EvalResult; |
| 6304 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6305 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6306 | EvalResult.HasSideEffects) { |
| 6307 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 6308 | |
| 6309 | if (EvalResult.Diag) { |
| 6310 | // We only show the note if it's not the usual "invalid subexpression" |
| 6311 | // or if it's actually in a subexpression. |
| 6312 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 6313 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 6314 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 6315 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6316 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6317 | return true; |
| 6318 | } |
| 6319 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6320 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 6321 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6322 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6323 | if (EvalResult.Diag && |
| 6324 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 6325 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6326 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6327 | if (Result) |
| 6328 | *Result = EvalResult.Val.getInt(); |
| 6329 | return false; |
| 6330 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6331 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6332 | Sema::ExpressionEvaluationContext |
| 6333 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6334 | // Introduce a new set of potentially referenced declarations to the stack. |
| 6335 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 6336 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6338 | std::swap(ExprEvalContext, NewContext); |
| 6339 | return NewContext; |
| 6340 | } |
| 6341 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6342 | void |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6343 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 6344 | ExpressionEvaluationContext NewContext) { |
| 6345 | ExprEvalContext = NewContext; |
| 6346 | |
| 6347 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 6348 | // Mark any remaining declarations in the current position of the stack |
| 6349 | // as "referenced". If they were not meant to be referenced, semantic |
| 6350 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6351 | PotentiallyReferencedDecls RemainingDecls; |
| 6352 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 6353 | PotentiallyReferencedDeclStack.pop_back(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6354 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6355 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 6356 | IEnd = RemainingDecls.end(); |
| 6357 | I != IEnd; ++I) |
| 6358 | MarkDeclarationReferenced(I->first, I->second); |
| 6359 | } |
| 6360 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6361 | |
| 6362 | /// \brief Note that the given declaration was referenced in the source code. |
| 6363 | /// |
| 6364 | /// This routine should be invoke whenever a given declaration is referenced |
| 6365 | /// in the source code, and where that reference occurred. If this declaration |
| 6366 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 6367 | /// C99 6.9p3), then the declaration will be marked as used. |
| 6368 | /// |
| 6369 | /// \param Loc the location where the declaration was referenced. |
| 6370 | /// |
| 6371 | /// \param D the declaration that has been referenced by the source code. |
| 6372 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 6373 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6374 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6375 | if (D->isUsed()) |
| 6376 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6377 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 6378 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 6379 | // template or not. The reason for this is that unevaluated expressions |
| 6380 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 6381 | // -Wunused-parameters) |
| 6382 | if (isa<ParmVarDecl>(D) || |
| 6383 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6384 | D->setUsed(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6385 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6386 | // Do not mark anything as "used" within a dependent context; wait for |
| 6387 | // an instantiation. |
| 6388 | if (CurContext->isDependentContext()) |
| 6389 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6390 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6391 | switch (ExprEvalContext) { |
| 6392 | case Unevaluated: |
| 6393 | // We are in an expression that is not potentially evaluated; do nothing. |
| 6394 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6395 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6396 | case PotentiallyEvaluated: |
| 6397 | // We are in a potentially-evaluated expression, so this declaration is |
| 6398 | // "used"; handle this below. |
| 6399 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6400 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6401 | case PotentiallyPotentiallyEvaluated: |
| 6402 | // We are in an expression that may be potentially evaluated; queue this |
| 6403 | // declaration reference until we know whether the expression is |
| 6404 | // potentially evaluated. |
| 6405 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 6406 | return; |
| 6407 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6408 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6409 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 6410 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6411 | unsigned TypeQuals; |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6412 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 6413 | if (!Constructor->isUsed()) |
| 6414 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6415 | } else if (Constructor->isImplicit() && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6416 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6417 | if (!Constructor->isUsed()) |
| 6418 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 6419 | } |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6420 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 6421 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 6422 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6423 | |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6424 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 6425 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 6426 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 6427 | if (!MethodDecl->isUsed()) |
| 6428 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 6429 | } |
| 6430 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 6431 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6432 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6433 | // class templates. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6434 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6435 | bool AlreadyInstantiated = false; |
| 6436 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 6437 | = Function->getTemplateSpecializationInfo()) { |
| 6438 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 6439 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6440 | else if (SpecInfo->getTemplateSpecializationKind() |
| 6441 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6442 | AlreadyInstantiated = true; |
| 6443 | } else if (MemberSpecializationInfo *MSInfo |
| 6444 | = Function->getMemberSpecializationInfo()) { |
| 6445 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 6446 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6447 | else if (MSInfo->getTemplateSpecializationKind() |
| 6448 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6449 | AlreadyInstantiated = true; |
| 6450 | } |
| 6451 | |
| 6452 | if (!AlreadyInstantiated) |
| 6453 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
| 6454 | } |
| 6455 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6456 | // FIXME: keep track of references to static functions |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6457 | Function->setUsed(true); |
| 6458 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6459 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6460 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6461 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6462 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6463 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6464 | Var->getInstantiatedFromStaticDataMember()) { |
| 6465 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 6466 | assert(MSInfo && "Missing member specialization information?"); |
| 6467 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 6468 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 6469 | MSInfo->setPointOfInstantiation(Loc); |
| 6470 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 6471 | } |
| 6472 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6473 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6474 | // FIXME: keep track of references to static data? |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6475 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6476 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6477 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 6478 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6479 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 6480 | |
| 6481 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 6482 | CallExpr *CE, FunctionDecl *FD) { |
| 6483 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 6484 | return false; |
| 6485 | |
| 6486 | PartialDiagnostic Note = |
| 6487 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 6488 | << FD->getDeclName() : PDiag(); |
| 6489 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 6490 | |
| 6491 | if (RequireCompleteType(Loc, ReturnType, |
| 6492 | FD ? |
| 6493 | PDiag(diag::err_call_function_incomplete_return) |
| 6494 | << CE->getSourceRange() << FD->getDeclName() : |
| 6495 | PDiag(diag::err_call_incomplete_return) |
| 6496 | << CE->getSourceRange(), |
| 6497 | std::make_pair(NoteLoc, Note))) |
| 6498 | return true; |
| 6499 | |
| 6500 | return false; |
| 6501 | } |
| 6502 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6503 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 6504 | // will prevent this condition from triggering, which is what we want. |
| 6505 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 6506 | SourceLocation Loc; |
| 6507 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6508 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 6509 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6510 | if (isa<BinaryOperator>(E)) { |
| 6511 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 6512 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 6513 | return; |
| 6514 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 6515 | // Greylist some idioms by putting them into a warning subcategory. |
| 6516 | if (ObjCMessageExpr *ME |
| 6517 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 6518 | Selector Sel = ME->getSelector(); |
| 6519 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 6520 | // self = [<foo> init...] |
| 6521 | if (isSelfExpr(Op->getLHS()) |
| 6522 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 6523 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 6524 | |
| 6525 | // <foo> = [<bar> nextObject] |
| 6526 | else if (Sel.isUnarySelector() && |
| 6527 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 6528 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 6529 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6530 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6531 | Loc = Op->getOperatorLoc(); |
| 6532 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 6533 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 6534 | if (Op->getOperator() != OO_Equal) |
| 6535 | return; |
| 6536 | |
| 6537 | Loc = Op->getOperatorLoc(); |
| 6538 | } else { |
| 6539 | // Not an assignment. |
| 6540 | return; |
| 6541 | } |
| 6542 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6543 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | e724ae9 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 6544 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6545 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6546 | Diag(Loc, diagnostic) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6547 | << E->getSourceRange() |
| 6548 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 6549 | << CodeModificationHint::CreateInsertion(Close, ")"); |
| 6550 | } |
| 6551 | |
| 6552 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 6553 | DiagnoseAssignmentAsCondition(E); |
| 6554 | |
| 6555 | if (!E->isTypeDependent()) { |
| 6556 | DefaultFunctionArrayConversion(E); |
| 6557 | |
| 6558 | QualType T = E->getType(); |
| 6559 | |
| 6560 | if (getLangOptions().CPlusPlus) { |
| 6561 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 6562 | return true; |
| 6563 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 6564 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 6565 | << T << E->getSourceRange(); |
| 6566 | return true; |
| 6567 | } |
| 6568 | } |
| 6569 | |
| 6570 | return false; |
| 6571 | } |