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" |
John McCall | 5cebab1 | 2009-11-18 07:57:50 +0000 | [diff] [blame] | 15 | #include "Lookup.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 21 | #include "clang/Basic/PartialDiagnostic.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 22 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 24 | #include "clang/Lex/LiteralSupport.h" |
| 25 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 26 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 07d754a | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 27 | #include "clang/Parse/Designator.h" |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 28 | #include "clang/Parse/Scope.h" |
Douglas Gregor | b53edfb | 2009-11-10 19:49:08 +0000 | [diff] [blame] | 29 | #include "clang/Parse/Template.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 33 | /// \brief Determine whether the use of this declaration is valid, and |
| 34 | /// emit any corresponding diagnostics. |
| 35 | /// |
| 36 | /// This routine diagnoses various problems with referencing |
| 37 | /// declarations that can occur when using a declaration. For example, |
| 38 | /// it might warn if a deprecated or unavailable declaration is being |
| 39 | /// used, or produce an error (and return true) if a C++0x deleted |
| 40 | /// function is being used. |
| 41 | /// |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 42 | /// If IgnoreDeprecated is set to true, this should not want about deprecated |
| 43 | /// decls. |
| 44 | /// |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 45 | /// \returns true if there was an error (this declaration cannot be |
| 46 | /// referenced), false otherwise. |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 47 | /// |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 48 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 49 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 50 | if (D->getAttr<DeprecatedAttr>()) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 51 | EmitDeprecationWarning(D, Loc); |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | a27dd59 | 2009-10-25 17:21:40 +0000 | [diff] [blame] | 54 | // See if the decl is unavailable |
| 55 | if (D->getAttr<UnavailableAttr>()) { |
| 56 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
| 57 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 58 | } |
| 59 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 60 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 61 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 62 | if (FD->isDeleted()) { |
| 63 | Diag(Loc, diag::err_deleted_function_use); |
| 64 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 65 | return true; |
| 66 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 67 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 68 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 69 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 72 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 74 | /// attribute. It warns if call does not have the sentinel argument. |
| 75 | /// |
| 76 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 78 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 80 | return; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 81 | int sentinelPos = attr->getSentinel(); |
| 82 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 84 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 85 | // 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] | 86 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 87 | bool warnNotEnoughArgs = false; |
| 88 | int isMethod = 0; |
| 89 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 90 | // skip over named parameters. |
| 91 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 92 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 93 | if (nullPos) |
| 94 | --nullPos; |
| 95 | else |
| 96 | ++i; |
| 97 | } |
| 98 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 99 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 100 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 101 | // skip over named parameters. |
| 102 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 103 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 104 | if (nullPos) |
| 105 | --nullPos; |
| 106 | else |
| 107 | ++i; |
| 108 | } |
| 109 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 110 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 111 | // block or function pointer call. |
| 112 | QualType Ty = V->getType(); |
| 113 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 115 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 116 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 117 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 118 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 119 | unsigned k; |
| 120 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 121 | if (nullPos) |
| 122 | --nullPos; |
| 123 | else |
| 124 | ++i; |
| 125 | } |
| 126 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 127 | } |
| 128 | if (Ty->isBlockPointerType()) |
| 129 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 130 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 131 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 132 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 133 | return; |
| 134 | |
| 135 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 136 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 137 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | int sentinel = i; |
| 141 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 142 | --sentinelPos; |
| 143 | ++i; |
| 144 | } |
| 145 | if (sentinelPos > 0) { |
| 146 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 147 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | while (i < NumArgs-1) { |
| 151 | ++i; |
| 152 | ++sentinel; |
| 153 | } |
| 154 | Expr *sentinelExpr = Args[sentinel]; |
Anders Carlsson | 0b11a3e | 2009-11-24 17:24:21 +0000 | [diff] [blame] | 155 | if (sentinelExpr && (!isa<GNUNullExpr>(sentinelExpr) && |
| 156 | (!sentinelExpr->getType()->isPointerType() || |
| 157 | !sentinelExpr->isNullPointerConstant(Context, |
| 158 | Expr::NPC_ValueDependentIsNull)))) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 159 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 160 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 161 | } |
| 162 | return; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 165 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 166 | Expr *Ex = (Expr *)E; |
| 167 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 168 | } |
| 169 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 170 | //===----------------------------------------------------------------------===// |
| 171 | // Standard Promotions and Conversions |
| 172 | //===----------------------------------------------------------------------===// |
| 173 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 174 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 175 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 176 | QualType Ty = E->getType(); |
| 177 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 178 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 179 | if (Ty->isFunctionType()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | 6904f64 | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 181 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 182 | else if (Ty->isArrayType()) { |
| 183 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 184 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 185 | // type 'array of type' is converted to an expression that has type 'pointer |
| 186 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 187 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 188 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 189 | // |
| 190 | // C++ 4.2p1: |
| 191 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 192 | // T" can be converted to an rvalue of type "pointer to T". |
| 193 | // |
| 194 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 195 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 8fc489d | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 196 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 197 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 198 | } |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 203 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 204 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 205 | /// In these instances, this routine should *not* be called. |
| 206 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 207 | QualType Ty = Expr->getType(); |
| 208 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 210 | // C99 6.3.1.1p2: |
| 211 | // |
| 212 | // The following may be used in an expression wherever an int or |
| 213 | // unsigned int may be used: |
| 214 | // - an object or expression with an integer type whose integer |
| 215 | // conversion rank is less than or equal to the rank of int |
| 216 | // and unsigned int. |
| 217 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 218 | // |
| 219 | // If an int can represent all values of the original type, the |
| 220 | // value is converted to an int; otherwise, it is converted to an |
| 221 | // unsigned int. These are called the integer promotions. All |
| 222 | // other types are unchanged by the integer promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 223 | QualType PTy = Context.isPromotableBitField(Expr); |
| 224 | if (!PTy.isNull()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 225 | ImpCastExprToType(Expr, PTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 226 | return Expr; |
| 227 | } |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 228 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 229 | QualType PT = Context.getPromotedIntegerType(Ty); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 230 | ImpCastExprToType(Expr, PT, CastExpr::CK_IntegralCast); |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 231 | return Expr; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Douglas Gregor | 8d9c509 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 234 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 235 | return Expr; |
| 236 | } |
| 237 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 238 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | /// 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] | 240 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 241 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 242 | QualType Ty = Expr->getType(); |
| 243 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 244 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 245 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 246 | if (const BuiltinType *BT = Ty->getAs<BuiltinType>()) |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 247 | if (BT->getKind() == BuiltinType::Float) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 248 | return ImpCastExprToType(Expr, Context.DoubleTy, |
| 249 | CastExpr::CK_FloatingCast); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 251 | UsualUnaryConversions(Expr); |
| 252 | } |
| 253 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 254 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 255 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 256 | /// interfaces passed by value. This returns true if the argument type is |
| 257 | /// completely illegal. |
| 258 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 259 | DefaultArgumentPromotion(Expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 260 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 261 | if (Expr->getType()->isObjCInterfaceType()) { |
| 262 | Diag(Expr->getLocStart(), |
| 263 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 264 | << Expr->getType() << CT; |
| 265 | return true; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 266 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 268 | if (!Expr->getType()->isPODType()) |
| 269 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 270 | << Expr->getType() << CT; |
| 271 | |
| 272 | return false; |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 276 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 277 | /// 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] | 278 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | /// responsible for emitting appropriate error diagnostics. |
| 280 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 281 | /// GCC. |
| 282 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 283 | bool isCompAssign) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 284 | if (!isCompAssign) |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 285 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 286 | |
| 287 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 288 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 290 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 291 | QualType lhs = |
| 292 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | QualType rhs = |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 294 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 295 | |
| 296 | // If both types are identical, no conversion is needed. |
| 297 | if (lhs == rhs) |
| 298 | return lhs; |
| 299 | |
| 300 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 301 | // The caller can deal with this (e.g. pointer + int). |
| 302 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 303 | return lhs; |
| 304 | |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 305 | // Perform bitfield promotions. |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 306 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 307 | if (!LHSBitfieldPromoteTy.isNull()) |
| 308 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 309 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 310 | if (!RHSBitfieldPromoteTy.isNull()) |
| 311 | rhs = RHSBitfieldPromoteTy; |
| 312 | |
Eli Friedman | 5ae98ee | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 313 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 314 | if (!isCompAssign) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 315 | ImpCastExprToType(lhsExpr, destType, CastExpr::CK_Unknown); |
| 316 | ImpCastExprToType(rhsExpr, destType, CastExpr::CK_Unknown); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 317 | return destType; |
| 318 | } |
| 319 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 320 | //===----------------------------------------------------------------------===// |
| 321 | // Semantic Analysis for various Expression Types |
| 322 | //===----------------------------------------------------------------------===// |
| 323 | |
| 324 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 325 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 326 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 327 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 328 | /// multiple tokens. However, the common case is that StringToks points to one |
| 329 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 330 | /// |
| 331 | Action::OwningExprResult |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 332 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 333 | assert(NumStringToks && "Must have at least one string!"); |
| 334 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 335 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 336 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 337 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 339 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 340 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 341 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 343 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | cbad725 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 344 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 345 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 346 | |
| 347 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 348 | if (getLangOptions().CPlusPlus) |
| 349 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 351 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 352 | // the nul terminator character as well as the string length for pascal |
| 353 | // strings. |
| 354 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 355 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 356 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 358 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 359 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | f83b5af | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 360 | Literal.GetStringLength(), |
| 361 | Literal.AnyWide, StrTy, |
| 362 | &StringTokLocs[0], |
| 363 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 366 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 367 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 368 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 369 | /// for values inside the block or for globals). |
| 370 | /// |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 371 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 372 | /// up-to-date. |
| 373 | /// |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 374 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 375 | ValueDecl *VD) { |
| 376 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 377 | // we wanted to. |
| 378 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 379 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 381 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 382 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 383 | return false; |
| 384 | |
| 385 | // If this is a reference to an extern, static, or global variable, no need to |
| 386 | // snapshot it. |
| 387 | // FIXME: What about 'const' variables in C++? |
| 388 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 389 | if (!Var->hasLocalStorage()) |
| 390 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 391 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 392 | // Blocks that have these can't be constant. |
| 393 | CurBlock->hasBlockDeclRefExprs = true; |
| 394 | |
| 395 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 396 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 397 | // be defined outside all of the current blocks (in which case the blocks do |
| 398 | // all get the bit). Walk the nesting chain. |
| 399 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 400 | NextBlock = NextBlock->PrevBlockInfo) { |
| 401 | // If we found the defining block for the variable, don't mark the block as |
| 402 | // having a reference outside it. |
| 403 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 404 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 406 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 407 | // a snapshot as well. |
| 408 | NextBlock->hasBlockDeclRefExprs = true; |
| 409 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 410 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 411 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 414 | |
| 415 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 416 | /// BuildDeclRefExpr - Build a DeclRefExpr. |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 417 | Sema::OwningExprResult |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 418 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 419 | const CXXScopeSpec *SS) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 420 | assert(!isa<OverloadedFunctionDecl>(D)); |
| 421 | |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 422 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 423 | Diag(Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | 364035d1 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 425 | << D->getDeclName(); |
| 426 | return ExprError(); |
| 427 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 429 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 430 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 431 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 432 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 434 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | 946b86d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 436 | << D->getIdentifier(); |
| 437 | return ExprError(); |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 443 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 445 | return Owned(DeclRefExpr::Create(Context, |
| 446 | SS? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
| 447 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 448 | D, Loc, Ty)); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 451 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 452 | /// variable corresponding to the anonymous union or struct whose type |
| 453 | /// is Record. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 454 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 455 | RecordDecl *Record) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 457 | "Record must be an anonymous struct or union!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 458 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 459 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 460 | // operation rather than a slow walk through DeclContext's vector (which |
| 461 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 462 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 463 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 464 | DEnd = Ctx->decls_end(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 465 | D != DEnd; ++D) { |
| 466 | if (*D == Record) { |
| 467 | // The object for the anonymous struct/union directly |
| 468 | // follows its type in the list of declarations. |
| 469 | ++D; |
| 470 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 6e6ad60 | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 471 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 472 | return *D; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | assert(false && "Missing object for anonymous record"); |
| 477 | return 0; |
| 478 | } |
| 479 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 480 | /// \brief Given a field that represents a member of an anonymous |
| 481 | /// struct/union, build the path from that field's context to the |
| 482 | /// actual member. |
| 483 | /// |
| 484 | /// Construct the sequence of field member references we'll have to |
| 485 | /// perform to get to the field in the anonymous union/struct. The |
| 486 | /// list of members is built from the field outward, so traverse it |
| 487 | /// backwards to go from an object in the current context to the field |
| 488 | /// we found. |
| 489 | /// |
| 490 | /// \returns The variable from which the field access should begin, |
| 491 | /// for an anonymous struct/union that is not a member of another |
| 492 | /// class. Otherwise, returns NULL. |
| 493 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 494 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 495 | assert(Field->getDeclContext()->isRecord() && |
| 496 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 497 | && "Field must be stored inside an anonymous struct or union"); |
| 498 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 499 | Path.push_back(Field); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 500 | VarDecl *BaseObject = 0; |
| 501 | DeclContext *Ctx = Field->getDeclContext(); |
| 502 | do { |
| 503 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 504 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 505 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 506 | Path.push_back(AnonField); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 507 | else { |
| 508 | BaseObject = cast<VarDecl>(AnonObject); |
| 509 | break; |
| 510 | } |
| 511 | Ctx = Ctx->getParent(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | } while (Ctx->isRecord() && |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 513 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 514 | |
| 515 | return BaseObject; |
| 516 | } |
| 517 | |
| 518 | Sema::OwningExprResult |
| 519 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 520 | FieldDecl *Field, |
| 521 | Expr *BaseObjectExpr, |
| 522 | SourceLocation OpLoc) { |
| 523 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 525 | AnonFields); |
| 526 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 527 | // Build the expression that refers to the base object, from |
| 528 | // which we will build a sequence of member references to each |
| 529 | // of the anonymous union objects and, eventually, the field we |
| 530 | // found via name lookup. |
| 531 | bool BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 532 | Qualifiers BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 533 | if (BaseObject) { |
| 534 | // BaseObject is an anonymous struct/union variable (and is, |
| 535 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 536 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 537 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 538 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 539 | SourceLocation()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 540 | BaseQuals |
| 541 | = Context.getCanonicalType(BaseObject->getType()).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 542 | } else if (BaseObjectExpr) { |
| 543 | // The caller provided the base object expression. Determine |
| 544 | // whether its a pointer and whether it adds any qualifiers to the |
| 545 | // anonymous struct/union fields we're looking into. |
| 546 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 547 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 548 | BaseObjectIsPointer = true; |
| 549 | ObjectType = ObjectPtr->getPointeeType(); |
| 550 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 551 | BaseQuals |
| 552 | = Context.getCanonicalType(ObjectType).getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 553 | } else { |
| 554 | // We've found a member of an anonymous struct/union that is |
| 555 | // inside a non-anonymous struct/union, so in a well-formed |
| 556 | // program our base object expression is "this". |
| 557 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 558 | if (!MD->isStatic()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | QualType AnonFieldType |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 560 | = Context.getTagDeclType( |
| 561 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 562 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 563 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 564 | == Context.getCanonicalType(ThisType)) || |
| 565 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 566 | // Our base object expression is "this". |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 567 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 568 | MD->getThisType(Context)); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 569 | BaseObjectIsPointer = true; |
| 570 | } |
| 571 | } else { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 572 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 573 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 574 | } |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 575 | BaseQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | if (!BaseObjectExpr) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 579 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 580 | << Field->getDeclName()); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | // Build the implicit member references to the field of the |
| 584 | // anonymous struct/union. |
| 585 | Expr *Result = BaseObjectExpr; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 586 | Qualifiers ResultQuals = BaseQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 587 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 588 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 589 | FI != FIEnd; ++FI) { |
| 590 | QualType MemberType = (*FI)->getType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 591 | Qualifiers MemberTypeQuals = |
| 592 | Context.getCanonicalType(MemberType).getQualifiers(); |
| 593 | |
| 594 | // CVR attributes from the base are picked up by members, |
| 595 | // except that 'mutable' members don't pick up 'const'. |
| 596 | if ((*FI)->isMutable()) |
| 597 | ResultQuals.removeConst(); |
| 598 | |
| 599 | // GC attributes are never picked up by members. |
| 600 | ResultQuals.removeObjCGCAttr(); |
| 601 | |
| 602 | // TR 18037 does not allow fields to be declared with address spaces. |
| 603 | assert(!MemberTypeQuals.hasAddressSpace()); |
| 604 | |
| 605 | Qualifiers NewQuals = ResultQuals + MemberTypeQuals; |
| 606 | if (NewQuals != MemberTypeQuals) |
| 607 | MemberType = Context.getQualifiedType(MemberType, NewQuals); |
| 608 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 609 | MarkDeclarationReferenced(Loc, *FI); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 610 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 611 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 612 | OpLoc, MemberType); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 613 | BaseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 614 | ResultQuals = NewQuals; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 617 | return Owned(Result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 620 | Sema::OwningExprResult Sema::ActOnIdExpression(Scope *S, |
| 621 | const CXXScopeSpec &SS, |
| 622 | UnqualifiedId &Name, |
| 623 | bool HasTrailingLParen, |
| 624 | bool IsAddressOfOperand) { |
John McCall | a9ee325 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 625 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
| 626 | "cannot be direct & operand and have a trailing lparen"); |
| 627 | |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 628 | if (Name.getKind() == UnqualifiedId::IK_TemplateId) { |
| 629 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 630 | Name.TemplateId->getTemplateArgs(), |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 631 | Name.TemplateId->NumArgs); |
| 632 | return ActOnTemplateIdExpr(SS, |
| 633 | TemplateTy::make(Name.TemplateId->Template), |
| 634 | Name.TemplateId->TemplateNameLoc, |
| 635 | Name.TemplateId->LAngleLoc, |
| 636 | TemplateArgsPtr, |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 637 | Name.TemplateId->RAngleLoc); |
| 638 | } |
| 639 | |
| 640 | // FIXME: We lose a bunch of source information by doing this. Later, |
| 641 | // we'll want to merge ActOnDeclarationNameExpr's logic into |
| 642 | // ActOnIdExpression. |
| 643 | return ActOnDeclarationNameExpr(S, |
| 644 | Name.StartLocation, |
| 645 | GetNameFromUnqualifiedId(Name), |
| 646 | HasTrailingLParen, |
| 647 | &SS, |
| 648 | IsAddressOfOperand); |
| 649 | } |
| 650 | |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 651 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 652 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 653 | /// performs lookup on that name and returns an expression that refers |
| 654 | /// to that name. This routine isn't directly called from the parser, |
| 655 | /// because the parser doesn't know about DeclarationName. Rather, |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 656 | /// this routine is called by ActOnIdExpression, which contains a |
| 657 | /// parsed UnqualifiedId. |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 658 | /// |
| 659 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 660 | /// function call context. LookupCtx is only used for a C++ |
| 661 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 662 | /// the identifier must be a member of. |
Douglas Gregor | b0846b0 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 663 | /// |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 664 | /// isAddressOfOperand means that this expression is the direct operand |
| 665 | /// of an address-of operator. This matters because this is the only |
| 666 | /// situation where a qualified name referencing a non-static member may |
| 667 | /// appear outside a member function of this class. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 668 | Sema::OwningExprResult |
| 669 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 670 | DeclarationName Name, bool HasTrailingLParen, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 671 | const CXXScopeSpec *SS, |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 672 | bool isAddressOfOperand) { |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 673 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 674 | if (SS && SS->isInvalid()) |
| 675 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | 601f4f0 | 2009-11-23 12:39:54 +0000 | [diff] [blame] | 677 | // Determine whether this is a member of an unknown specialization. |
| 678 | if (SS && SS->isSet() && !computeDeclContext(*SS, false)) { |
John McCall | 8cd7813 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 679 | return Owned(new (Context) DependentScopeDeclRefExpr(Name, Context.DependentTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | Loc, SS->getRange(), |
Anders Carlsson | 03f89b1 | 2009-07-09 00:05:08 +0000 | [diff] [blame] | 681 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()), |
| 682 | isAddressOfOperand)); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 685 | LookupResult Lookup(*this, Name, Loc, LookupOrdinaryName); |
| 686 | LookupParsedName(Lookup, S, SS, true); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 687 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 688 | if (Lookup.isAmbiguous()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 689 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 690 | |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 691 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 692 | // well. |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 693 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 694 | if (II && getCurMethodDecl()) { |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 695 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 696 | // 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] | 697 | // found a decl, but that decl is outside the current instance method (i.e. |
| 698 | // 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] | 699 | // this name, if the lookup sucedes, we replace it our current decl. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 700 | |
| 701 | // FIXME: we should change lookup to do this. |
| 702 | |
| 703 | // If we're in a class method, we don't normally want to look for |
| 704 | // ivars. But if we don't find anything else, and there's an |
| 705 | // ivar, that's an error. |
| 706 | bool IsClassMethod = getCurMethodDecl()->isClassMethod(); |
| 707 | |
| 708 | bool LookForIvars; |
| 709 | if (Lookup.empty()) |
| 710 | LookForIvars = true; |
| 711 | else if (IsClassMethod) |
| 712 | LookForIvars = false; |
| 713 | else |
| 714 | LookForIvars = (Lookup.isSingleResult() && |
| 715 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| 716 | |
| 717 | if (LookForIvars) { |
Argyrios Kyrtzidis | 853fbea | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 718 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 719 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 720 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 721 | // Diagnose using an ivar in a class method. |
| 722 | if (IsClassMethod) |
| 723 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 724 | << IV->getDeclName()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 726 | // If we're referencing an invalid decl, just return this as a silent |
| 727 | // error node. The error diagnostic was already emitted on the decl. |
| 728 | if (IV->isInvalidDecl()) |
| 729 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 731 | // Check if referencing a field with __attribute__((deprecated)). |
| 732 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 733 | return ExprError(); |
| 734 | |
| 735 | // Diagnose the use of an ivar outside of the declaring class. |
| 736 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 737 | ClassDeclared != IFace) |
| 738 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 739 | |
| 740 | // FIXME: This should use a new expr for a direct reference, don't |
| 741 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 742 | IdentifierInfo &II = Context.Idents.get("self"); |
| 743 | UnqualifiedId SelfName; |
| 744 | SelfName.setIdentifier(&II, SourceLocation()); |
| 745 | CXXScopeSpec SelfScopeSpec; |
| 746 | OwningExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| 747 | SelfName, false, false); |
| 748 | MarkDeclarationReferenced(Loc, IV); |
| 749 | return Owned(new (Context) |
| 750 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 751 | SelfExpr.takeAs<Expr>(), true, true)); |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 752 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 753 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 754 | // We should warn if a local variable hides an ivar. |
| 755 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 756 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 757 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 758 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 759 | IFace == ClassDeclared) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 760 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 761 | } |
Fariborz Jahanian | bf8e842 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 762 | } |
Steve Naroff | 0d7c6db | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 763 | // Needed to implement property "super.method" notation. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 764 | if (Lookup.empty() && II->isStr("super")) { |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 765 | QualType T; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 766 | |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 767 | if (getCurMethodDecl()->isInstanceMethod()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 768 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 769 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | e29c4dd | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 770 | else |
| 771 | T = Context.getObjCClassType(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 772 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 773 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 774 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 775 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 776 | // Determine whether this name might be a candidate for |
| 777 | // argument-dependent lookup. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 778 | bool ADL = UseArgumentDependentLookup(SS, Lookup, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 779 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 780 | if (Lookup.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 781 | // Otherwise, this could be an implicitly declared function reference (legal |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 782 | // in C90, extension in C99). |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 783 | if (HasTrailingLParen && II && |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 784 | !getLangOptions().CPlusPlus) { // Not in C++. |
| 785 | NamedDecl *D = ImplicitlyDefineFunction(Loc, *II, S); |
| 786 | if (D) Lookup.addDecl(D); |
| 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 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 803 | if (VarDecl *Var = Lookup.getAsSingle<VarDecl>()) { |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 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 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 826 | } else if (FunctionDecl *Func = Lookup.getAsSingle<FunctionDecl>()) { |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 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()); |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 840 | return BuildDeclRefExpr(Func, NoProtoType, Loc, SS); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 843 | |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 844 | // &SomeClass::foo is an abstract member reference, regardless of |
| 845 | // the nature of foo, but &SomeClass::foo(...) is not. If this is |
| 846 | // *not* an abstract member reference, and any of the results is a |
| 847 | // class member (which necessarily means they're all class members), |
| 848 | // then we make an implicit member reference instead. |
| 849 | // |
| 850 | // This check considers all the same information as the "needs ADL" |
| 851 | // check, but there's no simple logical relationship other than the |
| 852 | // fact that they can never be simultaneously true. We could |
| 853 | // calculate them both in one pass if that proves important for |
| 854 | // performance. |
| 855 | if (!ADL) { |
| 856 | bool isAbstractMemberPointer = |
John McCall | a9ee325 | 2009-11-22 02:49:43 +0000 | [diff] [blame] | 857 | (isAddressOfOperand && SS && !SS->isEmpty()); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 858 | |
| 859 | if (!isAbstractMemberPointer && !Lookup.empty() && |
| 860 | isa<CXXRecordDecl>((*Lookup.begin())->getDeclContext())) { |
| 861 | return BuildImplicitMemberReferenceExpr(SS, Lookup); |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | assert(Lookup.getResultKind() != LookupResult::FoundUnresolvedValue && |
| 866 | "found UnresolvedUsingValueDecl in non-class scope"); |
| 867 | |
| 868 | return BuildDeclarationNameExpr(SS, Lookup, ADL); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 869 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 870 | |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 871 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 872 | bool |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 873 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 874 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 876 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 877 | QualType DestType = |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 878 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 879 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 880 | return false; |
| 881 | QualType FromRecordType = From->getType(); |
| 882 | QualType DestRecordType = DestType; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 883 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 884 | DestType = Context.getPointerType(DestType); |
| 885 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 886 | } |
Fariborz Jahanian | 4b12ed1 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 887 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 888 | CheckDerivedToBaseConversion(FromRecordType, |
| 889 | DestRecordType, |
| 890 | From->getSourceRange().getBegin(), |
| 891 | From->getSourceRange())) |
| 892 | return true; |
Anders Carlsson | a076d14 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 893 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 894 | /*isLvalue=*/true); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 895 | } |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 896 | return false; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 897 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 899 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
| 901 | const CXXScopeSpec *SS, NamedDecl *Member, |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 902 | SourceLocation Loc, QualType Ty) { |
| 903 | if (SS && SS->isSet()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | return MemberExpr::Create(C, Base, isArrow, |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 905 | (NestedNameSpecifier *)SS->getScopeRep(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 906 | SS->getRange(), Member, Loc, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 907 | // FIXME: Explicit template argument lists |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 908 | 0, Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 910 | return new (C) MemberExpr(Base, isArrow, Member, Loc, Ty); |
| 911 | } |
| 912 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 913 | /// Builds an implicit member access expression from the given |
| 914 | /// unqualified lookup set, which is known to contain only class |
| 915 | /// members. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 916 | Sema::OwningExprResult |
| 917 | Sema::BuildImplicitMemberReferenceExpr(const CXXScopeSpec *SS, |
| 918 | LookupResult &R) { |
| 919 | NamedDecl *D = R.getAsSingleDecl(Context); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 920 | SourceLocation Loc = R.getNameLoc(); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 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()) |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 927 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 928 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 929 | QualType ThisType; |
| 930 | QualType MemberType; |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 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 | |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 945 | if (ShouldCheckUse && DiagnoseUseOfDecl(D, Loc)) |
| 946 | return ExprError(); |
| 947 | return Owned(BuildMemberExpr(Context, This, true, SS, D, Loc, MemberType)); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 948 | } |
| 949 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 950 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 951 | if (!Method->isStatic()) |
| 952 | return ExprError(Diag(Loc, diag::err_member_call_without_object)); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | if (isa<FieldDecl>(D)) { |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 956 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 957 | if (MD->isStatic()) { |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 958 | // "invalid use of member 'x' in static member function" |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 959 | Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 960 | << D->getDeclName(); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 961 | return ExprError(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 962 | } |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 965 | // Any other ways we could have found the field in a well-formed |
| 966 | // program would have been turned into implicit member expressions |
| 967 | // above. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 968 | Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 969 | << D->getDeclName(); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 970 | return ExprError(); |
Argyrios Kyrtzidis | ed98342 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 971 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 972 | |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 973 | // We're not in an implicit member-reference context, but the lookup |
| 974 | // results might not require an instance. Try to build a non-member |
| 975 | // decl reference. |
| 976 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 977 | } |
| 978 | |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 979 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec *SS, |
| 980 | const LookupResult &R, |
| 981 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 982 | // Only when used directly as the postfix-expression of a call. |
| 983 | if (!HasTrailingLParen) |
| 984 | return false; |
| 985 | |
| 986 | // Never if a scope specifier was provided. |
| 987 | if (SS && SS->isSet()) |
| 988 | return false; |
| 989 | |
| 990 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 991 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 992 | return false; |
| 993 | |
| 994 | // Turn off ADL when we find certain kinds of declarations during |
| 995 | // normal lookup: |
| 996 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 997 | NamedDecl *D = *I; |
| 998 | |
| 999 | // C++0x [basic.lookup.argdep]p3: |
| 1000 | // -- a declaration of a class member |
| 1001 | // Since using decls preserve this property, we check this on the |
| 1002 | // original decl. |
| 1003 | if (D->getDeclContext()->isRecord()) |
| 1004 | return false; |
| 1005 | |
| 1006 | // C++0x [basic.lookup.argdep]p3: |
| 1007 | // -- a block-scope function declaration that is not a |
| 1008 | // using-declaration |
| 1009 | // NOTE: we also trigger this for function templates (in fact, we |
| 1010 | // don't check the decl type at all, since all other decl types |
| 1011 | // turn off ADL anyway). |
| 1012 | if (isa<UsingShadowDecl>(D)) |
| 1013 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 1014 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 1015 | return false; |
| 1016 | |
| 1017 | // C++0x [basic.lookup.argdep]p3: |
| 1018 | // -- a declaration that is neither a function or a function |
| 1019 | // template |
| 1020 | // And also for builtin functions. |
| 1021 | if (isa<FunctionDecl>(D)) { |
| 1022 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 1023 | |
| 1024 | // But also builtin functions. |
| 1025 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 1026 | return false; |
| 1027 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 1028 | return false; |
| 1029 | } |
| 1030 | |
| 1031 | return true; |
| 1032 | } |
| 1033 | |
| 1034 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1035 | /// Diagnoses obvious problems with the use of the given declaration |
| 1036 | /// as an expression. This is only actually called for lookups that |
| 1037 | /// were not overloaded, and it doesn't promise that the declaration |
| 1038 | /// will in fact be used. |
| 1039 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| 1040 | if (isa<TypedefDecl>(D)) { |
| 1041 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
| 1045 | if (isa<ObjCInterfaceDecl>(D)) { |
| 1046 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 1047 | return true; |
| 1048 | } |
| 1049 | |
| 1050 | if (isa<NamespaceDecl>(D)) { |
| 1051 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 1052 | return true; |
| 1053 | } |
| 1054 | |
| 1055 | return false; |
| 1056 | } |
| 1057 | |
| 1058 | Sema::OwningExprResult |
| 1059 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec *SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1060 | LookupResult &R, |
| 1061 | bool NeedsADL) { |
| 1062 | assert(R.getResultKind() != LookupResult::FoundUnresolvedValue); |
| 1063 | |
| 1064 | if (!NeedsADL && !R.isOverloadedResult()) |
| 1065 | return BuildDeclarationNameExpr(SS, R.getNameLoc(), R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1066 | |
| 1067 | // We only need to check the declaration if there's exactly one |
| 1068 | // result, because in the overloaded case the results can only be |
| 1069 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1070 | if (R.isSingleResult() && |
| 1071 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1072 | return ExprError(); |
| 1073 | |
| 1074 | UnresolvedLookupExpr *ULE |
| 1075 | = UnresolvedLookupExpr::Create(Context, |
| 1076 | SS ? (NestedNameSpecifier *)SS->getScopeRep() : 0, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1077 | SS ? SS->getRange() : SourceRange(), |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1078 | R.getLookupName(), R.getNameLoc(), |
| 1079 | NeedsADL, R.isOverloadedResult()); |
| 1080 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1081 | ULE->addDecl(*I); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1082 | |
| 1083 | return Owned(ULE); |
| 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 1088 | Sema::OwningExprResult |
| 1089 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec *SS, |
| 1090 | SourceLocation Loc, NamedDecl *D) { |
| 1091 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 1092 | assert(!isa<FunctionTemplateDecl>(D) && |
| 1093 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1094 | DeclarationName Name = D->getDeclName(); |
| 1095 | |
| 1096 | if (CheckDeclInExpr(*this, Loc, D)) |
| 1097 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1098 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1099 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1101 | // Check whether this declaration can be used. Note that we suppress |
| 1102 | // this check when we're going to perform argument-dependent lookup |
| 1103 | // on this function name, because this might not be the function |
| 1104 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1105 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1106 | return ExprError(); |
| 1107 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1108 | // Only create DeclRefExpr's for valid Decl's. |
| 1109 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1110 | return ExprError(); |
| 1111 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1112 | // If the identifier reference is inside a block, and it refers to a value |
| 1113 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1114 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1115 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1116 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1117 | // We do not do this for things like enum constants, global variables, etc, |
| 1118 | // as they do not get snapshotted. |
| 1119 | // |
| 1120 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1121 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1122 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1123 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1124 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1125 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1126 | // This is to record that a 'const' was actually synthesize and added. |
| 1127 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1128 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1129 | |
Eli Friedman | 7fa3faa | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1130 | ExprTy.addConst(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1132 | constAdded)); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1133 | } |
| 1134 | // If this reference is not in a block or if the referenced variable is |
| 1135 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1136 | |
Douglas Gregor | ed6c744 | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 1137 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, SS); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1138 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1139 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1140 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1141 | tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1142 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1143 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1144 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1145 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1146 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1147 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1148 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1149 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1151 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1152 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1154 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1155 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1156 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1157 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1158 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1159 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1160 | QualType ResTy; |
| 1161 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 1162 | ResTy = Context.DependentTy; |
| 1163 | } else { |
| 1164 | unsigned Length = |
| 1165 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1166 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1167 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1168 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 1169 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 1170 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1171 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1174 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1175 | llvm::SmallString<16> CharBuffer; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1176 | CharBuffer.resize(Tok.getLength()); |
| 1177 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1178 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1179 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1180 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1181 | Tok.getLocation(), PP); |
| 1182 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1183 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1184 | |
| 1185 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1186 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1187 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1188 | Literal.isWide(), |
| 1189 | type, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1192 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1193 | // 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] | 1194 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1195 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1196 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1197 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1198 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1199 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1200 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1201 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 1202 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1203 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1204 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 1205 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1206 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1207 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 1208 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1209 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1210 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 1211 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 1212 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1213 | return ExprError(); |
| 1214 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1215 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1217 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1218 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1219 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1220 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1221 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1222 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1223 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1224 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1225 | |
| 1226 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1227 | |
Ted Kremenek | 3a2c950 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1228 | // isExact will be set by GetFloatValue(). |
| 1229 | bool isExact = false; |
Chris Lattner | e4edb8e | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1230 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1231 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1232 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1233 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1234 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1235 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1236 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1237 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1238 | // long long is a C99 feature. |
| 1239 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1240 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1241 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1242 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1243 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1244 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1245 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1246 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1247 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1248 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1249 | Ty = Context.UnsignedLongLongTy; |
| 1250 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1251 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1252 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1253 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1254 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1255 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1256 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1257 | // be an unsigned int. |
| 1258 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1259 | |
| 1260 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1261 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1262 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1263 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1264 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1265 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1266 | // Does it fit in a unsigned int? |
| 1267 | if (ResultVal.isIntN(IntSize)) { |
| 1268 | // Does it fit in a signed int? |
| 1269 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1270 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1271 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1272 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1273 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1274 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1275 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1277 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1278 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1279 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1280 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1281 | // Does it fit in a unsigned long? |
| 1282 | if (ResultVal.isIntN(LongSize)) { |
| 1283 | // Does it fit in a signed long? |
| 1284 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1285 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1286 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1287 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1288 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1289 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1292 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1293 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1294 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1296 | // Does it fit in a unsigned long long? |
| 1297 | if (ResultVal.isIntN(LongLongSize)) { |
| 1298 | // Does it fit in a signed long long? |
| 1299 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1300 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1301 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1302 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1303 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1304 | } |
| 1305 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1306 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1307 | // If we still couldn't decide a type, we probably have something that |
| 1308 | // 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] | 1309 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1310 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1311 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1312 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 1313 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1314 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1315 | if (ResultVal.getBitWidth() != Width) |
| 1316 | ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1317 | } |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1318 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 1319 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1320 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1321 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1322 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1324 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1325 | |
| 1326 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1329 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1330 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1331 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1332 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1333 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 1336 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 1337 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1338 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1339 | SourceLocation OpLoc, |
| 1340 | const SourceRange &ExprRange, |
| 1341 | bool isSizeof) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1342 | if (exprType->isDependentType()) |
| 1343 | return false; |
| 1344 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 1345 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 1346 | // the result is the size of the referenced type." |
| 1347 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 1348 | // result shall be the alignment of the referenced type." |
| 1349 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 1350 | exprType = Ref->getPointeeType(); |
| 1351 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1352 | // C99 6.5.3.4p1: |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1353 | if (exprType->isFunctionType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1354 | // alignof(function) is allowed as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1355 | if (isSizeof) |
| 1356 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1357 | return false; |
| 1358 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1360 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1361 | if (exprType->isVoidType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1362 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1363 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1367 | if (RequireCompleteType(OpLoc, exprType, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | isSizeof ? diag::err_sizeof_incomplete_type : |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1369 | PDiag(diag::err_alignof_incomplete_type) |
| 1370 | << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1371 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1373 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | 1dcb322 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1374 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1375 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1376 | << exprType << isSizeof << ExprRange; |
| 1377 | return true; |
Chris Lattner | 37920f5 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1378 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1380 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1383 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1384 | const SourceRange &ExprRange) { |
| 1385 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1386 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1388 | if (isa<DeclRefExpr>(E)) |
| 1389 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Cannot know anything else if the expression is dependent. |
| 1392 | if (E->isTypeDependent()) |
| 1393 | return false; |
| 1394 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1395 | if (E->getBitField()) { |
| 1396 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1397 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1398 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Alignment of a field access is always okay, so long as it isn't a |
| 1401 | // bit-field. |
| 1402 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1403 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1404 | return false; |
| 1405 | |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1406 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1407 | } |
| 1408 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1409 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | Action::OwningExprResult |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1411 | Sema::CreateSizeOfAlignOfExpr(DeclaratorInfo *DInfo, |
| 1412 | SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1413 | bool isSizeOf, SourceRange R) { |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1414 | if (!DInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1415 | return ExprError(); |
| 1416 | |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1417 | QualType T = DInfo->getType(); |
| 1418 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1419 | if (!T->isDependentType() && |
| 1420 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1421 | return ExprError(); |
| 1422 | |
| 1423 | // 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] | 1424 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, DInfo, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1425 | Context.getSizeType(), OpLoc, |
| 1426 | R.getEnd())); |
| 1427 | } |
| 1428 | |
| 1429 | /// \brief Build a sizeof or alignof expression given an expression |
| 1430 | /// operand. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1431 | Action::OwningExprResult |
| 1432 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1433 | bool isSizeOf, SourceRange R) { |
| 1434 | // Verify that the operand is valid. |
| 1435 | bool isInvalid = false; |
| 1436 | if (E->isTypeDependent()) { |
| 1437 | // Delay type-checking for type-dependent expressions. |
| 1438 | } else if (!isSizeOf) { |
| 1439 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1440 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1441 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1442 | isInvalid = true; |
| 1443 | } else { |
| 1444 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1445 | } |
| 1446 | |
| 1447 | if (isInvalid) |
| 1448 | return ExprError(); |
| 1449 | |
| 1450 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1451 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1452 | Context.getSizeType(), OpLoc, |
| 1453 | R.getEnd())); |
| 1454 | } |
| 1455 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1456 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1457 | /// the same for @c alignof and @c __alignof |
| 1458 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1459 | Action::OwningExprResult |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1460 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1461 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 1462 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1463 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1464 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1465 | if (isType) { |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 1466 | DeclaratorInfo *DInfo; |
| 1467 | (void) GetTypeFromParser(TyOrEx, &DInfo); |
| 1468 | return CreateSizeOfAlignOfExpr(DInfo, OpLoc, isSizeof, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1471 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1472 | Action::OwningExprResult Result |
| 1473 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1474 | |
| 1475 | if (Result.isInvalid()) |
| 1476 | DeleteExpr(ArgEx); |
| 1477 | |
| 1478 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1481 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1482 | if (V->isTypeDependent()) |
| 1483 | return Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1484 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1485 | // These operators return the element type of a complex type. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1486 | if (const ComplexType *CT = V->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1487 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1489 | // Otherwise they pass through real integer and floating point types here. |
| 1490 | if (V->getType()->isArithmeticType()) |
| 1491 | return V->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1493 | // Reject anything else. |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1494 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1495 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1496 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1500 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1501 | Action::OwningExprResult |
| 1502 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1503 | tok::TokenKind Kind, ExprArg Input) { |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1504 | UnaryOperator::Opcode Opc; |
| 1505 | switch (Kind) { |
| 1506 | default: assert(0 && "Unknown unary op!"); |
| 1507 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1508 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1509 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1510 | |
Eli Friedman | cfdd40c | 2009-11-18 03:38:04 +0000 | [diff] [blame] | 1511 | return BuildUnaryOp(S, OpLoc, Opc, move(Input)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1514 | Action::OwningExprResult |
| 1515 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1516 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1517 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1518 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1519 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1520 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1521 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1523 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1524 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1525 | Base.release(); |
| 1526 | Idx.release(); |
| 1527 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1528 | Context.DependentTy, RLoc)); |
| 1529 | } |
| 1530 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1531 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1532 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1533 | LHSExp->getType()->isEnumeralType() || |
| 1534 | RHSExp->getType()->isRecordType() || |
| 1535 | RHSExp->getType()->isEnumeralType())) { |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1536 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, move(Base),move(Idx)); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 1539 | return CreateBuiltinArraySubscriptExpr(move(Base), LLoc, move(Idx), RLoc); |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | Action::OwningExprResult |
| 1544 | Sema::CreateBuiltinArraySubscriptExpr(ExprArg Base, SourceLocation LLoc, |
| 1545 | ExprArg Idx, SourceLocation RLoc) { |
| 1546 | Expr *LHSExp = static_cast<Expr*>(Base.get()); |
| 1547 | Expr *RHSExp = static_cast<Expr*>(Idx.get()); |
| 1548 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1549 | // Perform default conversions. |
| 1550 | DefaultFunctionArrayConversion(LHSExp); |
| 1551 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1552 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1553 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1554 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 1555 | // 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] | 1556 | // 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] | 1557 | // 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] | 1558 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1559 | Expr *BaseExpr, *IndexExpr; |
| 1560 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1561 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1562 | BaseExpr = LHSExp; |
| 1563 | IndexExpr = RHSExp; |
| 1564 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1565 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1566 | BaseExpr = LHSExp; |
| 1567 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1568 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1569 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1570 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1571 | BaseExpr = RHSExp; |
| 1572 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1573 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1574 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1575 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1576 | BaseExpr = LHSExp; |
| 1577 | IndexExpr = RHSExp; |
| 1578 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1580 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1581 | // Handle the uncommon case of "123[Ptr]". |
| 1582 | BaseExpr = RHSExp; |
| 1583 | IndexExpr = LHSExp; |
| 1584 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1585 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1586 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1587 | IndexExpr = RHSExp; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1588 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1589 | // FIXME: need to deal with const... |
| 1590 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1591 | } else if (LHSTy->isArrayType()) { |
| 1592 | // If we see an array that wasn't promoted by |
| 1593 | // DefaultFunctionArrayConversion, it must be an array that |
| 1594 | // wasn't promoted because of the C90 rule that doesn't |
| 1595 | // allow promoting non-lvalue arrays. Warn, then |
| 1596 | // force the promotion here. |
| 1597 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1598 | LHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1599 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 1600 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1601 | LHSTy = LHSExp->getType(); |
| 1602 | |
| 1603 | BaseExpr = LHSExp; |
| 1604 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1605 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1606 | } else if (RHSTy->isArrayType()) { |
| 1607 | // Same as previous, except for 123[f().a] case |
| 1608 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1609 | RHSExp->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1610 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 1611 | CastExpr::CK_ArrayToPointerDecay); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1612 | RHSTy = RHSExp->getType(); |
| 1613 | |
| 1614 | BaseExpr = RHSExp; |
| 1615 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1616 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 1617 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1618 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1619 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1620 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 1621 | // C99 6.5.2.1p1 |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1622 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1623 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1624 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1625 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 1626 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1627 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 1628 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 1629 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 1630 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 1631 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1632 | // 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] | 1633 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1634 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1635 | // incomplete types are not object types. |
| 1636 | if (ResultType->isFunctionType()) { |
| 1637 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1638 | << ResultType << BaseExpr->getSourceRange(); |
| 1639 | return ExprError(); |
| 1640 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1642 | if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1644 | PDiag(diag::err_subscript_incomplete_type) |
| 1645 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1646 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1647 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1648 | // Diagnose bad cases where we step over interface counts. |
| 1649 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1650 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1651 | << ResultType << BaseExpr->getSourceRange(); |
| 1652 | return ExprError(); |
| 1653 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1655 | Base.release(); |
| 1656 | Idx.release(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1657 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1658 | ResultType, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1661 | QualType Sema:: |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1662 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1664 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 1665 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 1666 | // see FIXME there. |
| 1667 | // |
| 1668 | // FIXME: This logic can be greatly simplified by splitting it along |
| 1669 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1670 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1671 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1672 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1673 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1674 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1675 | // 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] | 1676 | // special names that indicate a subset of exactly half the elements are |
| 1677 | // to be selected. |
| 1678 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1679 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1680 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1681 | // 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] | 1682 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Check that we've found one of the special components, or that the component |
| 1685 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1686 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1687 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1688 | HalvingSwizzle = true; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1689 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1690 | do |
| 1691 | compStr++; |
| 1692 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1693 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1694 | do |
| 1695 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1696 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1697 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1698 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1699 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1700 | // We didn't get to the end of the string. This means the component names |
| 1701 | // 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] | 1702 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1703 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1704 | return QualType(); |
| 1705 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1706 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1707 | // Ensure no component accessor exceeds the width of the vector type it |
| 1708 | // operates on. |
| 1709 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 1710 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1711 | |
| 1712 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1713 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1714 | |
| 1715 | while (*compStr) { |
| 1716 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1717 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1718 | << baseType << SourceRange(CompLoc); |
| 1719 | return QualType(); |
| 1720 | } |
| 1721 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1722 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1723 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1724 | // If this is a halving swizzle, verify that the base type has an even |
| 1725 | // number of elements. |
| 1726 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1727 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1728 | << baseType << SourceRange(CompLoc); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1729 | return QualType(); |
| 1730 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1731 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1732 | // 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] | 1733 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1734 | // 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] | 1735 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1736 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1737 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1738 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1739 | if (HexSwizzle) |
| 1740 | CompSize--; |
| 1741 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1742 | if (CompSize == 1) |
| 1743 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1744 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1745 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1746 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1747 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1748 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1749 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1750 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1751 | } |
| 1752 | 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] | 1753 | } |
| 1754 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1755 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1756 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1757 | const Selector &Sel, |
| 1758 | ASTContext &Context) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1759 | |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1760 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1761 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1762 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1763 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1764 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1765 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1766 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1768 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1769 | return D; |
| 1770 | } |
| 1771 | return 0; |
| 1772 | } |
| 1773 | |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1774 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1775 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1776 | const Selector &Sel, |
| 1777 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1778 | // Check protocols on qualified interfaces. |
| 1779 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1780 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1781 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1782 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1783 | GDecl = PD; |
| 1784 | break; |
| 1785 | } |
| 1786 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1787 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1788 | GDecl = OMD; |
| 1789 | break; |
| 1790 | } |
| 1791 | } |
| 1792 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1793 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1794 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1795 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1796 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1797 | if (GDecl) |
| 1798 | return GDecl; |
| 1799 | } |
| 1800 | } |
| 1801 | return GDecl; |
| 1802 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1803 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | Action::OwningExprResult |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1805 | Sema::BuildMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1806 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | DeclarationName MemberName, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1808 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1809 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS, |
| 1810 | NamedDecl *FirstQualifierInScope) { |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1811 | if (SS && SS->isInvalid()) |
| 1812 | return ExprError(); |
| 1813 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1814 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1815 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1816 | |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1817 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1818 | assert(BaseExpr && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1819 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 1820 | // Perform default conversions. |
| 1821 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1822 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1823 | QualType BaseType = BaseExpr->getType(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 1824 | |
| 1825 | // If the user is trying to apply -> or . to a function pointer |
| 1826 | // type, it's probably because the forgot parentheses to call that |
| 1827 | // function. Suggest the addition of those parentheses, build the |
| 1828 | // call, and continue on. |
| 1829 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| 1830 | if (const FunctionProtoType *Fun |
| 1831 | = Ptr->getPointeeType()->getAs<FunctionProtoType>()) { |
| 1832 | QualType ResultTy = Fun->getResultType(); |
| 1833 | if (Fun->getNumArgs() == 0 && |
| 1834 | ((OpKind == tok::period && ResultTy->isRecordType()) || |
| 1835 | (OpKind == tok::arrow && ResultTy->isPointerType() && |
| 1836 | ResultTy->getAs<PointerType>()->getPointeeType() |
| 1837 | ->isRecordType()))) { |
| 1838 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 1839 | Diag(Loc, diag::err_member_reference_needs_call) |
| 1840 | << QualType(Fun, 0) |
| 1841 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 1842 | |
| 1843 | OwningExprResult NewBase |
| 1844 | = ActOnCallExpr(S, ExprArg(*this, BaseExpr), Loc, |
| 1845 | MultiExprArg(*this, 0, 0), 0, Loc); |
| 1846 | if (NewBase.isInvalid()) |
| 1847 | return move(NewBase); |
| 1848 | |
| 1849 | BaseExpr = NewBase.takeAs<Expr>(); |
| 1850 | DefaultFunctionArrayConversion(BaseExpr); |
| 1851 | BaseType = BaseExpr->getType(); |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1856 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 1857 | // use that. |
| 1858 | if (BaseType->isObjCIdType()) { |
| 1859 | // We have an 'id' type. Rather than fall through, we check if this |
| 1860 | // is a reference to 'isa'. |
| 1861 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 1862 | BaseType = Context.ObjCIdRedefinitionType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1863 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1864 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 1865 | } |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1866 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1867 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1868 | // Handle properties on ObjC 'Class' types. |
| 1869 | if (OpKind == tok::period && BaseType->isObjCClassType()) { |
| 1870 | // Also must look for a getter name which uses property syntax. |
| 1871 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 1872 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1873 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 1874 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 1875 | ObjCMethodDecl *Getter; |
| 1876 | // FIXME: need to also look locally in the implementation. |
| 1877 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 1878 | // Check the use of this method. |
| 1879 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1880 | return ExprError(); |
| 1881 | } |
| 1882 | // If we found a getter then this may be a valid dot-reference, we |
| 1883 | // will look for the matching setter, in case it is needed. |
| 1884 | Selector SetterSel = |
| 1885 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 1886 | PP.getSelectorTable(), Member); |
| 1887 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 1888 | if (!Setter) { |
| 1889 | // If this reference is in an @implementation, also check for 'private' |
| 1890 | // methods. |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 1891 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1892 | } |
| 1893 | // Look through local category implementations associated with the class. |
| 1894 | if (!Setter) |
| 1895 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| 1896 | |
| 1897 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1898 | return ExprError(); |
| 1899 | |
| 1900 | if (Getter || Setter) { |
| 1901 | QualType PType; |
| 1902 | |
| 1903 | if (Getter) |
| 1904 | PType = Getter->getResultType(); |
| 1905 | else |
| 1906 | // Get the expression type from Setter's incoming parameter. |
| 1907 | PType = (*(Setter->param_end() -1))->getType(); |
| 1908 | // FIXME: we must check that the setter has property type. |
| 1909 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, |
| 1910 | PType, |
| 1911 | Setter, MemberLoc, BaseExpr)); |
| 1912 | } |
| 1913 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 1914 | << MemberName << BaseType); |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | if (BaseType->isObjCClassType() && |
| 1919 | BaseType != Context.ObjCClassRedefinitionType) { |
| 1920 | BaseType = Context.ObjCClassRedefinitionType; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 1921 | ImpCastExprToType(BaseExpr, BaseType, CastExpr::CK_BitCast); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Chris Lattner | 4befd73 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1924 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 1925 | // must have pointer type, and the accessed type is the pointee. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 1926 | if (OpKind == tok::arrow) { |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1927 | if (BaseType->isDependentType()) { |
| 1928 | NestedNameSpecifier *Qualifier = 0; |
| 1929 | if (SS) { |
| 1930 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 1931 | if (!FirstQualifierInScope) |
| 1932 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 1933 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
John McCall | 8cd7813 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 1935 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, true, |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1936 | OpLoc, Qualifier, |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 1937 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1938 | FirstQualifierInScope, |
| 1939 | MemberName, |
| 1940 | MemberLoc, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1941 | ExplicitTemplateArgs)); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1942 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1943 | else if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1944 | BaseType = PT->getPointeeType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1945 | else if (BaseType->isObjCObjectPointerType()) |
| 1946 | ; |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1947 | else |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1948 | return ExprError(Diag(MemberLoc, |
| 1949 | diag::err_typecheck_member_reference_arrow) |
| 1950 | << BaseType << BaseExpr->getSourceRange()); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 1951 | } else if (BaseType->isDependentType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | // Require that the base type isn't a pointer type |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 1953 | // (so we'll report an error for) |
| 1954 | // T* t; |
| 1955 | // t.f; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1956 | // |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 1957 | // In Obj-C++, however, the above expression is valid, since it could be |
| 1958 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 1959 | // 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] | 1960 | const PointerType *PT = BaseType->getAs<PointerType>(); |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 1961 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | if (!PT || (getLangOptions().ObjC1 && |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1963 | !PT->getPointeeType()->isRecordType())) { |
| 1964 | NestedNameSpecifier *Qualifier = 0; |
| 1965 | if (SS) { |
| 1966 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 1967 | if (!FirstQualifierInScope) |
| 1968 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 1969 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1970 | |
John McCall | 8cd7813 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 1971 | return Owned(CXXDependentScopeMemberExpr::Create(Context, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1972 | BaseExpr, false, |
| 1973 | OpLoc, |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1974 | Qualifier, |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 1975 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 308047d | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 1976 | FirstQualifierInScope, |
| 1977 | MemberName, |
| 1978 | MemberLoc, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1979 | ExplicitTemplateArgs)); |
Douglas Gregor | 2b6ca46 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1980 | } |
Anders Carlsson | 524d5a4 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 1981 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1982 | |
Chris Lattner | 4befd73 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 1983 | // Handle field access to simple records. This also handles access to fields |
| 1984 | // of the ObjC 'id' struct. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1985 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 1986 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 1987 | if (RequireCompleteType(OpLoc, BaseType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1988 | PDiag(diag::err_typecheck_incomplete_tag) |
| 1989 | << BaseExpr->getSourceRange())) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 1990 | return ExprError(); |
| 1991 | |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1992 | DeclContext *DC = RDecl; |
| 1993 | if (SS && SS->isSet()) { |
| 1994 | // If the member name was a qualified-id, look into the |
| 1995 | // nested-name-specifier. |
| 1996 | DC = computeDeclContext(*SS, false); |
Douglas Gregor | 0b3d95a | 2009-10-17 22:37:54 +0000 | [diff] [blame] | 1997 | |
| 1998 | if (!isa<TypeDecl>(DC)) { |
| 1999 | Diag(MemberLoc, diag::err_qualified_member_nonclass) |
| 2000 | << DC << SS->getRange(); |
| 2001 | return ExprError(); |
| 2002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
| 2004 | // FIXME: If DC is not computable, we should build a |
John McCall | 8cd7813 | 2009-11-19 22:55:06 +0000 | [diff] [blame] | 2005 | // CXXDependentScopeMemberExpr. |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2006 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2007 | } |
| 2008 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2009 | // The record definition is complete, now make sure the member is valid. |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2010 | LookupResult Result(*this, MemberName, MemberLoc, LookupMemberName); |
| 2011 | LookupQualifiedName(Result, DC); |
Douglas Gregor | 960b5bc | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2012 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2013 | if (Result.empty()) |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 2014 | return ExprError(Diag(MemberLoc, diag::err_no_member) |
| 2015 | << MemberName << DC << BaseExpr->getSourceRange()); |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 2016 | if (Result.isAmbiguous()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2017 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2019 | NamedDecl *MemberDecl = Result.getAsSingleDecl(Context); |
| 2020 | |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2021 | if (SS && SS->isSet()) { |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2022 | TypeDecl* TyD = cast<TypeDecl>(MemberDecl->getDeclContext()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2023 | QualType BaseTypeCanon |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2024 | = Context.getCanonicalType(BaseType).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | QualType MemberTypeCanon |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2026 | = Context.getCanonicalType(Context.getTypeDeclType(TyD)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | c26e0f6 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2028 | if (BaseTypeCanon != MemberTypeCanon && |
| 2029 | !IsDerivedFrom(BaseTypeCanon, MemberTypeCanon)) |
| 2030 | return ExprError(Diag(SS->getBeginLoc(), |
| 2031 | diag::err_not_direct_base_or_virtual) |
| 2032 | << MemberTypeCanon << BaseTypeCanon); |
| 2033 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Chris Lattner | 303284a | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2035 | // If the decl being referenced had an error, return an error for this |
| 2036 | // sub-expr without emitting another error, in order to avoid cascading |
| 2037 | // error cases. |
| 2038 | if (MemberDecl->isInvalidDecl()) |
| 2039 | return ExprError(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2040 | |
Anders Carlsson | 04e1e22 | 2009-09-10 20:48:14 +0000 | [diff] [blame] | 2041 | bool ShouldCheckUse = true; |
| 2042 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 2043 | // Don't diagnose the use of a virtual member function unless it's |
| 2044 | // explicitly qualified. |
| 2045 | if (MD->isVirtual() && (!SS || !SS->isSet())) |
| 2046 | ShouldCheckUse = false; |
| 2047 | } |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2049 | // Check the use of this field |
Anders Carlsson | 04e1e22 | 2009-09-10 20:48:14 +0000 | [diff] [blame] | 2050 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2051 | return ExprError(); |
Chris Lattner | 303284a | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2052 | |
Douglas Gregor | 55297ac | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2053 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2054 | // We may have found a field within an anonymous union or struct |
| 2055 | // (C++ [class.union]). |
| 2056 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2057 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2058 | BaseExpr, OpLoc); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2060 | // 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] | 2061 | QualType MemberType = FD->getType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2062 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2063 | MemberType = Ref->getPointeeType(); |
| 2064 | else { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2065 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2066 | BaseQuals.removeObjCGCAttr(); |
| 2067 | if (FD->isMutable()) BaseQuals.removeConst(); |
| 2068 | |
| 2069 | Qualifiers MemberQuals |
| 2070 | = Context.getCanonicalType(MemberType).getQualifiers(); |
| 2071 | |
| 2072 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2073 | if (Combined != MemberQuals) |
| 2074 | MemberType = Context.getQualifiedType(MemberType, Combined); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2075 | } |
Eli Friedman | 1242fff | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2076 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2077 | MarkDeclarationReferenced(MemberLoc, FD); |
Fariborz Jahanian | 3f15083 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2078 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2079 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2081 | FD, MemberLoc, MemberType)); |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2082 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2084 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2085 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2086 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2087 | Var, MemberLoc, |
| 2088 | Var->getType().getNonReferenceType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2089 | } |
| 2090 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2091 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2092 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2093 | MemberFn, MemberLoc, |
| 2094 | MemberFn->getType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2095 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2097 | = dyn_cast<FunctionTemplateDecl>(MemberDecl)) { |
| 2098 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2099 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2100 | if (ExplicitTemplateArgs) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2102 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2103 | SS? SS->getRange() : SourceRange(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2104 | FunTmpl, MemberLoc, |
| 2105 | ExplicitTemplateArgs, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2106 | Context.OverloadTy)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2108 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2109 | FunTmpl, MemberLoc, |
| 2110 | Context.OverloadTy)); |
Douglas Gregor | 97628d6 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2111 | } |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2112 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2113 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) { |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2114 | if (ExplicitTemplateArgs) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2116 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2117 | SS? SS->getRange() : SourceRange(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2118 | Ovl, MemberLoc, ExplicitTemplateArgs, |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2119 | Context.OverloadTy)); |
| 2120 | |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2121 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2122 | Ovl, MemberLoc, Context.OverloadTy)); |
Douglas Gregor | 84f14dd | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2123 | } |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2124 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2125 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2126 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2127 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2128 | } |
Chris Lattner | fe4847e | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2129 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2130 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2131 | << MemberName << int(OpKind == tok::arrow)); |
Eli Friedman | 1242fff | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2133 | // We found a declaration kind that we didn't expect. This is a |
| 2134 | // generic error message that tells the user that she can't refer |
| 2135 | // to this member with '.' or '->'. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2136 | return ExprError(Diag(MemberLoc, |
| 2137 | diag::err_typecheck_member_reference_unknown) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2138 | << MemberName << int(OpKind == tok::arrow)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2139 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2141 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2142 | // into a record type was handled above, any destructor we see here is a |
| 2143 | // pseudo-destructor. |
| 2144 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2145 | // C++ [expr.pseudo]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | // The left hand side of the dot operator shall be of scalar type. The |
| 2147 | // 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] | 2148 | // type. |
| 2149 | if (!BaseType->isScalarType()) |
| 2150 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2151 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2153 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2154 | // same as the object type. |
| 2155 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2156 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2157 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2158 | << BaseType << MemberName.getCXXNameType() |
| 2159 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2160 | |
| 2161 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2162 | // the form |
| 2163 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2164 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2165 | // |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2166 | // shall designate the same scalar type. |
| 2167 | // |
| 2168 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2169 | // isn't checked here. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2171 | // FIXME: We've lost the precise spelling of the type by going through |
| 2172 | // DeclarationName. Can we do better? |
| 2173 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | OpKind == tok::arrow, |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2175 | OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2177 | SS? SS->getRange() : SourceRange(), |
| 2178 | MemberName.getCXXNameType(), |
| 2179 | MemberLoc)); |
| 2180 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2182 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2183 | // (*Obj).ivar. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2184 | if ((OpKind == tok::arrow && BaseType->isObjCObjectPointerType()) || |
| 2185 | (OpKind == tok::period && BaseType->isObjCInterfaceType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2186 | const ObjCObjectPointerType *OPT = BaseType->getAs<ObjCObjectPointerType>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2187 | const ObjCInterfaceType *IFaceT = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2188 | OPT ? OPT->getInterfaceType() : BaseType->getAs<ObjCInterfaceType>(); |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2189 | if (IFaceT) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2190 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2191 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2192 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2193 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2194 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2196 | if (IV) { |
| 2197 | // If the decl being referenced had an error, return an error for this |
| 2198 | // sub-expr without emitting another error, in order to avoid cascading |
| 2199 | // error cases. |
| 2200 | if (IV->isInvalidDecl()) |
| 2201 | return ExprError(); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2202 | |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2203 | // Check whether we can reference this field. |
| 2204 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2205 | return ExprError(); |
| 2206 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2207 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2208 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2209 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2210 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2211 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2212 | // Case of a c-function declared inside an objc implementation. |
| 2213 | // FIXME: For a c-style function nested inside an objc implementation |
| 2214 | // class, there is no implementation context available, so we pass |
| 2215 | // down the context as argument to this routine. Ideally, this context |
| 2216 | // need be passed down in the AST node and somehow calculated from the |
| 2217 | // AST for a function decl. |
| 2218 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2220 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2221 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2222 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2223 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2224 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2225 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | |
| 2227 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2228 | if (ClassDeclared != IDecl || |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2229 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2230 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2231 | << IV->getDeclName(); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2232 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2233 | // @protected |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2235 | << IV->getDeclName(); |
Steve Naroff | d1b64be | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2236 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2237 | |
| 2238 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2239 | MemberLoc, BaseExpr, |
| 2240 | OpKind == tok::arrow)); |
Fariborz Jahanian | a458c4f | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2241 | } |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2242 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2243 | << IDecl->getDeclName() << MemberName |
Steve Naroff | a057ba9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2244 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | b1378f9 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2245 | } |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2246 | } |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2247 | // Handle properties on 'id' and qualified "id". |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | if (OpKind == tok::period && (BaseType->isObjCIdType() || |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2249 | BaseType->isObjCQualifiedIdType())) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2250 | const ObjCObjectPointerType *QIdTy = BaseType->getAs<ObjCObjectPointerType>(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2251 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2252 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2253 | // Check protocols on qualified interfaces. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2254 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2255 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2256 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2257 | // Check the use of this declaration |
| 2258 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2259 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2261 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2262 | MemberLoc, BaseExpr)); |
| 2263 | } |
| 2264 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2265 | // Check the use of this method. |
| 2266 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2267 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2268 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2269 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2270 | OMD->getResultType(), |
| 2271 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2272 | NULL, 0)); |
| 2273 | } |
| 2274 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2275 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2276 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2277 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2278 | } |
Chris Lattner | dc420f4 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2279 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2280 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2281 | const ObjCObjectPointerType *OPT; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2282 | if (OpKind == tok::period && |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2283 | (OPT = BaseType->getAsObjCInterfacePointerType())) { |
| 2284 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2285 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2286 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2287 | |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2288 | // Search for a declared property first. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2289 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2290 | // Check whether we can reference this property. |
| 2291 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2292 | return ExprError(); |
Fariborz Jahanian | b2ab73d | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2293 | QualType ResTy = PD->getType(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2294 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2295 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | fe9e394 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2296 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2297 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | b2ab73d | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2298 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2299 | MemberLoc, BaseExpr)); |
| 2300 | } |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2301 | // Check protocols on qualified interfaces. |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2302 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2303 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2304 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2305 | // Check whether we can reference this property. |
| 2306 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2307 | return ExprError(); |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2308 | |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2309 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 43df556 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2310 | MemberLoc, BaseExpr)); |
| 2311 | } |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2312 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2313 | // selector is implemented. |
| 2314 | |
| 2315 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2316 | // shared with the code in ActOnInstanceMessage. |
| 2317 | |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2318 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2319 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2320 | |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2321 | // If this reference is in an @implementation, check for 'private' methods. |
| 2322 | if (!Getter) |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2323 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2324 | |
Steve Naroff | 1df6269 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2325 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2326 | if (!Getter) |
| 2327 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | ef89086c | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2328 | if (Getter) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2329 | // Check if we can reference this property. |
| 2330 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2331 | return ExprError(); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2332 | } |
| 2333 | // If we found a getter then this may be a valid dot-reference, we |
| 2334 | // will look for the matching setter, in case it is needed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | Selector SetterSel = |
| 2336 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2337 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2338 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2339 | if (!Setter) { |
| 2340 | // If this reference is in an @implementation, also check for 'private' |
| 2341 | // methods. |
Steve Naroff | bb69c94 | 2009-10-01 23:46:04 +0000 | [diff] [blame] | 2342 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2343 | } |
| 2344 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2345 | if (!Setter) |
| 2346 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2347 | |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2348 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2349 | return ExprError(); |
| 2350 | |
| 2351 | if (Getter || Setter) { |
| 2352 | QualType PType; |
| 2353 | |
| 2354 | if (Getter) |
| 2355 | PType = Getter->getResultType(); |
Fariborz Jahanian | 88cc234 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2356 | else |
| 2357 | // Get the expression type from Setter's incoming parameter. |
| 2358 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2359 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 9a84665 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2360 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1d984fe | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2361 | Setter, MemberLoc, BaseExpr)); |
| 2362 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2363 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2364 | << MemberName << BaseType); |
Fariborz Jahanian | 21f54ee | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2365 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2367 | // Handle the following exceptional case (*Obj).isa. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | if (OpKind == tok::period && |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2369 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2370 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | e87026a | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2371 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
| 2372 | Context.getObjCIdType())); |
| 2373 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2374 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2375 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2376 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2377 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2378 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2379 | return ExprError(); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2380 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2381 | MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2382 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2384 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2385 | << BaseType << BaseExpr->getSourceRange(); |
| 2386 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2387 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2388 | } |
| 2389 | |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2390 | Sema::OwningExprResult Sema::ActOnMemberAccessExpr(Scope *S, ExprArg Base, |
| 2391 | SourceLocation OpLoc, |
| 2392 | tok::TokenKind OpKind, |
| 2393 | const CXXScopeSpec &SS, |
| 2394 | UnqualifiedId &Member, |
| 2395 | DeclPtrTy ObjCImpDecl, |
| 2396 | bool HasTrailingLParen) { |
| 2397 | if (Member.getKind() == UnqualifiedId::IK_TemplateId) { |
| 2398 | TemplateName Template |
| 2399 | = TemplateName::getFromVoidPointer(Member.TemplateId->Template); |
| 2400 | |
| 2401 | // FIXME: We're going to end up looking up the template based on its name, |
| 2402 | // twice! |
| 2403 | DeclarationName Name; |
| 2404 | if (TemplateDecl *ActualTemplate = Template.getAsTemplateDecl()) |
| 2405 | Name = ActualTemplate->getDeclName(); |
| 2406 | else if (OverloadedFunctionDecl *Ovl = Template.getAsOverloadedFunctionDecl()) |
| 2407 | Name = Ovl->getDeclName(); |
Douglas Gregor | 71395fa | 2009-11-04 00:56:37 +0000 | [diff] [blame] | 2408 | else { |
| 2409 | DependentTemplateName *DTN = Template.getAsDependentTemplateName(); |
| 2410 | if (DTN->isIdentifier()) |
| 2411 | Name = DTN->getIdentifier(); |
| 2412 | else |
| 2413 | Name = Context.DeclarationNames.getCXXOperatorName(DTN->getOperator()); |
| 2414 | } |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2415 | |
| 2416 | // Translate the parser's template argument list in our AST format. |
| 2417 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
| 2418 | Member.TemplateId->getTemplateArgs(), |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2419 | Member.TemplateId->NumArgs); |
| 2420 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2421 | TemplateArgumentListInfo TemplateArgs; |
| 2422 | TemplateArgs.setLAngleLoc(Member.TemplateId->LAngleLoc); |
| 2423 | TemplateArgs.setRAngleLoc(Member.TemplateId->RAngleLoc); |
| 2424 | translateTemplateArguments(TemplateArgsPtr, TemplateArgs); |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2425 | TemplateArgsPtr.release(); |
| 2426 | |
| 2427 | // Do we have the save the actual template name? We might need it... |
| 2428 | return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, |
| 2429 | Member.TemplateId->TemplateNameLoc, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2430 | Name, &TemplateArgs, DeclPtrTy(), |
Douglas Gregor | 30d60cb | 2009-11-03 19:44:04 +0000 | [diff] [blame] | 2431 | &SS); |
| 2432 | } |
| 2433 | |
| 2434 | // FIXME: We lose a lot of source information by mapping directly to the |
| 2435 | // DeclarationName. |
| 2436 | OwningExprResult Result |
| 2437 | = BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, |
| 2438 | Member.getSourceRange().getBegin(), |
| 2439 | GetNameFromUnqualifiedId(Member), |
| 2440 | ObjCImpDecl, &SS); |
| 2441 | |
| 2442 | if (Result.isInvalid() || HasTrailingLParen || |
| 2443 | Member.getKind() != UnqualifiedId::IK_DestructorName) |
| 2444 | return move(Result); |
| 2445 | |
| 2446 | // The only way a reference to a destructor can be used is to |
| 2447 | // immediately call them. Since the next token is not a '(', produce a |
| 2448 | // diagnostic and build the call now. |
| 2449 | Expr *E = (Expr *)Result.get(); |
| 2450 | SourceLocation ExpectedLParenLoc |
| 2451 | = PP.getLocForEndOfToken(Member.getSourceRange().getEnd()); |
| 2452 | Diag(E->getLocStart(), diag::err_dtor_expr_without_call) |
| 2453 | << isa<CXXPseudoDestructorExpr>(E) |
| 2454 | << CodeModificationHint::CreateInsertion(ExpectedLParenLoc, "()"); |
| 2455 | |
| 2456 | return ActOnCallExpr(0, move(Result), ExpectedLParenLoc, |
| 2457 | MultiExprArg(*this, 0, 0), 0, ExpectedLParenLoc); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2460 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 2461 | FunctionDecl *FD, |
| 2462 | ParmVarDecl *Param) { |
| 2463 | if (Param->hasUnparsedDefaultArg()) { |
| 2464 | Diag (CallLoc, |
| 2465 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2466 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2467 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2468 | diag::note_default_argument_declared_here); |
| 2469 | } else { |
| 2470 | if (Param->hasUninstantiatedDefaultArg()) { |
| 2471 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 2472 | |
| 2473 | // Instantiate the expression. |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2474 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 2475 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2476 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 2477 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | 01afeef | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2478 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2479 | |
John McCall | 76d824f | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2480 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | if (Result.isInvalid()) |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2482 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
| 2484 | if (SetParamDefaultArgument(Param, move(Result), |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2485 | /*FIXME:EqualLoc*/ |
| 2486 | UninstExpr->getSourceRange().getBegin())) |
| 2487 | return ExprError(); |
| 2488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2490 | Expr *DefaultExpr = Param->getDefaultArg(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2491 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2492 | // If the default expression creates temporaries, we need to |
| 2493 | // push them to the current stack of expression temporaries so they'll |
| 2494 | // be properly destroyed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | if (CXXExprWithTemporaries *E |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2496 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | assert(!E->shouldDestroyTemporaries() && |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2498 | "Can't destroy temporaries in a default argument expr!"); |
| 2499 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2500 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2501 | } |
| 2502 | } |
| 2503 | |
| 2504 | // We already type-checked the argument, so we know it works. |
| 2505 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 2506 | } |
| 2507 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2508 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2509 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2510 | /// function prototype Proto. Call is the call expression itself, and |
| 2511 | /// Fn is the function expression. For a C++ member function, this |
| 2512 | /// routine does not attempt to convert the object argument. Returns |
| 2513 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2514 | bool |
| 2515 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2516 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2517 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2518 | Expr **Args, unsigned NumArgs, |
| 2519 | SourceLocation RParenLoc) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2520 | // 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] | 2521 | // assignment, to the types of the corresponding parameter, ... |
| 2522 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2523 | bool Invalid = false; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2524 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2525 | // If too few arguments are available (and we don't have default |
| 2526 | // arguments for the remaining parameters), don't make the call. |
| 2527 | if (NumArgs < NumArgsInProto) { |
| 2528 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2529 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2530 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2531 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | // If too many are passed and not variadic, error on the extras and drop |
| 2535 | // them. |
| 2536 | if (NumArgs > NumArgsInProto) { |
| 2537 | if (!Proto->isVariadic()) { |
| 2538 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2539 | diag::err_typecheck_call_too_many_args) |
| 2540 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2541 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2542 | Args[NumArgs-1]->getLocEnd()); |
| 2543 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2544 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2545 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2546 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2547 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2548 | llvm::SmallVector<Expr *, 8> AllArgs; |
| 2549 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
| 2550 | Proto, 0, Args, NumArgs, AllArgs, Fn); |
| 2551 | if (Invalid) |
| 2552 | return true; |
| 2553 | unsigned TotalNumArgs = AllArgs.size(); |
| 2554 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 2555 | Call->setArg(i, AllArgs[i]); |
| 2556 | |
| 2557 | return false; |
| 2558 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2559 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2560 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 2561 | FunctionDecl *FDecl, |
| 2562 | const FunctionProtoType *Proto, |
| 2563 | unsigned FirstProtoArg, |
| 2564 | Expr **Args, unsigned NumArgs, |
| 2565 | llvm::SmallVector<Expr *, 8> &AllArgs, |
| 2566 | Expr *Fn) { |
| 2567 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2568 | unsigned NumArgsToCheck = NumArgs; |
| 2569 | bool Invalid = false; |
| 2570 | if (NumArgs != NumArgsInProto) |
| 2571 | // Use default arguments for missing arguments |
| 2572 | NumArgsToCheck = NumArgsInProto; |
| 2573 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2574 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2575 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2576 | QualType ProtoArgType = Proto->getArgType(i); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2577 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2578 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2579 | if (ArgIx < NumArgs) { |
| 2580 | Arg = Args[ArgIx++]; |
| 2581 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2582 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2583 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2584 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2585 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2586 | return true; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2587 | |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2588 | // Pass the argument. |
| 2589 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2590 | return true; |
Anders Carlsson | 78cfaa9 | 2009-11-13 04:34:45 +0000 | [diff] [blame] | 2591 | |
Anders Carlsson | 97df0b4 | 2009-11-13 17:04:35 +0000 | [diff] [blame] | 2592 | if (!ProtoArgType->isReferenceType()) |
| 2593 | Arg = MaybeBindToTemporary(Arg).takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2594 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 2595 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2596 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | OwningExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2598 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2599 | if (ArgExpr.isInvalid()) |
| 2600 | return true; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2601 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2602 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2603 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2604 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2605 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2606 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2607 | // If this is a variadic call, handle args passed through "...". |
| 2608 | if (Proto->isVariadic()) { |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2609 | VariadicCallType CallType = VariadicFunction; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2610 | if (Fn) { |
| 2611 | if (Fn->getType()->isBlockPointerType()) |
| 2612 | CallType = VariadicBlock; // Block |
| 2613 | else if (isa<MemberExpr>(Fn)) |
| 2614 | CallType = VariadicMethod; |
| 2615 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2616 | // Promote the arguments (C99 6.5.2.2p7). |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2617 | for (unsigned i = ArgIx; i < NumArgs; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2618 | Expr *Arg = Args[i]; |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2619 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame^] | 2620 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2621 | } |
| 2622 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2623 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2626 | /// \brief "Deconstruct" the function argument of a call expression to find |
| 2627 | /// the underlying declaration (if any), the name of the called function, |
| 2628 | /// whether argument-dependent lookup is available, whether it has explicit |
| 2629 | /// template arguments, etc. |
| 2630 | void Sema::DeconstructCallFunction(Expr *FnExpr, |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2631 | llvm::SmallVectorImpl<NamedDecl*> &Fns, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2632 | DeclarationName &Name, |
| 2633 | NestedNameSpecifier *&Qualifier, |
| 2634 | SourceRange &QualifierRange, |
| 2635 | bool &ArgumentDependentLookup, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2636 | bool &Overloaded, |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2637 | bool &HasExplicitTemplateArguments, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2638 | TemplateArgumentListInfo &ExplicitTemplateArgs) { |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2639 | // Set defaults for all of the output parameters. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2640 | Name = DeclarationName(); |
| 2641 | Qualifier = 0; |
| 2642 | QualifierRange = SourceRange(); |
| 2643 | ArgumentDependentLookup = getLangOptions().CPlusPlus; |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2644 | Overloaded = false; |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2645 | HasExplicitTemplateArguments = false; |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2646 | |
| 2647 | // Most of the explicit tracking of ArgumentDependentLookup in this |
| 2648 | // function can disappear when we handle unresolved |
| 2649 | // TemplateIdRefExprs properly. |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2650 | |
| 2651 | // If we're directly calling a function, get the appropriate declaration. |
| 2652 | // Also, in C++, keep track of whether we should perform argument-dependent |
| 2653 | // lookup and whether there were any explicitly-specified template arguments. |
| 2654 | while (true) { |
| 2655 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2656 | FnExpr = IcExpr->getSubExpr(); |
| 2657 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
| 2658 | // Parentheses around a function disable ADL |
| 2659 | // (C++0x [basic.lookup.argdep]p1). |
| 2660 | ArgumentDependentLookup = false; |
| 2661 | FnExpr = PExpr->getSubExpr(); |
| 2662 | } else if (isa<UnaryOperator>(FnExpr) && |
| 2663 | cast<UnaryOperator>(FnExpr)->getOpcode() |
| 2664 | == UnaryOperator::AddrOf) { |
| 2665 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2666 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2667 | Fns.push_back(cast<NamedDecl>(DRExpr->getDecl())); |
| 2668 | ArgumentDependentLookup = false; |
| 2669 | if ((Qualifier = DRExpr->getQualifier())) |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2670 | QualifierRange = DRExpr->getQualifierRange(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2671 | break; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2672 | } else if (UnresolvedLookupExpr *UnresLookup |
| 2673 | = dyn_cast<UnresolvedLookupExpr>(FnExpr)) { |
| 2674 | Name = UnresLookup->getName(); |
| 2675 | Fns.append(UnresLookup->decls_begin(), UnresLookup->decls_end()); |
| 2676 | ArgumentDependentLookup = UnresLookup->requiresADL(); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2677 | Overloaded = UnresLookup->isOverloaded(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2678 | if ((Qualifier = UnresLookup->getQualifier())) |
| 2679 | QualifierRange = UnresLookup->getQualifierRange(); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2680 | break; |
| 2681 | } else if (TemplateIdRefExpr *TemplateIdRef |
| 2682 | = dyn_cast<TemplateIdRefExpr>(FnExpr)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2683 | if (NamedDecl *Function |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2684 | = TemplateIdRef->getTemplateName().getAsTemplateDecl()) { |
| 2685 | Name = Function->getDeclName(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2686 | Fns.push_back(Function); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2687 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2688 | else { |
| 2689 | OverloadedFunctionDecl *Overload |
| 2690 | = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl(); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2691 | Name = Overload->getDeclName(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2692 | Fns.append(Overload->function_begin(), Overload->function_end()); |
| 2693 | } |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2694 | Overloaded = true; |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2695 | HasExplicitTemplateArguments = true; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2696 | TemplateIdRef->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2697 | |
| 2698 | // C++ [temp.arg.explicit]p6: |
| 2699 | // [Note: For simple function names, argument dependent lookup (3.4.2) |
| 2700 | // applies even when the function name is not visible within the |
| 2701 | // scope of the call. This is because the call still has the syntactic |
| 2702 | // form of a function call (3.4.1). But when a function template with |
| 2703 | // explicit template arguments is used, the call does not have the |
| 2704 | // correct syntactic form unless there is a function template with |
| 2705 | // that name visible at the point of the call. If no such name is |
| 2706 | // visible, the call is not syntactically well-formed and |
| 2707 | // argument-dependent lookup does not apply. If some such name is |
| 2708 | // visible, argument dependent lookup applies and additional function |
| 2709 | // templates may be found in other namespaces. |
| 2710 | // |
| 2711 | // The summary of this paragraph is that, if we get to this point and the |
| 2712 | // template-id was not a qualified name, then argument-dependent lookup |
| 2713 | // is still possible. |
| 2714 | if ((Qualifier = TemplateIdRef->getQualifier())) { |
| 2715 | ArgumentDependentLookup = false; |
| 2716 | QualifierRange = TemplateIdRef->getQualifierRange(); |
| 2717 | } |
| 2718 | break; |
| 2719 | } else { |
| 2720 | // Any kind of name that does not refer to a declaration (or |
| 2721 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2722 | ArgumentDependentLookup = false; |
| 2723 | break; |
| 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2728 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2729 | /// This provides the location of the left/right parens and a list of comma |
| 2730 | /// locations. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2731 | Action::OwningExprResult |
| 2732 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2733 | MultiExprArg args, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2734 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2735 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2736 | |
| 2737 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2738 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2740 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2741 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 2742 | assert(Fn && "no function call expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2743 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2744 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2745 | // If this is a pseudo-destructor expression, build the call immediately. |
| 2746 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 2747 | if (NumArgs > 0) { |
| 2748 | // Pseudo-destructor calls should not have any arguments. |
| 2749 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 2750 | << CodeModificationHint::CreateRemoval( |
| 2751 | SourceRange(Args[0]->getLocStart(), |
| 2752 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2753 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2754 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2755 | Args[I]->Destroy(Context); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2757 | NumArgs = 0; |
| 2758 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2760 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 2761 | RParenLoc)); |
| 2762 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2764 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2765 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2766 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2767 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2768 | bool Dependent = false; |
| 2769 | if (Fn->isTypeDependent()) |
| 2770 | Dependent = true; |
| 2771 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2772 | Dependent = true; |
| 2773 | |
| 2774 | if (Dependent) |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2775 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2776 | Context.DependentTy, RParenLoc)); |
| 2777 | |
| 2778 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2779 | if (Fn->getType()->isRecordType()) |
| 2780 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2781 | CommaLocs, RParenLoc)); |
| 2782 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2783 | // Determine whether this is a call to a member function. |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2784 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) { |
| 2785 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
| 2786 | if (isa<OverloadedFunctionDecl>(MemDecl) || |
| 2787 | isa<CXXMethodDecl>(MemDecl) || |
| 2788 | (isa<FunctionTemplateDecl>(MemDecl) && |
| 2789 | isa<CXXMethodDecl>( |
| 2790 | cast<FunctionTemplateDecl>(MemDecl)->getTemplatedDecl()))) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2791 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2792 | CommaLocs, RParenLoc)); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2793 | } |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2794 | |
| 2795 | // Determine whether this is a call to a pointer-to-member function. |
| 2796 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Fn->IgnoreParens())) { |
| 2797 | if (BO->getOpcode() == BinaryOperator::PtrMemD || |
| 2798 | BO->getOpcode() == BinaryOperator::PtrMemI) { |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2799 | if (const FunctionProtoType *FPT = |
| 2800 | dyn_cast<FunctionProtoType>(BO->getType())) { |
| 2801 | QualType ResultTy = FPT->getResultType().getNonReferenceType(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2802 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2803 | ExprOwningPtr<CXXMemberCallExpr> |
| 2804 | TheCall(this, new (Context) CXXMemberCallExpr(Context, BO, Args, |
| 2805 | NumArgs, ResultTy, |
| 2806 | RParenLoc)); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2807 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2808 | if (CheckCallReturnType(FPT->getResultType(), |
| 2809 | BO->getRHS()->getSourceRange().getBegin(), |
| 2810 | TheCall.get(), 0)) |
| 2811 | return ExprError(); |
Anders Carlsson | 63dce02 | 2009-10-15 00:41:48 +0000 | [diff] [blame] | 2812 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2813 | if (ConvertArgumentsForCall(&*TheCall, BO, 0, FPT, Args, NumArgs, |
| 2814 | RParenLoc)) |
| 2815 | return ExprError(); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2816 | |
Fariborz Jahanian | 42f6663 | 2009-10-28 16:49:46 +0000 | [diff] [blame] | 2817 | return Owned(MaybeBindToTemporary(TheCall.release()).release()); |
| 2818 | } |
| 2819 | return ExprError(Diag(Fn->getLocStart(), |
| 2820 | diag::err_typecheck_call_not_function) |
| 2821 | << Fn->getType() << Fn->getSourceRange()); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 2822 | } |
| 2823 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2826 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2827 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2828 | // lookup and whether there were any explicitly-specified template arguments. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2829 | llvm::SmallVector<NamedDecl*,8> Fns; |
| 2830 | DeclarationName UnqualifiedName; |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2831 | bool Overloaded; |
| 2832 | bool ADL; |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2833 | bool HasExplicitTemplateArgs = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2834 | TemplateArgumentListInfo ExplicitTemplateArgs; |
Douglas Gregor | cabea40 | 2009-09-22 15:41:20 +0000 | [diff] [blame] | 2835 | NestedNameSpecifier *Qualifier = 0; |
| 2836 | SourceRange QualifierRange; |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2837 | DeconstructCallFunction(Fn, Fns, UnqualifiedName, Qualifier, QualifierRange, |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2838 | ADL, Overloaded, HasExplicitTemplateArgs, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2839 | ExplicitTemplateArgs); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2840 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2841 | NamedDecl *NDecl; // the specific declaration we're calling, if applicable |
| 2842 | FunctionDecl *FDecl; // same, if it's known to be a function |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2843 | |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2844 | if (Overloaded || ADL) { |
| 2845 | #ifndef NDEBUG |
| 2846 | if (ADL) { |
| 2847 | // To do ADL, we must have found an unqualified name. |
| 2848 | assert(UnqualifiedName && "found no unqualified name for ADL"); |
| 2849 | |
| 2850 | // We don't perform ADL for implicit declarations of builtins. |
| 2851 | // Verify that this was correctly set up. |
| 2852 | if (Fns.size() == 1 && (FDecl = dyn_cast<FunctionDecl>(Fns[0])) && |
| 2853 | FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2854 | assert(0 && "performing ADL for builtin"); |
| 2855 | |
| 2856 | // We don't perform ADL in C. |
| 2857 | assert(getLangOptions().CPlusPlus && "ADL enabled in C"); |
| 2858 | } |
| 2859 | |
| 2860 | if (Overloaded) { |
| 2861 | // To be overloaded, we must either have multiple functions or |
| 2862 | // at least one function template (which is effectively an |
| 2863 | // infinite set of functions). |
| 2864 | assert((Fns.size() > 1 || |
| 2865 | (Fns.size() == 1 && |
| 2866 | isa<FunctionTemplateDecl>(Fns[0]->getUnderlyingDecl()))) |
| 2867 | && "unrecognized overload situation"); |
| 2868 | } |
| 2869 | #endif |
| 2870 | |
| 2871 | FDecl = ResolveOverloadedCallFn(Fn, Fns, UnqualifiedName, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2872 | (HasExplicitTemplateArgs ? &ExplicitTemplateArgs : 0), |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2873 | LParenLoc, Args, NumArgs, CommaLocs, |
| 2874 | RParenLoc, ADL); |
| 2875 | if (!FDecl) |
| 2876 | return ExprError(); |
| 2877 | |
| 2878 | Fn = FixOverloadedFunctionReference(Fn, FDecl); |
| 2879 | |
| 2880 | NDecl = FDecl; |
| 2881 | } else { |
| 2882 | assert(Fns.size() <= 1 && "overloaded without Overloaded flag"); |
| 2883 | if (Fns.empty()) |
| 2884 | NDecl = FDecl = 0; |
| 2885 | else { |
| 2886 | NDecl = Fns[0]; |
Douglas Gregor | a727cb9 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2887 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2888 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2889 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2890 | |
| 2891 | // Promote the function operand. |
| 2892 | UsualUnaryConversions(Fn); |
| 2893 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2894 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2895 | // of arguments and function on error. |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2896 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2897 | Args, NumArgs, |
| 2898 | Context.BoolTy, |
| 2899 | RParenLoc)); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2900 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2901 | const FunctionType *FuncT; |
| 2902 | if (!Fn->getType()->isBlockPointerType()) { |
| 2903 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2904 | // have type pointer to function". |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2905 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2906 | if (PT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2907 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2908 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2909 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2910 | } else { // This is a block call. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2911 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2912 | getAs<FunctionType>(); |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2913 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2914 | if (FuncT == 0) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2915 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2916 | << Fn->getType() << Fn->getSourceRange()); |
| 2917 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2918 | // Check for a valid return type |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 2919 | if (CheckCallReturnType(FuncT->getResultType(), |
| 2920 | Fn->getSourceRange().getBegin(), TheCall.get(), |
| 2921 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2922 | return ExprError(); |
| 2923 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2924 | // We know the result type of the call, set it. |
Douglas Gregor | 786ab21 | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2925 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2927 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2928 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2929 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2930 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2931 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2932 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2933 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2934 | if (FDecl) { |
| 2935 | // Check if we have too few/too many template arguments, based |
| 2936 | // on our knowledge of the function definition. |
| 2937 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | ddcd132 | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2938 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2939 | const FunctionProtoType *Proto = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2940 | Def->getType()->getAs<FunctionProtoType>(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2941 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2942 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2943 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2944 | } |
| 2945 | } |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2946 | } |
| 2947 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2948 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2949 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2950 | Expr *Arg = Args[i]; |
| 2951 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2952 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2953 | Arg->getType(), |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2954 | PDiag(diag::err_call_incomplete_argument) |
| 2955 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2956 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2957 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2958 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2959 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2961 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2962 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2963 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2964 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2965 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2966 | // Check for sentinels |
| 2967 | if (NDecl) |
| 2968 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2969 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2970 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2971 | if (FDecl) { |
| 2972 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 2973 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 2975 | if (unsigned BuiltinID = FDecl->getBuiltinID()) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2976 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 2977 | } else if (NDecl) { |
| 2978 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 2979 | return ExprError(); |
| 2980 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2981 | |
Anders Carlsson | f898401 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 2982 | return MaybeBindToTemporary(TheCall.take()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2985 | Action::OwningExprResult |
| 2986 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2987 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2988 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 2989 | //FIXME: Preserve type source info. |
| 2990 | QualType literalType = GetTypeFromParser(Ty); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 2991 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2992 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2993 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2994 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2995 | if (literalType->isArrayType()) { |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2996 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2997 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2998 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2999 | } else if (!literalType->isDependentType() && |
| 3000 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3001 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3002 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3003 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3004 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3005 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3006 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 5fb5397 | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3007 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3008 | return ExprError(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3009 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3010 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3011 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3012 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3013 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3014 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3015 | InitExpr.release(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3016 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3017 | literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3020 | Action::OwningExprResult |
| 3021 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3022 | SourceLocation RBraceLoc) { |
| 3023 | unsigned NumInit = initlist.size(); |
| 3024 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3025 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3026 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3027 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3028 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3029 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3030 | RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3031 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3032 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3035 | static CastExpr::CastKind getScalarCastKind(ASTContext &Context, |
| 3036 | QualType SrcTy, QualType DestTy) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3037 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3038 | return CastExpr::CK_NoOp; |
| 3039 | |
| 3040 | if (SrcTy->hasPointerRepresentation()) { |
| 3041 | if (DestTy->hasPointerRepresentation()) |
| 3042 | return CastExpr::CK_BitCast; |
| 3043 | if (DestTy->isIntegerType()) |
| 3044 | return CastExpr::CK_PointerToIntegral; |
| 3045 | } |
| 3046 | |
| 3047 | if (SrcTy->isIntegerType()) { |
| 3048 | if (DestTy->isIntegerType()) |
| 3049 | return CastExpr::CK_IntegralCast; |
| 3050 | if (DestTy->hasPointerRepresentation()) |
| 3051 | return CastExpr::CK_IntegralToPointer; |
| 3052 | if (DestTy->isRealFloatingType()) |
| 3053 | return CastExpr::CK_IntegralToFloating; |
| 3054 | } |
| 3055 | |
| 3056 | if (SrcTy->isRealFloatingType()) { |
| 3057 | if (DestTy->isRealFloatingType()) |
| 3058 | return CastExpr::CK_FloatingCast; |
| 3059 | if (DestTy->isIntegerType()) |
| 3060 | return CastExpr::CK_FloatingToIntegral; |
| 3061 | } |
| 3062 | |
| 3063 | // FIXME: Assert here. |
| 3064 | // assert(false && "Unhandled cast combination!"); |
| 3065 | return CastExpr::CK_Unknown; |
| 3066 | } |
| 3067 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3068 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | 955a067 | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3069 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3071 | CXXMethodDecl *& ConversionDecl, |
| 3072 | bool FunctionalStyle) { |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3073 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | 1cec0c4 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3074 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3075 | ConversionDecl); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3076 | |
Eli Friedman | da8d4de | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3077 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3078 | |
| 3079 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3080 | // type needs to be scalar. |
| 3081 | if (castType->isVoidType()) { |
| 3082 | // Cast to void allows any expr type. |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3083 | Kind = CastExpr::CK_ToVoid; |
| 3084 | return false; |
| 3085 | } |
| 3086 | |
| 3087 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3088 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3089 | (castType->isStructureType() || castType->isUnionType())) { |
| 3090 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3091 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3092 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3093 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | ec14377 | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3094 | Kind = CastExpr::CK_NoOp; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3095 | return false; |
| 3096 | } |
| 3097 | |
| 3098 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3099 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3100 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3101 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3102 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3103 | Field != FieldEnd; ++Field) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3104 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| 3105 | castExpr->getType())) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3106 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3107 | << castExpr->getSourceRange(); |
| 3108 | break; |
| 3109 | } |
| 3110 | } |
| 3111 | if (Field == FieldEnd) |
| 3112 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3113 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | ec14377 | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3114 | Kind = CastExpr::CK_ToUnion; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3115 | return false; |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3116 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3117 | |
| 3118 | // Reject any other conversions to non-scalar types. |
| 3119 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| 3120 | << castType << castExpr->getSourceRange(); |
| 3121 | } |
| 3122 | |
| 3123 | if (!castExpr->getType()->isScalarType() && |
| 3124 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3125 | return Diag(castExpr->getLocStart(), |
| 3126 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3127 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3130 | if (castType->isExtVectorType()) |
| 3131 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| 3132 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3133 | if (castType->isVectorType()) |
| 3134 | return CheckVectorCast(TyR, castType, castExpr->getType(), Kind); |
| 3135 | if (castExpr->getType()->isVectorType()) |
| 3136 | return CheckVectorCast(TyR, castExpr->getType(), castType, Kind); |
| 3137 | |
| 3138 | if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) |
Steve Naroff | b47acdb | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3139 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3140 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3141 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3142 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 3143 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3144 | if (!castType->isArithmeticType()) { |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3145 | QualType castExprType = castExpr->getType(); |
| 3146 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3147 | return Diag(castExpr->getLocStart(), |
| 3148 | diag::err_cast_pointer_from_non_pointer_int) |
| 3149 | << castExprType << castExpr->getSourceRange(); |
| 3150 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3151 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3152 | return Diag(castExpr->getLocStart(), |
| 3153 | diag::err_cast_pointer_to_non_pointer_int) |
| 3154 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3155 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3156 | |
| 3157 | Kind = getScalarCastKind(Context, castExpr->getType(), castType); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3158 | return false; |
| 3159 | } |
| 3160 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3161 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| 3162 | CastExpr::CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3163 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3164 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3165 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3166 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3167 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3168 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3169 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3170 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3171 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3172 | } else |
| 3173 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3174 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3175 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3176 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3177 | Kind = CastExpr::CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3178 | return false; |
| 3179 | } |
| 3180 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3181 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, Expr *&CastExpr, |
| 3182 | CastExpr::CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3183 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3184 | |
| 3185 | QualType SrcTy = CastExpr->getType(); |
| 3186 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3187 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3188 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3189 | if (SrcTy->isVectorType()) { |
| 3190 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3191 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3192 | << DestTy << SrcTy << R; |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3193 | Kind = CastExpr::CK_BitCast; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3194 | return false; |
| 3195 | } |
| 3196 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3197 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3198 | // conversion will take place first from scalar to elt type, and then |
| 3199 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3200 | if (SrcTy->isPointerType()) |
| 3201 | return Diag(R.getBegin(), |
| 3202 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3203 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3204 | |
| 3205 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| 3206 | ImpCastExprToType(CastExpr, DestElemTy, |
| 3207 | getScalarCastKind(Context, SrcTy, DestElemTy)); |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 3208 | |
| 3209 | Kind = CastExpr::CK_VectorSplat; |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3210 | return false; |
| 3211 | } |
| 3212 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3213 | Action::OwningExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3214 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3215 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3216 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3218 | assert((Ty != 0) && (Op.get() != 0) && |
| 3219 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3220 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3221 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3222 | //FIXME: Preserve type source info. |
| 3223 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3225 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3226 | if (isa<ParenListExpr>(castExpr)) |
| 3227 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3228 | CXXMethodDecl *Method = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3230 | Kind, Method)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3231 | return ExprError(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3232 | |
| 3233 | if (Method) { |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3234 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3235 | Method, move(Op)); |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3236 | |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3237 | if (CastArg.isInvalid()) |
| 3238 | return ExprError(); |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3239 | |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 3240 | castExpr = CastArg.takeAs<Expr>(); |
| 3241 | } else { |
| 3242 | Op.release(); |
Fariborz Jahanian | 3df8767 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3243 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3245 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3246 | Kind, castExpr, castType, |
Anders Carlsson | f10e414 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3247 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3248 | } |
| 3249 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3250 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3251 | /// of comma binary operators. |
| 3252 | Action::OwningExprResult |
| 3253 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3254 | Expr *expr = EA.takeAs<Expr>(); |
| 3255 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3256 | if (!E) |
| 3257 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3258 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3259 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3261 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3262 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3263 | Owned(E->getExpr(i))); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3265 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3266 | } |
| 3267 | |
| 3268 | Action::OwningExprResult |
| 3269 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3270 | SourceLocation RParenLoc, ExprArg Op, |
| 3271 | QualType Ty) { |
| 3272 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3273 | |
| 3274 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3275 | // then handle it as such. |
| 3276 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3277 | if (PE->getNumExprs() == 0) { |
| 3278 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3279 | return ExprError(); |
| 3280 | } |
| 3281 | |
| 3282 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3283 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3284 | initExprs.push_back(PE->getExpr(i)); |
| 3285 | |
| 3286 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3287 | // braces instead of the original commas. |
| 3288 | Op.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3289 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3290 | initExprs.size(), RParenLoc); |
| 3291 | E->setType(Ty); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3292 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3293 | Owned(E)); |
| 3294 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | // 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] | 3296 | // sequence of BinOp comma operators. |
| 3297 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3298 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 3303 | SourceLocation R, |
| 3304 | MultiExprArg Val) { |
| 3305 | unsigned nexprs = Val.size(); |
| 3306 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| 3307 | assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); |
| 3308 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| 3309 | return Owned(expr); |
| 3310 | } |
| 3311 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3312 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3313 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3314 | /// C99 6.5.15 |
| 3315 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3316 | SourceLocation QuestionLoc) { |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3317 | // C++ is sufficiently different to merit its own checker. |
| 3318 | if (getLangOptions().CPlusPlus) |
| 3319 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3320 | |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 3321 | CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); |
| 3322 | |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3323 | UsualUnaryConversions(Cond); |
| 3324 | UsualUnaryConversions(LHS); |
| 3325 | UsualUnaryConversions(RHS); |
| 3326 | QualType CondTy = Cond->getType(); |
| 3327 | QualType LHSTy = LHS->getType(); |
| 3328 | QualType RHSTy = RHS->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3329 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3330 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3331 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3332 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3333 | << CondTy; |
| 3334 | return QualType(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3335 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3336 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3337 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3338 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3339 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3340 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3341 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3342 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3343 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3344 | UsualArithmeticConversions(LHS, RHS); |
| 3345 | return LHS->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3346 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3347 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3348 | // If both operands are the same structure or union type, the result is that |
| 3349 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3350 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3351 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3352 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3353 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3354 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3355 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3356 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3357 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3358 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3359 | // 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] | 3360 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3361 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3362 | if (!LHSTy->isVoidType()) |
| 3363 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3364 | << RHS->getSourceRange(); |
| 3365 | if (!RHSTy->isVoidType()) |
| 3366 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3367 | << LHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3368 | ImpCastExprToType(LHS, Context.VoidTy, CastExpr::CK_ToVoid); |
| 3369 | ImpCastExprToType(RHS, Context.VoidTy, CastExpr::CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3370 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3371 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3372 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3373 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3374 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3375 | RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3376 | // promote the null to a pointer. |
| 3377 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3378 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3379 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3380 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3381 | LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3382 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_Unknown); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3383 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3384 | } |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3385 | // Handle things like Class and struct objc_class*. Here we case the result |
| 3386 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 3387 | // redefinition type if an attempt is made to access its fields. |
| 3388 | if (LHSTy->isObjCClassType() && |
| 3389 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3390 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3391 | return LHSTy; |
| 3392 | } |
| 3393 | if (RHSTy->isObjCClassType() && |
| 3394 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3395 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3396 | return RHSTy; |
| 3397 | } |
| 3398 | // And the same for struct objc_object* / id |
| 3399 | if (LHSTy->isObjCIdType() && |
| 3400 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3401 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3402 | return LHSTy; |
| 3403 | } |
| 3404 | if (RHSTy->isObjCIdType() && |
| 3405 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3406 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_BitCast); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3407 | return RHSTy; |
| 3408 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3409 | // Handle block pointer types. |
| 3410 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3411 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3412 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3413 | QualType destType = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3414 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
| 3415 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3416 | return destType; |
| 3417 | } |
| 3418 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3419 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3420 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3421 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3422 | // We have 2 block pointer types. |
| 3423 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3424 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3425 | return LHSTy; |
| 3426 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3427 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3428 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3429 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3430 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3431 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3432 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3433 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3434 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3435 | // In this situation, we assume void* type. No especially good |
| 3436 | // reason, but this is what gcc does, and we do have to pick |
| 3437 | // to get a consistent AST. |
| 3438 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3439 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3440 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3441 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3442 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3443 | // The block pointer types are compatible. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3444 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3445 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3446 | return LHSTy; |
| 3447 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3448 | // Check constraints for Objective-C object pointers types. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3449 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3450 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3451 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3452 | // Two identical object pointer types are always compatible. |
| 3453 | return LHSTy; |
| 3454 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3455 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 3456 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3457 | QualType compositeType = LHSTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3459 | // If both operands are interfaces and either operand can be |
| 3460 | // assigned to the other, use that type as the composite |
| 3461 | // type. This allows |
| 3462 | // xxx ? (A*) a : (B*) b |
| 3463 | // where B is a subclass of A. |
| 3464 | // |
| 3465 | // Additionally, as for assignment, if either type is 'id' |
| 3466 | // allow silent coercion. Finally, if the types are |
| 3467 | // incompatible then make sure to use 'id' as the composite |
| 3468 | // type so the result is acceptable for sending messages to. |
| 3469 | |
| 3470 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3471 | // It could return the composite type. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3472 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
Fariborz Jahanian | a83c016 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3473 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3474 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
Fariborz Jahanian | a83c016 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3475 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3476 | } else if ((LHSTy->isObjCQualifiedIdType() || |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3477 | RHSTy->isObjCQualifiedIdType()) && |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3478 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3479 | // Need to handle "id<xx>" explicitly. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3480 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3481 | // id. Currently localizing to here until clear this should be |
| 3482 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 3483 | compositeType = Context.getObjCIdType(); |
| 3484 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3485 | compositeType = Context.getObjCIdType(); |
Fariborz Jahanian | ef8b8ce | 2009-10-27 23:02:38 +0000 | [diff] [blame] | 3486 | } else if (!(compositeType = |
| 3487 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 3488 | ; |
| 3489 | else { |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3490 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3491 | << LHSTy << RHSTy |
| 3492 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3493 | QualType incompatTy = Context.getObjCIdType(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3494 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3495 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3496 | return incompatTy; |
| 3497 | } |
| 3498 | // The object pointer types are compatible. |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3499 | ImpCastExprToType(LHS, compositeType, CastExpr::CK_BitCast); |
| 3500 | ImpCastExprToType(RHS, compositeType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3501 | return compositeType; |
| 3502 | } |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3503 | // Check Objective-C object pointer types and 'void *' |
| 3504 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3505 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3506 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3507 | QualType destPointee |
| 3508 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3509 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3510 | // Add qualifiers if necessary. |
| 3511 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 3512 | // Promote to void*. |
| 3513 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3514 | return destType; |
| 3515 | } |
| 3516 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3517 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3518 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3519 | QualType destPointee |
| 3520 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3521 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3522 | // Add qualifiers if necessary. |
| 3523 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
| 3524 | // Promote to void*. |
| 3525 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 85d9715 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3526 | return destType; |
| 3527 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3528 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3529 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3530 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3531 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3532 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3533 | |
| 3534 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3535 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3536 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3537 | QualType destPointee |
| 3538 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3539 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3540 | // Add qualifiers if necessary. |
| 3541 | ImpCastExprToType(LHS, destType, CastExpr::CK_NoOp); |
| 3542 | // Promote to void*. |
| 3543 | ImpCastExprToType(RHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3544 | return destType; |
| 3545 | } |
| 3546 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 3547 | QualType destPointee |
| 3548 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3549 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3550 | // Add qualifiers if necessary. |
Eli Friedman | b0bc559 | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3551 | ImpCastExprToType(RHS, destType, CastExpr::CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3552 | // Promote to void*. |
Eli Friedman | b0bc559 | 2009-11-17 01:22:05 +0000 | [diff] [blame] | 3553 | ImpCastExprToType(LHS, destType, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3554 | return destType; |
| 3555 | } |
| 3556 | |
| 3557 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3558 | // Two identical pointer types are always compatible. |
| 3559 | return LHSTy; |
| 3560 | } |
| 3561 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3562 | rhptee.getUnqualifiedType())) { |
| 3563 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3564 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3565 | // In this situation, we assume void* type. No especially good |
| 3566 | // reason, but this is what gcc does, and we do have to pick |
| 3567 | // to get a consistent AST. |
| 3568 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3569 | ImpCastExprToType(LHS, incompatTy, CastExpr::CK_BitCast); |
| 3570 | ImpCastExprToType(RHS, incompatTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3571 | return incompatTy; |
| 3572 | } |
| 3573 | // The pointer types are compatible. |
| 3574 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3575 | // differently qualified versions of compatible types, the result type is |
| 3576 | // a pointer to an appropriately qualified version of the *composite* |
| 3577 | // type. |
| 3578 | // FIXME: Need to calculate the composite type. |
| 3579 | // FIXME: Need to add qualifiers |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3580 | ImpCastExprToType(LHS, LHSTy, CastExpr::CK_BitCast); |
| 3581 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3582 | return LHSTy; |
| 3583 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3584 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3585 | // GCC compatibility: soften pointer/integer mismatch. |
| 3586 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3587 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3588 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3589 | ImpCastExprToType(LHS, RHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3590 | return RHSTy; |
| 3591 | } |
| 3592 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3593 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3594 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3595 | ImpCastExprToType(RHS, LHSTy, CastExpr::CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3596 | return LHSTy; |
| 3597 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3598 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3599 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3600 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3601 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 3602 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3605 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3606 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3607 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3608 | SourceLocation ColonLoc, |
| 3609 | ExprArg Cond, ExprArg LHS, |
| 3610 | ExprArg RHS) { |
| 3611 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3612 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3613 | |
| 3614 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3615 | // was the condition. |
| 3616 | bool isLHSNull = LHSExpr == 0; |
| 3617 | if (isLHSNull) |
| 3618 | LHSExpr = CondExpr; |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3619 | |
| 3620 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | daaa9f2 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 3621 | RHSExpr, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 3622 | if (result.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3623 | return ExprError(); |
| 3624 | |
| 3625 | Cond.release(); |
| 3626 | LHS.release(); |
| 3627 | RHS.release(); |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3628 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3629 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 7e112b0 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3630 | ColonLoc, RHSExpr, result)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3633 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3634 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3635 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3636 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3637 | // FIXME: add a couple examples in this comment. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3638 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3639 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3640 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3641 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3642 | if ((lhsType->isObjCClassType() && |
| 3643 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3644 | (rhsType->isObjCClassType() && |
| 3645 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3646 | return Compatible; |
| 3647 | } |
| 3648 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3649 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3650 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 3651 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3652 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3653 | // make sure we operate on the canonical type |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3654 | lhptee = Context.getCanonicalType(lhptee); |
| 3655 | rhptee = Context.getCanonicalType(rhptee); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3656 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3657 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3658 | |
| 3659 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3660 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3661 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | ece8582 | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3662 | // FIXME: Handle ExtQualType |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3663 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3664 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 3665 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3666 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3667 | // 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] | 3668 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3669 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3670 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3671 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3672 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3673 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3674 | assert(rhptee->isFunctionType()); |
| 3675 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3676 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3678 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3679 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3680 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3681 | |
| 3682 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3683 | assert(lhptee->isFunctionType()); |
| 3684 | return FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3685 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3686 | // 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] | 3687 | // unqualified versions of compatible types, ... |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3688 | lhptee = lhptee.getUnqualifiedType(); |
| 3689 | rhptee = rhptee.getUnqualifiedType(); |
| 3690 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3691 | // Check if the pointee types are compatible ignoring the sign. |
| 3692 | // We explicitly check for char so that we catch "char" vs |
| 3693 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3694 | if (lhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3695 | lhptee = Context.UnsignedCharTy; |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3696 | else if (lhptee->isSignedIntegerType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3697 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3698 | |
| 3699 | if (rhptee->isCharType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3700 | rhptee = Context.UnsignedCharTy; |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3701 | else if (rhptee->isSignedIntegerType()) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3702 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 3703 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3704 | if (lhptee == rhptee) { |
| 3705 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3706 | // takes priority over sign incompatibility because the sign |
| 3707 | // warning can be disabled. |
| 3708 | if (ConvTy != Compatible) |
| 3709 | return ConvTy; |
| 3710 | return IncompatiblePointerSign; |
| 3711 | } |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 3712 | |
| 3713 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 3714 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 3715 | // the eventual target type is the same and the pointers have the same |
| 3716 | // level of indirection, this must be the issue. |
| 3717 | if (lhptee->isPointerType() && rhptee->isPointerType()) { |
| 3718 | do { |
| 3719 | lhptee = lhptee->getAs<PointerType>()->getPointeeType(); |
| 3720 | rhptee = rhptee->getAs<PointerType>()->getPointeeType(); |
| 3721 | |
| 3722 | lhptee = Context.getCanonicalType(lhptee); |
| 3723 | rhptee = Context.getCanonicalType(rhptee); |
| 3724 | } while (lhptee->isPointerType() && rhptee->isPointerType()); |
| 3725 | |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3726 | if (Context.hasSameUnqualifiedType(lhptee, rhptee)) |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 3727 | return IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3730 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3731 | return IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3732 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3733 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3736 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3737 | /// block pointer types are compatible or whether a block and normal pointer |
| 3738 | /// are compatible. It is more restrict than comparing two function pointer |
| 3739 | // types. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3740 | Sema::AssignConvertType |
| 3741 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3742 | QualType rhsType) { |
| 3743 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3744 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3745 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3746 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 3747 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3748 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3749 | // make sure we operate on the canonical type |
| 3750 | lhptee = Context.getCanonicalType(lhptee); |
| 3751 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3752 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3753 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3754 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3755 | // For blocks we enforce that qualifiers are identical. |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3756 | if (lhptee.getLocalCVRQualifiers() != rhptee.getLocalCVRQualifiers()) |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3757 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3758 | |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3759 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3760 | return IncompatibleBlockPointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3761 | return ConvTy; |
| 3762 | } |
| 3763 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3764 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3765 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 3766 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3767 | /// |
| 3768 | /// int a, *pint; |
| 3769 | /// short *pshort; |
| 3770 | /// struct foo *pfoo; |
| 3771 | /// |
| 3772 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3773 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3774 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3775 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3776 | /// |
| 3777 | /// 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] | 3778 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 3779 | /// |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3780 | Sema::AssignConvertType |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3781 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3782 | // Get canonical types. We're not formatting these types, just comparing |
| 3783 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3784 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3785 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3786 | |
| 3787 | if (lhsType == rhsType) |
Chris Lattner | f5c973d | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3788 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 3789 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3790 | if ((lhsType->isObjCClassType() && |
| 3791 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3792 | (rhsType->isObjCClassType() && |
| 3793 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3794 | return Compatible; |
| 3795 | } |
| 3796 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3797 | // If the left-hand side is a reference type, then we are in a |
| 3798 | // (rare!) case where we've allowed the use of references in C, |
| 3799 | // e.g., as a parameter type in a built-in function. In this case, |
| 3800 | // just make sure that the type referenced is compatible with the |
| 3801 | // right-hand side type. The caller is responsible for adjusting |
| 3802 | // lhsType so that the resulting expression does not have reference |
| 3803 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3804 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3805 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3806 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3807 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3808 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3809 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 3810 | // to the same ExtVector type. |
| 3811 | if (lhsType->isExtVectorType()) { |
| 3812 | if (rhsType->isExtVectorType()) |
| 3813 | return lhsType == rhsType ? Compatible : Incompatible; |
| 3814 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 3815 | return Compatible; |
| 3816 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3817 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3818 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3819 | // 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] | 3820 | // 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] | 3821 | // no bits are changed but the result type is different. |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3822 | if (getLangOptions().LaxVectorConversions && |
| 3823 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3824 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3825 | return IncompatibleVectors; |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3826 | } |
| 3827 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3828 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3829 | |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3830 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3831 | return Compatible; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3832 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3833 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3834 | if (rhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3835 | return IntToPointer; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3836 | |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3837 | if (isa<PointerType>(rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3838 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3839 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3840 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3841 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3842 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3843 | return Compatible; |
| 3844 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3845 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3846 | if (rhsType->getAs<BlockPointerType>()) { |
| 3847 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3848 | return Compatible; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3849 | |
| 3850 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3851 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3852 | return Compatible; |
| 3853 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3854 | return Incompatible; |
| 3855 | } |
| 3856 | |
| 3857 | if (isa<BlockPointerType>(lhsType)) { |
| 3858 | if (rhsType->isIntegerType()) |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3859 | return IntToBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3860 | |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3861 | // Treat block pointers as objects. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3862 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3863 | return Compatible; |
| 3864 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3865 | if (rhsType->isBlockPointerType()) |
| 3866 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3867 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3868 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3869 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3870 | return Compatible; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3871 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3872 | return Incompatible; |
| 3873 | } |
| 3874 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3875 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 3876 | if (rhsType->isIntegerType()) |
| 3877 | return IntToPointer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3879 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3880 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3881 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 3882 | return Compatible; |
| 3883 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3884 | } |
| 3885 | if (rhsType->isObjCObjectPointerType()) { |
Steve Naroff | 1329fa0 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3886 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 3887 | return Compatible; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3888 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 3889 | return Compatible; |
Steve Naroff | 8e6aee5 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3890 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 3891 | return IncompatibleObjCQualifiedId; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3892 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3893 | } |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3894 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3895 | if (RHSPT->getPointeeType()->isVoidType()) |
| 3896 | return Compatible; |
| 3897 | } |
| 3898 | // Treat block pointers as objects. |
| 3899 | if (rhsType->isBlockPointerType()) |
| 3900 | return Compatible; |
| 3901 | return Incompatible; |
| 3902 | } |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3903 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3904 | // 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] | 3905 | if (lhsType == Context.BoolTy) |
| 3906 | return Compatible; |
| 3907 | |
| 3908 | if (lhsType->isIntegerType()) |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3909 | return PointerToInt; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3910 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3911 | if (isa<PointerType>(lhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3912 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3913 | |
| 3914 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3915 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3916 | return Compatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3917 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3918 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3919 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 3920 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 3921 | if (lhsType == Context.BoolTy) |
| 3922 | return Compatible; |
| 3923 | |
| 3924 | if (lhsType->isIntegerType()) |
| 3925 | return PointerToInt; |
| 3926 | |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3927 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3928 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3929 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3930 | return Compatible; |
| 3931 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3932 | } |
| 3933 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3934 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3935 | return Compatible; |
| 3936 | return Incompatible; |
| 3937 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3938 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3939 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3940 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3941 | return Compatible; |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 3942 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 3943 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3946 | /// \brief Constructs a transparent union from an expression that is |
| 3947 | /// used to initialize the transparent union. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3948 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3949 | QualType UnionType, FieldDecl *Field) { |
| 3950 | // Build an initializer list that designates the appropriate member |
| 3951 | // of the transparent union. |
| 3952 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3953 | &E, 1, |
| 3954 | SourceLocation()); |
| 3955 | Initializer->setType(UnionType); |
| 3956 | Initializer->setInitializedFieldInUnion(Field); |
| 3957 | |
| 3958 | // Build a compound literal constructing a value of the transparent |
| 3959 | // union type from this initializer list. |
| 3960 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3961 | false); |
| 3962 | } |
| 3963 | |
| 3964 | Sema::AssignConvertType |
| 3965 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3966 | QualType FromType = rExpr->getType(); |
| 3967 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3968 | // 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] | 3969 | // transparent_union GCC extension. |
| 3970 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3971 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3972 | return Incompatible; |
| 3973 | |
| 3974 | // The field to initialize within the transparent union. |
| 3975 | RecordDecl *UD = UT->getDecl(); |
| 3976 | FieldDecl *InitField = 0; |
| 3977 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3978 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 3979 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3980 | it != itend; ++it) { |
| 3981 | if (it->getType()->isPointerType()) { |
| 3982 | // If the transparent union contains a pointer type, we allow: |
| 3983 | // 1) void pointer |
| 3984 | // 2) null pointer constant |
| 3985 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3986 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3987 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3988 | InitField = *it; |
| 3989 | break; |
| 3990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3992 | if (rExpr->isNullPointerConstant(Context, |
| 3993 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 3994 | ImpCastExprToType(rExpr, it->getType(), CastExpr::CK_IntegralToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3995 | InitField = *it; |
| 3996 | break; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 4001 | == Compatible) { |
| 4002 | InitField = *it; |
| 4003 | break; |
| 4004 | } |
| 4005 | } |
| 4006 | |
| 4007 | if (!InitField) |
| 4008 | return Incompatible; |
| 4009 | |
| 4010 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 4011 | return Compatible; |
| 4012 | } |
| 4013 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4014 | Sema::AssignConvertType |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4015 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4016 | if (getLangOptions().CPlusPlus) { |
| 4017 | if (!lhsType->isRecordType()) { |
| 4018 | // C++ 5.17p3: If the left operand is not of class type, the |
| 4019 | // expression is implicitly converted (C++ 4) to the |
| 4020 | // cv-unqualified type of the left operand. |
Douglas Gregor | 47d3f27 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 4021 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 4022 | "assigning")) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4023 | return Incompatible; |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 4024 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 4028 | // structures. |
| 4029 | } |
| 4030 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4031 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 4032 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4033 | if ((lhsType->isPointerType() || |
| 4034 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4035 | lhsType->isBlockPointerType()) |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4036 | && rExpr->isNullPointerConstant(Context, |
| 4037 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4038 | ImpCastExprToType(rExpr, lhsType, CastExpr::CK_Unknown); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 4039 | return Compatible; |
| 4040 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4041 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4042 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4043 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 4044 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4045 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4046 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4047 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 4048 | if (!lhsType->isReferenceType()) |
| 4049 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4050 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4051 | Sema::AssignConvertType result = |
| 4052 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4053 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4054 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 4055 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 4056 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 4057 | // so that we can use references in built-in functions even in C. |
| 4058 | // The getNonReferenceType() call makes sure that the resulting expression |
| 4059 | // does not have reference type. |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 4060 | if (result != Incompatible && rExpr->getType() != lhsType) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4061 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType(), |
| 4062 | CastExpr::CK_Unknown); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 4063 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4066 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4067 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 4068 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4069 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4070 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4073 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4074 | Expr *&rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4075 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4076 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 4077 | QualType lhsType = |
| 4078 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 4079 | QualType rhsType = |
| 4080 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4081 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4082 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 4083 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 4084 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4085 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4086 | // Handle the case of a vector & extvector type of the same size and element |
| 4087 | // 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] | 4088 | if (getLangOptions().LaxVectorConversions) { |
| 4089 | // FIXME: Should we warn here? |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4090 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| 4091 | if (const VectorType *RV = rhsType->getAs<VectorType>()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4092 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4093 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4094 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4098 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4099 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4100 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4101 | bool swapped = false; |
| 4102 | if (rhsType->isExtVectorType()) { |
| 4103 | swapped = true; |
| 4104 | std::swap(rex, lex); |
| 4105 | std::swap(rhsType, lhsType); |
| 4106 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4107 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4108 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4109 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4110 | QualType EltTy = LV->getElementType(); |
| 4111 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4112 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4113 | ImpCastExprToType(rex, lhsType, CastExpr::CK_IntegralCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4114 | if (swapped) std::swap(rex, lex); |
| 4115 | return lhsType; |
| 4116 | } |
| 4117 | } |
| 4118 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4119 | rhsType->isRealFloatingType()) { |
| 4120 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4121 | ImpCastExprToType(rex, lhsType, CastExpr::CK_FloatingCast); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4122 | if (swapped) std::swap(rex, lex); |
| 4123 | return lhsType; |
| 4124 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4125 | } |
| 4126 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4128 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4129 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4130 | << lex->getType() << rex->getType() |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4131 | << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 4132 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4133 | } |
| 4134 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4135 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4136 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 060d5e2 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4137 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4138 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4139 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4140 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4141 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4142 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4143 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4144 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4147 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4148 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4149 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4150 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4151 | return CheckVectorOperands(Loc, lex, rex); |
| 4152 | return InvalidOperands(Loc, lex, rex); |
| 4153 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4154 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4155 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4156 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4157 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4158 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4159 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4163 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4164 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4165 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4166 | if (CompLHSTy) *CompLHSTy = compType; |
| 4167 | return compType; |
| 4168 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4169 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4170 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4171 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 4172 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4173 | if (lex->getType()->isArithmeticType() && |
| 4174 | rex->getType()->isArithmeticType()) { |
| 4175 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4176 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4177 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4178 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4179 | // Put any potential pointer into PExp |
| 4180 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4181 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4182 | std::swap(PExp, IExp); |
| 4183 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4184 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4185 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4186 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4187 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4188 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4189 | // Check for arithmetic on pointers to incomplete types. |
| 4190 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4191 | if (getLangOptions().CPlusPlus) { |
| 4192 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4193 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4194 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4195 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4196 | |
| 4197 | // GNU extension: arithmetic on pointer to void |
| 4198 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4199 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4200 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4201 | if (getLangOptions().CPlusPlus) { |
| 4202 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4203 | << lex->getType() << lex->getSourceRange(); |
| 4204 | return QualType(); |
| 4205 | } |
| 4206 | |
| 4207 | // GNU extension: arithmetic on pointer to function |
| 4208 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4209 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4210 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4211 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4212 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4213 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4214 | PExp->getType()->isObjCObjectPointerType()) && |
| 4215 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4216 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4217 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4218 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4219 | return QualType(); |
| 4220 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4221 | // Diagnose bad cases where we step over interface counts. |
| 4222 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4223 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4224 | << PointeeTy << PExp->getSourceRange(); |
| 4225 | return QualType(); |
| 4226 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4227 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4228 | if (CompLHSTy) { |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4229 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4230 | if (LHSTy.isNull()) { |
| 4231 | LHSTy = lex->getType(); |
| 4232 | if (LHSTy->isPromotableIntegerType()) |
| 4233 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4234 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4235 | *CompLHSTy = LHSTy; |
| 4236 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4237 | return PExp->getType(); |
| 4238 | } |
| 4239 | } |
| 4240 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4241 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4244 | // C99 6.5.6 |
| 4245 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4246 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4247 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4248 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4249 | if (CompLHSTy) *CompLHSTy = compType; |
| 4250 | return compType; |
| 4251 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4252 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4253 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4254 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4255 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4256 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4257 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4258 | if (lex->getType()->isArithmeticType() |
| 4259 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4260 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4261 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4262 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4263 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4264 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4265 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 4eed7a1 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4266 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4267 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4268 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4269 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4270 | bool ComplainAboutVoid = false; |
| 4271 | Expr *ComplainAboutFunc = 0; |
| 4272 | if (lpointee->isVoidType()) { |
| 4273 | if (getLangOptions().CPlusPlus) { |
| 4274 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4275 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4276 | return QualType(); |
| 4277 | } |
| 4278 | |
| 4279 | // GNU C extension: arithmetic on pointer to void |
| 4280 | ComplainAboutVoid = true; |
| 4281 | } else if (lpointee->isFunctionType()) { |
| 4282 | if (getLangOptions().CPlusPlus) { |
| 4283 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4284 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4285 | return QualType(); |
| 4286 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4287 | |
| 4288 | // GNU C extension: arithmetic on pointer to function |
| 4289 | ComplainAboutFunc = lex; |
| 4290 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4291 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4292 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4293 | << lex->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4294 | << lex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4295 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4296 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4297 | // Diagnose bad cases where we step over interface counts. |
| 4298 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4299 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4300 | << lpointee << lex->getSourceRange(); |
| 4301 | return QualType(); |
| 4302 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4303 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4304 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4305 | if (rex->getType()->isIntegerType()) { |
| 4306 | if (ComplainAboutVoid) |
| 4307 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4308 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4309 | if (ComplainAboutFunc) |
| 4310 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4311 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4312 | << ComplainAboutFunc->getSourceRange(); |
| 4313 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4314 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4315 | return lex->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4316 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4317 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4318 | // Handle pointer-pointer subtractions. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4319 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4320 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4321 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4322 | // RHS must be a completely-type object type. |
| 4323 | // Handle the GNU void* extension. |
| 4324 | if (rpointee->isVoidType()) { |
| 4325 | if (getLangOptions().CPlusPlus) { |
| 4326 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4327 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4328 | return QualType(); |
| 4329 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4330 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4331 | ComplainAboutVoid = true; |
| 4332 | } else if (rpointee->isFunctionType()) { |
| 4333 | if (getLangOptions().CPlusPlus) { |
| 4334 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4335 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4336 | return QualType(); |
| 4337 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4338 | |
| 4339 | // GNU extension: arithmetic on pointer to function |
| 4340 | if (!ComplainAboutFunc) |
| 4341 | ComplainAboutFunc = rex; |
| 4342 | } else if (!rpointee->isDependentType() && |
| 4343 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4344 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4345 | << rex->getSourceRange() |
| 4346 | << rex->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4347 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4348 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4349 | if (getLangOptions().CPlusPlus) { |
| 4350 | // Pointee types must be the same: C++ [expr.add] |
| 4351 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4352 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4353 | << lex->getType() << rex->getType() |
| 4354 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4355 | return QualType(); |
| 4356 | } |
| 4357 | } else { |
| 4358 | // Pointee types must be compatible C99 6.5.6p3 |
| 4359 | if (!Context.typesAreCompatible( |
| 4360 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4361 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4362 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4363 | << lex->getType() << rex->getType() |
| 4364 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4365 | return QualType(); |
| 4366 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4367 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4369 | if (ComplainAboutVoid) |
| 4370 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4371 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4372 | if (ComplainAboutFunc) |
| 4373 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4374 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4375 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4376 | |
| 4377 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4378 | return Context.getPointerDiffType(); |
| 4379 | } |
| 4380 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4381 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4382 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4383 | } |
| 4384 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4385 | // C99 6.5.7 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4386 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4387 | bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4388 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4389 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4390 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4391 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 4392 | // Vector shifts promote their scalar inputs to vector type. |
| 4393 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 4394 | return CheckVectorOperands(Loc, lex, rex); |
| 4395 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4396 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4397 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4398 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4399 | if (LHSTy.isNull()) { |
| 4400 | LHSTy = lex->getType(); |
| 4401 | if (LHSTy->isPromotableIntegerType()) |
| 4402 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4403 | } |
Chris Lattner | 3c13340 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4404 | if (!isCompAssign) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4405 | ImpCastExprToType(lex, LHSTy, CastExpr::CK_IntegralCast); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4406 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4407 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4408 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4409 | // Sanity-check shift operands |
| 4410 | llvm::APSInt Right; |
| 4411 | // Check right/shifter operand |
Daniel Dunbar | 687fa86 | 2009-09-17 06:31:27 +0000 | [diff] [blame] | 4412 | if (!rex->isValueDependent() && |
| 4413 | rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 2f08571 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4414 | if (Right.isNegative()) |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4415 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4416 | else { |
| 4417 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4418 | Context.getTypeSize(lex->getType())); |
| 4419 | if (Right.uge(LeftBits)) |
| 4420 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4421 | } |
| 4422 | } |
| 4423 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4424 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4425 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4426 | } |
| 4427 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4428 | /// \brief Implements -Wsign-compare. |
| 4429 | /// |
| 4430 | /// \param lex the left-hand expression |
| 4431 | /// \param rex the right-hand expression |
| 4432 | /// \param OpLoc the location of the joining operator |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4433 | /// \param Equality whether this is an "equality-like" join, which |
| 4434 | /// suppresses the warning in some cases |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4435 | void Sema::CheckSignCompare(Expr *lex, Expr *rex, SourceLocation OpLoc, |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4436 | const PartialDiagnostic &PD, bool Equality) { |
John McCall | e2c91e6 | 2009-11-06 18:16:06 +0000 | [diff] [blame] | 4437 | // Don't warn if we're in an unevaluated context. |
| 4438 | if (ExprEvalContext == Unevaluated) |
| 4439 | return; |
| 4440 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4441 | QualType lt = lex->getType(), rt = rex->getType(); |
| 4442 | |
| 4443 | // Only warn if both operands are integral. |
| 4444 | if (!lt->isIntegerType() || !rt->isIntegerType()) |
| 4445 | return; |
| 4446 | |
Sebastian Redl | 0b7c85f | 2009-11-05 21:09:23 +0000 | [diff] [blame] | 4447 | // If either expression is value-dependent, don't warn. We'll get another |
| 4448 | // chance at instantiation time. |
| 4449 | if (lex->isValueDependent() || rex->isValueDependent()) |
| 4450 | return; |
| 4451 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4452 | // The rule is that the signed operand becomes unsigned, so isolate the |
| 4453 | // signed operand. |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4454 | Expr *signedOperand, *unsignedOperand; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4455 | if (lt->isSignedIntegerType()) { |
| 4456 | if (rt->isSignedIntegerType()) return; |
| 4457 | signedOperand = lex; |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4458 | unsignedOperand = rex; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4459 | } else { |
| 4460 | if (!rt->isSignedIntegerType()) return; |
| 4461 | signedOperand = rex; |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4462 | unsignedOperand = lex; |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4463 | } |
| 4464 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4465 | // If the unsigned type is strictly smaller than the signed type, |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4466 | // then (1) the result type will be signed and (2) the unsigned |
| 4467 | // value will fit fully within the signed type, and thus the result |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4468 | // of the comparison will be exact. |
| 4469 | if (Context.getIntWidth(signedOperand->getType()) > |
| 4470 | Context.getIntWidth(unsignedOperand->getType())) |
| 4471 | return; |
| 4472 | |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4473 | // If the value is a non-negative integer constant, then the |
| 4474 | // signed->unsigned conversion won't change it. |
| 4475 | llvm::APSInt value; |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4476 | if (signedOperand->isIntegerConstantExpr(value, Context)) { |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4477 | assert(value.isSigned() && "result of signed expression not signed"); |
| 4478 | |
| 4479 | if (value.isNonNegative()) |
| 4480 | return; |
| 4481 | } |
| 4482 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4483 | if (Equality) { |
| 4484 | // For (in)equality comparisons, if the unsigned operand is a |
John McCall | e46fd85 | 2009-11-06 08:53:51 +0000 | [diff] [blame] | 4485 | // constant which cannot collide with a overflowed signed operand, |
| 4486 | // then reinterpreting the signed operand as unsigned will not |
| 4487 | // change the result of the comparison. |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4488 | if (unsignedOperand->isIntegerConstantExpr(value, Context)) { |
| 4489 | assert(!value.isSigned() && "result of unsigned expression is signed"); |
| 4490 | |
| 4491 | // 2's complement: test the top bit. |
| 4492 | if (value.isNonNegative()) |
| 4493 | return; |
| 4494 | } |
| 4495 | } |
| 4496 | |
John McCall | 1fa36b7 | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 4497 | Diag(OpLoc, PD) |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4498 | << lex->getType() << rex->getType() |
| 4499 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4500 | } |
| 4501 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4502 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4503 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4504 | unsigned OpaqueOpc, bool isRelational) { |
| 4505 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 4506 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4507 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4508 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4509 | |
John McCall | 99ce6bf | 2009-11-06 08:49:08 +0000 | [diff] [blame] | 4510 | CheckSignCompare(lex, rex, Loc, diag::warn_mixed_sign_comparison, |
| 4511 | (Opc == BinaryOperator::EQ || Opc == BinaryOperator::NE)); |
John McCall | 644a418 | 2009-11-05 00:40:04 +0000 | [diff] [blame] | 4512 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4513 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 47fea35 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 4514 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 4515 | UsualArithmeticConversions(lex, rex); |
| 4516 | else { |
| 4517 | UsualUnaryConversions(lex); |
| 4518 | UsualUnaryConversions(rex); |
| 4519 | } |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4520 | QualType lType = lex->getType(); |
| 4521 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4522 | |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4523 | if (!lType->isFloatingType() |
| 4524 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4525 | // For non-floating point types, check for self-comparisons of the form |
| 4526 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4527 | // often indicate logic errors in the program. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4528 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | de9e968 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 4529 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4530 | Expr *LHSStripped = lex->IgnoreParens(); |
| 4531 | Expr *RHSStripped = rex->IgnoreParens(); |
| 4532 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 4533 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | 9ffbe41 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 4534 | if (DRL->getDecl() == DRR->getDecl() && |
| 4535 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4536 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4537 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4538 | if (isa<CastExpr>(LHSStripped)) |
| 4539 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 4540 | if (isa<CastExpr>(RHSStripped)) |
| 4541 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4542 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4543 | // Warn about comparisons against a string constant (unless the other |
| 4544 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4545 | Expr *literalString = 0; |
| 4546 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4547 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4548 | !RHSStripped->isNullPointerConstant(Context, |
| 4549 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4550 | literalString = lex; |
| 4551 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4552 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 4553 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4554 | !LHSStripped->isNullPointerConstant(Context, |
| 4555 | Expr::NPC_ValueDependentIsNull)) { |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4556 | literalString = rex; |
| 4557 | literalStringStripped = RHSStripped; |
| 4558 | } |
| 4559 | |
| 4560 | if (literalString) { |
| 4561 | std::string resultComparison; |
| 4562 | switch (Opc) { |
| 4563 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 4564 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 4565 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 4566 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 4567 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 4568 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 4569 | default: assert(false && "Invalid comparison operator"); |
| 4570 | } |
| 4571 | Diag(Loc, diag::warn_stringcompare) |
| 4572 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 4573 | << literalString->getSourceRange() |
Douglas Gregor | 170512f | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4574 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 4575 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 4576 | "strcmp(") |
| 4577 | << CodeModificationHint::CreateInsertion( |
| 4578 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4579 | resultComparison); |
| 4580 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 4581 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4582 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4583 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4584 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4585 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4586 | if (isRelational) { |
| 4587 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4588 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4589 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4590 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4591 | if (lType->isFloatingType()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4592 | assert(rType->isFloatingType()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4593 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | d4ecc6d | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 4594 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4595 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4596 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4597 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4598 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 4600 | bool LHSIsNull = lex->isNullPointerConstant(Context, |
| 4601 | Expr::NPC_ValueDependentIsNull); |
| 4602 | bool RHSIsNull = rex->isNullPointerConstant(Context, |
| 4603 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4604 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4605 | // All of the following pointer related warnings are GCC extensions, except |
| 4606 | // when handling null pointer constants. One day, we can consider making them |
| 4607 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 4608 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4609 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4610 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4611 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4612 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4614 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4615 | if (LCanPointeeTy == RCanPointeeTy) |
| 4616 | return ResultTy; |
| 4617 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4618 | // C++ [expr.rel]p2: |
| 4619 | // [...] Pointer conversions (4.10) and qualification |
| 4620 | // conversions (4.4) are performed on pointer operands (or on |
| 4621 | // a pointer operand and a null pointer constant) to bring |
| 4622 | // them to their composite pointer type. [...] |
| 4623 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4624 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4625 | // comparisons of pointers. |
Douglas Gregor | b842046 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 4626 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4627 | if (T.isNull()) { |
| 4628 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4629 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4630 | return QualType(); |
| 4631 | } |
| 4632 | |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4633 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 4634 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4635 | return ResultTy; |
| 4636 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4637 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 4638 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 4639 | RCanPointeeTy.getUnqualifiedType())) { |
| 4640 | // Valid unless a relational comparison of function pointers |
| 4641 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 4642 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 4643 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4644 | } |
| 4645 | } else if (!isRelational && |
| 4646 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 4647 | // Valid unless comparison between non-null pointer and function pointer |
| 4648 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 4649 | && !LHSIsNull && !RHSIsNull) { |
| 4650 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 4651 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4652 | } |
| 4653 | } else { |
| 4654 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4655 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4656 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 4657 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4658 | if (LCanPointeeTy != RCanPointeeTy) |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4659 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4660 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4661 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4662 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4663 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4664 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4665 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4666 | if (RHSIsNull && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4667 | (lType->isPointerType() || |
| 4668 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 83133d9 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4669 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4670 | return ResultTy; |
| 4671 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4672 | if (LHSIsNull && |
| 4673 | (rType->isPointerType() || |
| 4674 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 83133d9 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4675 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4676 | return ResultTy; |
| 4677 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4678 | |
| 4679 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4681 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 4682 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | // In addition, pointers to members can be compared, or a pointer to |
| 4684 | // member and a null pointer constant. Pointer to member conversions |
| 4685 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 4686 | // them to a common type. If one operand is a null pointer constant, |
| 4687 | // the common type is the type of the other operand. Otherwise, the |
| 4688 | // common type is a pointer to member type similar (4.4) to the type |
| 4689 | // of one of the operands, with a cv-qualification signature (4.4) |
| 4690 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4691 | // types. |
| 4692 | QualType T = FindCompositePointerType(lex, rex); |
| 4693 | if (T.isNull()) { |
| 4694 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4695 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4696 | return QualType(); |
| 4697 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4699 | ImpCastExprToType(lex, T, CastExpr::CK_BitCast); |
| 4700 | ImpCastExprToType(rex, T, CastExpr::CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4701 | return ResultTy; |
| 4702 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4704 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4705 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4706 | return ResultTy; |
| 4707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4709 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4710 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4711 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 4712 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4713 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4714 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4715 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4716 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4717 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4718 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4719 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4720 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4721 | } |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4722 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4723 | if (!isRelational |
| 4724 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4725 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4726 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4727 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4728 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4729 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4730 | ->getPointeeType()->isVoidType()))) |
| 4731 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4732 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4733 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4734 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4735 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4736 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4737 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4738 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4739 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4740 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 4741 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4742 | bool LPtrToVoid = LPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4743 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4744 | bool RPtrToVoid = RPT ? |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4745 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4746 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4747 | if (!LPtrToVoid && !RPtrToVoid && |
| 4748 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4749 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4750 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4751 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4752 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4753 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4754 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4755 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4756 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4757 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 4758 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4759 | ImpCastExprToType(rex, lType, CastExpr::CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4760 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4761 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4762 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4763 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4764 | unsigned DiagID = 0; |
| 4765 | if (RHSIsNull) { |
| 4766 | if (isRelational) |
| 4767 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4768 | } else if (isRelational) |
| 4769 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4770 | else |
| 4771 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4772 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4773 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4774 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4775 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4776 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4777 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4778 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4779 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4780 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4781 | unsigned DiagID = 0; |
| 4782 | if (LHSIsNull) { |
| 4783 | if (isRelational) |
| 4784 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4785 | } else if (isRelational) |
| 4786 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4787 | else |
| 4788 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4790 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4791 | Diag(Loc, DiagID) |
Chris Lattner | d466ea1 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4792 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4793 | } |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4794 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4795 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 4796 | } |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4797 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4798 | if (!isRelational && RHSIsNull |
| 4799 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4800 | ImpCastExprToType(rex, lType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4801 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4802 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4803 | if (!isRelational && LHSIsNull |
| 4804 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4805 | ImpCastExprToType(lex, rType, CastExpr::CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4806 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4807 | } |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4808 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4811 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4812 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4813 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4814 | /// types. |
| 4815 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4816 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4817 | bool isRelational) { |
| 4818 | // Check to make sure we're operating on vectors of the same type and width, |
| 4819 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4820 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4821 | if (vType.isNull()) |
| 4822 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4823 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4824 | QualType lType = lex->getType(); |
| 4825 | QualType rType = rex->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4826 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4827 | // For non-floating point types, check for self-comparisons of the form |
| 4828 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4829 | // often indicate logic errors in the program. |
| 4830 | if (!lType->isFloatingType()) { |
| 4831 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4832 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4833 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4834 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4835 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4836 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4837 | // Check for comparisons of floating point operands using != and ==. |
| 4838 | if (!isRelational && lType->isFloatingType()) { |
| 4839 | assert (rType->isFloatingType()); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4840 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4841 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4842 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4843 | // Return the type for the comparison, which is the same as vector type for |
| 4844 | // integer vectors, or an integer type of identical size and number of |
| 4845 | // elements for floating point vectors. |
| 4846 | if (lType->isIntegerType()) |
| 4847 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4848 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 4849 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4850 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4851 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4852 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4853 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4854 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4855 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4856 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4857 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4858 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4859 | } |
| 4860 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4861 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4862 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 4863 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4864 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4865 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4866 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4867 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4868 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4869 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4870 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 4871 | } |
| 4872 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4873 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 4875 | if (!Context.getLangOptions().CPlusPlus) { |
| 4876 | UsualUnaryConversions(lex); |
| 4877 | UsualUnaryConversions(rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4878 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 4879 | if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType()) |
| 4880 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 4881 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 4882 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 4883 | } |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 4884 | |
| 4885 | // C++ [expr.log.and]p1 |
| 4886 | // C++ [expr.log.or]p1 |
| 4887 | // The operands are both implicitly converted to type bool (clause 4). |
| 4888 | StandardConversionSequence LHS; |
| 4889 | if (!IsStandardConversion(lex, Context.BoolTy, |
| 4890 | /*InOverloadResolution=*/false, LHS)) |
| 4891 | return InvalidOperands(Loc, lex, rex); |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 4892 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 4893 | if (PerformImplicitConversion(lex, Context.BoolTy, LHS, |
| 4894 | "passing", /*IgnoreBaseAccess=*/false)) |
| 4895 | return InvalidOperands(Loc, lex, rex); |
| 4896 | |
| 4897 | StandardConversionSequence RHS; |
| 4898 | if (!IsStandardConversion(rex, Context.BoolTy, |
| 4899 | /*InOverloadResolution=*/false, RHS)) |
| 4900 | return InvalidOperands(Loc, lex, rex); |
| 4901 | |
| 4902 | if (PerformImplicitConversion(rex, Context.BoolTy, RHS, |
| 4903 | "passing", /*IgnoreBaseAccess=*/false)) |
| 4904 | return InvalidOperands(Loc, lex, rex); |
| 4905 | |
| 4906 | // C++ [expr.log.and]p2 |
| 4907 | // C++ [expr.log.or]p2 |
| 4908 | // The result is a bool. |
| 4909 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 4910 | } |
| 4911 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4912 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4913 | /// is a read-only property; return true if so. A readonly property expression |
| 4914 | /// depends on various declarations and thus must be treated specially. |
| 4915 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4916 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4917 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4918 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4919 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4920 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4921 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4922 | BaseType->getAsObjCInterfacePointerType()) |
| 4923 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 4924 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4925 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | return false; |
| 4929 | } |
| 4930 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4931 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4932 | /// emit an error and return true. If so, return false. |
| 4933 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4934 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4935 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4936 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4937 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4938 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4939 | if (IsLV == Expr::MLV_Valid) |
| 4940 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4941 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4942 | unsigned Diag = 0; |
| 4943 | bool NeedType = false; |
| 4944 | switch (IsLV) { // C99 6.5.16p2 |
| 4945 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4946 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4947 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4948 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4949 | NeedType = true; |
| 4950 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4951 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4952 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4953 | NeedType = true; |
| 4954 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4955 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4956 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4957 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4958 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4959 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4960 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4961 | case Expr::MLV_IncompleteType: |
| 4962 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4963 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4964 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 4965 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4966 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4967 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4968 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4969 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4970 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4971 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4972 | case Expr::MLV_ReadonlyProperty: |
| 4973 | Diag = diag::error_readonly_property_assignment; |
| 4974 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4975 | case Expr::MLV_NoSetterProperty: |
| 4976 | Diag = diag::error_nosetter_property_assignment; |
| 4977 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 4978 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4979 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4980 | SourceRange Assign; |
| 4981 | if (Loc != OrigLoc) |
| 4982 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4983 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4984 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4985 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4987 | return true; |
| 4988 | } |
| 4989 | |
| 4990 | |
| 4991 | |
| 4992 | // C99 6.5.16.1 |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4993 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4994 | SourceLocation Loc, |
| 4995 | QualType CompoundType) { |
| 4996 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4997 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4998 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4999 | |
| 5000 | QualType LHSType = LHS->getType(); |
| 5001 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5002 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5003 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5004 | if (CompoundType.isNull()) { |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5005 | // Simple assignment "x = y". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5006 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5007 | // Special case of NSObject attributes on c-style pointer types. |
| 5008 | if (ConvTy == IncompatiblePointer && |
| 5009 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5010 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5011 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 5012 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 5013 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5014 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5015 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 5016 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 5017 | // instead of "x += 4". |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5018 | Expr *RHSCheck = RHS; |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5019 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 5020 | RHSCheck = ICE->getSubExpr(); |
| 5021 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 5022 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 5023 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5024 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5025 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5026 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 5027 | // And there is a space or other character before the subexpr of the |
| 5028 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 5029 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 5030 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5031 | Diag(Loc, diag::warn_not_compound_assign) |
| 5032 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 5033 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 5034 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5035 | } |
| 5036 | } else { |
| 5037 | // Compound assignment "x += y" |
Eli Friedman | b05c41e | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 5038 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 5039 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5040 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5041 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 5042 | RHS, "assigning")) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5043 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5044 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5045 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 5046 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5047 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5048 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 5049 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5050 | // 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] | 5051 | // operand. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5052 | return LHSType.getUnqualifiedType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5055 | // C99 6.5.17 |
| 5056 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | f6e1e30 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 5057 | // 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] | 5058 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5059 | |
| 5060 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 5061 | // incomplete in C++). |
| 5062 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5063 | return RHS->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 5064 | } |
| 5065 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5066 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 5067 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5068 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 5069 | bool isInc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5070 | if (Op->isTypeDependent()) |
| 5071 | return Context.DependentTy; |
| 5072 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5073 | QualType ResType = Op->getType(); |
| 5074 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 5075 | |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5076 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 5077 | // Decrement of bool is not allowed. |
| 5078 | if (!isInc) { |
| 5079 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 5080 | return QualType(); |
| 5081 | } |
| 5082 | // Increment of bool sets it to true, but is deprecated. |
| 5083 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 5084 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5085 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5086 | } else if (ResType->isAnyPointerType()) { |
| 5087 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5089 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5090 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5091 | if (getLangOptions().CPlusPlus) { |
| 5092 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 5093 | << Op->getSourceRange(); |
| 5094 | return QualType(); |
| 5095 | } |
| 5096 | |
| 5097 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5098 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5099 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 5100 | if (getLangOptions().CPlusPlus) { |
| 5101 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 5102 | << Op->getType() << Op->getSourceRange(); |
| 5103 | return QualType(); |
| 5104 | } |
| 5105 | |
| 5106 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5107 | << ResType << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5108 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5109 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5110 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 5111 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 5112 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 5113 | // Diagnose bad cases where we step over interface counts. |
| 5114 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 5115 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5116 | << PointeeTy << Op->getSourceRange(); |
| 5117 | return QualType(); |
| 5118 | } |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5119 | } else if (ResType->isComplexType()) { |
| 5120 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 5121 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5122 | << ResType << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5123 | } else { |
| 5124 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5125 | << ResType << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5126 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 5127 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5128 | // 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] | 5129 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5130 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5131 | return QualType(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 5132 | return ResType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5133 | } |
| 5134 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5135 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5136 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5137 | /// where the declaration is needed for type checking. We only need to |
| 5138 | /// handle cases when the expression references a function designator |
| 5139 | /// or is an lvalue. Here are some examples: |
| 5140 | /// - &(x) => x |
| 5141 | /// - &*****f => f for f a function designator. |
| 5142 | /// - &s.xx => s |
| 5143 | /// - &s.zz[1].yy -> s, if zz is an array |
| 5144 | /// - *(x + 1) -> x, if x is an array |
| 5145 | /// - &"123"[2] -> 0 |
| 5146 | /// - & __real__ x -> x |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5147 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5148 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5149 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5150 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5151 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5152 | // If this is an arrow operator, the address is an offset from |
| 5153 | // the base's value, so the object the base refers to is |
| 5154 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5155 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5156 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5157 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5158 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5159 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5160 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 5161 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5162 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 5163 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 5164 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 5165 | return getPrimaryDecl(ICE->getSubExpr()); |
| 5166 | } |
| 5167 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 5168 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5169 | case Stmt::UnaryOperatorClass: { |
| 5170 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5171 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5172 | switch(UO->getOpcode()) { |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 5173 | case UnaryOperator::Real: |
| 5174 | case UnaryOperator::Imag: |
| 5175 | case UnaryOperator::Extension: |
| 5176 | return getPrimaryDecl(UO->getSubExpr()); |
| 5177 | default: |
| 5178 | return 0; |
| 5179 | } |
| 5180 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5181 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5182 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5183 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5184 | // If the result of an implicit cast is an l-value, we care about |
| 5185 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5186 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5187 | default: |
| 5188 | return 0; |
| 5189 | } |
| 5190 | } |
| 5191 | |
| 5192 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5193 | /// 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] | 5194 | /// 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] | 5195 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5196 | /// 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] | 5197 | /// 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] | 5198 | /// we allow the '&' but retain the overloaded-function type. |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5199 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5200 | // Make sure to ignore parentheses in subsequent checks |
| 5201 | op = op->IgnoreParens(); |
| 5202 | |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 5203 | if (op->isTypeDependent()) |
| 5204 | return Context.DependentTy; |
| 5205 | |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5206 | if (getLangOptions().C99) { |
| 5207 | // Implement C99-only parts of addressof rules. |
| 5208 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5209 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5210 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5211 | // (assuming the deref expression is valid). |
| 5212 | return uOp->getSubExpr()->getType(); |
| 5213 | } |
| 5214 | // Technically, there should be a check for array subscript |
| 5215 | // expressions here, but the result of one is always an lvalue anyway. |
| 5216 | } |
Douglas Gregor | 5251f1b | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5217 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 6731544 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5218 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5219 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5220 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5221 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5222 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5223 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5224 | // FIXME: emit more specific diag... |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5225 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5226 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5227 | return QualType(); |
| 5228 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5229 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5230 | // The operand cannot be a bit-field |
| 5231 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5232 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5233 | return QualType(); |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5234 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5235 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5236 | // The operand cannot be an element of a vector |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5237 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5238 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5239 | return QualType(); |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5240 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5241 | // cannot take address of a property expression. |
| 5242 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5243 | << "property expression" << op->getSourceRange(); |
| 5244 | return QualType(); |
Anders Carlsson | 3fa58d1 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 5245 | } else if (ConditionalOperator *CO = dyn_cast<ConditionalOperator>(op)) { |
| 5246 | // FIXME: Can LHS ever be null here? |
Anders Carlsson | 01ccf99 | 2009-09-15 16:03:44 +0000 | [diff] [blame] | 5247 | if (!CheckAddressOfOperand(CO->getTrueExpr(), OpLoc).isNull()) |
| 5248 | return CheckAddressOfOperand(CO->getFalseExpr(), OpLoc); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5249 | } else if (isa<UnresolvedLookupExpr>(op)) { |
| 5250 | return Context.OverloadTy; |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5251 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5252 | // 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] | 5253 | // with the register storage-class specifier. |
| 5254 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5255 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5256 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5257 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5258 | return QualType(); |
| 5259 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 5260 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5261 | return Context.OverloadTy; |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5262 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5263 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5264 | // Could be a pointer to member, though, if there is an explicit |
| 5265 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5266 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5267 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5268 | if (Ctx && Ctx->isRecord()) { |
| 5269 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5270 | Diag(OpLoc, |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5271 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5272 | << FD->getDeclName() << FD->getType(); |
| 5273 | return QualType(); |
| 5274 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5276 | return Context.getMemberPointerType(op->getType(), |
| 5277 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5278 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5279 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5280 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 5773a1b | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5281 | // Okay: we can take the address of a function. |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5282 | // As above. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 5283 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier() && |
| 5284 | MD->isInstance()) |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5285 | return Context.getMemberPointerType(op->getType(), |
| 5286 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5287 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 5288 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5289 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5290 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5291 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5292 | // Taking the address of a void variable is technically illegal, but we |
| 5293 | // allow it in cases which are otherwise valid. |
| 5294 | // Example: "extern void x; void* y = &x;". |
| 5295 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5296 | } |
| 5297 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5298 | // 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] | 5299 | return Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5302 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5303 | if (Op->isTypeDependent()) |
| 5304 | return Context.DependentTy; |
| 5305 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5306 | UsualUnaryConversions(Op); |
| 5307 | QualType Ty = Op->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5308 | |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5309 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5310 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5311 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5312 | // unlikely to catch any mistakes. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5313 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5314 | return PT->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5315 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5316 | if (const ObjCObjectPointerType *OPT = Ty->getAs<ObjCObjectPointerType>()) |
Fariborz Jahanian | f15d4b6 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5317 | return OPT->getPointeeType(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5318 | |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5319 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 6a2ed6f | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5320 | << Ty << Op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5321 | return QualType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 5322 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5323 | |
| 5324 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5325 | tok::TokenKind Kind) { |
| 5326 | BinaryOperator::Opcode Opc; |
| 5327 | switch (Kind) { |
| 5328 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5329 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5330 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5331 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5332 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5333 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5334 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5335 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5336 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5337 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5338 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5339 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5340 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5341 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5342 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5343 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5344 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5345 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5346 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5347 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5348 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5349 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5350 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5351 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5352 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5353 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5354 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5355 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5356 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5357 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5358 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5359 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5360 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5361 | } |
| 5362 | return Opc; |
| 5363 | } |
| 5364 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5365 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5366 | tok::TokenKind Kind) { |
| 5367 | UnaryOperator::Opcode Opc; |
| 5368 | switch (Kind) { |
| 5369 | default: assert(0 && "Unknown unary op!"); |
| 5370 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5371 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5372 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5373 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5374 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5375 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5376 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5377 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5378 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5379 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
Chris Lattner | d0f7651 | 2007-06-08 22:16:53 +0000 | [diff] [blame] | 5380 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5381 | } |
| 5382 | return Opc; |
| 5383 | } |
| 5384 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5385 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5386 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5387 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5388 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5389 | unsigned Op, |
| 5390 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5391 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5392 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5393 | // The following two variables are used for compound assignment operators |
| 5394 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5395 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5396 | |
| 5397 | switch (Opc) { |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5398 | case BinaryOperator::Assign: |
| 5399 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5400 | break; |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5401 | case BinaryOperator::PtrMemD: |
| 5402 | case BinaryOperator::PtrMemI: |
| 5403 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5404 | Opc == BinaryOperator::PtrMemI); |
| 5405 | break; |
| 5406 | case BinaryOperator::Mul: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5407 | case BinaryOperator::Div: |
| 5408 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5409 | break; |
| 5410 | case BinaryOperator::Rem: |
| 5411 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5412 | break; |
| 5413 | case BinaryOperator::Add: |
| 5414 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5415 | break; |
| 5416 | case BinaryOperator::Sub: |
| 5417 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5418 | break; |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5419 | case BinaryOperator::Shl: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5420 | case BinaryOperator::Shr: |
| 5421 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5422 | break; |
| 5423 | case BinaryOperator::LE: |
| 5424 | case BinaryOperator::LT: |
| 5425 | case BinaryOperator::GE: |
| 5426 | case BinaryOperator::GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5427 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5428 | break; |
| 5429 | case BinaryOperator::EQ: |
| 5430 | case BinaryOperator::NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5431 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5432 | break; |
| 5433 | case BinaryOperator::And: |
| 5434 | case BinaryOperator::Xor: |
| 5435 | case BinaryOperator::Or: |
| 5436 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5437 | break; |
| 5438 | case BinaryOperator::LAnd: |
| 5439 | case BinaryOperator::LOr: |
| 5440 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5441 | break; |
| 5442 | case BinaryOperator::MulAssign: |
| 5443 | case BinaryOperator::DivAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5444 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 5445 | CompLHSTy = CompResultTy; |
| 5446 | if (!CompResultTy.isNull()) |
| 5447 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5448 | break; |
| 5449 | case BinaryOperator::RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5450 | CompResultTy = CheckRemainderOperands(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::AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5456 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5457 | if (!CompResultTy.isNull()) |
| 5458 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5459 | break; |
| 5460 | case BinaryOperator::SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5461 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5462 | if (!CompResultTy.isNull()) |
| 5463 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5464 | break; |
| 5465 | case BinaryOperator::ShlAssign: |
| 5466 | case BinaryOperator::ShrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5467 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 5468 | CompLHSTy = CompResultTy; |
| 5469 | if (!CompResultTy.isNull()) |
| 5470 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | case BinaryOperator::AndAssign: |
| 5473 | case BinaryOperator::XorAssign: |
| 5474 | case BinaryOperator::OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5475 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 5476 | CompLHSTy = CompResultTy; |
| 5477 | if (!CompResultTy.isNull()) |
| 5478 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5479 | break; |
| 5480 | case BinaryOperator::Comma: |
| 5481 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 5482 | break; |
| 5483 | } |
| 5484 | if (ResultTy.isNull()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5485 | return ExprError(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5486 | if (CompResultTy.isNull()) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5487 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 5488 | else |
| 5489 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5490 | CompLHSTy, CompResultTy, |
| 5491 | OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5492 | } |
| 5493 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5494 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 5495 | /// ParenRange in parentheses. |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 5496 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 5497 | const PartialDiagnostic &PD, |
| 5498 | SourceRange ParenRange) |
| 5499 | { |
| 5500 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 5501 | if (!ParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 5502 | // We can't display the parentheses, so just dig the |
| 5503 | // warning/error and return. |
| 5504 | Self.Diag(Loc, PD); |
| 5505 | return; |
| 5506 | } |
| 5507 | |
| 5508 | Self.Diag(Loc, PD) |
| 5509 | << CodeModificationHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 5510 | << CodeModificationHint::CreateInsertion(EndLoc, ")"); |
| 5511 | } |
| 5512 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5513 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 5514 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 5515 | /// comparison operators have higher precedence. The most typical example of |
| 5516 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5517 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 5518 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5519 | typedef BinaryOperator BinOp; |
| 5520 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 5521 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 5522 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5523 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5524 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5525 | rhsopc = BO->getOpcode(); |
| 5526 | |
| 5527 | // Subs are not binary operators. |
| 5528 | if (lhsopc == -1 && rhsopc == -1) |
| 5529 | return; |
| 5530 | |
| 5531 | // Bitwise operations are sometimes used as eager logical ops. |
| 5532 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5533 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 5534 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5535 | return; |
| 5536 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5537 | if (BinOp::isComparisonOp(lhsopc)) |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 5538 | SuggestParentheses(Self, OpLoc, |
| 5539 | PDiag(diag::warn_precedence_bitwise_rel) |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5540 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 5541 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
| 5542 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| 5543 | else if (BinOp::isComparisonOp(rhsopc)) |
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(OpLoc, rhs->getLocEnd()) |
| 5547 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
| 5548 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| 5552 | /// precedence. This currently diagnoses only "arg1 'bitwise' arg2 'eq' arg3". |
| 5553 | /// But it could also warn about arg1 && arg2 || arg3, as GCC 4.3+ does. |
| 5554 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperator::Opcode Opc, |
| 5555 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 5556 | if (BinaryOperator::isBitwiseOp(Opc)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5557 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 5558 | } |
| 5559 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5560 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5561 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 5562 | tok::TokenKind Kind, |
| 5563 | ExprArg LHS, ExprArg RHS) { |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5564 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5565 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5566 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5567 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 5568 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5569 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 5570 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 5571 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 5572 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5573 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 5574 | } |
| 5575 | |
| 5576 | Action::OwningExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| 5577 | BinaryOperator::Opcode Opc, |
| 5578 | Expr *lhs, Expr *rhs) { |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5579 | if (getLangOptions().CPlusPlus && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5580 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5581 | rhs->getType()->isOverloadableType())) { |
| 5582 | // Find all of the overloaded operators visible from this |
| 5583 | // point. We perform both an operator-name lookup from the local |
| 5584 | // scope and an argument-dependent lookup based on the types of |
| 5585 | // the arguments. |
Douglas Gregor | d2b7ef6 | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5586 | FunctionSet Functions; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5587 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 5588 | if (OverOp != OO_None) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5589 | if (S) |
| 5590 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 5591 | Functions); |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5592 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5593 | DeclarationName OpName |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5594 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 5595 | ArgumentDependentLookup(OpName, /*Operator*/true, Args, 2, Functions); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5596 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5597 | |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5598 | // Build the (potentially-overloaded, potentially-dependent) |
| 5599 | // binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5600 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5601 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5603 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5604 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5607 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5608 | unsigned OpcIn, |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5609 | ExprArg InputArg) { |
| 5610 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5611 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5612 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5613 | Expr *Input = (Expr *)InputArg.get(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5614 | QualType resultType; |
| 5615 | switch (Opc) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5616 | case UnaryOperator::OffsetOf: |
| 5617 | assert(false && "Invalid unary operator"); |
| 5618 | break; |
| 5619 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5620 | case UnaryOperator::PreInc: |
| 5621 | case UnaryOperator::PreDec: |
Eli Friedman | 6aea575 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5622 | case UnaryOperator::PostInc: |
| 5623 | case UnaryOperator::PostDec: |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5624 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | 6aea575 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5625 | Opc == UnaryOperator::PreInc || |
| 5626 | Opc == UnaryOperator::PostInc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5627 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5628 | case UnaryOperator::AddrOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5629 | resultType = CheckAddressOfOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5630 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5631 | case UnaryOperator::Deref: |
Steve Naroff | b723564 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 5632 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5633 | resultType = CheckIndirectionOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5634 | break; |
| 5635 | case UnaryOperator::Plus: |
| 5636 | case UnaryOperator::Minus: |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5637 | UsualUnaryConversions(Input); |
| 5638 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5639 | if (resultType->isDependentType()) |
| 5640 | break; |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5641 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 5642 | break; |
| 5643 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 5644 | resultType->isEnumeralType()) |
| 5645 | break; |
| 5646 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 5647 | Opc == UnaryOperator::Plus && |
| 5648 | resultType->isPointerType()) |
| 5649 | break; |
| 5650 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5651 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5652 | << resultType << Input->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5653 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5654 | UsualUnaryConversions(Input); |
| 5655 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5656 | if (resultType->isDependentType()) |
| 5657 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5658 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 5659 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 5660 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5661 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5662 | << resultType << Input->getSourceRange(); |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5663 | else if (!resultType->isIntegerType()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5664 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5665 | << resultType << Input->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5666 | break; |
| 5667 | case UnaryOperator::LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 5668 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5669 | DefaultFunctionArrayConversion(Input); |
| 5670 | resultType = Input->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5671 | if (resultType->isDependentType()) |
| 5672 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5673 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5674 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5675 | << resultType << Input->getSourceRange()); |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 5676 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5677 | // In C++, it's bool. C++ 5.3.1p8 |
| 5678 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5679 | break; |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5680 | case UnaryOperator::Real: |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5681 | case UnaryOperator::Imag: |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 5682 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5683 | break; |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5684 | case UnaryOperator::Extension: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 5685 | resultType = Input->getType(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 5686 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5687 | } |
| 5688 | if (resultType.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5689 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5690 | |
| 5691 | InputArg.release(); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5692 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 5693 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5694 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5695 | Action::OwningExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5696 | UnaryOperator::Opcode Opc, |
| 5697 | ExprArg input) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5698 | Expr *Input = (Expr*)input.get(); |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 5699 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| 5700 | Opc != UnaryOperator::Extension) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5701 | // Find all of the overloaded operators visible from this |
| 5702 | // point. We perform both an operator-name lookup from the local |
| 5703 | // scope and an argument-dependent lookup based on the types of |
| 5704 | // the arguments. |
| 5705 | FunctionSet Functions; |
| 5706 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 5707 | if (OverOp != OO_None) { |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5708 | if (S) |
| 5709 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 5710 | Functions); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5711 | DeclarationName OpName |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5712 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
Sebastian Redl | c057f42 | 2009-10-23 19:23:15 +0000 | [diff] [blame] | 5713 | ArgumentDependentLookup(OpName, /*Operator*/true, &Input, 1, Functions); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5714 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5715 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5716 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 5717 | } |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5718 | |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5719 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 5720 | } |
| 5721 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 5722 | // Unary Operators. 'Tok' is the token for the operator. |
| 5723 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5724 | tok::TokenKind Op, ExprArg input) { |
| 5725 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), move(input)); |
| 5726 | } |
| 5727 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 5728 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5729 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 5730 | SourceLocation LabLoc, |
| 5731 | IdentifierInfo *LabelII) { |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5732 | // Look up the record for this label identifier. |
Chris Lattner | 3318e86 | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 5733 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5734 | |
Daniel Dunbar | 88402ce | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 5735 | // If we haven't seen this label yet, create a forward reference. It |
| 5736 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | 846b1ec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 5737 | if (LabelDecl == 0) |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5738 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5739 | |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5740 | // 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] | 5741 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 5742 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 5743 | } |
| 5744 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5745 | Sema::OwningExprResult |
| 5746 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 5747 | SourceLocation RPLoc) { // "({..})" |
| 5748 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5749 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 5750 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 5751 | |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5752 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 5753 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5754 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5755 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5756 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 5757 | // example, it is not possible to goto into a stmt expression apparently. |
| 5758 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5759 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5760 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 5761 | // as the type of the stmtexpr. |
| 5762 | QualType Ty = Context.VoidTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5763 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5764 | if (!Compound->body_empty()) { |
| 5765 | Stmt *LastStmt = Compound->body_back(); |
| 5766 | // If LastStmt is a label, skip down through into the body. |
| 5767 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 5768 | LastStmt = Label->getSubStmt(); |
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 (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5771 | Ty = LastExpr->getType(); |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5772 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5773 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5774 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 5775 | // expressions are not lvalues. |
| 5776 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5777 | substmt.release(); |
| 5778 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5779 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5780 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5781 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 5782 | SourceLocation BuiltinLoc, |
| 5783 | SourceLocation TypeLoc, |
| 5784 | TypeTy *argty, |
| 5785 | OffsetOfComponent *CompPtr, |
| 5786 | unsigned NumComponents, |
| 5787 | SourceLocation RPLoc) { |
| 5788 | // FIXME: This function leaks all expressions in the offset components on |
| 5789 | // error. |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5790 | // FIXME: Preserve type source info. |
| 5791 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5792 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5793 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5794 | bool Dependent = ArgTy->isDependentType(); |
| 5795 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5796 | // We must have at least one component that refers to the type, and the first |
| 5797 | // one is known to be a field designator. Verify that the ArgTy represents |
| 5798 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5799 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5800 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5801 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5802 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 5803 | // with an incomplete type would be illegal. |
Douglas Gregor | 2689746 | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 5804 | |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5805 | // Otherwise, create a null pointer as the base, and iteratively process |
| 5806 | // the offsetof designators. |
| 5807 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 5808 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5809 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5810 | ArgTy, SourceLocation()); |
Eli Friedman | 16c88df | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 5811 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5812 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 5813 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5814 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 5815 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5816 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5817 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 5818 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5819 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5820 | if (!Dependent) { |
Eli Friedman | 8469bc7 | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 5821 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5822 | |
John McCall | 9eff4e6 | 2009-11-04 03:03:43 +0000 | [diff] [blame] | 5823 | if (RequireCompleteType(TypeLoc, Res->getType(), |
| 5824 | diag::err_offsetof_incomplete_type)) |
| 5825 | return ExprError(); |
| 5826 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5827 | // FIXME: Dependent case loses a lot of information here. And probably |
| 5828 | // leaks like a sieve. |
| 5829 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 5830 | const OffsetOfComponent &OC = CompPtr[i]; |
| 5831 | if (OC.isBrackets) { |
| 5832 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 5833 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 5834 | if (!AT) { |
| 5835 | Res->Destroy(Context); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5836 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 5837 | << Res->getType()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5838 | } |
| 5839 | |
| 5840 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 5841 | |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5842 | // Promote the array so it looks more like a normal array subscript |
| 5843 | // expression. |
| 5844 | DefaultFunctionArrayConversion(Res); |
| 5845 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5846 | // C99 6.5.2.1p1 |
| 5847 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5848 | // FIXME: Leaks Res |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5849 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5850 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5851 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5852 | << Idx->getSourceRange()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5853 | |
| 5854 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5855 | OC.LocEnd); |
| 5856 | continue; |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5857 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5858 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5859 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5860 | if (!RC) { |
| 5861 | Res->Destroy(Context); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5862 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5863 | << Res->getType()); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5864 | } |
Chris Lattner | 98dbf0a | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5865 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5866 | // Get the decl corresponding to this. |
| 5867 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 2bbb86b | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5868 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 38ebcaa | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5869 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | 8b98d02 | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5870 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5871 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5872 | << Res->getType()); |
Anders Carlsson | 38ebcaa | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5873 | DidWarnAboutNonPOD = true; |
| 5874 | } |
Anders Carlsson | 2bbb86b | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5875 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5876 | |
John McCall | 27b18f8 | 2009-11-17 02:14:36 +0000 | [diff] [blame] | 5877 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 5878 | LookupQualifiedName(R, RD); |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5879 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5880 | FieldDecl *MemberDecl |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 5881 | = dyn_cast_or_null<FieldDecl>(R.getAsSingleDecl(Context)); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5882 | // FIXME: Leaks Res |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5883 | if (!MemberDecl) |
Douglas Gregor | e40876a | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 5884 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 5885 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5886 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5887 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5888 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 64fc3c6 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5889 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | 3cbc859 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5890 | Res = BuildAnonymousStructUnionMemberReference( |
John McCall | 7e1d6d7 | 2009-11-11 03:23:23 +0000 | [diff] [blame] | 5891 | OC.LocEnd, MemberDecl, Res, OC.LocEnd).takeAs<Expr>(); |
Eli Friedman | 64fc3c6 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5892 | } else { |
| 5893 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5894 | // doesn't matter here. |
| 5895 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5896 | MemberDecl->getType().getNonReferenceType()); |
| 5897 | } |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5898 | } |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5899 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5900 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5901 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5902 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5903 | } |
| 5904 | |
| 5905 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5906 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5907 | TypeTy *arg1,TypeTy *arg2, |
| 5908 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5909 | // FIXME: Preserve type source info. |
| 5910 | QualType argT1 = GetTypeFromParser(arg1); |
| 5911 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5912 | |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5913 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | f907cbf | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5915 | if (getLangOptions().CPlusPlus) { |
| 5916 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5917 | << SourceRange(BuiltinLoc, RPLoc); |
| 5918 | return ExprError(); |
| 5919 | } |
| 5920 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5921 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5922 | argT1, argT2, RPLoc)); |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5923 | } |
| 5924 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5925 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5926 | ExprArg cond, |
| 5927 | ExprArg expr1, ExprArg expr2, |
| 5928 | SourceLocation RPLoc) { |
| 5929 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5930 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5931 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5932 | |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5933 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5934 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5935 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5936 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5937 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5938 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5939 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5940 | } else { |
| 5941 | // The conditional expression is required to be a constant expression. |
| 5942 | llvm::APSInt condEval(32); |
| 5943 | SourceLocation ExpLoc; |
| 5944 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5945 | return ExprError(Diag(ExpLoc, |
| 5946 | diag::err_typecheck_choose_expr_requires_constant) |
| 5947 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5948 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5949 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5950 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5951 | ValueDependent = condEval.getZExtValue() ? LHSExpr->isValueDependent() |
| 5952 | : RHSExpr->isValueDependent(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5955 | cond.release(); expr1.release(); expr2.release(); |
| 5956 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5957 | resType, RPLoc, |
| 5958 | resType->isDependentType(), |
| 5959 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5962 | //===----------------------------------------------------------------------===// |
| 5963 | // Clang Extensions. |
| 5964 | //===----------------------------------------------------------------------===// |
| 5965 | |
| 5966 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5967 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5968 | // Analyze block parameters. |
| 5969 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5970 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5971 | // Add BSI to CurBlock. |
| 5972 | BSI->PrevBlockInfo = CurBlock; |
| 5973 | CurBlock = BSI; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5974 | |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5975 | BSI->ReturnType = QualType(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5976 | BSI->TheScope = BlockScope; |
Mike Stump | a670332 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5977 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | b9a6861 | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 5978 | BSI->hasPrototype = false; |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5979 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5980 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5981 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5982 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5983 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5986 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5987 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5988 | |
| 5989 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5990 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5991 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5992 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5993 | |
Mike Stump | d456c48 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5994 | if (T->isArrayType()) { |
| 5995 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5996 | diag::err_block_returns_array); |
| 5997 | return; |
| 5998 | } |
| 5999 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6000 | // The parameter list is optional, if there was none, assume (). |
| 6001 | if (!T->isFunctionType()) |
| 6002 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 6003 | |
| 6004 | CurBlock->hasPrototype = true; |
| 6005 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6006 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6007 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6009 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6010 | // FIXME: remove the attribute. |
| 6011 | } |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6012 | QualType RetTy = T.getTypePtr()->getAs<FunctionType>()->getResultType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6013 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6014 | // Do not allow returning a objc interface by-value. |
| 6015 | if (RetTy->isObjCInterfaceType()) { |
| 6016 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6017 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6018 | return; |
| 6019 | } |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 6020 | return; |
| 6021 | } |
| 6022 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6023 | // Analyze arguments to block. |
| 6024 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 6025 | "Not a function declarator!"); |
| 6026 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6027 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6028 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 6029 | CurBlock->isVariadic = true; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6030 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6031 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 6032 | // no arguments, not a function that takes a single void argument. |
| 6033 | if (FTI.hasPrototype && |
| 6034 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6035 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 6036 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6037 | // empty arg list, don't push any params. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6038 | CurBlock->isVariadic = false; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6039 | } else if (FTI.hasPrototype) { |
| 6040 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 83f095c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 6041 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6042 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6043 | } |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6044 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6045 | CurBlock->Params.size()); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 6046 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 6047 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6048 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 6049 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 6050 | // If this has an identifier, add it to the scope stack. |
| 6051 | if ((*AI)->getIdentifier()) |
| 6052 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6053 | |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6054 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6055 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6056 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6057 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 6058 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 6059 | // FIXME: remove the attribute. |
| 6060 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6061 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6062 | // Analyze the return type. |
| 6063 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6064 | QualType RetTy = T->getAs<FunctionType>()->getResultType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6065 | |
Chris Lattner | 6de0508 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 6066 | // Do not allow returning a objc interface by-value. |
| 6067 | if (RetTy->isObjCInterfaceType()) { |
| 6068 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 6069 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 6070 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6071 | CurBlock->ReturnType = RetTy; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6072 | } |
| 6073 | |
| 6074 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 6075 | /// is invoked to pop the information about the block from the action impl. |
| 6076 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 6077 | // Ensure that CurBlock is deleted. |
| 6078 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6079 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6080 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 6081 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6082 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 6083 | PopDeclContext(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6084 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6085 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6086 | } |
| 6087 | |
| 6088 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 6089 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6090 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 6091 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 6092 | // If blocks are disabled, emit an error. |
| 6093 | if (!LangOpts.Blocks) |
| 6094 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6095 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6096 | // Ensure that CurBlock is deleted. |
| 6097 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6098 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 6099 | PopDeclContext(); |
| 6100 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6101 | // Pop off CurBlock, handle nested blocks. |
| 6102 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6103 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6104 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 6105 | if (!BSI->ReturnType.isNull()) |
| 6106 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6107 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6108 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 6109 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 6110 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6111 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6112 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6113 | QualType BlockTy; |
| 6114 | if (!BSI->hasPrototype) |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6115 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 6116 | NoReturn); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6117 | else |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 6118 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6119 | BSI->isVariadic, 0, false, false, 0, 0, |
| 6120 | NoReturn); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6121 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6122 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6123 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6124 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6125 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 6126 | // If needed, diagnose invalid gotos and switches in the block. |
| 6127 | if (CurFunctionNeedsScopeChecking) |
| 6128 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 6129 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6130 | |
Anders Carlsson | b781bcd | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 6131 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 6132 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6133 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 6134 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 6135 | } |
| 6136 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6137 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 6138 | ExprArg expr, TypeTy *type, |
| 6139 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 6140 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6141 | Expr *E = static_cast<Expr*>(expr.get()); |
| 6142 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6143 | |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6144 | InitBuiltinVaListType(); |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6145 | |
| 6146 | // Get the va_list type |
| 6147 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6148 | if (VaListType->isArrayType()) { |
| 6149 | // Deal with implicit array decay; for example, on x86-64, |
| 6150 | // va_list is an array, but it's supposed to decay to |
| 6151 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6152 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6153 | // Make sure the input expression also decays appropriately. |
| 6154 | UsualUnaryConversions(E); |
| 6155 | } else { |
| 6156 | // Otherwise, the va_list argument must be an l-value because |
| 6157 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6158 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6159 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 6160 | return ExprError(); |
| 6161 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 6162 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 6163 | if (!E->isTypeDependent() && |
| 6164 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6165 | return ExprError(Diag(E->getLocStart(), |
| 6166 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 6167 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 6168 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6169 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 6170 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6171 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6172 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6173 | expr.release(); |
| 6174 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 6175 | RPLoc)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 6176 | } |
| 6177 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6178 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6179 | // The type of __null will be int or long, depending on the size of |
| 6180 | // pointers on the target. |
| 6181 | QualType Ty; |
| 6182 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 6183 | Ty = Context.IntTy; |
| 6184 | else |
| 6185 | Ty = Context.LongTy; |
| 6186 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 6187 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 6188 | } |
| 6189 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6190 | static void |
| 6191 | MakeObjCStringLiteralCodeModificationHint(Sema& SemaRef, |
| 6192 | QualType DstType, |
| 6193 | Expr *SrcExpr, |
| 6194 | CodeModificationHint &Hint) { |
| 6195 | if (!SemaRef.getLangOptions().ObjC1) |
| 6196 | return; |
| 6197 | |
| 6198 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 6199 | if (!PT) |
| 6200 | return; |
| 6201 | |
| 6202 | // Check if the destination is of type 'id'. |
| 6203 | if (!PT->isObjCIdType()) { |
| 6204 | // Check if the destination is the 'NSString' interface. |
| 6205 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 6206 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 6207 | return; |
| 6208 | } |
| 6209 | |
| 6210 | // Strip off any parens and casts. |
| 6211 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 6212 | if (!SL || SL->isWide()) |
| 6213 | return; |
| 6214 | |
| 6215 | Hint = CodeModificationHint::CreateInsertion(SL->getLocStart(), "@"); |
| 6216 | } |
| 6217 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6218 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 6219 | SourceLocation Loc, |
| 6220 | QualType DstType, QualType SrcType, |
| 6221 | Expr *SrcExpr, const char *Flavor) { |
| 6222 | // Decode the result (notice that AST's are still created for extensions). |
| 6223 | bool isInvalid = false; |
| 6224 | unsigned DiagKind; |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6225 | CodeModificationHint Hint; |
| 6226 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6227 | switch (ConvTy) { |
| 6228 | default: assert(0 && "Unknown conversion type"); |
| 6229 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6230 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6231 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 6232 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6233 | case IntToPointer: |
| 6234 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 6235 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6236 | case IncompatiblePointer: |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6237 | MakeObjCStringLiteralCodeModificationHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6238 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 6239 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6240 | case IncompatiblePointerSign: |
| 6241 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 6242 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6243 | case FunctionVoidPointer: |
| 6244 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 6245 | break; |
| 6246 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 6247 | // If the qualifiers lost were because we were applying the |
| 6248 | // (deprecated) C++ conversion from a string literal to a char* |
| 6249 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 6250 | // Ideally, this check would be performed in |
| 6251 | // CheckPointerTypesForAssignment. However, that would require a |
| 6252 | // bit of refactoring (so that the second argument is an |
| 6253 | // expression, rather than a type), which should be done as part |
| 6254 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 6255 | // C++ semantics. |
| 6256 | if (getLangOptions().CPlusPlus && |
| 6257 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 6258 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6259 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 6260 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 6261 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 6262 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6263 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6264 | case IntToBlockPointer: |
| 6265 | DiagKind = diag::err_int_to_block_pointer; |
| 6266 | break; |
| 6267 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 6268 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6269 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6270 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6271 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 6272 | // it can give a more specific diagnostic. |
| 6273 | DiagKind = diag::warn_incompatible_qualified_id; |
| 6274 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6275 | case IncompatibleVectors: |
| 6276 | DiagKind = diag::warn_incompatible_vectors; |
| 6277 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6278 | case Incompatible: |
| 6279 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 6280 | isInvalid = true; |
| 6281 | break; |
| 6282 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6283 | |
Chris Lattner | f3d3fae | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 6284 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 6285 | << SrcExpr->getSourceRange() << Hint; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6286 | return isInvalid; |
| 6287 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6288 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 6289 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6290 | llvm::APSInt ICEResult; |
| 6291 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 6292 | if (Result) |
| 6293 | *Result = ICEResult; |
| 6294 | return false; |
| 6295 | } |
| 6296 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6297 | Expr::EvalResult EvalResult; |
| 6298 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6299 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6300 | EvalResult.HasSideEffects) { |
| 6301 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 6302 | |
| 6303 | if (EvalResult.Diag) { |
| 6304 | // We only show the note if it's not the usual "invalid subexpression" |
| 6305 | // or if it's actually in a subexpression. |
| 6306 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 6307 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 6308 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 6309 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6310 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6311 | return true; |
| 6312 | } |
| 6313 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6314 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 6315 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6316 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 6317 | if (EvalResult.Diag && |
| 6318 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 6319 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6320 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 6321 | if (Result) |
| 6322 | *Result = EvalResult.Val.getInt(); |
| 6323 | return false; |
| 6324 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6325 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6326 | Sema::ExpressionEvaluationContext |
| 6327 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6328 | // Introduce a new set of potentially referenced declarations to the stack. |
| 6329 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 6330 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6331 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6332 | std::swap(ExprEvalContext, NewContext); |
| 6333 | return NewContext; |
| 6334 | } |
| 6335 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6336 | void |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6337 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 6338 | ExpressionEvaluationContext NewContext) { |
| 6339 | ExprEvalContext = NewContext; |
| 6340 | |
| 6341 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 6342 | // Mark any remaining declarations in the current position of the stack |
| 6343 | // as "referenced". If they were not meant to be referenced, semantic |
| 6344 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6345 | PotentiallyReferencedDecls RemainingDecls; |
| 6346 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 6347 | PotentiallyReferencedDeclStack.pop_back(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6348 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6349 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 6350 | IEnd = RemainingDecls.end(); |
| 6351 | I != IEnd; ++I) |
| 6352 | MarkDeclarationReferenced(I->first, I->second); |
| 6353 | } |
| 6354 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6355 | |
| 6356 | /// \brief Note that the given declaration was referenced in the source code. |
| 6357 | /// |
| 6358 | /// This routine should be invoke whenever a given declaration is referenced |
| 6359 | /// in the source code, and where that reference occurred. If this declaration |
| 6360 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 6361 | /// C99 6.9p3), then the declaration will be marked as used. |
| 6362 | /// |
| 6363 | /// \param Loc the location where the declaration was referenced. |
| 6364 | /// |
| 6365 | /// \param D the declaration that has been referenced by the source code. |
| 6366 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 6367 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6368 | |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6369 | if (D->isUsed()) |
| 6370 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6371 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 6372 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 6373 | // template or not. The reason for this is that unevaluated expressions |
| 6374 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 6375 | // -Wunused-parameters) |
| 6376 | if (isa<ParmVarDecl>(D) || |
| 6377 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6378 | D->setUsed(true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6379 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6380 | // Do not mark anything as "used" within a dependent context; wait for |
| 6381 | // an instantiation. |
| 6382 | if (CurContext->isDependentContext()) |
| 6383 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6384 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6385 | switch (ExprEvalContext) { |
| 6386 | case Unevaluated: |
| 6387 | // We are in an expression that is not potentially evaluated; do nothing. |
| 6388 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6389 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6390 | case PotentiallyEvaluated: |
| 6391 | // We are in a potentially-evaluated expression, so this declaration is |
| 6392 | // "used"; handle this below. |
| 6393 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6394 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6395 | case PotentiallyPotentiallyEvaluated: |
| 6396 | // We are in an expression that may be potentially evaluated; queue this |
| 6397 | // declaration reference until we know whether the expression is |
| 6398 | // potentially evaluated. |
| 6399 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 6400 | return; |
| 6401 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6402 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6403 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 6404 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6405 | unsigned TypeQuals; |
Fariborz Jahanian | 18eb69a | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6406 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 6407 | if (!Constructor->isUsed()) |
| 6408 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6409 | } else if (Constructor->isImplicit() && |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6410 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6411 | if (!Constructor->isUsed()) |
| 6412 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 6413 | } |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6414 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 6415 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 6416 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6417 | |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6418 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 6419 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 6420 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 6421 | if (!MethodDecl->isUsed()) |
| 6422 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 6423 | } |
| 6424 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 6425 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6426 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6427 | // class templates. |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6428 | if (!Function->getBody() && Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6429 | bool AlreadyInstantiated = false; |
| 6430 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 6431 | = Function->getTemplateSpecializationInfo()) { |
| 6432 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 6433 | SpecInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6434 | else if (SpecInfo->getTemplateSpecializationKind() |
| 6435 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6436 | AlreadyInstantiated = true; |
| 6437 | } else if (MemberSpecializationInfo *MSInfo |
| 6438 | = Function->getMemberSpecializationInfo()) { |
| 6439 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 6440 | MSInfo->setPointOfInstantiation(Loc); |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 6441 | else if (MSInfo->getTemplateSpecializationKind() |
| 6442 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6443 | AlreadyInstantiated = true; |
| 6444 | } |
| 6445 | |
| 6446 | if (!AlreadyInstantiated) |
| 6447 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
| 6448 | } |
| 6449 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6450 | // FIXME: keep track of references to static functions |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6451 | Function->setUsed(true); |
| 6452 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6453 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6454 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6455 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6456 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6457 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 6458 | Var->getInstantiatedFromStaticDataMember()) { |
| 6459 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 6460 | assert(MSInfo && "Missing member specialization information?"); |
| 6461 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 6462 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 6463 | MSInfo->setPointOfInstantiation(Loc); |
| 6464 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 6465 | } |
| 6466 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6467 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6468 | // FIXME: keep track of references to static data? |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6469 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6470 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6471 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 6472 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6473 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 6474 | |
| 6475 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 6476 | CallExpr *CE, FunctionDecl *FD) { |
| 6477 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 6478 | return false; |
| 6479 | |
| 6480 | PartialDiagnostic Note = |
| 6481 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 6482 | << FD->getDeclName() : PDiag(); |
| 6483 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| 6484 | |
| 6485 | if (RequireCompleteType(Loc, ReturnType, |
| 6486 | FD ? |
| 6487 | PDiag(diag::err_call_function_incomplete_return) |
| 6488 | << CE->getSourceRange() << FD->getDeclName() : |
| 6489 | PDiag(diag::err_call_incomplete_return) |
| 6490 | << CE->getSourceRange(), |
| 6491 | std::make_pair(NoteLoc, Note))) |
| 6492 | return true; |
| 6493 | |
| 6494 | return false; |
| 6495 | } |
| 6496 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6497 | // Diagnose the common s/=/==/ typo. Note that adding parentheses |
| 6498 | // will prevent this condition from triggering, which is what we want. |
| 6499 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 6500 | SourceLocation Loc; |
| 6501 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6502 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| 6503 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6504 | if (isa<BinaryOperator>(E)) { |
| 6505 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| 6506 | if (Op->getOpcode() != BinaryOperator::Assign) |
| 6507 | return; |
| 6508 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 6509 | // Greylist some idioms by putting them into a warning subcategory. |
| 6510 | if (ObjCMessageExpr *ME |
| 6511 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 6512 | Selector Sel = ME->getSelector(); |
| 6513 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 6514 | // self = [<foo> init...] |
| 6515 | if (isSelfExpr(Op->getLHS()) |
| 6516 | && Sel.getIdentifierInfoForSlot(0)->getName().startswith("init")) |
| 6517 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 6518 | |
| 6519 | // <foo> = [<bar> nextObject] |
| 6520 | else if (Sel.isUnarySelector() && |
| 6521 | Sel.getIdentifierInfoForSlot(0)->getName() == "nextObject") |
| 6522 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 6523 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6524 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6525 | Loc = Op->getOperatorLoc(); |
| 6526 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 6527 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| 6528 | if (Op->getOperator() != OO_Equal) |
| 6529 | return; |
| 6530 | |
| 6531 | Loc = Op->getOperatorLoc(); |
| 6532 | } else { |
| 6533 | // Not an assignment. |
| 6534 | return; |
| 6535 | } |
| 6536 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6537 | SourceLocation Open = E->getSourceRange().getBegin(); |
John McCall | e724ae9 | 2009-10-12 22:25:59 +0000 | [diff] [blame] | 6538 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6539 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 6540 | Diag(Loc, diagnostic) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 6541 | << E->getSourceRange() |
| 6542 | << CodeModificationHint::CreateInsertion(Open, "(") |
| 6543 | << CodeModificationHint::CreateInsertion(Close, ")"); |
| 6544 | } |
| 6545 | |
| 6546 | bool Sema::CheckBooleanCondition(Expr *&E, SourceLocation Loc) { |
| 6547 | DiagnoseAssignmentAsCondition(E); |
| 6548 | |
| 6549 | if (!E->isTypeDependent()) { |
| 6550 | DefaultFunctionArrayConversion(E); |
| 6551 | |
| 6552 | QualType T = E->getType(); |
| 6553 | |
| 6554 | if (getLangOptions().CPlusPlus) { |
| 6555 | if (CheckCXXBooleanCondition(E)) // C++ 6.4p4 |
| 6556 | return true; |
| 6557 | } else if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 6558 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 6559 | << T << E->getSourceRange(); |
| 6560 | return true; |
| 6561 | } |
| 6562 | } |
| 6563 | |
| 6564 | return false; |
| 6565 | } |