Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 64789f8 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | 9ed3e77 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/Basic/PartialDiagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Lex/LiteralSupport.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 71ca8c8 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Designator.h" |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 27 | #include "clang/Parse/Scope.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 31 | /// \brief Determine whether the use of this declaration is valid, and |
| 32 | /// emit any corresponding diagnostics. |
| 33 | /// |
| 34 | /// This routine diagnoses various problems with referencing |
| 35 | /// declarations that can occur when using a declaration. For example, |
| 36 | /// it might warn if a deprecated or unavailable declaration is being |
| 37 | /// used, or produce an error (and return true) if a C++0x deleted |
| 38 | /// function is being used. |
| 39 | /// |
| 40 | /// \returns true if there was an error (this declaration cannot be |
| 41 | /// referenced), false otherwise. |
| 42 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 43 | // See if the decl is deprecated. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 44 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 45 | // Implementing deprecated stuff requires referencing deprecated |
| 46 | // stuff. Don't warn if we are implementing a deprecated |
| 47 | // construct. |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 48 | bool isSilenced = false; |
| 49 | |
| 50 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 51 | // If this reference happens *in* a deprecated function or method, don't |
| 52 | // warn. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 53 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 54 | |
| 55 | // If this is an Objective-C method implementation, check to see if the |
| 56 | // method was deprecated on the declaration, not the definition. |
| 57 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(ND)) { |
| 58 | // The semantic decl context of a ObjCMethodDecl is the |
| 59 | // ObjCImplementationDecl. |
| 60 | if (ObjCImplementationDecl *Impl |
| 61 | = dyn_cast<ObjCImplementationDecl>(MD->getParent())) { |
| 62 | |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 63 | MD = Impl->getClassInterface()->getMethod(MD->getSelector(), |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 64 | MD->isInstanceMethod()); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 65 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
Chris Lattner | fb1bb82 | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (!isSilenced) |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 71 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 72 | } |
| 73 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 74 | // See if this is a deleted function. |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 75 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 76 | if (FD->isDeleted()) { |
| 77 | Diag(Loc, diag::err_deleted_function_use); |
| 78 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 79 | return true; |
| 80 | } |
Douglas Gregor | 6f8c368 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 81 | } |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 82 | |
| 83 | // See if the decl is unavailable |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 84 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 85 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 86 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 87 | } |
| 88 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | return false; |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 92 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
| 93 | /// (and other functions in future), which have been declared with sentinel |
| 94 | /// attribute. It warns if call does not have the sentinel argument. |
| 95 | /// |
| 96 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
| 97 | Expr **Args, unsigned NumArgs) |
| 98 | { |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 99 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 100 | if (!attr) |
| 101 | return; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 102 | int sentinelPos = attr->getSentinel(); |
| 103 | int nullPos = attr->getNullPos(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 104 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 105 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 106 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 107 | unsigned int i = 0; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 108 | bool warnNotEnoughArgs = false; |
| 109 | int isMethod = 0; |
| 110 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 111 | // skip over named parameters. |
| 112 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 113 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 114 | if (nullPos) |
| 115 | --nullPos; |
| 116 | else |
| 117 | ++i; |
| 118 | } |
| 119 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 120 | isMethod = 1; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 121 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 122 | // skip over named parameters. |
| 123 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 124 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 125 | if (nullPos) |
| 126 | --nullPos; |
| 127 | else |
| 128 | ++i; |
| 129 | } |
| 130 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 131 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 132 | // block or function pointer call. |
| 133 | QualType Ty = V->getType(); |
| 134 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| 135 | const FunctionType *FT = Ty->isFunctionPointerType() |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 136 | ? Ty->getAs<PointerType>()->getPointeeType()->getAsFunctionType() |
| 137 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType(); |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 138 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 139 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 140 | unsigned k; |
| 141 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 142 | if (nullPos) |
| 143 | --nullPos; |
| 144 | else |
| 145 | ++i; |
| 146 | } |
| 147 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 148 | } |
| 149 | if (Ty->isBlockPointerType()) |
| 150 | isMethod = 2; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 151 | } else |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 152 | return; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 153 | } else |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 154 | return; |
| 155 | |
| 156 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 158 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | int sentinel = i; |
| 162 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 163 | --sentinelPos; |
| 164 | ++i; |
| 165 | } |
| 166 | if (sentinelPos > 0) { |
| 167 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 168 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | while (i < NumArgs-1) { |
| 172 | ++i; |
| 173 | ++sentinel; |
| 174 | } |
| 175 | Expr *sentinelExpr = Args[sentinel]; |
| 176 | if (sentinelExpr && (!sentinelExpr->getType()->isPointerType() || |
| 177 | !sentinelExpr->isNullPointerConstant(Context))) { |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 178 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 09f2e3f | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 179 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 79d29e7 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 180 | } |
| 181 | return; |
Fariborz Jahanian | 180f341 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Douglas Gregor | 3bb3000 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 184 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 185 | Expr *Ex = (Expr *)E; |
| 186 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 187 | } |
| 188 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 189 | //===----------------------------------------------------------------------===// |
| 190 | // Standard Promotions and Conversions |
| 191 | //===----------------------------------------------------------------------===// |
| 192 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 193 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 194 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 195 | QualType Ty = E->getType(); |
| 196 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 197 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 198 | if (Ty->isFunctionType()) |
Anders Carlsson | b6feaf3 | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 199 | ImpCastExprToType(E, Context.getPointerType(Ty), |
| 200 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 201 | else if (Ty->isArrayType()) { |
| 202 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 203 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 204 | // type 'array of type' is converted to an expression that has type 'pointer |
| 205 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 206 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 207 | // (C90) to "an expression" (C99). |
Argiris Kirtzidis | f580b4d | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 208 | // |
| 209 | // C++ 4.2p1: |
| 210 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 211 | // T" can be converted to an rvalue of type "pointer to T". |
| 212 | // |
| 213 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 214 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 5c09af0 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 215 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 216 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 2aa6882 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 217 | } |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| 221 | /// operators (C99 6.3). The conversions of array and function types are |
| 222 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 223 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 224 | /// In these instances, this routine should *not* be called. |
| 225 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 226 | QualType Ty = Expr->getType(); |
| 227 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| 228 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 229 | // C99 6.3.1.1p2: |
| 230 | // |
| 231 | // The following may be used in an expression wherever an int or |
| 232 | // unsigned int may be used: |
| 233 | // - an object or expression with an integer type whose integer |
| 234 | // conversion rank is less than or equal to the rank of int |
| 235 | // and unsigned int. |
| 236 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 237 | // |
| 238 | // If an int can represent all values of the original type, the |
| 239 | // value is converted to an int; otherwise, it is converted to an |
| 240 | // unsigned int. These are called the integer promotions. All |
| 241 | // other types are unchanged by the integer promotions. |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 242 | QualType PTy = Context.isPromotableBitField(Expr); |
| 243 | if (!PTy.isNull()) { |
| 244 | ImpCastExprToType(Expr, PTy); |
| 245 | return Expr; |
| 246 | } |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 247 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | 6ae7d11 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 248 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 249 | ImpCastExprToType(Expr, PT); |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 250 | return Expr; |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Douglas Gregor | 70b307e | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 253 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 254 | return Expr; |
| 255 | } |
| 256 | |
Chris Lattner | 9305c3d | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 257 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 258 | /// do not have a prototype. Arguments that have type float are promoted to |
| 259 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 260 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 261 | QualType Ty = Expr->getType(); |
| 262 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| 263 | |
| 264 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 265 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 266 | if (BT->getKind() == BuiltinType::Float) |
| 267 | return ImpCastExprToType(Expr, Context.DoubleTy); |
| 268 | |
| 269 | UsualUnaryConversions(Expr); |
| 270 | } |
| 271 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 272 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 273 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 274 | /// interfaces passed by value. This returns true if the argument type is |
| 275 | /// completely illegal. |
| 276 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 277 | DefaultArgumentPromotion(Expr); |
| 278 | |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 279 | if (Expr->getType()->isObjCInterfaceType()) { |
| 280 | Diag(Expr->getLocStart(), |
| 281 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 282 | << Expr->getType() << CT; |
| 283 | return true; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 284 | } |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 285 | |
| 286 | if (!Expr->getType()->isPODType()) |
| 287 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 288 | << Expr->getType() << CT; |
| 289 | |
| 290 | return false; |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 294 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 295 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 296 | /// routine returns the first non-arithmetic type found. The client is |
| 297 | /// responsible for emitting appropriate error diagnostics. |
| 298 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 299 | /// GCC. |
| 300 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 301 | bool isCompAssign) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 302 | if (!isCompAssign) |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 303 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 304 | |
| 305 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 307 | // For conversion purposes, we ignore any qualifiers. |
| 308 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 309 | QualType lhs = |
| 310 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
| 311 | QualType rhs = |
| 312 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 313 | |
| 314 | // If both types are identical, no conversion is needed. |
| 315 | if (lhs == rhs) |
| 316 | return lhs; |
| 317 | |
| 318 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 319 | // The caller can deal with this (e.g. pointer + int). |
| 320 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 321 | return lhs; |
| 322 | |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 323 | // Perform bitfield promotions. |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 324 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 325 | if (!LHSBitfieldPromoteTy.isNull()) |
| 326 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 327 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 328 | if (!RHSBitfieldPromoteTy.isNull()) |
| 329 | rhs = RHSBitfieldPromoteTy; |
| 330 | |
Eli Friedman | 6ae7d11 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 331 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 332 | if (!isCompAssign) |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 333 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 334 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 335 | return destType; |
| 336 | } |
| 337 | |
Chris Lattner | 299b884 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 338 | //===----------------------------------------------------------------------===// |
| 339 | // Semantic Analysis for various Expression Types |
| 340 | //===----------------------------------------------------------------------===// |
| 341 | |
| 342 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 343 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 344 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 345 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 346 | /// multiple tokens. However, the common case is that StringToks points to one |
| 347 | /// string. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 348 | /// |
| 349 | Action::OwningExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 350 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 351 | assert(NumStringToks && "Must have at least one string!"); |
| 352 | |
Chris Lattner | 9eaf2b7 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 353 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 354 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 355 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | |
| 357 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 358 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 359 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 360 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 361 | QualType StrTy = Context.CharTy; |
Argiris Kirtzidis | 2a4e116 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 362 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 363 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 364 | |
| 365 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 366 | if (getLangOptions().CPlusPlus) |
| 367 | StrTy.addConst(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 368 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 369 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 370 | // the nul terminator character as well as the string length for pascal |
| 371 | // strings. |
| 372 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | 1403222 | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 373 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 374 | ArrayType::Normal, 0); |
Chris Lattner | c314474 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 376 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Chris Lattner | aa49119 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 377 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 378 | Literal.GetStringLength(), |
| 379 | Literal.AnyWide, StrTy, |
| 380 | &StringTokLocs[0], |
| 381 | StringTokLocs.size())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 384 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 385 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 386 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 387 | /// for values inside the block or for globals). |
| 388 | /// |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 389 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 390 | /// up-to-date. |
| 391 | /// |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 392 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 393 | ValueDecl *VD) { |
| 394 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 395 | // we wanted to. |
| 396 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 397 | return false; |
| 398 | |
| 399 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 400 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 401 | return false; |
| 402 | |
| 403 | // If this is a reference to an extern, static, or global variable, no need to |
| 404 | // snapshot it. |
| 405 | // FIXME: What about 'const' variables in C++? |
| 406 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 0b46425 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 407 | if (!Var->hasLocalStorage()) |
| 408 | return false; |
| 409 | |
| 410 | // Blocks that have these can't be constant. |
| 411 | CurBlock->hasBlockDeclRefExprs = true; |
| 412 | |
| 413 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 414 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 415 | // be defined outside all of the current blocks (in which case the blocks do |
| 416 | // all get the bit). Walk the nesting chain. |
| 417 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 418 | NextBlock = NextBlock->PrevBlockInfo) { |
| 419 | // If we found the defining block for the variable, don't mark the block as |
| 420 | // having a reference outside it. |
| 421 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 422 | break; |
| 423 | |
| 424 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 425 | // a snapshot as well. |
| 426 | NextBlock->hasBlockDeclRefExprs = true; |
| 427 | } |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 428 | |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 434 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 435 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 436 | /// identifier is used in a function call context. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 437 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 438 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 439 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 440 | IdentifierInfo &II, |
| 441 | bool HasTrailingLParen, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 442 | const CXXScopeSpec *SS, |
| 443 | bool isAddressOfOperand) { |
| 444 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 445 | isAddressOfOperand); |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 448 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 449 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 450 | /// nested-name-specifier. |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 451 | Sema::OwningExprResult |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 452 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 453 | bool TypeDependent, bool ValueDependent, |
| 454 | const CXXScopeSpec *SS) { |
Anders Carlsson | 9bd4866 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 455 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 456 | Diag(Loc, |
| 457 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 458 | << D->getDeclName(); |
| 459 | return ExprError(); |
| 460 | } |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 461 | |
| 462 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 463 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 464 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 465 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
| 466 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 467 | << D->getIdentifier() << FD->getDeclName(); |
| 468 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
| 469 | << D->getIdentifier(); |
| 470 | return ExprError(); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 476 | MarkDeclarationReferenced(Loc, D); |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 477 | |
| 478 | Expr *E; |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 479 | if (SS && !SS->isEmpty()) { |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 480 | E = new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
| 481 | ValueDependent, SS->getRange(), |
Douglas Gregor | 041e929 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 482 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | 7e50826 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 483 | } else |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 484 | E = new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
| 485 | |
| 486 | return Owned(E); |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 489 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 490 | /// variable corresponding to the anonymous union or struct whose type |
| 491 | /// is Record. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 492 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 493 | RecordDecl *Record) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 494 | assert(Record->isAnonymousStructOrUnion() && |
| 495 | "Record must be an anonymous struct or union!"); |
| 496 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 497 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 498 | // operation rather than a slow walk through DeclContext's vector (which |
| 499 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 500 | DeclContext *Ctx = Record->getDeclContext(); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 501 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
| 502 | DEnd = Ctx->decls_end(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 503 | D != DEnd; ++D) { |
| 504 | if (*D == Record) { |
| 505 | // The object for the anonymous struct/union directly |
| 506 | // follows its type in the list of declarations. |
| 507 | ++D; |
| 508 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 509 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 510 | return *D; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | assert(false && "Missing object for anonymous record"); |
| 515 | return 0; |
| 516 | } |
| 517 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 518 | /// \brief Given a field that represents a member of an anonymous |
| 519 | /// struct/union, build the path from that field's context to the |
| 520 | /// actual member. |
| 521 | /// |
| 522 | /// Construct the sequence of field member references we'll have to |
| 523 | /// perform to get to the field in the anonymous union/struct. The |
| 524 | /// list of members is built from the field outward, so traverse it |
| 525 | /// backwards to go from an object in the current context to the field |
| 526 | /// we found. |
| 527 | /// |
| 528 | /// \returns The variable from which the field access should begin, |
| 529 | /// for an anonymous struct/union that is not a member of another |
| 530 | /// class. Otherwise, returns NULL. |
| 531 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 532 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 533 | assert(Field->getDeclContext()->isRecord() && |
| 534 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 535 | && "Field must be stored inside an anonymous struct or union"); |
| 536 | |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 537 | Path.push_back(Field); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 538 | VarDecl *BaseObject = 0; |
| 539 | DeclContext *Ctx = Field->getDeclContext(); |
| 540 | do { |
| 541 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 542 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 543 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 544 | Path.push_back(AnonField); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 545 | else { |
| 546 | BaseObject = cast<VarDecl>(AnonObject); |
| 547 | break; |
| 548 | } |
| 549 | Ctx = Ctx->getParent(); |
| 550 | } while (Ctx->isRecord() && |
| 551 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | cc94ab7 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 552 | |
| 553 | return BaseObject; |
| 554 | } |
| 555 | |
| 556 | Sema::OwningExprResult |
| 557 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 558 | FieldDecl *Field, |
| 559 | Expr *BaseObjectExpr, |
| 560 | SourceLocation OpLoc) { |
| 561 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
| 562 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
| 563 | AnonFields); |
| 564 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 565 | // Build the expression that refers to the base object, from |
| 566 | // which we will build a sequence of member references to each |
| 567 | // of the anonymous union objects and, eventually, the field we |
| 568 | // found via name lookup. |
| 569 | bool BaseObjectIsPointer = false; |
| 570 | unsigned ExtraQuals = 0; |
| 571 | if (BaseObject) { |
| 572 | // BaseObject is an anonymous struct/union variable (and is, |
| 573 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 574 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 575 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 576 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 577 | SourceLocation()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 578 | ExtraQuals |
| 579 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 580 | } else if (BaseObjectExpr) { |
| 581 | // The caller provided the base object expression. Determine |
| 582 | // whether its a pointer and whether it adds any qualifiers to the |
| 583 | // anonymous struct/union fields we're looking into. |
| 584 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 585 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 586 | BaseObjectIsPointer = true; |
| 587 | ObjectType = ObjectPtr->getPointeeType(); |
| 588 | } |
| 589 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 590 | } else { |
| 591 | // We've found a member of an anonymous struct/union that is |
| 592 | // inside a non-anonymous struct/union, so in a well-formed |
| 593 | // program our base object expression is "this". |
| 594 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 595 | if (!MD->isStatic()) { |
| 596 | QualType AnonFieldType |
| 597 | = Context.getTagDeclType( |
| 598 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 599 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 600 | if ((Context.getCanonicalType(AnonFieldType) |
| 601 | == Context.getCanonicalType(ThisType)) || |
| 602 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 603 | // Our base object expression is "this". |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 604 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 605 | MD->getThisType(Context)); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 606 | BaseObjectIsPointer = true; |
| 607 | } |
| 608 | } else { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 609 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 610 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 611 | } |
| 612 | ExtraQuals = MD->getTypeQualifiers(); |
| 613 | } |
| 614 | |
| 615 | if (!BaseObjectExpr) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 616 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 617 | << Field->getDeclName()); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | // Build the implicit member references to the field of the |
| 621 | // anonymous struct/union. |
| 622 | Expr *Result = BaseObjectExpr; |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 623 | unsigned BaseAddrSpace = BaseObjectExpr->getType().getAddressSpace(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 624 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 625 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 626 | FI != FIEnd; ++FI) { |
| 627 | QualType MemberType = (*FI)->getType(); |
| 628 | if (!(*FI)->isMutable()) { |
| 629 | unsigned combinedQualifiers |
| 630 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 631 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 632 | } |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 633 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 634 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 635 | MarkDeclarationReferenced(Loc, *FI); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 636 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 637 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 638 | OpLoc, MemberType); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 639 | BaseObjectIsPointer = false; |
| 640 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 643 | return Owned(Result); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 646 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 647 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 648 | /// performs lookup on that name and returns an expression that refers |
| 649 | /// to that name. This routine isn't directly called from the parser, |
| 650 | /// because the parser doesn't know about DeclarationName. Rather, |
| 651 | /// this routine is called by ActOnIdentifierExpr, |
| 652 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 653 | /// which form the DeclarationName from the corresponding syntactic |
| 654 | /// forms. |
| 655 | /// |
| 656 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 657 | /// function call context. LookupCtx is only used for a C++ |
| 658 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 659 | /// the identifier must be a member of. |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 660 | /// |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 661 | /// isAddressOfOperand means that this expression is the direct operand |
| 662 | /// of an address-of operator. This matters because this is the only |
| 663 | /// situation where a qualified name referencing a non-static member may |
| 664 | /// appear outside a member function of this class. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 665 | Sema::OwningExprResult |
| 666 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 667 | DeclarationName Name, bool HasTrailingLParen, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 668 | const CXXScopeSpec *SS, |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 669 | bool isAddressOfOperand) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 670 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 52ae30c | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 671 | if (SS && SS->isInvalid()) |
| 672 | return ExprError(); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 673 | |
| 674 | // C++ [temp.dep.expr]p3: |
| 675 | // An id-expression is type-dependent if it contains: |
| 676 | // -- a nested-name-specifier that contains a class-name that |
| 677 | // names a dependent type. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 678 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 679 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 680 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
| 681 | Loc, SS->getRange(), |
Anders Carlsson | 4e8d569 | 2009-07-09 00:05:08 +0000 | [diff] [blame] | 682 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()), |
| 683 | isAddressOfOperand)); |
Douglas Gregor | 47bde7c | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 686 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 687 | false, true, Loc); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 688 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 689 | if (Lookup.isAmbiguous()) { |
| 690 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 691 | SS && SS->isSet() ? SS->getRange() |
| 692 | : SourceRange()); |
| 693 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | NamedDecl *D = Lookup.getAsDecl(); |
Douglas Gregor | a133e26 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 697 | |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 698 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 699 | // well. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 700 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 701 | if (II && getCurMethodDecl()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 702 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 703 | // in which case we should look for an ivar. 2) scoped lookup could have |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 704 | // found a decl, but that decl is outside the current instance method (i.e. |
| 705 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 706 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 707 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argiris Kirtzidis | 95256e6 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 708 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 709 | ObjCInterfaceDecl *ClassDeclared; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 710 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Chris Lattner | 2a3bef9 | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 711 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 712 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 713 | return ExprError(); |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 714 | |
| 715 | // If we're referencing an invalid decl, just return this as a silent |
| 716 | // error node. The error diagnostic was already emitted on the decl. |
| 717 | if (IV->isInvalidDecl()) |
| 718 | return ExprError(); |
| 719 | |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 720 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 721 | // If a class method attemps to use a free standing ivar, this is |
| 722 | // an error. |
| 723 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 724 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 725 | << IV->getDeclName()); |
| 726 | // If a class method uses a global variable, even if an ivar with |
| 727 | // same name exists, use the global. |
| 728 | if (!IsClsMethod) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 729 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 730 | ClassDeclared != IFace) |
| 731 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 732 | // FIXME: This should use a new expr for a direct reference, don't |
| 733 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 734 | IdentifierInfo &II = Context.Idents.get("self"); |
Argiris Kirtzidis | 3bb4904 | 2009-07-18 08:49:37 +0000 | [diff] [blame] | 735 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, SourceLocation(), |
| 736 | II, false); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 737 | MarkDeclarationReferenced(Loc, IV); |
Daniel Dunbar | f5254bd | 2009-04-21 01:19:28 +0000 | [diff] [blame] | 738 | return Owned(new (Context) |
| 739 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 740 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 741 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 742 | } |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 743 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 744 | // We should warn if a local variable hides an ivar. |
| 745 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 746 | ObjCInterfaceDecl *ClassDeclared; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 747 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 748 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 749 | IFace == ClassDeclared) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 750 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 751 | } |
Fariborz Jahanian | 67502db | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 752 | } |
Steve Naroff | 0ccfaa4 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 753 | // Needed to implement property "super.method" notation. |
Douglas Gregor | af8ad2b | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 754 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 755 | QualType T; |
| 756 | |
| 757 | if (getCurMethodDecl()->isInstanceMethod()) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 758 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 759 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 760 | else |
| 761 | T = Context.getObjCClassType(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 762 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | 6f78625 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 764 | } |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 766 | // Determine whether this name might be a candidate for |
| 767 | // argument-dependent lookup. |
| 768 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 769 | HasTrailingLParen; |
| 770 | |
| 771 | if (ADL && D == 0) { |
Douglas Gregor | e2d88fd | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 772 | // We've seen something of the form |
| 773 | // |
| 774 | // identifier( |
| 775 | // |
| 776 | // and we did not find any entity by the name |
| 777 | // "identifier". However, this identifier is still subject to |
| 778 | // argument-dependent lookup, so keep track of the name. |
| 779 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 780 | Context.OverloadTy, |
| 781 | Loc)); |
| 782 | } |
| 783 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 784 | if (D == 0) { |
| 785 | // Otherwise, this could be an implicitly declared function reference (legal |
| 786 | // in C90, extension in C99). |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 787 | if (HasTrailingLParen && II && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 788 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 789 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 790 | else { |
| 791 | // If this name wasn't predeclared and if this is not a function call, |
| 792 | // diagnose the problem. |
Anders Carlsson | 4355a39 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 793 | if (SS && !SS->isEmpty()) { |
| 794 | DiagnoseMissingMember(Loc, Name, |
| 795 | (NestedNameSpecifier *)SS->getScopeRep(), |
| 796 | SS->getRange()); |
| 797 | return ExprError(); |
| 798 | } else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
Douglas Gregor | aee3bf8 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 799 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 800 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 801 | << Name.getAsString()); |
Argiris Kirtzidis | 054a263 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 802 | else |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 803 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 806 | |
| 807 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 808 | // Warn about constructs like: |
| 809 | // if (void *X = foo()) { ... } else { X }. |
| 810 | // In the else block, the pointer is always false. |
| 811 | |
| 812 | // FIXME: In a template instantiation, we don't have scope |
| 813 | // information to check this property. |
| 814 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 815 | Scope *CheckS = S; |
| 816 | while (CheckS) { |
| 817 | if (CheckS->isWithinElse() && |
| 818 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
| 819 | if (Var->getType()->isBooleanType()) |
| 820 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 821 | << Var->getDeclName()); |
| 822 | else |
| 823 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 824 | << Var->getDeclName()); |
| 825 | break; |
| 826 | } |
| 827 | |
| 828 | // Move up one more control parent to check again. |
| 829 | CheckS = CheckS->getControlParent(); |
| 830 | if (CheckS) |
| 831 | CheckS = CheckS->getParent(); |
| 832 | } |
| 833 | } |
| 834 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 835 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 836 | // C99 DR 316 says that, if a function type comes from a |
| 837 | // function definition (without a prototype), that type is only |
| 838 | // used for checking compatibility. Therefore, when referencing |
| 839 | // the function, we pretend that we don't have the full function |
| 840 | // type. |
| 841 | if (DiagnoseUseOfDecl(Func, Loc)) |
| 842 | return ExprError(); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 844 | QualType T = Func->getType(); |
| 845 | QualType NoProtoType = T; |
| 846 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 847 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
| 848 | return BuildDeclRefExpr(Func, NoProtoType, Loc, false, false, SS); |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | return BuildDeclarationNameExpr(Loc, D, HasTrailingLParen, SS, isAddressOfOperand); |
| 853 | } |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 854 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 855 | bool |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 856 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 857 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
| 858 | if (CXXRecordDecl *RD = |
| 859 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
| 860 | QualType DestType = |
| 861 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 862 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 863 | return false; |
| 864 | QualType FromRecordType = From->getType(); |
| 865 | QualType DestRecordType = DestType; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 866 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 867 | DestType = Context.getPointerType(DestType); |
| 868 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 869 | } |
Fariborz Jahanian | 3ae1c80 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 870 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 871 | CheckDerivedToBaseConversion(FromRecordType, |
| 872 | DestRecordType, |
| 873 | From->getSourceRange().getBegin(), |
| 874 | From->getSourceRange())) |
| 875 | return true; |
Anders Carlsson | 8518694 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 876 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 877 | /*isLvalue=*/true); |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 878 | } |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 879 | return false; |
Fariborz Jahanian | 80b859e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 880 | } |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | c1991bf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 882 | /// \brief Build a MemberExpr AST node. |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 883 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
| 884 | const CXXScopeSpec *SS, NamedDecl *Member, |
| 885 | SourceLocation Loc, QualType Ty) { |
| 886 | if (SS && SS->isSet()) |
Douglas Gregor | c1991bf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 887 | return MemberExpr::Create(C, Base, isArrow, |
| 888 | (NestedNameSpecifier *)SS->getScopeRep(), |
Douglas Gregor | d33e328 | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 889 | SS->getRange(), Member, Loc, |
| 890 | // FIXME: Explicit template argument lists |
| 891 | false, SourceLocation(), 0, 0, SourceLocation(), |
| 892 | Ty); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 893 | |
| 894 | return new (C) MemberExpr(Base, isArrow, Member, Loc, Ty); |
| 895 | } |
| 896 | |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 897 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 898 | Sema::OwningExprResult |
| 899 | Sema::BuildDeclarationNameExpr(SourceLocation Loc, NamedDecl *D, |
| 900 | bool HasTrailingLParen, |
| 901 | const CXXScopeSpec *SS, |
| 902 | bool isAddressOfOperand) { |
| 903 | assert(D && "Cannot refer to a NULL declaration"); |
| 904 | DeclarationName Name = D->getDeclName(); |
| 905 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 906 | // If this is an expression of the form &Class::member, don't build an |
| 907 | // implicit member ref, because we want a pointer to the member in general, |
| 908 | // not any specific instance's member. |
| 909 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 910 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 09be81b | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 911 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 912 | QualType DType; |
| 913 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 914 | DType = FD->getType().getNonReferenceType(); |
| 915 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 916 | DType = Method->getType(); |
| 917 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 918 | DType = Context.OverloadTy; |
| 919 | } |
| 920 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 921 | if (!DType.isNull()) { |
| 922 | // The pointer is type- and value-dependent if it points into something |
| 923 | // dependent. |
Douglas Gregor | f3a200f | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 924 | bool Dependent = DC->isDependentContext(); |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 925 | return BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS); |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | } |
| 929 | |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 930 | // We may have found a field within an anonymous union or struct |
| 931 | // (C++ [class.union]). |
| 932 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 933 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 934 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 936 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 937 | if (!MD->isStatic()) { |
| 938 | // C++ [class.mfct.nonstatic]p2: |
| 939 | // [...] if name lookup (3.4.1) resolves the name in the |
| 940 | // id-expression to a nonstatic nontype member of class X or of |
| 941 | // a base class of X, the id-expression is transformed into a |
| 942 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 943 | // as the postfix-expression to the left of the '.' operator. |
| 944 | DeclContext *Ctx = 0; |
| 945 | QualType MemberType; |
| 946 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 947 | Ctx = FD->getDeclContext(); |
| 948 | MemberType = FD->getType(); |
| 949 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 950 | if (const ReferenceType *RefType = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 951 | MemberType = RefType->getPointeeType(); |
| 952 | else if (!FD->isMutable()) { |
| 953 | unsigned combinedQualifiers |
| 954 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 955 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 956 | } |
| 957 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 958 | if (!Method->isStatic()) { |
| 959 | Ctx = Method->getParent(); |
| 960 | MemberType = Method->getType(); |
| 961 | } |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 962 | } else if (FunctionTemplateDecl *FunTmpl |
| 963 | = dyn_cast<FunctionTemplateDecl>(D)) { |
| 964 | if (CXXMethodDecl *Method |
| 965 | = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())) { |
| 966 | if (!Method->isStatic()) { |
| 967 | Ctx = Method->getParent(); |
| 968 | MemberType = Context.OverloadTy; |
| 969 | } |
| 970 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 971 | } else if (OverloadedFunctionDecl *Ovl |
| 972 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 973 | // FIXME: We need an abstraction for iterating over one or more function |
| 974 | // templates or functions. This code is far too repetitive! |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 975 | for (OverloadedFunctionDecl::function_iterator |
| 976 | Func = Ovl->function_begin(), |
| 977 | FuncEnd = Ovl->function_end(); |
| 978 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 979 | CXXMethodDecl *DMethod = 0; |
| 980 | if (FunctionTemplateDecl *FunTmpl |
| 981 | = dyn_cast<FunctionTemplateDecl>(*Func)) |
| 982 | DMethod = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 983 | else |
| 984 | DMethod = dyn_cast<CXXMethodDecl>(*Func); |
| 985 | |
| 986 | if (DMethod && !DMethod->isStatic()) { |
| 987 | Ctx = DMethod->getDeclContext(); |
| 988 | MemberType = Context.OverloadTy; |
| 989 | break; |
| 990 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 993 | |
| 994 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 995 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 996 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
| 997 | if ((Context.getCanonicalType(CtxType) |
| 998 | == Context.getCanonicalType(ThisType)) || |
| 999 | IsDerivedFrom(ThisType, CtxType)) { |
| 1000 | // Build the implicit member access expression. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1001 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1002 | MD->getThisType(Context)); |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1003 | MarkDeclarationReferenced(Loc, D); |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1004 | if (PerformObjectMemberConversion(This, D)) |
| 1005 | return ExprError(); |
Anders Carlsson | 9fbe687 | 2009-08-08 16:55:18 +0000 | [diff] [blame] | 1006 | if (DiagnoseUseOfDecl(D, Loc)) |
| 1007 | return ExprError(); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1008 | return Owned(BuildMemberExpr(Context, This, true, SS, D, |
| 1009 | Loc, MemberType)); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | } |
| 1014 | |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1015 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1016 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 1017 | if (MD->isStatic()) |
| 1018 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1019 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 1020 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1023 | // Any other ways we could have found the field in a well-formed |
| 1024 | // program would have been turned into implicit member expressions |
| 1025 | // above. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1026 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1027 | << FD->getDeclName()); |
Argiris Kirtzidis | 38f1671 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1028 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1029 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1030 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1031 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1032 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1033 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argiris Kirtzidis | 03e6aaf | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1034 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1035 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1036 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1037 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1038 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1039 | return BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 1040 | false, false, SS); |
Douglas Gregor | 35d81bb | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1041 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1042 | return BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 1043 | false, false, SS); |
Anders Carlsson | 8990854 | 2009-08-29 01:06:32 +0000 | [diff] [blame] | 1044 | else if (UnresolvedUsingDecl *UD = dyn_cast<UnresolvedUsingDecl>(D)) |
| 1045 | return BuildDeclRefExpr(UD, Context.DependentTy, Loc, |
| 1046 | /*TypeDependent=*/true, |
| 1047 | /*ValueDependent=*/true, SS); |
| 1048 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1049 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1050 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1051 | // Check whether this declaration can be used. Note that we suppress |
| 1052 | // this check when we're going to perform argument-dependent lookup |
| 1053 | // on this function name, because this might not be the function |
| 1054 | // that overload resolution actually selects. |
Douglas Gregor | 6ef403d | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1055 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
| 1056 | HasTrailingLParen; |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1057 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 1058 | return ExprError(); |
| 1059 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1060 | // Only create DeclRefExpr's for valid Decl's. |
| 1061 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1062 | return ExprError(); |
| 1063 | |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1064 | // If the identifier reference is inside a block, and it refers to a value |
| 1065 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1066 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1067 | // the block is formed. |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1068 | // |
Chris Lattner | b2ebd48 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1069 | // We do not do this for things like enum constants, global variables, etc, |
| 1070 | // as they do not get snapshotted. |
| 1071 | // |
| 1072 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1073 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1074 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1075 | // The BlocksAttr indicates the variable is bound by-reference. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1076 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1077 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1078 | // This is to record that a 'const' was actually synthesize and added. |
| 1079 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1080 | // Variable will be bound by-copy, make it const within the closure. |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1081 | |
Eli Friedman | 9c2b33f | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1082 | ExprTy.addConst(); |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1083 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
| 1084 | constAdded)); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1085 | } |
| 1086 | // If this reference is not in a block or if the referenced variable is |
| 1087 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1089 | bool TypeDependent = false; |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1090 | bool ValueDependent = false; |
| 1091 | if (getLangOptions().CPlusPlus) { |
| 1092 | // C++ [temp.dep.expr]p3: |
| 1093 | // An id-expression is type-dependent if it contains: |
| 1094 | // - an identifier that was declared with a dependent type, |
| 1095 | if (VD->getType()->isDependentType()) |
| 1096 | TypeDependent = true; |
| 1097 | // - FIXME: a template-id that is dependent, |
| 1098 | // - a conversion-function-id that specifies a dependent type, |
| 1099 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1100 | Name.getCXXNameType()->isDependentType()) |
| 1101 | TypeDependent = true; |
| 1102 | // - a nested-name-specifier that contains a class-name that |
| 1103 | // names a dependent type. |
| 1104 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | 734b4ba | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1105 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1106 | DC; DC = DC->getParent()) { |
| 1107 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1108 | if (DC->isRecord()) { |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1109 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1110 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1111 | TypeDependent = true; |
| 1112 | break; |
| 1113 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1118 | // C++ [temp.dep.constexpr]p2: |
| 1119 | // |
| 1120 | // An identifier is value-dependent if it is: |
| 1121 | // - a name declared with a dependent type, |
| 1122 | if (TypeDependent) |
| 1123 | ValueDependent = true; |
| 1124 | // - the name of a non-type template parameter, |
| 1125 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1126 | ValueDependent = true; |
| 1127 | // - a constant with integral or enumeration type and is |
| 1128 | // initialized with an expression that is value-dependent |
Eli Friedman | 1f7744a | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1129 | else if (const VarDecl *Dcl = dyn_cast<VarDecl>(VD)) { |
| 1130 | if (Dcl->getType().getCVRQualifiers() == QualType::Const && |
| 1131 | Dcl->getInit()) { |
| 1132 | ValueDependent = Dcl->getInit()->isValueDependent(); |
| 1133 | } |
| 1134 | } |
Douglas Gregor | a5d8461 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1135 | } |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1136 | |
Anders Carlsson | 4571d81 | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1137 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1138 | TypeDependent, ValueDependent, SS); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1141 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1142 | tok::TokenKind Kind) { |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1143 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1145 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1146 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6990929 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1147 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1148 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1149 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1150 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1152 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1153 | // string. |
Anders Carlsson | 2c087d5 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1154 | |
| 1155 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1156 | if (!currentDecl) { |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1157 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2c087d5 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1158 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | bce5e4f | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1159 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1160 | |
Anders Carlsson | 2c087d5 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1161 | unsigned Length = |
| 1162 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1163 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1164 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1165 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1166 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1167 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1170 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1171 | llvm::SmallString<16> CharBuffer; |
| 1172 | CharBuffer.resize(Tok.getLength()); |
| 1173 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1174 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1175 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1176 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1177 | Tok.getLocation(), PP); |
| 1178 | if (Literal.hadError()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1179 | return ExprError(); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1180 | |
| 1181 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1182 | |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1183 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1184 | Literal.isWide(), |
| 1185 | type, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1188 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1189 | // Fast path for a single digit (which is quite common). A single digit |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1190 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1191 | if (Tok.getLength() == 1) { |
Chris Lattner | c374f8b | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1192 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | fd5f143 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1193 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1194 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | e5f128a | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1195 | Context.IntTy, Tok.getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1196 | } |
Ted Kremenek | dbde228 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1197 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1198 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 46d9134 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1199 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1200 | IntegerBuffer.resize(Tok.getLength()+1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1201 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1202 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1203 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1204 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1205 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1206 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1207 | Tok.getLocation(), PP); |
| 1208 | if (Literal.hadError) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1209 | return ExprError(); |
| 1210 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1211 | Expr *Res; |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1213 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1214 | QualType Ty; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1215 | if (Literal.isFloat) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1216 | Ty = Context.FloatTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1217 | else if (!Literal.isLong) |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1218 | Ty = Context.DoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1219 | else |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1220 | Ty = Context.LongDoubleTy; |
Chris Lattner | 2a674dc | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1221 | |
| 1222 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1223 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1224 | // isExact will be set by GetFloatValue(). |
| 1225 | bool isExact = false; |
Chris Lattner | ff1bf1a | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1226 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1227 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1228 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1229 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1230 | return ExprError(); |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1231 | } else { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1232 | QualType Ty; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1233 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1234 | // long long is a C99 feature. |
| 1235 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1236 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1237 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1238 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1239 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1240 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1242 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1243 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1244 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1245 | Ty = Context.UnsignedLongLongTy; |
| 1246 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1247 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1248 | } else { |
| 1249 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1250 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1251 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1252 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1253 | // be an unsigned int. |
| 1254 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1255 | |
| 1256 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1257 | unsigned Width = 0; |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1258 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1259 | // Are int/unsigned possibilities? |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1260 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1261 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1262 | // Does it fit in a unsigned int? |
| 1263 | if (ResultVal.isIntN(IntSize)) { |
| 1264 | // Does it fit in a signed int? |
| 1265 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1266 | Ty = Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1267 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1268 | Ty = Context.UnsignedIntTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1269 | Width = IntSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1270 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1271 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1272 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1273 | // Are long/unsigned long possibilities? |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1274 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1275 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1276 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1277 | // Does it fit in a unsigned long? |
| 1278 | if (ResultVal.isIntN(LongSize)) { |
| 1279 | // Does it fit in a signed long? |
| 1280 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1281 | Ty = Context.LongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1282 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1283 | Ty = Context.UnsignedLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1284 | Width = LongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1285 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1288 | // Finally, check long long if needed. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1289 | if (Ty.isNull()) { |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1290 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1291 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1292 | // Does it fit in a unsigned long long? |
| 1293 | if (ResultVal.isIntN(LongLongSize)) { |
| 1294 | // Does it fit in a signed long long? |
| 1295 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1296 | Ty = Context.LongLongTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1297 | else if (AllowUnsigned) |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1298 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1299 | Width = LongLongSize; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1300 | } |
| 1301 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1302 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1303 | // If we still couldn't decide a type, we probably have something that |
| 1304 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1305 | if (Ty.isNull()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1306 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1307 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1308 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1309 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | e406887 | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1311 | if (ResultVal.getBitWidth() != Width) |
| 1312 | ResultVal.trunc(Width); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1313 | } |
Sebastian Redl | 7532493 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1314 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1315 | } |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1316 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1317 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1318 | if (Literal.isImaginary) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1319 | Res = new (Context) ImaginaryLiteral(Res, |
| 1320 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1321 | |
| 1322 | return Owned(Res); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1325 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1326 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1327 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1328 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1329 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1333 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1334 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1335 | SourceLocation OpLoc, |
| 1336 | const SourceRange &ExprRange, |
| 1337 | bool isSizeof) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1338 | if (exprType->isDependentType()) |
| 1339 | return false; |
| 1340 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1341 | // C99 6.5.3.4p1: |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1342 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1343 | // alignof(function) is allowed as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1344 | if (isSizeof) |
| 1345 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1346 | return false; |
| 1347 | } |
| 1348 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1349 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1350 | if (exprType->isVoidType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1351 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1352 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 159fe08 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1353 | return false; |
| 1354 | } |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1355 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1356 | if (RequireCompleteType(OpLoc, exprType, |
| 1357 | isSizeof ? diag::err_sizeof_incomplete_type : |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1358 | PDiag(diag::err_alignof_incomplete_type) |
| 1359 | << ExprRange)) |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1360 | return true; |
| 1361 | |
| 1362 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | bf2b095 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1363 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1364 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | f3ce857 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1365 | << exprType << isSizeof << ExprRange; |
| 1366 | return true; |
Chris Lattner | e1127c4 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1369 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1372 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1373 | const SourceRange &ExprRange) { |
| 1374 | E = E->IgnoreParens(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1375 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1376 | // alignof decl is always ok. |
| 1377 | if (isa<DeclRefExpr>(E)) |
| 1378 | return false; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1379 | |
| 1380 | // Cannot know anything else if the expression is dependent. |
| 1381 | if (E->isTypeDependent()) |
| 1382 | return false; |
| 1383 | |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1384 | if (E->getBitField()) { |
| 1385 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1386 | return true; |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1387 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1388 | |
| 1389 | // Alignment of a field access is always okay, so long as it isn't a |
| 1390 | // bit-field. |
| 1391 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 6eeaa78 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1392 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1393 | return false; |
| 1394 | |
Chris Lattner | 8d9f796 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1395 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1396 | } |
| 1397 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1398 | /// \brief Build a sizeof or alignof expression given a type operand. |
| 1399 | Action::OwningExprResult |
| 1400 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
| 1401 | bool isSizeOf, SourceRange R) { |
| 1402 | if (T.isNull()) |
| 1403 | return ExprError(); |
| 1404 | |
| 1405 | if (!T->isDependentType() && |
| 1406 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1407 | return ExprError(); |
| 1408 | |
| 1409 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1410 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1411 | Context.getSizeType(), OpLoc, |
| 1412 | R.getEnd())); |
| 1413 | } |
| 1414 | |
| 1415 | /// \brief Build a sizeof or alignof expression given an expression |
| 1416 | /// operand. |
| 1417 | Action::OwningExprResult |
| 1418 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1419 | bool isSizeOf, SourceRange R) { |
| 1420 | // Verify that the operand is valid. |
| 1421 | bool isInvalid = false; |
| 1422 | if (E->isTypeDependent()) { |
| 1423 | // Delay type-checking for type-dependent expressions. |
| 1424 | } else if (!isSizeOf) { |
| 1425 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1426 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1427 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1428 | isInvalid = true; |
| 1429 | } else { |
| 1430 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1431 | } |
| 1432 | |
| 1433 | if (isInvalid) |
| 1434 | return ExprError(); |
| 1435 | |
| 1436 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1437 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1438 | Context.getSizeType(), OpLoc, |
| 1439 | R.getEnd())); |
| 1440 | } |
| 1441 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1442 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1443 | /// the same for @c alignof and @c __alignof |
| 1444 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1445 | Action::OwningExprResult |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1446 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1447 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1448 | // If error parsing type, ignore. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1449 | if (TyOrEx == 0) return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1450 | |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1451 | if (isType) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1452 | // FIXME: Preserve type source info. |
| 1453 | QualType ArgTy = GetTypeFromParser(TyOrEx); |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1454 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
| 1455 | } |
Sebastian Redl | 0cb7c87 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1456 | |
Douglas Gregor | 396f114 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1457 | // Get the end location. |
| 1458 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1459 | Action::OwningExprResult Result |
| 1460 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1461 | |
| 1462 | if (Result.isInvalid()) |
| 1463 | DeleteExpr(ArgEx); |
| 1464 | |
| 1465 | return move(Result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1468 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1469 | if (V->isTypeDependent()) |
| 1470 | return Context.DependentTy; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1472 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1473 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1474 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Otherwise they pass through real integer and floating point types here. |
| 1477 | if (V->getType()->isArithmeticType()) |
| 1478 | return V->getType(); |
| 1479 | |
| 1480 | // Reject anything else. |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1481 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1482 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1483 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1487 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1488 | Action::OwningExprResult |
| 1489 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1490 | tok::TokenKind Kind, ExprArg Input) { |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1491 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1492 | Input = MaybeConvertParenListExprToParenExpr(S, move(Input)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1493 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1495 | UnaryOperator::Opcode Opc; |
| 1496 | switch (Kind) { |
| 1497 | default: assert(0 && "Unknown unary op!"); |
| 1498 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1499 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1500 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1502 | if (getLangOptions().CPlusPlus && |
| 1503 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1504 | // Which overloaded operator? |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1505 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1506 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1507 | |
| 1508 | // C++ [over.inc]p1: |
| 1509 | // |
| 1510 | // [...] If the function is a member function with one |
| 1511 | // parameter (which shall be of type int) or a non-member |
| 1512 | // function with two parameters (the second of which shall be |
| 1513 | // of type int), it defines the postfix increment operator ++ |
| 1514 | // for objects of that type. When the postfix increment is |
| 1515 | // called as a result of using the ++ operator, the int |
| 1516 | // argument will have value zero. |
| 1517 | Expr *Args[2] = { |
| 1518 | Arg, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1519 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
| 1520 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1521 | }; |
| 1522 | |
| 1523 | // Build the candidate set for overloading |
| 1524 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1525 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Perform overload resolution. |
| 1528 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1529 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1530 | case OR_Success: { |
| 1531 | // We found a built-in operator or an overloaded operator. |
| 1532 | FunctionDecl *FnDecl = Best->Function; |
| 1533 | |
| 1534 | if (FnDecl) { |
| 1535 | // We matched an overloaded operator. Build a call to that |
| 1536 | // operator. |
| 1537 | |
| 1538 | // Convert the arguments. |
| 1539 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1540 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1541 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1542 | } else { |
| 1543 | // Convert the arguments. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1544 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1545 | FnDecl->getParamDecl(0)->getType(), |
| 1546 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1547 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1551 | QualType ResultTy |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1552 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1553 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1555 | // Build the actual expression node. |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1556 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 6d8e573 | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1557 | SourceLocation()); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1558 | UsualUnaryConversions(FnExpr); |
| 1559 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1560 | Input.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1561 | Args[0] = Arg; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1562 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
| 1563 | Args, 2, ResultTy, |
| 1564 | OpLoc)); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1565 | } else { |
| 1566 | // We matched a built-in operator. Convert the arguments, then |
| 1567 | // break out so that we will build the appropriate built-in |
| 1568 | // operator node. |
| 1569 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1570 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1571 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1572 | |
| 1573 | break; |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1574 | } |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | case OR_No_Viable_Function: |
| 1578 | // No viable function; fall through to handling this as a |
| 1579 | // built-in operator, which will produce an error message for us. |
| 1580 | break; |
| 1581 | |
| 1582 | case OR_Ambiguous: |
| 1583 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1584 | << UnaryOperator::getOpcodeStr(Opc) |
| 1585 | << Arg->getSourceRange(); |
| 1586 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1587 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1588 | |
| 1589 | case OR_Deleted: |
| 1590 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1591 | << Best->Function->isDeleted() |
| 1592 | << UnaryOperator::getOpcodeStr(Opc) |
| 1593 | << Arg->getSourceRange(); |
| 1594 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1595 | return ExprError(); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | // Either we found no viable overloaded operator or we matched a |
| 1599 | // built-in operator. In either case, fall through to trying to |
| 1600 | // build a built-in operation. |
| 1601 | } |
| 1602 | |
Eli Friedman | 94d3095 | 2009-07-22 23:24:42 +0000 | [diff] [blame] | 1603 | Input.release(); |
| 1604 | Input = Arg; |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 1605 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(Input)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1608 | Action::OwningExprResult |
| 1609 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1610 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1611 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1612 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1613 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1614 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1615 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1616 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1617 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | de72f3e | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1618 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1619 | Base.release(); |
| 1620 | Idx.release(); |
| 1621 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1622 | Context.DependentTy, RLoc)); |
| 1623 | } |
| 1624 | |
| 1625 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1626 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | e658bf5 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1627 | LHSExp->getType()->isEnumeralType() || |
| 1628 | RHSExp->getType()->isRecordType() || |
| 1629 | RHSExp->getType()->isEnumeralType())) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1630 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
| 1631 | // to the candidate set. |
| 1632 | OverloadCandidateSet CandidateSet; |
| 1633 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1634 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1635 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1636 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1637 | // Perform overload resolution. |
| 1638 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1639 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1640 | case OR_Success: { |
| 1641 | // We found a built-in operator or an overloaded operator. |
| 1642 | FunctionDecl *FnDecl = Best->Function; |
| 1643 | |
| 1644 | if (FnDecl) { |
| 1645 | // We matched an overloaded operator. Build a call to that |
| 1646 | // operator. |
| 1647 | |
| 1648 | // Convert the arguments. |
| 1649 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1650 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
| 1651 | PerformCopyInitialization(RHSExp, |
| 1652 | FnDecl->getParamDecl(0)->getType(), |
| 1653 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1654 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1655 | } else { |
| 1656 | // Convert the arguments. |
| 1657 | if (PerformCopyInitialization(LHSExp, |
| 1658 | FnDecl->getParamDecl(0)->getType(), |
| 1659 | "passing") || |
| 1660 | PerformCopyInitialization(RHSExp, |
| 1661 | FnDecl->getParamDecl(1)->getType(), |
| 1662 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1663 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | // Determine the result type |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1667 | QualType ResultTy |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1668 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1669 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1671 | // Build the actual expression node. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1672 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1673 | SourceLocation()); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1674 | UsualUnaryConversions(FnExpr); |
| 1675 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1676 | Base.release(); |
| 1677 | Idx.release(); |
Douglas Gregor | b2f81ac | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1678 | Args[0] = LHSExp; |
| 1679 | Args[1] = RHSExp; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1680 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
| 1681 | FnExpr, Args, 2, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1682 | ResultTy, LLoc)); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1683 | } else { |
| 1684 | // We matched a built-in operator. Convert the arguments, then |
| 1685 | // break out so that we will build the appropriate built-in |
| 1686 | // operator node. |
| 1687 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1688 | "passing") || |
| 1689 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1690 | "passing")) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1691 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1692 | |
| 1693 | break; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | case OR_No_Viable_Function: |
| 1698 | // No viable function; fall through to handling this as a |
| 1699 | // built-in operator, which will produce an error message for us. |
| 1700 | break; |
| 1701 | |
| 1702 | case OR_Ambiguous: |
| 1703 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1704 | << "[]" |
| 1705 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1706 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1707 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1708 | |
| 1709 | case OR_Deleted: |
| 1710 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1711 | << Best->Function->isDeleted() |
| 1712 | << "[]" |
| 1713 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1714 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1715 | return ExprError(); |
Douglas Gregor | 80723c5 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | // Either we found no viable overloaded operator or we matched a |
| 1719 | // built-in operator. In either case, fall through to trying to |
| 1720 | // build a built-in operation. |
| 1721 | } |
| 1722 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1723 | // Perform default conversions. |
| 1724 | DefaultFunctionArrayConversion(LHSExp); |
| 1725 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1726 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1727 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 1728 | |
| 1729 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1730 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1731 | // in the subscript position. As a result, we need to derive the array base |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1732 | // and index from the expression types. |
| 1733 | Expr *BaseExpr, *IndexExpr; |
| 1734 | QualType ResultType; |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1735 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1736 | BaseExpr = LHSExp; |
| 1737 | IndexExpr = RHSExp; |
| 1738 | ResultType = Context.DependentTy; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1739 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1740 | BaseExpr = LHSExp; |
| 1741 | IndexExpr = RHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1742 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1743 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1744 | // Handle the uncommon case of "123[Ptr]". |
| 1745 | BaseExpr = RHSExp; |
| 1746 | IndexExpr = LHSExp; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1747 | ResultType = PTy->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1748 | } else if (const ObjCObjectPointerType *PTy = |
| 1749 | LHSTy->getAsObjCObjectPointerType()) { |
| 1750 | BaseExpr = LHSExp; |
| 1751 | IndexExpr = RHSExp; |
| 1752 | ResultType = PTy->getPointeeType(); |
| 1753 | } else if (const ObjCObjectPointerType *PTy = |
| 1754 | RHSTy->getAsObjCObjectPointerType()) { |
| 1755 | // Handle the uncommon case of "123[Ptr]". |
| 1756 | BaseExpr = RHSExp; |
| 1757 | IndexExpr = LHSExp; |
| 1758 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1759 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1760 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1761 | IndexExpr = RHSExp; |
Nate Begeman | 5738547 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1762 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1763 | // FIXME: need to deal with const... |
| 1764 | ResultType = VTy->getElementType(); |
Eli Friedman | d461407 | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1765 | } else if (LHSTy->isArrayType()) { |
| 1766 | // If we see an array that wasn't promoted by |
| 1767 | // DefaultFunctionArrayConversion, it must be an array that |
| 1768 | // wasn't promoted because of the C90 rule that doesn't |
| 1769 | // allow promoting non-lvalue arrays. Warn, then |
| 1770 | // force the promotion here. |
| 1771 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1772 | LHSExp->getSourceRange(); |
| 1773 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy)); |
| 1774 | LHSTy = LHSExp->getType(); |
| 1775 | |
| 1776 | BaseExpr = LHSExp; |
| 1777 | IndexExpr = RHSExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1778 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | d461407 | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1779 | } else if (RHSTy->isArrayType()) { |
| 1780 | // Same as previous, except for 123[f().a] case |
| 1781 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1782 | RHSExp->getSourceRange(); |
| 1783 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy)); |
| 1784 | RHSTy = RHSExp->getType(); |
| 1785 | |
| 1786 | BaseExpr = RHSExp; |
| 1787 | IndexExpr = LHSExp; |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1788 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1789 | } else { |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1790 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1791 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1792 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1793 | // C99 6.5.2.1p1 |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1794 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1795 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1796 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1797 | << IndexExpr->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1798 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1799 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| 1800 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1801 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| 1802 | // incomplete types are not object types. |
| 1803 | if (ResultType->isFunctionType()) { |
| 1804 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1805 | << ResultType << BaseExpr->getSourceRange(); |
| 1806 | return ExprError(); |
| 1807 | } |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1809 | if (!ResultType->isDependentType() && |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1810 | RequireCompleteType(LLoc, ResultType, |
| 1811 | PDiag(diag::err_subscript_incomplete_type) |
| 1812 | << BaseExpr->getSourceRange())) |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1813 | return ExprError(); |
Chris Lattner | 95933c1 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1814 | |
| 1815 | // Diagnose bad cases where we step over interface counts. |
| 1816 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1817 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1818 | << ResultType << BaseExpr->getSourceRange(); |
| 1819 | return ExprError(); |
| 1820 | } |
| 1821 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1822 | Base.release(); |
| 1823 | Idx.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1824 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1825 | ResultType, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1828 | QualType Sema:: |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1829 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1830 | const IdentifierInfo *CompName, |
| 1831 | SourceLocation CompLoc) { |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1832 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1833 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1834 | // The vector accessor can't exceed the number of elements. |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1835 | const char *compStr = CompName->getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1836 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1837 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1838 | // special names that indicate a subset of exactly half the elements are |
| 1839 | // to be selected. |
| 1840 | bool HalvingSwizzle = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1841 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1842 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1843 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | e2ed6f7 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 1844 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1845 | |
| 1846 | // Check that we've found one of the special components, or that the component |
| 1847 | // names must come from the same set. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1848 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1849 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1850 | HalvingSwizzle = true; |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1851 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1852 | do |
| 1853 | compStr++; |
| 1854 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1855 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1856 | do |
| 1857 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1858 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1859 | } |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1860 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1861 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1862 | // We didn't get to the end of the string. This means the component names |
| 1863 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1864 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1865 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1866 | return QualType(); |
| 1867 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1868 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1869 | // Ensure no component accessor exceeds the width of the vector type it |
| 1870 | // operates on. |
| 1871 | if (!HalvingSwizzle) { |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1872 | compStr = CompName->getName(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1873 | |
| 1874 | if (HexSwizzle) |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1875 | compStr++; |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1876 | |
| 1877 | while (*compStr) { |
| 1878 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1879 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1880 | << baseType << SourceRange(CompLoc); |
| 1881 | return QualType(); |
| 1882 | } |
| 1883 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1884 | } |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1885 | |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1886 | // If this is a halving swizzle, verify that the base type has an even |
| 1887 | // number of elements. |
| 1888 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1889 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1890 | << baseType << SourceRange(CompLoc); |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1891 | return QualType(); |
| 1892 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1893 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1894 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1895 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1896 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1897 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | c8e51f8 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1898 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1899 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1900 | : CompName->getLength(); |
Nate Begeman | 1486b50 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1901 | if (HexSwizzle) |
| 1902 | CompSize--; |
| 1903 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1904 | if (CompSize == 1) |
| 1905 | return vecType->getElementType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1906 | |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1907 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1908 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | af6ed50 | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1909 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1910 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1911 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1912 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1913 | } |
| 1914 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1917 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1918 | IdentifierInfo *Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1919 | const Selector &Sel, |
| 1920 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1921 | |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1922 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1923 | return PD; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1924 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1925 | return OMD; |
| 1926 | |
| 1927 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1928 | E = PDecl->protocol_end(); I != E; ++I) { |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1929 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
| 1930 | Context)) |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1931 | return D; |
| 1932 | } |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1936 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1937 | IdentifierInfo *Member, |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1938 | const Selector &Sel, |
| 1939 | ASTContext &Context) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1940 | // Check protocols on qualified interfaces. |
| 1941 | Decl *GDecl = 0; |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1942 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1943 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1944 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1945 | GDecl = PD; |
| 1946 | break; |
| 1947 | } |
| 1948 | // Also must look for a getter name which uses property syntax. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1949 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1950 | GDecl = OMD; |
| 1951 | break; |
| 1952 | } |
| 1953 | } |
| 1954 | if (!GDecl) { |
Steve Naroff | c75c1a8 | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1955 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1956 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1957 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1958 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 854f400 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1959 | if (GDecl) |
| 1960 | return GDecl; |
| 1961 | } |
| 1962 | } |
| 1963 | return GDecl; |
| 1964 | } |
Chris Lattner | 2cb744b | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1965 | |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1966 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 1967 | /// all base class implementations. |
| 1968 | /// |
| 1969 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 1970 | const ObjCInterfaceDecl *IFace, |
| 1971 | const Selector &Sel) { |
| 1972 | ObjCMethodDecl *Method = 0; |
Argiris Kirtzidis | b1c4ee5 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1973 | if (ObjCImplementationDecl *ImpDecl = IFace->getImplementation()) |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1974 | Method = ImpDecl->getInstanceMethod(Sel); |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1975 | |
| 1976 | if (!Method && IFace->getSuperClass()) |
| 1977 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 1978 | return Method; |
| 1979 | } |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1980 | |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1981 | Action::OwningExprResult |
| 1982 | Sema::BuildMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1983 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1984 | DeclarationName MemberName, |
Douglas Gregor | d33e328 | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 1985 | bool HasExplicitTemplateArgs, |
| 1986 | SourceLocation LAngleLoc, |
| 1987 | const TemplateArgument *ExplicitTemplateArgs, |
| 1988 | unsigned NumExplicitTemplateArgs, |
| 1989 | SourceLocation RAngleLoc, |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1990 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS, |
| 1991 | NamedDecl *FirstQualifierInScope) { |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1992 | if (SS && SS->isInvalid()) |
| 1993 | return ExprError(); |
| 1994 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1995 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1996 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1997 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1998 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1999 | assert(BaseExpr && "no base expression"); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2000 | |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2001 | // Perform default conversions. |
| 2002 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2003 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2004 | QualType BaseType = BaseExpr->getType(); |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2005 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2006 | // use that. |
| 2007 | if (BaseType->isObjCIdType()) { |
| 2008 | // We have an 'id' type. Rather than fall through, we check if this |
| 2009 | // is a reference to 'isa'. |
| 2010 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2011 | BaseType = Context.ObjCIdRedefinitionType; |
| 2012 | ImpCastExprToType(BaseExpr, BaseType); |
| 2013 | } |
| 2014 | } else if (BaseType->isObjCClassType() && |
Douglas Gregor | cfa0a63 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 2015 | BaseType != Context.ObjCClassRedefinitionType) { |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2016 | BaseType = Context.ObjCClassRedefinitionType; |
| 2017 | ImpCastExprToType(BaseExpr, BaseType); |
| 2018 | } |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2019 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2021 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2022 | // must have pointer type, and the accessed type is the pointee. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2023 | if (OpKind == tok::arrow) { |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2024 | if (BaseType->isDependentType()) { |
| 2025 | NestedNameSpecifier *Qualifier = 0; |
| 2026 | if (SS) { |
| 2027 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 2028 | if (!FirstQualifierInScope) |
| 2029 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 2030 | } |
| 2031 | |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2032 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2033 | BaseExpr, true, |
Douglas Gregor | 0f927cf | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2034 | OpLoc, |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2035 | Qualifier, |
Douglas Gregor | 0f927cf | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2036 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2037 | FirstQualifierInScope, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2038 | MemberName, |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2039 | MemberLoc)); |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2040 | } |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2041 | else if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2042 | BaseType = PT->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2043 | else if (BaseType->isObjCObjectPointerType()) |
| 2044 | ; |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2045 | else |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2046 | return ExprError(Diag(MemberLoc, |
| 2047 | diag::err_typecheck_member_reference_arrow) |
| 2048 | << BaseType << BaseExpr->getSourceRange()); |
Anders Carlsson | 72d3c66 | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2049 | } else { |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2050 | if (BaseType->isDependentType()) { |
| 2051 | // Require that the base type isn't a pointer type |
| 2052 | // (so we'll report an error for) |
| 2053 | // T* t; |
| 2054 | // t.f; |
| 2055 | // |
| 2056 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2057 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2058 | // allows this, while still reporting an error if T is a struct pointer. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2059 | const PointerType *PT = BaseType->getAs<PointerType>(); |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2060 | |
| 2061 | if (!PT || (getLangOptions().ObjC1 && |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2062 | !PT->getPointeeType()->isRecordType())) { |
| 2063 | NestedNameSpecifier *Qualifier = 0; |
| 2064 | if (SS) { |
| 2065 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 2066 | if (!FirstQualifierInScope) |
| 2067 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 2068 | } |
| 2069 | |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2070 | return Owned(new (Context) CXXUnresolvedMemberExpr(Context, |
| 2071 | BaseExpr, false, |
| 2072 | OpLoc, |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2073 | Qualifier, |
Douglas Gregor | 0f927cf | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2074 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2075 | FirstQualifierInScope, |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2076 | MemberName, |
Douglas Gregor | 93b8b0f | 2009-05-22 21:13:27 +0000 | [diff] [blame] | 2077 | MemberLoc)); |
Douglas Gregor | bc2fb7f | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2078 | } |
Anders Carlsson | 4082ecd | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2079 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2080 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2081 | |
Chris Lattner | b2b9da7 | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2082 | // Handle field access to simple records. This also handles access to fields |
| 2083 | // of the ObjC 'id' struct. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2084 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2085 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2086 | if (RequireCompleteType(OpLoc, BaseType, |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2087 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2088 | << BaseExpr->getSourceRange())) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2089 | return ExprError(); |
| 2090 | |
Douglas Gregor | da61ad2 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2091 | DeclContext *DC = RDecl; |
| 2092 | if (SS && SS->isSet()) { |
| 2093 | // If the member name was a qualified-id, look into the |
| 2094 | // nested-name-specifier. |
| 2095 | DC = computeDeclContext(*SS, false); |
| 2096 | |
| 2097 | // FIXME: If DC is not computable, we should build a |
| 2098 | // CXXUnresolvedMemberExpr. |
| 2099 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2100 | } |
| 2101 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2102 | // The record definition is complete, now make sure the member is valid. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2103 | LookupResult Result |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2104 | = LookupQualifiedName(DC, MemberName, LookupMemberName, false); |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 29dfa2f | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2106 | if (!Result) |
Anders Carlsson | 4355a39 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 2107 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member_deprecated) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2108 | << MemberName << BaseExpr->getSourceRange()); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2109 | if (Result.isAmbiguous()) { |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2110 | DiagnoseAmbiguousLookup(Result, MemberName, MemberLoc, |
| 2111 | BaseExpr->getSourceRange()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2112 | return ExprError(); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
Douglas Gregor | 0f927cf | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2115 | if (SS && SS->isSet()) { |
| 2116 | QualType BaseTypeCanon |
| 2117 | = Context.getCanonicalType(BaseType).getUnqualifiedType(); |
| 2118 | QualType MemberTypeCanon |
| 2119 | = Context.getCanonicalType( |
| 2120 | Context.getTypeDeclType( |
| 2121 | dyn_cast<TypeDecl>(Result.getAsDecl()->getDeclContext()))); |
| 2122 | |
| 2123 | if (BaseTypeCanon != MemberTypeCanon && |
| 2124 | !IsDerivedFrom(BaseTypeCanon, MemberTypeCanon)) |
| 2125 | return ExprError(Diag(SS->getBeginLoc(), |
| 2126 | diag::err_not_direct_base_or_virtual) |
| 2127 | << MemberTypeCanon << BaseTypeCanon); |
| 2128 | } |
| 2129 | |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2130 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2131 | |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2132 | // If the decl being referenced had an error, return an error for this |
| 2133 | // sub-expr without emitting another error, in order to avoid cascading |
| 2134 | // error cases. |
| 2135 | if (MemberDecl->isInvalidDecl()) |
| 2136 | return ExprError(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2138 | // Check the use of this field |
| 2139 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 2140 | return ExprError(); |
Chris Lattner | fd57ecc | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2141 | |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2142 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2143 | // We may have found a field within an anonymous union or struct |
| 2144 | // (C++ [class.union]). |
| 2145 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd883f7 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2146 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2147 | BaseExpr, OpLoc); |
Douglas Gregor | 723d333 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2149 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2150 | QualType MemberType = FD->getType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2151 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2152 | MemberType = Ref->getPointeeType(); |
| 2153 | else { |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2154 | unsigned BaseAddrSpace = BaseType.getAddressSpace(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2155 | unsigned combinedQualifiers = |
| 2156 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | ddfd9d5 | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2157 | if (FD->isMutable()) |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2158 | combinedQualifiers &= ~QualType::Const; |
| 2159 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
Mon P Wang | 04d89cb | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2160 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 2161 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2162 | } |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2164 | MarkDeclarationReferenced(MemberLoc, FD); |
Fariborz Jahanian | 843336e | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2165 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2166 | return ExprError(); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2167 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2168 | FD, MemberLoc, MemberType)); |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2171 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2172 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2173 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2174 | Var, MemberLoc, |
| 2175 | Var->getType().getNonReferenceType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2176 | } |
| 2177 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2178 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2179 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2180 | MemberFn, MemberLoc, |
| 2181 | MemberFn->getType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2182 | } |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2183 | if (FunctionTemplateDecl *FunTmpl |
| 2184 | = dyn_cast<FunctionTemplateDecl>(MemberDecl)) { |
| 2185 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | d33e328 | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2186 | |
| 2187 | if (HasExplicitTemplateArgs) |
| 2188 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2189 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
| 2190 | SS? SS->getRange() : SourceRange(), |
| 2191 | FunTmpl, MemberLoc, true, |
| 2192 | LAngleLoc, ExplicitTemplateArgs, |
| 2193 | NumExplicitTemplateArgs, RAngleLoc, |
| 2194 | Context.OverloadTy)); |
| 2195 | |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2196 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2197 | FunTmpl, MemberLoc, |
| 2198 | Context.OverloadTy)); |
Douglas Gregor | 4fdcdda | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2199 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2200 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | d33e328 | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2201 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) { |
| 2202 | if (HasExplicitTemplateArgs) |
| 2203 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2204 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
| 2205 | SS? SS->getRange() : SourceRange(), |
| 2206 | Ovl, MemberLoc, true, |
| 2207 | LAngleLoc, ExplicitTemplateArgs, |
| 2208 | NumExplicitTemplateArgs, RAngleLoc, |
| 2209 | Context.OverloadTy)); |
| 2210 | |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2211 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2212 | Ovl, MemberLoc, Context.OverloadTy)); |
Douglas Gregor | d33e328 | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2213 | } |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2214 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2215 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | e399ad4 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2216 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2217 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2218 | } |
Chris Lattner | 84ad833 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2219 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2220 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2221 | << MemberName << int(OpKind == tok::arrow)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2222 | |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2223 | // We found a declaration kind that we didn't expect. This is a |
| 2224 | // generic error message that tells the user that she can't refer |
| 2225 | // to this member with '.' or '->'. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2226 | return ExprError(Diag(MemberLoc, |
| 2227 | diag::err_typecheck_member_reference_unknown) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2228 | << MemberName << int(OpKind == tok::arrow)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2229 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2230 | |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2231 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2232 | // into a record type was handled above, any destructor we see here is a |
| 2233 | // pseudo-destructor. |
| 2234 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2235 | // C++ [expr.pseudo]p2: |
| 2236 | // The left hand side of the dot operator shall be of scalar type. The |
| 2237 | // left hand side of the arrow operator shall be of pointer to scalar |
| 2238 | // type. |
| 2239 | if (!BaseType->isScalarType()) |
| 2240 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2241 | << BaseType << BaseExpr->getSourceRange()); |
| 2242 | |
| 2243 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2244 | // same as the object type. |
| 2245 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2246 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2247 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2248 | << BaseType << MemberName.getCXXNameType() |
| 2249 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
| 2250 | |
| 2251 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
| 2252 | // the form |
| 2253 | // |
| 2254 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2255 | // |
| 2256 | // shall designate the same scalar type. |
| 2257 | // |
| 2258 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2259 | // isn't checked here. |
| 2260 | |
| 2261 | // FIXME: We've lost the precise spelling of the type by going through |
| 2262 | // DeclarationName. Can we do better? |
| 2263 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
| 2264 | OpKind == tok::arrow, |
| 2265 | OpLoc, |
| 2266 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
| 2267 | SS? SS->getRange() : SourceRange(), |
| 2268 | MemberName.getCXXNameType(), |
| 2269 | MemberLoc)); |
| 2270 | } |
| 2271 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2272 | // Handle properties on ObjC 'Class' types. |
Steve Naroff | 7982a64 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 2273 | if (OpKind == tok::period && BaseType->isObjCClassType()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2274 | // Also must look for a getter name which uses property syntax. |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2275 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2276 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2277 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2278 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2279 | ObjCMethodDecl *Getter; |
| 2280 | // FIXME: need to also look locally in the implementation. |
| 2281 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2282 | // Check the use of this method. |
| 2283 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2284 | return ExprError(); |
| 2285 | } |
| 2286 | // If we found a getter then this may be a valid dot-reference, we |
| 2287 | // will look for the matching setter, in case it is needed. |
| 2288 | Selector SetterSel = |
| 2289 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2290 | PP.getSelectorTable(), Member); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2291 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2292 | if (!Setter) { |
| 2293 | // If this reference is in an @implementation, also check for 'private' |
| 2294 | // methods. |
| 2295 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
| 2296 | } |
| 2297 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2298 | if (!Setter) |
| 2299 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2300 | |
| 2301 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2302 | return ExprError(); |
| 2303 | |
| 2304 | if (Getter || Setter) { |
| 2305 | QualType PType; |
| 2306 | |
| 2307 | if (Getter) |
| 2308 | PType = Getter->getResultType(); |
Fariborz Jahanian | 1c4da45 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2309 | else |
| 2310 | // Get the expression type from Setter's incoming parameter. |
| 2311 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2312 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 128cdc5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2313 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2314 | Setter, MemberLoc, BaseExpr)); |
| 2315 | } |
| 2316 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2317 | << MemberName << BaseType); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2318 | } |
| 2319 | } |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2320 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2321 | // (*Obj).ivar. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2322 | if ((OpKind == tok::arrow && BaseType->isObjCObjectPointerType()) || |
| 2323 | (OpKind == tok::period && BaseType->isObjCInterfaceType())) { |
| 2324 | const ObjCObjectPointerType *OPT = BaseType->getAsObjCObjectPointerType(); |
| 2325 | const ObjCInterfaceType *IFaceT = |
| 2326 | OPT ? OPT->getInterfaceType() : BaseType->getAsObjCInterfaceType(); |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2327 | if (IFaceT) { |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2328 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2329 | |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2330 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2331 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2332 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2333 | |
| 2334 | if (IV) { |
| 2335 | // If the decl being referenced had an error, return an error for this |
| 2336 | // sub-expr without emitting another error, in order to avoid cascading |
| 2337 | // error cases. |
| 2338 | if (IV->isInvalidDecl()) |
| 2339 | return ExprError(); |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2340 | |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2341 | // Check whether we can reference this field. |
| 2342 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2343 | return ExprError(); |
| 2344 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2345 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2346 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2347 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2348 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2349 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2350 | // Case of a c-function declared inside an objc implementation. |
| 2351 | // FIXME: For a c-style function nested inside an objc implementation |
| 2352 | // class, there is no implementation context available, so we pass |
| 2353 | // down the context as argument to this routine. Ideally, this context |
| 2354 | // need be passed down in the AST node and somehow calculated from the |
| 2355 | // AST for a function decl. |
| 2356 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
| 2357 | if (ObjCImplementationDecl *IMPD = |
| 2358 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2359 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2360 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2361 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2362 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2363 | } |
| 2364 | |
| 2365 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2366 | if (ClassDeclared != IDecl || |
| 2367 | ClassOfMethodDecl != ClassDeclared) |
| 2368 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 2369 | << IV->getDeclName(); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2370 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2371 | // @protected |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2372 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 2373 | << IV->getDeclName(); |
Steve Naroff | f960657 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2374 | } |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2375 | |
| 2376 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2377 | MemberLoc, BaseExpr, |
| 2378 | OpKind == tok::arrow)); |
Fariborz Jahanian | dd71e75 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2379 | } |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2380 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2381 | << IDecl->getDeclName() << MemberName |
Steve Naroff | 4e74396 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2382 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | 0977239 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2383 | } |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2384 | } |
Steve Naroff | 7bffd37 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2385 | // Handle properties on 'id' and qualified "id". |
| 2386 | if (OpKind == tok::period && (BaseType->isObjCIdType() || |
| 2387 | BaseType->isObjCQualifiedIdType())) { |
| 2388 | const ObjCObjectPointerType *QIdTy = BaseType->getAsObjCObjectPointerType(); |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2389 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Steve Naroff | 7bffd37 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2390 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2391 | // Check protocols on qualified interfaces. |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2392 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2393 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2394 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2395 | // Check the use of this declaration |
| 2396 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2397 | return ExprError(); |
| 2398 | |
| 2399 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2400 | MemberLoc, BaseExpr)); |
| 2401 | } |
| 2402 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2403 | // Check the use of this method. |
| 2404 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2405 | return ExprError(); |
| 2406 | |
| 2407 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
| 2408 | OMD->getResultType(), |
| 2409 | OMD, OpLoc, MemberLoc, |
| 2410 | NULL, 0)); |
| 2411 | } |
| 2412 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2413 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2414 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2415 | << MemberName << BaseType); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2416 | } |
Chris Lattner | e9d7161 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2417 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2418 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2419 | const ObjCObjectPointerType *OPT; |
| 2420 | if (OpKind == tok::period && |
| 2421 | (OPT = BaseType->getAsObjCInterfacePointerType())) { |
| 2422 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2423 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2424 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2425 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2426 | // Search for a declared property first. |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2427 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2428 | // Check whether we can reference this property. |
| 2429 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2430 | return ExprError(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2431 | QualType ResTy = PD->getType(); |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2432 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2433 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | 80ccaa9 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2434 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2435 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | a996bb0 | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2436 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2437 | MemberLoc, BaseExpr)); |
| 2438 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2439 | // Check protocols on qualified interfaces. |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2440 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2441 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2442 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2443 | // Check whether we can reference this property. |
| 2444 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2445 | return ExprError(); |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2446 | |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2447 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 51f6fb3 | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2448 | MemberLoc, BaseExpr)); |
| 2449 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2450 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2451 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2452 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2453 | // Check whether we can reference this property. |
| 2454 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2455 | return ExprError(); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2456 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2457 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2458 | MemberLoc, BaseExpr)); |
| 2459 | } |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2460 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2461 | // selector is implemented. |
| 2462 | |
| 2463 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2464 | // shared with the code in ActOnInstanceMessage. |
| 2465 | |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2466 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2467 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2468 | |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2469 | // If this reference is in an @implementation, check for 'private' methods. |
| 2470 | if (!Getter) |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2471 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2472 | |
Steve Naroff | 04151f3 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2473 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2474 | if (!Getter) |
| 2475 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 60e8b16 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2476 | if (Getter) { |
Douglas Gregor | aa57e86 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2477 | // Check if we can reference this property. |
| 2478 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2479 | return ExprError(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2480 | } |
| 2481 | // If we found a getter then this may be a valid dot-reference, we |
| 2482 | // will look for the matching setter, in case it is needed. |
| 2483 | Selector SetterSel = |
| 2484 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2485 | PP.getSelectorTable(), Member); |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2486 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2487 | if (!Setter) { |
| 2488 | // If this reference is in an @implementation, also check for 'private' |
| 2489 | // methods. |
Fariborz Jahanian | 0119fd2 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2490 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2491 | } |
| 2492 | // Look through local category implementations associated with the class. |
Argiris Kirtzidis | 2009686 | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2493 | if (!Setter) |
| 2494 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2495 | |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2496 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2497 | return ExprError(); |
| 2498 | |
| 2499 | if (Getter || Setter) { |
| 2500 | QualType PType; |
| 2501 | |
| 2502 | if (Getter) |
| 2503 | PType = Getter->getResultType(); |
Fariborz Jahanian | 1c4da45 | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2504 | else |
| 2505 | // Get the expression type from Setter's incoming parameter. |
| 2506 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2507 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 128cdc5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2508 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | dede0c9 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2509 | Setter, MemberLoc, BaseExpr)); |
| 2510 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2511 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2512 | << MemberName << BaseType); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2513 | } |
Steve Naroff | e3aa06f | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 2514 | |
Steve Naroff | 29d293b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2515 | // Handle the following exceptional case (*Obj).isa. |
| 2516 | if (OpKind == tok::period && |
| 2517 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2518 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | 29d293b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2519 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
| 2520 | Context.getObjCIdType())); |
| 2521 | |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2522 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 09020ee | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2523 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2524 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2525 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2526 | if (ret.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2527 | return ExprError(); |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2528 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2529 | MemberLoc)); |
Chris Lattner | a57cf47 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2530 | } |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2531 | |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2532 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2533 | << BaseType << BaseExpr->getSourceRange(); |
| 2534 | |
| 2535 | // If the user is trying to apply -> or . to a function or function |
| 2536 | // pointer, it's probably because they forgot parentheses to call |
| 2537 | // the function. Suggest the addition of those parentheses. |
| 2538 | if (BaseType == Context.OverloadTy || |
| 2539 | BaseType->isFunctionType() || |
| 2540 | (BaseType->isPointerType() && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2541 | BaseType->getAs<PointerType>()->isFunctionType())) { |
Douglas Gregor | 762da55 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2542 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2543 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2544 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2545 | } |
| 2546 | |
| 2547 | return ExprError(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Anders Carlsson | 9935ab9 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2550 | Action::OwningExprResult |
| 2551 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2552 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 2553 | IdentifierInfo &Member, |
| 2554 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS) { |
| 2555 | return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, MemberLoc, |
| 2556 | DeclarationName(&Member), ObjCImpDecl, SS); |
| 2557 | } |
| 2558 | |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2559 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 2560 | FunctionDecl *FD, |
| 2561 | ParmVarDecl *Param) { |
| 2562 | if (Param->hasUnparsedDefaultArg()) { |
| 2563 | Diag (CallLoc, |
| 2564 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2565 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
| 2566 | Diag(UnparsedDefaultArgLocs[Param], |
| 2567 | diag::note_default_argument_declared_here); |
| 2568 | } else { |
| 2569 | if (Param->hasUninstantiatedDefaultArg()) { |
| 2570 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 2571 | |
| 2572 | // Instantiate the expression. |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2573 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 21d18f2 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 2574 | |
| 2575 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 2576 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | 8dbd038 | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2577 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2578 | |
John McCall | 0ba26ee | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2579 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2580 | if (Result.isInvalid()) |
| 2581 | return ExprError(); |
| 2582 | |
| 2583 | if (SetParamDefaultArgument(Param, move(Result), |
| 2584 | /*FIXME:EqualLoc*/ |
| 2585 | UninstExpr->getSourceRange().getBegin())) |
| 2586 | return ExprError(); |
| 2587 | } |
| 2588 | |
| 2589 | Expr *DefaultExpr = Param->getDefaultArg(); |
| 2590 | |
| 2591 | // If the default expression creates temporaries, we need to |
| 2592 | // push them to the current stack of expression temporaries so they'll |
| 2593 | // be properly destroyed. |
| 2594 | if (CXXExprWithTemporaries *E |
| 2595 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
| 2596 | assert(!E->shouldDestroyTemporaries() && |
| 2597 | "Can't destroy temporaries in a default argument expr!"); |
| 2598 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2599 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | // We already type-checked the argument, so we know it works. |
| 2604 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 2605 | } |
| 2606 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2607 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2608 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2609 | /// function prototype Proto. Call is the call expression itself, and |
| 2610 | /// Fn is the function expression. For a C++ member function, this |
| 2611 | /// routine does not attempt to convert the object argument. Returns |
| 2612 | /// true if the call is ill-formed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2613 | bool |
| 2614 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2615 | FunctionDecl *FDecl, |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2616 | const FunctionProtoType *Proto, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2617 | Expr **Args, unsigned NumArgs, |
| 2618 | SourceLocation RParenLoc) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2619 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2620 | // assignment, to the types of the corresponding parameter, ... |
| 2621 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2622 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2623 | bool Invalid = false; |
| 2624 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2625 | // If too few arguments are available (and we don't have default |
| 2626 | // arguments for the remaining parameters), don't make the call. |
| 2627 | if (NumArgs < NumArgsInProto) { |
| 2628 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2629 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2630 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2631 | // Use default arguments for missing arguments |
| 2632 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2633 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | // If too many are passed and not variadic, error on the extras and drop |
| 2637 | // them. |
| 2638 | if (NumArgs > NumArgsInProto) { |
| 2639 | if (!Proto->isVariadic()) { |
| 2640 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2641 | diag::err_typecheck_call_too_many_args) |
| 2642 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2643 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2644 | Args[NumArgs-1]->getLocEnd()); |
| 2645 | // This deletes the extra arguments. |
Ted Kremenek | 0c97e04 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2646 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2647 | Invalid = true; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2648 | } |
| 2649 | NumArgsToCheck = NumArgsInProto; |
| 2650 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2652 | // Continue to check argument types (even if we have too few/many args). |
| 2653 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2654 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2655 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2656 | Expr *Arg; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2657 | if (i < NumArgs) { |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2658 | Arg = Args[i]; |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2659 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2660 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2661 | ProtoArgType, |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2662 | PDiag(diag::err_call_incomplete_argument) |
| 2663 | << Arg->getSourceRange())) |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2664 | return true; |
| 2665 | |
Douglas Gregor | 62ae25a | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2666 | // Pass the argument. |
| 2667 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2668 | return true; |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2669 | } else { |
Anders Carlsson | 60eb3be | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 2670 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Anders Carlsson | 0ab9db2 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2671 | |
| 2672 | OwningExprResult ArgExpr = |
| 2673 | BuildCXXDefaultArgExpr(Call->getSourceRange().getBegin(), |
| 2674 | FDecl, Param); |
| 2675 | if (ArgExpr.isInvalid()) |
| 2676 | return true; |
| 2677 | |
| 2678 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | a116e6e | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2681 | Call->setArg(i, Arg); |
| 2682 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2683 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2684 | // If this is a variadic call, handle args passed through "...". |
| 2685 | if (Proto->isVariadic()) { |
Anders Carlsson | 4b8e38c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2686 | VariadicCallType CallType = VariadicFunction; |
| 2687 | if (Fn->getType()->isBlockPointerType()) |
| 2688 | CallType = VariadicBlock; // Block |
| 2689 | else if (isa<MemberExpr>(Fn)) |
| 2690 | CallType = VariadicMethod; |
| 2691 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2692 | // Promote the arguments (C99 6.5.2.2p7). |
| 2693 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2694 | Expr *Arg = Args[i]; |
Chris Lattner | 81f00ed | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2695 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2696 | Call->setArg(i, Arg); |
| 2697 | } |
| 2698 | } |
| 2699 | |
Douglas Gregor | 4ac887b | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2700 | return Invalid; |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2703 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2704 | /// This provides the location of the left/right parens and a list of comma |
| 2705 | /// locations. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2706 | Action::OwningExprResult |
| 2707 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2708 | MultiExprArg args, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2709 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2710 | unsigned NumArgs = args.size(); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2711 | |
| 2712 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2713 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
| 2714 | |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2715 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2716 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2717 | assert(Fn && "no function call expression"); |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2718 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2719 | NamedDecl *NDecl = NULL; |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2720 | DeclarationName UnqualifiedName; |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2721 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2722 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 3e36851 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2723 | // If this is a pseudo-destructor expression, build the call immediately. |
| 2724 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 2725 | if (NumArgs > 0) { |
| 2726 | // Pseudo-destructor calls should not have any arguments. |
| 2727 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 2728 | << CodeModificationHint::CreateRemoval( |
| 2729 | SourceRange(Args[0]->getLocStart(), |
| 2730 | Args[NumArgs-1]->getLocEnd())); |
| 2731 | |
| 2732 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2733 | Args[I]->Destroy(Context); |
| 2734 | |
| 2735 | NumArgs = 0; |
| 2736 | } |
| 2737 | |
| 2738 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 2739 | RParenLoc)); |
| 2740 | } |
| 2741 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2742 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2743 | // in which case we won't do any semantic analysis now. |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2744 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2745 | // Fn. |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2746 | bool Dependent = false; |
| 2747 | if (Fn->isTypeDependent()) |
| 2748 | Dependent = true; |
| 2749 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2750 | Dependent = true; |
| 2751 | |
| 2752 | if (Dependent) |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2753 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2754 | Context.DependentTy, RParenLoc)); |
| 2755 | |
| 2756 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2757 | if (Fn->getType()->isRecordType()) |
| 2758 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2759 | CommaLocs, RParenLoc)); |
| 2760 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2761 | // Determine whether this is a call to a member function. |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2762 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) { |
| 2763 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
| 2764 | if (isa<OverloadedFunctionDecl>(MemDecl) || |
| 2765 | isa<CXXMethodDecl>(MemDecl) || |
| 2766 | (isa<FunctionTemplateDecl>(MemDecl) && |
| 2767 | isa<CXXMethodDecl>( |
| 2768 | cast<FunctionTemplateDecl>(MemDecl)->getTemplatedDecl()))) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2769 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2770 | CommaLocs, RParenLoc)); |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2771 | } |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2774 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2775 | // Also, in C++, keep track of whether we should perform argument-dependent |
| 2776 | // lookup and whether there were any explicitly-specified template arguments. |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2777 | Expr *FnExpr = Fn; |
| 2778 | bool ADL = true; |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2779 | bool HasExplicitTemplateArgs = 0; |
| 2780 | const TemplateArgument *ExplicitTemplateArgs = 0; |
| 2781 | unsigned NumExplicitTemplateArgs = 0; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2782 | while (true) { |
| 2783 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2784 | FnExpr = IcExpr->getSubExpr(); |
| 2785 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2786 | // Parentheses around a function disable ADL |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2787 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2788 | ADL = false; |
| 2789 | FnExpr = PExpr->getSubExpr(); |
| 2790 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2791 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2792 | == UnaryOperator::AddrOf) { |
| 2793 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2794 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2795 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2796 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2797 | NDecl = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2798 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2799 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2800 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2801 | UnqualifiedName = DepName->getName(); |
| 2802 | break; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2803 | } else if (TemplateIdRefExpr *TemplateIdRef |
| 2804 | = dyn_cast<TemplateIdRefExpr>(FnExpr)) { |
| 2805 | NDecl = TemplateIdRef->getTemplateName().getAsTemplateDecl(); |
Douglas Gregor | 6631cb4 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2806 | if (!NDecl) |
| 2807 | NDecl = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl(); |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2808 | HasExplicitTemplateArgs = true; |
| 2809 | ExplicitTemplateArgs = TemplateIdRef->getTemplateArgs(); |
| 2810 | NumExplicitTemplateArgs = TemplateIdRef->getNumTemplateArgs(); |
| 2811 | |
| 2812 | // C++ [temp.arg.explicit]p6: |
| 2813 | // [Note: For simple function names, argument dependent lookup (3.4.2) |
| 2814 | // applies even when the function name is not visible within the |
| 2815 | // scope of the call. This is because the call still has the syntactic |
| 2816 | // form of a function call (3.4.1). But when a function template with |
| 2817 | // explicit template arguments is used, the call does not have the |
| 2818 | // correct syntactic form unless there is a function template with |
| 2819 | // that name visible at the point of the call. If no such name is |
| 2820 | // visible, the call is not syntactically well-formed and |
| 2821 | // argument-dependent lookup does not apply. If some such name is |
| 2822 | // visible, argument dependent lookup applies and additional function |
| 2823 | // templates may be found in other namespaces. |
| 2824 | // |
| 2825 | // The summary of this paragraph is that, if we get to this point and the |
| 2826 | // template-id was not a qualified name, then argument-dependent lookup |
| 2827 | // is still possible. |
| 2828 | if (TemplateIdRef->getQualifier()) |
| 2829 | ADL = false; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2830 | break; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2831 | } else { |
Chris Lattner | e50fb0b | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2832 | // Any kind of name that does not refer to a declaration (or |
| 2833 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2834 | ADL = false; |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2835 | break; |
| 2836 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2837 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2839 | OverloadedFunctionDecl *Ovl = 0; |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2840 | FunctionTemplateDecl *FunctionTemplate = 0; |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2841 | if (NDecl) { |
| 2842 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2843 | if ((FunctionTemplate = dyn_cast<FunctionTemplateDecl>(NDecl))) |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2844 | FDecl = FunctionTemplate->getTemplatedDecl(); |
| 2845 | else |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2846 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2847 | Ovl = dyn_cast<OverloadedFunctionDecl>(NDecl); |
Douglas Gregor | 4646f9c | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2848 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2849 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2850 | if (Ovl || FunctionTemplate || |
| 2851 | (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 411889e | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2852 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | b5af738 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2853 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2854 | ADL = false; |
| 2855 | |
Douglas Gregor | fcb1919 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2856 | // We don't perform ADL in C. |
| 2857 | if (!getLangOptions().CPlusPlus) |
| 2858 | ADL = false; |
| 2859 | |
Douglas Gregor | b60eb75 | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2860 | if (Ovl || FunctionTemplate || ADL) { |
Douglas Gregor | c9a03b7 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2861 | FDecl = ResolveOverloadedCallFn(Fn, NDecl, UnqualifiedName, |
| 2862 | HasExplicitTemplateArgs, |
| 2863 | ExplicitTemplateArgs, |
| 2864 | NumExplicitTemplateArgs, |
| 2865 | LParenLoc, Args, NumArgs, CommaLocs, |
| 2866 | RParenLoc, ADL); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2867 | if (!FDecl) |
| 2868 | return ExprError(); |
| 2869 | |
| 2870 | // Update Fn to refer to the actual function selected. |
| 2871 | Expr *NewFn = 0; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2872 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | 2885775 | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2873 | = dyn_cast<QualifiedDeclRefExpr>(FnExpr)) |
Douglas Gregor | 1e589cc | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2874 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2875 | QDRExpr->getLocation(), |
| 2876 | false, false, |
| 2877 | QDRExpr->getQualifierRange(), |
| 2878 | QDRExpr->getQualifier()); |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2879 | else |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2880 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | aa1da4a | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2881 | Fn->getSourceRange().getBegin()); |
| 2882 | Fn->Destroy(Context); |
| 2883 | Fn = NewFn; |
| 2884 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2885 | } |
Chris Lattner | 3e254fb | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2886 | |
| 2887 | // Promote the function operand. |
| 2888 | UsualUnaryConversions(Fn); |
| 2889 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2890 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2891 | // of arguments and function on error. |
Ted Kremenek | 362abcd | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2892 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2893 | Args, NumArgs, |
| 2894 | Context.BoolTy, |
| 2895 | RParenLoc)); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2896 | |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2897 | const FunctionType *FuncT; |
| 2898 | if (!Fn->getType()->isBlockPointerType()) { |
| 2899 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2900 | // have type pointer to function". |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2901 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2902 | if (PT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2903 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2904 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2905 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2906 | } else { // This is a block call. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2907 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
Steve Naroff | d6163f3 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2908 | getAsFunctionType(); |
| 2909 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2910 | if (FuncT == 0) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2911 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2912 | << Fn->getType() << Fn->getSourceRange()); |
| 2913 | |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2914 | // Check for a valid return type |
| 2915 | if (!FuncT->getResultType()->isVoidType() && |
| 2916 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2917 | FuncT->getResultType(), |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2918 | PDiag(diag::err_call_incomplete_return) |
| 2919 | << TheCall->getSourceRange())) |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2920 | return ExprError(); |
| 2921 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2922 | // We know the result type of the call, set it. |
Douglas Gregor | 2aecd1f | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2923 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2925 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2926 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2927 | RParenLoc)) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2928 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2929 | } else { |
Douglas Gregor | 4fa5890 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2930 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2932 | if (FDecl) { |
| 2933 | // Check if we have too few/too many template arguments, based |
| 2934 | // on our knowledge of the function definition. |
| 2935 | const FunctionDecl *Def = 0; |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2936 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | f7ed781 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2937 | const FunctionProtoType *Proto = |
| 2938 | Def->getType()->getAsFunctionProtoType(); |
| 2939 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2940 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2941 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2942 | } |
| 2943 | } |
Douglas Gregor | a8f2ae6 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2944 | } |
| 2945 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2946 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2947 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2948 | Expr *Arg = Args[i]; |
| 2949 | DefaultArgumentPromotion(Arg); |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2950 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2951 | Arg->getType(), |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2952 | PDiag(diag::err_call_incomplete_argument) |
| 2953 | << Arg->getSourceRange())) |
Eli Friedman | 83dec9e | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2954 | return ExprError(); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2955 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2956 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2957 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2958 | |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2959 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2960 | if (!Method->isStatic()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2961 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2962 | << Fn->getSourceRange()); |
Douglas Gregor | 3257fb5 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2963 | |
Fariborz Jahanian | c10357d | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2964 | // Check for sentinels |
| 2965 | if (NDecl) |
| 2966 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Anders Carlsson | 7fb1380 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2967 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2968 | // Do special checking on direct calls to functions. |
Anders Carlsson | 7fb1380 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2969 | if (FDecl) { |
| 2970 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 2971 | return ExprError(); |
| 2972 | |
| 2973 | if (unsigned BuiltinID = FDecl->getBuiltinID(Context)) |
| 2974 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 2975 | } else if (NDecl) { |
| 2976 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 2977 | return ExprError(); |
| 2978 | } |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2979 | |
Anders Carlsson | 54ad8a0 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 2980 | return MaybeBindToTemporary(TheCall.take()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2983 | Action::OwningExprResult |
| 2984 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2985 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2986 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 2987 | //FIXME: Preserve type source info. |
| 2988 | QualType literalType = GetTypeFromParser(Ty); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2989 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2990 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2991 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2992 | |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 2993 | if (literalType->isArrayType()) { |
Chris Lattner | a1923f6 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 2994 | if (literalType->isVariableArrayType()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2995 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 2996 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | ed71c54 | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 2997 | } else if (!literalType->isDependentType() && |
| 2998 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2999 | PDiag(diag::err_typecheck_decl_incomplete_type) |
| 3000 | << SourceRange(LParenLoc, |
| 3001 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3002 | return ExprError(); |
Eli Friedman | 8c2173d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3003 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3004 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 6214d8a | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3005 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3006 | return ExprError(); |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3007 | |
Chris Lattner | e5cb586 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3008 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3009 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3010 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3011 | return ExprError(); |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3012 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3013 | InitExpr.release(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3014 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3015 | literalExpr, isFileScope)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3018 | Action::OwningExprResult |
| 3019 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3020 | SourceLocation RBraceLoc) { |
| 3021 | unsigned NumInit = initlist.size(); |
| 3022 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3023 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3024 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3025 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3026 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3027 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | f603b47 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3028 | RBraceLoc); |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3029 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3030 | return Owned(E); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3033 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | c358b62 | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3034 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Fariborz Jahanian | cf13d4a | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3035 | CastExpr::CastKind& Kind, |
| 3036 | CXXMethodDecl *& ConversionDecl, |
| 3037 | bool FunctionalStyle) { |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3038 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | cf13d4a | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3039 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3040 | ConversionDecl); |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3041 | |
Eli Friedman | 01e0f65 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3042 | DefaultFunctionArrayConversion(castExpr); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3043 | |
| 3044 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3045 | // type needs to be scalar. |
| 3046 | if (castType->isVoidType()) { |
| 3047 | // Cast to void allows any expr type. |
| 3048 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3049 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 3050 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 3051 | (castType->isStructureType() || castType->isUnionType())) { |
| 3052 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3053 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3054 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3055 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | b4671fa | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3056 | Kind = CastExpr::CK_NoOp; |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3057 | } else if (castType->isUnionType()) { |
| 3058 | // GCC cast to union extension |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3059 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3060 | RecordDecl::field_iterator Field, FieldEnd; |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3061 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3062 | Field != FieldEnd; ++Field) { |
| 3063 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 3064 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 3065 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3066 | << castExpr->getSourceRange(); |
| 3067 | break; |
| 3068 | } |
| 3069 | } |
| 3070 | if (Field == FieldEnd) |
| 3071 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3072 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | b4671fa | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3073 | Kind = CastExpr::CK_ToUnion; |
Seo Sanghyeon | 27b3395 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3074 | } else { |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3075 | // Reject any other conversions to non-scalar types. |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3076 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3077 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3078 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3079 | } else if (!castExpr->getType()->isScalarType() && |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3080 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3081 | return Diag(castExpr->getLocStart(), |
| 3082 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3083 | << castExpr->getType() << castExpr->getSourceRange(); |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3084 | } else if (castType->isExtVectorType()) { |
| 3085 | if (CheckExtVectorCast(TyR, castType, castExpr->getType())) |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3086 | return true; |
| 3087 | } else if (castType->isVectorType()) { |
| 3088 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 3089 | return true; |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3090 | } else if (castExpr->getType()->isVectorType()) { |
| 3091 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 3092 | return true; |
Steve Naroff | ff6c802 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 3093 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | 49fd7ad | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3094 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Eli Friedman | 970e56c | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3095 | } else if (!castType->isArithmeticType()) { |
| 3096 | QualType castExprType = castExpr->getType(); |
| 3097 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3098 | return Diag(castExpr->getLocStart(), |
| 3099 | diag::err_cast_pointer_from_non_pointer_int) |
| 3100 | << castExprType << castExpr->getSourceRange(); |
| 3101 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3102 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3103 | return Diag(castExpr->getLocStart(), |
| 3104 | diag::err_cast_pointer_to_non_pointer_int) |
| 3105 | << castType << castExpr->getSourceRange(); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3106 | } |
Fariborz Jahanian | 4862e87 | 2009-05-22 21:42:52 +0000 | [diff] [blame] | 3107 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3108 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Argiris Kirtzidis | 95de23a | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3109 | return false; |
| 3110 | } |
| 3111 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 3112 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3113 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3114 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3115 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3116 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3117 | return Diag(R.getBegin(), |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3118 | Ty->isVectorType() ? |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3119 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3120 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3121 | << VectorTy << Ty << R; |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3122 | } else |
| 3123 | return Diag(R.getBegin(), |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3124 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3125 | << VectorTy << Ty << R; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3126 | |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3127 | return false; |
| 3128 | } |
| 3129 | |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3130 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, QualType SrcTy) { |
| 3131 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
| 3132 | |
Nate Begeman | 9e06370 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3133 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3134 | // an ExtVectorType. |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3135 | if (SrcTy->isVectorType()) { |
| 3136 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3137 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3138 | << DestTy << SrcTy << R; |
| 3139 | return false; |
| 3140 | } |
| 3141 | |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3142 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3143 | // conversion will take place first from scalar to elt type, and then |
| 3144 | // splat from elt type to vector. |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3145 | if (SrcTy->isPointerType()) |
| 3146 | return Diag(R.getBegin(), |
| 3147 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3148 | << DestTy << SrcTy << R; |
Nate Begeman | bd42e02 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3149 | return false; |
| 3150 | } |
| 3151 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3152 | Action::OwningExprResult |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3153 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3154 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3155 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
| 3156 | |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3157 | assert((Ty != 0) && (Op.get() != 0) && |
| 3158 | "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3159 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3160 | Expr *castExpr = (Expr *)Op.get(); |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3161 | //FIXME: Preserve type source info. |
| 3162 | QualType castType = GetTypeFromParser(Ty); |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3163 | |
| 3164 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3165 | if (isa<ParenListExpr>(castExpr)) |
| 3166 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Fariborz Jahanian | cf13d4a | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3167 | CXXMethodDecl *ConversionDecl = 0; |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3168 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Fariborz Jahanian | cf13d4a | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3169 | Kind, ConversionDecl)) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3170 | return ExprError(); |
Fariborz Jahanian | ec17213 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3171 | if (ConversionDecl) { |
| 3172 | // encounterred a c-style cast requiring a conversion function. |
| 3173 | if (CXXConversionDecl *CD = dyn_cast<CXXConversionDecl>(ConversionDecl)) { |
| 3174 | castExpr = |
| 3175 | new (Context) CXXFunctionalCastExpr(castType.getNonReferenceType(), |
| 3176 | castType, LParenLoc, |
| 3177 | CastExpr::CK_UserDefinedConversion, |
| 3178 | castExpr, CD, |
| 3179 | RParenLoc); |
| 3180 | Kind = CastExpr::CK_UserDefinedConversion; |
| 3181 | } |
| 3182 | // FIXME. AST for when dealing with conversion functions (FunctionDecl). |
| 3183 | } |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3184 | |
| 3185 | Op.release(); |
Sebastian Redl | 0e35d04 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3186 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Anders Carlsson | 9583fa7 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3187 | Kind, castExpr, castType, |
| 3188 | LParenLoc, RParenLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3189 | } |
| 3190 | |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3191 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3192 | /// of comma binary operators. |
| 3193 | Action::OwningExprResult |
| 3194 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3195 | Expr *expr = EA.takeAs<Expr>(); |
| 3196 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3197 | if (!E) |
| 3198 | return Owned(expr); |
| 3199 | |
| 3200 | OwningExprResult Result(*this, E->getExpr(0)); |
| 3201 | |
| 3202 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3203 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3204 | Owned(E->getExpr(i))); |
| 3205 | |
| 3206 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3207 | } |
| 3208 | |
| 3209 | Action::OwningExprResult |
| 3210 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3211 | SourceLocation RParenLoc, ExprArg Op, |
| 3212 | QualType Ty) { |
| 3213 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
| 3214 | |
| 3215 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
| 3216 | // then handle it as such. |
| 3217 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3218 | if (PE->getNumExprs() == 0) { |
| 3219 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3220 | return ExprError(); |
| 3221 | } |
| 3222 | |
| 3223 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3224 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3225 | initExprs.push_back(PE->getExpr(i)); |
| 3226 | |
| 3227 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3228 | // braces instead of the original commas. |
| 3229 | Op.release(); |
| 3230 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
| 3231 | initExprs.size(), RParenLoc); |
| 3232 | E->setType(Ty); |
| 3233 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
| 3234 | Owned(E)); |
| 3235 | } else { |
| 3236 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 3237 | // sequence of BinOp comma operators. |
| 3238 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3239 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3240 | } |
| 3241 | } |
| 3242 | |
| 3243 | Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 3244 | SourceLocation R, |
| 3245 | MultiExprArg Val) { |
| 3246 | unsigned nexprs = Val.size(); |
| 3247 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| 3248 | assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); |
| 3249 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| 3250 | return Owned(expr); |
| 3251 | } |
| 3252 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3253 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3254 | /// In that case, lhs = cond. |
Chris Lattner | 9c039b5 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3255 | /// C99 6.5.15 |
| 3256 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3257 | SourceLocation QuestionLoc) { |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3258 | // C++ is sufficiently different to merit its own checker. |
| 3259 | if (getLangOptions().CPlusPlus) |
| 3260 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3261 | |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3262 | UsualUnaryConversions(Cond); |
| 3263 | UsualUnaryConversions(LHS); |
| 3264 | UsualUnaryConversions(RHS); |
| 3265 | QualType CondTy = Cond->getType(); |
| 3266 | QualType LHSTy = LHS->getType(); |
| 3267 | QualType RHSTy = RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3268 | |
| 3269 | // first, check the condition. |
Sebastian Redl | bd26196 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3270 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3271 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3272 | << CondTy; |
| 3273 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3274 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3275 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3276 | // Now check the two expressions. |
Nate Begeman | e85f43d | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3277 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3278 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 1b21c7f | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3279 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3280 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3281 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3282 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3283 | UsualArithmeticConversions(LHS, RHS); |
| 3284 | return LHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3285 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3286 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3287 | // If both operands are the same structure or union type, the result is that |
| 3288 | // type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3289 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3290 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3291 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3292 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3293 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3294 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3295 | // FIXME: Type of conditional expression must be complete in C mode. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3296 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3297 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3298 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3299 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3300 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3301 | if (!LHSTy->isVoidType()) |
| 3302 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3303 | << RHS->getSourceRange(); |
| 3304 | if (!RHSTy->isVoidType()) |
| 3305 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3306 | << LHS->getSourceRange(); |
| 3307 | ImpCastExprToType(LHS, Context.VoidTy); |
| 3308 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | f025aac | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3309 | return Context.VoidTy; |
Steve Naroff | 95cb389 | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3310 | } |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3311 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3312 | // the type of the other operand." |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3313 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3314 | RHS->isNullPointerConstant(Context)) { |
| 3315 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 3316 | return LHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3317 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3318 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3319 | LHS->isNullPointerConstant(Context)) { |
| 3320 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 3321 | return RHSTy; |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3322 | } |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3323 | // Handle things like Class and struct objc_class*. Here we case the result |
| 3324 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 3325 | // redefinition type if an attempt is made to access its fields. |
| 3326 | if (LHSTy->isObjCClassType() && |
| 3327 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3328 | ImpCastExprToType(RHS, LHSTy); |
| 3329 | return LHSTy; |
| 3330 | } |
| 3331 | if (RHSTy->isObjCClassType() && |
| 3332 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3333 | ImpCastExprToType(LHS, RHSTy); |
| 3334 | return RHSTy; |
| 3335 | } |
| 3336 | // And the same for struct objc_object* / id |
| 3337 | if (LHSTy->isObjCIdType() && |
| 3338 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3339 | ImpCastExprToType(RHS, LHSTy); |
| 3340 | return LHSTy; |
| 3341 | } |
| 3342 | if (RHSTy->isObjCIdType() && |
| 3343 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3344 | ImpCastExprToType(LHS, RHSTy); |
| 3345 | return RHSTy; |
| 3346 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3347 | // Handle block pointer types. |
| 3348 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3349 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3350 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3351 | QualType destType = Context.getPointerType(Context.VoidTy); |
| 3352 | ImpCastExprToType(LHS, destType); |
| 3353 | ImpCastExprToType(RHS, destType); |
| 3354 | return destType; |
| 3355 | } |
| 3356 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3357 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3358 | return QualType(); |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3359 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3360 | // We have 2 block pointer types. |
| 3361 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3362 | // Two identical block pointer types are always compatible. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3363 | return LHSTy; |
| 3364 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3365 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3366 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3367 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3368 | |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3369 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3370 | rhptee.getUnqualifiedType())) { |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3371 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3372 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3373 | // In this situation, we assume void* type. No especially good |
| 3374 | // reason, but this is what gcc does, and we do have to pick |
| 3375 | // to get a consistent AST. |
| 3376 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3377 | ImpCastExprToType(LHS, incompatTy); |
| 3378 | ImpCastExprToType(RHS, incompatTy); |
| 3379 | return incompatTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3380 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3381 | // The block pointer types are compatible. |
| 3382 | ImpCastExprToType(LHS, LHSTy); |
| 3383 | ImpCastExprToType(RHS, LHSTy); |
Steve Naroff | 6ba2268 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3384 | return LHSTy; |
| 3385 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3386 | // Check constraints for Objective-C object pointers types. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3387 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3388 | |
| 3389 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3390 | // Two identical object pointer types are always compatible. |
| 3391 | return LHSTy; |
| 3392 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3393 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAsObjCObjectPointerType(); |
| 3394 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAsObjCObjectPointerType(); |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3395 | QualType compositeType = LHSTy; |
| 3396 | |
| 3397 | // If both operands are interfaces and either operand can be |
| 3398 | // assigned to the other, use that type as the composite |
| 3399 | // type. This allows |
| 3400 | // xxx ? (A*) a : (B*) b |
| 3401 | // where B is a subclass of A. |
| 3402 | // |
| 3403 | // Additionally, as for assignment, if either type is 'id' |
| 3404 | // allow silent coercion. Finally, if the types are |
| 3405 | // incompatible then make sure to use 'id' as the composite |
| 3406 | // type so the result is acceptable for sending messages to. |
| 3407 | |
| 3408 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3409 | // It could return the composite type. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3410 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
Fariborz Jahanian | 2e006d2 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3411 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3412 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
Fariborz Jahanian | 2e006d2 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3413 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3414 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 3415 | RHSTy->isObjCQualifiedIdType()) && |
Steve Naroff | 99eb86b | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3416 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3417 | // Need to handle "id<xx>" explicitly. |
| 3418 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3419 | // id. Currently localizing to here until clear this should be |
| 3420 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 3421 | compositeType = Context.getObjCIdType(); |
| 3422 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3423 | compositeType = Context.getObjCIdType(); |
| 3424 | } else { |
| 3425 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3426 | << LHSTy << RHSTy |
| 3427 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3428 | QualType incompatTy = Context.getObjCIdType(); |
| 3429 | ImpCastExprToType(LHS, incompatTy); |
| 3430 | ImpCastExprToType(RHS, incompatTy); |
| 3431 | return incompatTy; |
| 3432 | } |
| 3433 | // The object pointer types are compatible. |
| 3434 | ImpCastExprToType(LHS, compositeType); |
| 3435 | ImpCastExprToType(RHS, compositeType); |
| 3436 | return compositeType; |
| 3437 | } |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3438 | // Check Objective-C object pointer types and 'void *' |
| 3439 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3440 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3441 | QualType rhptee = RHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
| 3442 | QualType destPointee = lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3443 | QualType destType = Context.getPointerType(destPointee); |
| 3444 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3445 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3446 | return destType; |
| 3447 | } |
| 3448 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 3449 | QualType lhptee = LHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3450 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 4ace8ac | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3451 | QualType destPointee = rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3452 | QualType destType = Context.getPointerType(destPointee); |
| 3453 | ImpCastExprToType(RHS, destType); // add qualifiers if necessary |
| 3454 | ImpCastExprToType(LHS, destType); // promote to void* |
| 3455 | return destType; |
| 3456 | } |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3457 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3458 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3459 | // get the "pointed to" types |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3460 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3461 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 5ca84bc | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3462 | |
| 3463 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3464 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3465 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 3466 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3467 | QualType destType = Context.getPointerType(destPointee); |
| 3468 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3469 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3470 | return destType; |
| 3471 | } |
| 3472 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 3473 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3474 | QualType destType = Context.getPointerType(destPointee); |
| 3475 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3476 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3477 | return destType; |
| 3478 | } |
| 3479 | |
| 3480 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3481 | // Two identical pointer types are always compatible. |
| 3482 | return LHSTy; |
| 3483 | } |
| 3484 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3485 | rhptee.getUnqualifiedType())) { |
| 3486 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3487 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3488 | // In this situation, we assume void* type. No especially good |
| 3489 | // reason, but this is what gcc does, and we do have to pick |
| 3490 | // to get a consistent AST. |
| 3491 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3492 | ImpCastExprToType(LHS, incompatTy); |
| 3493 | ImpCastExprToType(RHS, incompatTy); |
| 3494 | return incompatTy; |
| 3495 | } |
| 3496 | // The pointer types are compatible. |
| 3497 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3498 | // differently qualified versions of compatible types, the result type is |
| 3499 | // a pointer to an appropriately qualified version of the *composite* |
| 3500 | // type. |
| 3501 | // FIXME: Need to calculate the composite type. |
| 3502 | // FIXME: Need to add qualifiers |
| 3503 | ImpCastExprToType(LHS, LHSTy); |
| 3504 | ImpCastExprToType(RHS, LHSTy); |
| 3505 | return LHSTy; |
| 3506 | } |
| 3507 | |
| 3508 | // GCC compatibility: soften pointer/integer mismatch. |
| 3509 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3510 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3511 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3512 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 3513 | return RHSTy; |
| 3514 | } |
| 3515 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3516 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3517 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3518 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 3519 | return LHSTy; |
| 3520 | } |
Daniel Dunbar | a7b5fb9 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3521 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3522 | // Otherwise, the operands are not compatible. |
Chris Lattner | e289726 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3523 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3524 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3525 | return QualType(); |
| 3526 | } |
| 3527 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3528 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3529 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3530 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3531 | SourceLocation ColonLoc, |
| 3532 | ExprArg Cond, ExprArg LHS, |
| 3533 | ExprArg RHS) { |
| 3534 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3535 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3536 | |
| 3537 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3538 | // was the condition. |
| 3539 | bool isLHSNull = LHSExpr == 0; |
| 3540 | if (isLHSNull) |
| 3541 | LHSExpr = CondExpr; |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3542 | |
| 3543 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3544 | RHSExpr, QuestionLoc); |
| 3545 | if (result.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3546 | return ExprError(); |
| 3547 | |
| 3548 | Cond.release(); |
| 3549 | LHS.release(); |
| 3550 | RHS.release(); |
Douglas Gregor | 3461987 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3551 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3552 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 3461987 | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3553 | ColonLoc, RHSExpr, result)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3556 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3557 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3558 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3559 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3560 | // FIXME: add a couple examples in this comment. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3561 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3562 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 3563 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3564 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3565 | if ((lhsType->isObjCClassType() && |
| 3566 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3567 | (rhsType->isObjCClassType() && |
| 3568 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3569 | return Compatible; |
| 3570 | } |
| 3571 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3572 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3573 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 3574 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3575 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3576 | // make sure we operate on the canonical type |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3577 | lhptee = Context.getCanonicalType(lhptee); |
| 3578 | rhptee = Context.getCanonicalType(rhptee); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3579 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3580 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3581 | |
| 3582 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3583 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3584 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | b60352a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3585 | // FIXME: Handle ExtQualType |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3586 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3587 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3588 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3589 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3590 | // incomplete type and the other is a pointer to a qualified or unqualified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3591 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3592 | if (lhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3593 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3594 | return ConvTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3595 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3596 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3597 | assert(rhptee->isFunctionType()); |
| 3598 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3599 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3600 | |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3601 | if (rhptee->isVoidType()) { |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3602 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3603 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3604 | |
| 3605 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | 9db553e | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3606 | assert(lhptee->isFunctionType()); |
| 3607 | return FunctionVoidPointer; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3608 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3609 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3610 | // unqualified versions of compatible types, ... |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3611 | lhptee = lhptee.getUnqualifiedType(); |
| 3612 | rhptee = rhptee.getUnqualifiedType(); |
| 3613 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3614 | // Check if the pointee types are compatible ignoring the sign. |
| 3615 | // We explicitly check for char so that we catch "char" vs |
| 3616 | // "unsigned char" on systems where "char" is unsigned. |
| 3617 | if (lhptee->isCharType()) { |
| 3618 | lhptee = Context.UnsignedCharTy; |
| 3619 | } else if (lhptee->isSignedIntegerType()) { |
| 3620 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 3621 | } |
| 3622 | if (rhptee->isCharType()) { |
| 3623 | rhptee = Context.UnsignedCharTy; |
| 3624 | } else if (rhptee->isSignedIntegerType()) { |
| 3625 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 3626 | } |
| 3627 | if (lhptee == rhptee) { |
| 3628 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3629 | // takes priority over sign incompatibility because the sign |
| 3630 | // warning can be disabled. |
| 3631 | if (ConvTy != Compatible) |
| 3632 | return ConvTy; |
| 3633 | return IncompatiblePointerSign; |
| 3634 | } |
| 3635 | // General pointer incompatibility takes priority over qualifiers. |
| 3636 | return IncompatiblePointer; |
| 3637 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3638 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3641 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3642 | /// block pointer types are compatible or whether a block and normal pointer |
| 3643 | /// are compatible. It is more restrict than comparing two function pointer |
| 3644 | // types. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3645 | Sema::AssignConvertType |
| 3646 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3647 | QualType rhsType) { |
| 3648 | QualType lhptee, rhptee; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3649 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3650 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3651 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 3652 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3653 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3654 | // make sure we operate on the canonical type |
| 3655 | lhptee = Context.getCanonicalType(lhptee); |
| 3656 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3657 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3658 | AssignConvertType ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3659 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3660 | // For blocks we enforce that qualifiers are identical. |
| 3661 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 3662 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3663 | |
Eli Friedman | b6eed6e | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3664 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3665 | return IncompatibleBlockPointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3666 | return ConvTy; |
| 3667 | } |
| 3668 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3669 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3670 | /// has code to accommodate several GCC extensions when type checking |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3671 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3672 | /// |
| 3673 | /// int a, *pint; |
| 3674 | /// short *pshort; |
| 3675 | /// struct foo *pfoo; |
| 3676 | /// |
| 3677 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3678 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3679 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3680 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3681 | /// |
| 3682 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3683 | /// C99 spec dictates. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3684 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3685 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3686 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3687 | // Get canonical types. We're not formatting these types, just comparing |
| 3688 | // them. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3689 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3690 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3691 | |
| 3692 | if (lhsType == rhsType) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3693 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3694 | |
David Chisnall | 44663db | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3695 | if ((lhsType->isObjCClassType() && |
| 3696 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3697 | (rhsType->isObjCClassType() && |
| 3698 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3699 | return Compatible; |
| 3700 | } |
| 3701 | |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3702 | // If the left-hand side is a reference type, then we are in a |
| 3703 | // (rare!) case where we've allowed the use of references in C, |
| 3704 | // e.g., as a parameter type in a built-in function. In this case, |
| 3705 | // just make sure that the type referenced is compatible with the |
| 3706 | // right-hand side type. The caller is responsible for adjusting |
| 3707 | // lhsType so that the resulting expression does not have reference |
| 3708 | // type. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3709 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3710 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3711 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3712 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3713 | } |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3714 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 3715 | // to the same ExtVector type. |
| 3716 | if (lhsType->isExtVectorType()) { |
| 3717 | if (rhsType->isExtVectorType()) |
| 3718 | return lhsType == rhsType ? Compatible : Incompatible; |
| 3719 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 3720 | return Compatible; |
| 3721 | } |
| 3722 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3723 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3724 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3725 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3726 | // no bits are changed but the result type is different. |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3727 | if (getLangOptions().LaxVectorConversions && |
| 3728 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3729 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3730 | return IncompatibleVectors; |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3731 | } |
| 3732 | return Incompatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3733 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3734 | |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3735 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3736 | return Compatible; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3737 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3738 | if (isa<PointerType>(lhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3739 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3740 | return IntToPointer; |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3741 | |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3742 | if (isa<PointerType>(rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3743 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3744 | |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3745 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3746 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3747 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3748 | return Compatible; |
| 3749 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3750 | } |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3751 | if (rhsType->getAs<BlockPointerType>()) { |
| 3752 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3753 | return Compatible; |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3754 | |
| 3755 | // Treat block pointers as objects. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3756 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3757 | return Compatible; |
| 3758 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3759 | return Incompatible; |
| 3760 | } |
| 3761 | |
| 3762 | if (isa<BlockPointerType>(lhsType)) { |
| 3763 | if (rhsType->isIntegerType()) |
Eli Friedman | c589830 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3764 | return IntToBlockPointer; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3765 | |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3766 | // Treat block pointers as objects. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3767 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | a982c71 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3768 | return Compatible; |
| 3769 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3770 | if (rhsType->isBlockPointerType()) |
| 3771 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3772 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3773 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3774 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3775 | return Compatible; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3776 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3777 | return Incompatible; |
| 3778 | } |
| 3779 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3780 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 3781 | if (rhsType->isIntegerType()) |
| 3782 | return IntToPointer; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3783 | |
| 3784 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3785 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3786 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 3787 | return Compatible; |
| 3788 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3789 | } |
| 3790 | if (rhsType->isObjCObjectPointerType()) { |
Steve Naroff | 7bffd37 | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3791 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 3792 | return Compatible; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3793 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 3794 | return Compatible; |
Steve Naroff | 99eb86b | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3795 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 3796 | return IncompatibleObjCQualifiedId; |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3797 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3798 | } |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3799 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3800 | if (RHSPT->getPointeeType()->isVoidType()) |
| 3801 | return Compatible; |
| 3802 | } |
| 3803 | // Treat block pointers as objects. |
| 3804 | if (rhsType->isBlockPointerType()) |
| 3805 | return Compatible; |
| 3806 | return Incompatible; |
| 3807 | } |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3808 | if (isa<PointerType>(rhsType)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3809 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3810 | if (lhsType == Context.BoolTy) |
| 3811 | return Compatible; |
| 3812 | |
| 3813 | if (lhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3814 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3815 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3816 | if (isa<PointerType>(lhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3817 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3818 | |
| 3819 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3820 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 7abc143 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3821 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3822 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3823 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3824 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 3825 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 3826 | if (lhsType == Context.BoolTy) |
| 3827 | return Compatible; |
| 3828 | |
| 3829 | if (lhsType->isIntegerType()) |
| 3830 | return PointerToInt; |
| 3831 | |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3832 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3833 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 8194a54 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3834 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3835 | return Compatible; |
| 3836 | return IncompatiblePointer; |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3837 | } |
| 3838 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3839 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3840 | return Compatible; |
| 3841 | return Incompatible; |
| 3842 | } |
Eli Friedman | 48d0bb0 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3843 | |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3844 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 390564e | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3845 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3846 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3847 | } |
| 3848 | return Incompatible; |
| 3849 | } |
| 3850 | |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3851 | /// \brief Constructs a transparent union from an expression that is |
| 3852 | /// used to initialize the transparent union. |
| 3853 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
| 3854 | QualType UnionType, FieldDecl *Field) { |
| 3855 | // Build an initializer list that designates the appropriate member |
| 3856 | // of the transparent union. |
| 3857 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3858 | &E, 1, |
| 3859 | SourceLocation()); |
| 3860 | Initializer->setType(UnionType); |
| 3861 | Initializer->setInitializedFieldInUnion(Field); |
| 3862 | |
| 3863 | // Build a compound literal constructing a value of the transparent |
| 3864 | // union type from this initializer list. |
| 3865 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3866 | false); |
| 3867 | } |
| 3868 | |
| 3869 | Sema::AssignConvertType |
| 3870 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3871 | QualType FromType = rExpr->getType(); |
| 3872 | |
| 3873 | // If the ArgType is a Union type, we want to handle a potential |
| 3874 | // transparent_union GCC extension. |
| 3875 | const RecordType *UT = ArgType->getAsUnionType(); |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3876 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3877 | return Incompatible; |
| 3878 | |
| 3879 | // The field to initialize within the transparent union. |
| 3880 | RecordDecl *UD = UT->getDecl(); |
| 3881 | FieldDecl *InitField = 0; |
| 3882 | // It's compatible if the expression matches any of the fields. |
Argiris Kirtzidis | ab6e38a | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3883 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 3884 | itend = UD->field_end(); |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3885 | it != itend; ++it) { |
| 3886 | if (it->getType()->isPointerType()) { |
| 3887 | // If the transparent union contains a pointer type, we allow: |
| 3888 | // 1) void pointer |
| 3889 | // 2) null pointer constant |
| 3890 | if (FromType->isPointerType()) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3891 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3892 | ImpCastExprToType(rExpr, it->getType()); |
| 3893 | InitField = *it; |
| 3894 | break; |
| 3895 | } |
| 3896 | |
| 3897 | if (rExpr->isNullPointerConstant(Context)) { |
| 3898 | ImpCastExprToType(rExpr, it->getType()); |
| 3899 | InitField = *it; |
| 3900 | break; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 3905 | == Compatible) { |
| 3906 | InitField = *it; |
| 3907 | break; |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | if (!InitField) |
| 3912 | return Incompatible; |
| 3913 | |
| 3914 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 3915 | return Compatible; |
| 3916 | } |
| 3917 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3918 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3919 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3920 | if (getLangOptions().CPlusPlus) { |
| 3921 | if (!lhsType->isRecordType()) { |
| 3922 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3923 | // expression is implicitly converted (C++ 4) to the |
| 3924 | // cv-unqualified type of the left operand. |
Douglas Gregor | 6fd3557 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3925 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3926 | "assigning")) |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3927 | return Incompatible; |
Chris Lattner | 79e9a42 | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 3928 | return Compatible; |
Douglas Gregor | 6573cfd | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3929 | } |
| 3930 | |
| 3931 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3932 | // structures. |
| 3933 | } |
| 3934 | |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3935 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3936 | // a null pointer constant. |
Steve Naroff | d305a86 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 3937 | if ((lhsType->isPointerType() || |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3938 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3939 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3940 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3941 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3942 | return Compatible; |
| 3943 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3944 | |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3945 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3946 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3947 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3948 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3949 | // |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3950 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3951 | if (!lhsType->isReferenceType()) |
| 3952 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3953 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3954 | Sema::AssignConvertType result = |
| 3955 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3956 | |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3957 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3958 | // type of the assignment expression. |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3959 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3960 | // so that we can use references in built-in functions even in C. |
| 3961 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3962 | // does not have reference type. |
Douglas Gregor | 144b06c | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3963 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | 0d5d89d | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3964 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3965 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3968 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3969 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3970 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3971 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3972 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3973 | } |
| 3974 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3975 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3976 | Expr *&rex) { |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3977 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3978 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | d5a56aa | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3979 | QualType lhsType = |
| 3980 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3981 | QualType rhsType = |
| 3982 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3983 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3984 | // If the vector types are identical, return. |
Nate Begeman | 0310557 | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3985 | if (lhsType == rhsType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 3986 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3987 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3988 | // Handle the case of a vector & extvector type of the same size and element |
| 3989 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3990 | if (getLangOptions().LaxVectorConversions) { |
| 3991 | // FIXME: Should we warn here? |
| 3992 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3993 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3994 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3995 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3996 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4000 | |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4001 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4002 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4003 | bool swapped = false; |
| 4004 | if (rhsType->isExtVectorType()) { |
| 4005 | swapped = true; |
| 4006 | std::swap(rex, lex); |
| 4007 | std::swap(rhsType, lhsType); |
| 4008 | } |
| 4009 | |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4010 | // Handle the case of an ext vector and scalar. |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4011 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) { |
| 4012 | QualType EltTy = LV->getElementType(); |
| 4013 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4014 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4015 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4016 | if (swapped) std::swap(rex, lex); |
| 4017 | return lhsType; |
| 4018 | } |
| 4019 | } |
| 4020 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4021 | rhsType->isRealFloatingType()) { |
| 4022 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4023 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4024 | if (swapped) std::swap(rex, lex); |
| 4025 | return lhsType; |
| 4026 | } |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4027 | } |
| 4028 | } |
Nate Begeman | 0e0eadd | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4029 | |
Nate Begeman | f169589 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4030 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4031 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4032 | << lex->getType() << rex->getType() |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4033 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4034 | return QualType(); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4035 | } |
| 4036 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4037 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4038 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4039 | { |
Daniel Dunbar | 2f08d81 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4040 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4041 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4042 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4043 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4044 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4045 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4046 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4047 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4051 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4052 | { |
Daniel Dunbar | b27282f | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4053 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4054 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4055 | return CheckVectorOperands(Loc, lex, rex); |
| 4056 | return InvalidOperands(Loc, lex, rex); |
| 4057 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4058 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4059 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4060 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4061 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4062 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4063 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4067 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4068 | { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4069 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4070 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4071 | if (CompLHSTy) *CompLHSTy = compType; |
| 4072 | return compType; |
| 4073 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4074 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4075 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4076 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4077 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4078 | if (lex->getType()->isArithmeticType() && |
| 4079 | rex->getType()->isArithmeticType()) { |
| 4080 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4081 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4082 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4083 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4084 | // Put any potential pointer into PExp |
| 4085 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4086 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4087 | std::swap(PExp, IExp); |
| 4088 | |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4089 | if (PExp->getType()->isAnyPointerType()) { |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4090 | |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4091 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4092 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4094 | // Check for arithmetic on pointers to incomplete types. |
| 4095 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4096 | if (getLangOptions().CPlusPlus) { |
| 4097 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | 8ba580c | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4098 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4099 | return QualType(); |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4100 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4101 | |
| 4102 | // GNU extension: arithmetic on pointer to void |
| 4103 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4104 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4105 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4106 | if (getLangOptions().CPlusPlus) { |
| 4107 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4108 | << lex->getType() << lex->getSourceRange(); |
| 4109 | return QualType(); |
| 4110 | } |
| 4111 | |
| 4112 | // GNU extension: arithmetic on pointer to function |
| 4113 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4114 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 3fc227b | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4115 | } else { |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4116 | // Check if we require a complete type. |
| 4117 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 3fc227b | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4118 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4119 | PExp->getType()->isObjCObjectPointerType()) && |
| 4120 | RequireCompleteType(Loc, PointeeTy, |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4121 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4122 | << PExp->getSourceRange() |
| 4123 | << PExp->getType())) |
Steve Naroff | 18b3812 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4124 | return QualType(); |
| 4125 | } |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4126 | // Diagnose bad cases where we step over interface counts. |
| 4127 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4128 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4129 | << PointeeTy << PExp->getSourceRange(); |
| 4130 | return QualType(); |
| 4131 | } |
| 4132 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4133 | if (CompLHSTy) { |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4134 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4135 | if (LHSTy.isNull()) { |
| 4136 | LHSTy = lex->getType(); |
| 4137 | if (LHSTy->isPromotableIntegerType()) |
| 4138 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4139 | } |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4140 | *CompLHSTy = LHSTy; |
| 4141 | } |
Eli Friedman | d9b1fec | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4142 | return PExp->getType(); |
| 4143 | } |
| 4144 | } |
| 4145 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4146 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4149 | // C99 6.5.6 |
| 4150 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4151 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4152 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4153 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4154 | if (CompLHSTy) *CompLHSTy = compType; |
| 4155 | return compType; |
| 4156 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4157 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4158 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4160 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4161 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4162 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4163 | if (lex->getType()->isArithmeticType() |
| 4164 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4165 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4166 | return compType; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4167 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4168 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4169 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4170 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 7982a64 | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4171 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4173 | // The LHS must be an completely-defined object type. |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4175 | bool ComplainAboutVoid = false; |
| 4176 | Expr *ComplainAboutFunc = 0; |
| 4177 | if (lpointee->isVoidType()) { |
| 4178 | if (getLangOptions().CPlusPlus) { |
| 4179 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4180 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4181 | return QualType(); |
| 4182 | } |
| 4183 | |
| 4184 | // GNU C extension: arithmetic on pointer to void |
| 4185 | ComplainAboutVoid = true; |
| 4186 | } else if (lpointee->isFunctionType()) { |
| 4187 | if (getLangOptions().CPlusPlus) { |
| 4188 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4189 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4190 | return QualType(); |
| 4191 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4192 | |
| 4193 | // GNU C extension: arithmetic on pointer to function |
| 4194 | ComplainAboutFunc = lex; |
| 4195 | } else if (!lpointee->isDependentType() && |
| 4196 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4197 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4198 | << lex->getSourceRange() |
| 4199 | << lex->getType())) |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4200 | return QualType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4201 | |
Chris Lattner | 184f92d | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4202 | // Diagnose bad cases where we step over interface counts. |
| 4203 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4204 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4205 | << lpointee << lex->getSourceRange(); |
| 4206 | return QualType(); |
| 4207 | } |
| 4208 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4209 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4210 | if (rex->getType()->isIntegerType()) { |
| 4211 | if (ComplainAboutVoid) |
| 4212 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4213 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4214 | if (ComplainAboutFunc) |
| 4215 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4216 | << ComplainAboutFunc->getType() |
| 4217 | << ComplainAboutFunc->getSourceRange(); |
| 4218 | |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4219 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4220 | return lex->getType(); |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4221 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4222 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4223 | // Handle pointer-pointer subtractions. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4224 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4225 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4226 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4227 | // RHS must be a completely-type object type. |
| 4228 | // Handle the GNU void* extension. |
| 4229 | if (rpointee->isVoidType()) { |
| 4230 | if (getLangOptions().CPlusPlus) { |
| 4231 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4232 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4233 | return QualType(); |
| 4234 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4235 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4236 | ComplainAboutVoid = true; |
| 4237 | } else if (rpointee->isFunctionType()) { |
| 4238 | if (getLangOptions().CPlusPlus) { |
| 4239 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4240 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4241 | return QualType(); |
| 4242 | } |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4243 | |
| 4244 | // GNU extension: arithmetic on pointer to function |
| 4245 | if (!ComplainAboutFunc) |
| 4246 | ComplainAboutFunc = rex; |
| 4247 | } else if (!rpointee->isDependentType() && |
| 4248 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4249 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4250 | << rex->getSourceRange() |
| 4251 | << rex->getType())) |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4252 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4253 | |
Eli Friedman | 143ddc9 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4254 | if (getLangOptions().CPlusPlus) { |
| 4255 | // Pointee types must be the same: C++ [expr.add] |
| 4256 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4257 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4258 | << lex->getType() << rex->getType() |
| 4259 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4260 | return QualType(); |
| 4261 | } |
| 4262 | } else { |
| 4263 | // Pointee types must be compatible C99 6.5.6p3 |
| 4264 | if (!Context.typesAreCompatible( |
| 4265 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4266 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4267 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4268 | << lex->getType() << rex->getType() |
| 4269 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4270 | return QualType(); |
| 4271 | } |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4272 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | 05e28f6 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4274 | if (ComplainAboutVoid) |
| 4275 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4276 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4277 | if (ComplainAboutFunc) |
| 4278 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4279 | << ComplainAboutFunc->getType() |
| 4280 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4281 | |
| 4282 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4283 | return Context.getPointerDiffType(); |
| 4284 | } |
| 4285 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4286 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4287 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4288 | } |
| 4289 | |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4290 | // C99 6.5.7 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4291 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | fe1f403 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4292 | bool isCompAssign) { |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4293 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4294 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4295 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4296 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4297 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4298 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 1931cc8 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4299 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4300 | if (LHSTy.isNull()) { |
| 4301 | LHSTy = lex->getType(); |
| 4302 | if (LHSTy->isPromotableIntegerType()) |
| 4303 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4304 | } |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4305 | if (!isCompAssign) |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4306 | ImpCastExprToType(lex, LHSTy); |
| 4307 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4308 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4309 | |
Ryan Flynn | f109fff | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4310 | // Sanity-check shift operands |
| 4311 | llvm::APSInt Right; |
| 4312 | // Check right/shifter operand |
| 4313 | if (rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | a5e7693 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4314 | if (Right.isNegative()) |
Ryan Flynn | f109fff | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4315 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4316 | else { |
| 4317 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4318 | Context.getTypeSize(lex->getType())); |
| 4319 | if (Right.uge(LeftBits)) |
| 4320 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4321 | } |
| 4322 | } |
| 4323 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4324 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4325 | return LHSTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4328 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4329 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4330 | unsigned OpaqueOpc, bool isRelational) { |
| 4331 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 4332 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4333 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4334 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4335 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4336 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 4337 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 4338 | UsualArithmeticConversions(lex, rex); |
| 4339 | else { |
| 4340 | UsualUnaryConversions(lex); |
| 4341 | UsualUnaryConversions(rex); |
| 4342 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4343 | QualType lType = lex->getType(); |
| 4344 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4345 | |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4346 | if (!lType->isFloatingType() |
| 4347 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4348 | // For non-floating point types, check for self-comparisons of the form |
| 4349 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4350 | // often indicate logic errors in the program. |
Ted Kremenek | 264b5cb | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 4351 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
| 4352 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4353 | Expr *LHSStripped = lex->IgnoreParens(); |
| 4354 | Expr *RHSStripped = rex->IgnoreParens(); |
| 4355 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 4356 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | f042dc6 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 4357 | if (DRL->getDecl() == DRR->getDecl() && |
| 4358 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4359 | Diag(Loc, diag::warn_selfcomparison); |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4360 | |
| 4361 | if (isa<CastExpr>(LHSStripped)) |
| 4362 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 4363 | if (isa<CastExpr>(RHSStripped)) |
| 4364 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| 4365 | |
| 4366 | // Warn about comparisons against a string constant (unless the other |
| 4367 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4368 | Expr *literalString = 0; |
| 4369 | Expr *literalStringStripped = 0; |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4370 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4371 | !RHSStripped->isNullPointerConstant(Context)) { |
| 4372 | literalString = lex; |
| 4373 | literalStringStripped = LHSStripped; |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4374 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 4375 | isa<ObjCEncodeExpr>(RHSStripped)) && |
| 4376 | !LHSStripped->isNullPointerConstant(Context)) { |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4377 | literalString = rex; |
| 4378 | literalStringStripped = RHSStripped; |
| 4379 | } |
| 4380 | |
| 4381 | if (literalString) { |
| 4382 | std::string resultComparison; |
| 4383 | switch (Opc) { |
| 4384 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 4385 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 4386 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 4387 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 4388 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 4389 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 4390 | default: assert(false && "Invalid comparison operator"); |
| 4391 | } |
| 4392 | Diag(Loc, diag::warn_stringcompare) |
| 4393 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 4394 | << literalString->getSourceRange() |
Douglas Gregor | 3faaa81 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4395 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 4396 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 4397 | "strcmp(") |
| 4398 | << CodeModificationHint::CreateInsertion( |
| 4399 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4400 | resultComparison); |
| 4401 | } |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 4402 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4403 | |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4404 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4405 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4406 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4407 | if (isRelational) { |
| 4408 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4409 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4410 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4411 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4412 | if (lType->isFloatingType()) { |
Chris Lattner | 4e479f9 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4413 | assert(rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4414 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 4415 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4416 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4417 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4418 | return ResultTy; |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4419 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4420 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4421 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 4422 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4423 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4424 | // All of the following pointer related warnings are GCC extensions, except |
| 4425 | // when handling null pointer constants. One day, we can consider making them |
| 4426 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 4427 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4428 | QualType LCanPointeeTy = |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4429 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 56a5cd6 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4430 | QualType RCanPointeeTy = |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4431 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4433 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4434 | if (LCanPointeeTy == RCanPointeeTy) |
| 4435 | return ResultTy; |
| 4436 | |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4437 | // C++ [expr.rel]p2: |
| 4438 | // [...] Pointer conversions (4.10) and qualification |
| 4439 | // conversions (4.4) are performed on pointer operands (or on |
| 4440 | // a pointer operand and a null pointer constant) to bring |
| 4441 | // them to their composite pointer type. [...] |
| 4442 | // |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4443 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4444 | // comparisons of pointers. |
Douglas Gregor | cf651d2 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 4445 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 30eed0f | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4446 | if (T.isNull()) { |
| 4447 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4448 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4449 | return QualType(); |
| 4450 | } |
| 4451 | |
| 4452 | ImpCastExprToType(lex, T); |
| 4453 | ImpCastExprToType(rex, T); |
| 4454 | return ResultTy; |
| 4455 | } |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4456 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 4457 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 4458 | RCanPointeeTy.getUnqualifiedType())) { |
| 4459 | // Valid unless a relational comparison of function pointers |
| 4460 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 4461 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 4462 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4463 | } |
| 4464 | } else if (!isRelational && |
| 4465 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 4466 | // Valid unless comparison between non-null pointer and function pointer |
| 4467 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 4468 | && !LHSIsNull && !RHSIsNull) { |
| 4469 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 4470 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4471 | } |
| 4472 | } else { |
| 4473 | // Invalid |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4474 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4475 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4476 | } |
Eli Friedman | 02fdbfe | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4477 | if (LCanPointeeTy != RCanPointeeTy) |
| 4478 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4479 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4480 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4481 | |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4482 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4483 | // Comparison of pointers with null pointer constants and equality |
| 4484 | // comparisons of member pointers to null pointer constants. |
| 4485 | if (RHSIsNull && |
| 4486 | (lType->isPointerType() || |
| 4487 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 9a38552 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4488 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4489 | return ResultTy; |
| 4490 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4491 | if (LHSIsNull && |
| 4492 | (rType->isPointerType() || |
| 4493 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 9a38552 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4494 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4495 | return ResultTy; |
| 4496 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4497 | |
| 4498 | // Comparison of member pointers. |
| 4499 | if (!isRelational && |
| 4500 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 4501 | // C++ [expr.eq]p2: |
| 4502 | // In addition, pointers to members can be compared, or a pointer to |
| 4503 | // member and a null pointer constant. Pointer to member conversions |
| 4504 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 4505 | // them to a common type. If one operand is a null pointer constant, |
| 4506 | // the common type is the type of the other operand. Otherwise, the |
| 4507 | // common type is a pointer to member type similar (4.4) to the type |
| 4508 | // of one of the operands, with a cv-qualification signature (4.4) |
| 4509 | // that is the union of the cv-qualification signatures of the operand |
| 4510 | // types. |
| 4511 | QualType T = FindCompositePointerType(lex, rex); |
| 4512 | if (T.isNull()) { |
| 4513 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4514 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4515 | return QualType(); |
| 4516 | } |
| 4517 | |
| 4518 | ImpCastExprToType(lex, T); |
| 4519 | ImpCastExprToType(rex, T); |
| 4520 | return ResultTy; |
| 4521 | } |
| 4522 | |
| 4523 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 5d0ead7 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4524 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4525 | return ResultTy; |
| 4526 | } |
Douglas Gregor | 70be4db | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4527 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4528 | // Handle block pointer types. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4529 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4530 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 4531 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4532 | |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4533 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | b6eed6e | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4534 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4535 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4536 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4537 | } |
| 4538 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4539 | return ResultTy; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4540 | } |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4541 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4542 | if (!isRelational |
| 4543 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4544 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4545 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4546 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4547 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4548 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | e97a854 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4549 | ->getPointeeType()->isVoidType()))) |
| 4550 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4551 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4552 | } |
| 4553 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4554 | return ResultTy; |
Steve Naroff | f85d66c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4555 | } |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4556 | |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4557 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4558 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4559 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 4560 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4561 | bool LPtrToVoid = LPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4562 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4563 | bool RPtrToVoid = RPT ? |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4564 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4565 | |
Steve Naroff | 030fcda | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4566 | if (!LPtrToVoid && !RPtrToVoid && |
| 4567 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 70b93d8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4568 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4569 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 3d081ae | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4570 | } |
Daniel Dunbar | 11c5f82 | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4571 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4572 | return ResultTy; |
Steve Naroff | 3b2ceea | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4573 | } |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4574 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4575 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4576 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 4577 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4578 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4579 | return ResultTy; |
Steve Naroff | 936c436 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4580 | } |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4581 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4582 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4583 | unsigned DiagID = 0; |
| 4584 | if (RHSIsNull) { |
| 4585 | if (isRelational) |
| 4586 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4587 | } else if (isRelational) |
| 4588 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4589 | else |
| 4590 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
| 4591 | |
| 4592 | if (DiagID) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4593 | Diag(Loc, DiagID) |
Chris Lattner | f350c6e | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4594 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4595 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4596 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4597 | return ResultTy; |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4598 | } |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4599 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4600 | unsigned DiagID = 0; |
| 4601 | if (LHSIsNull) { |
| 4602 | if (isRelational) |
| 4603 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4604 | } else if (isRelational) |
| 4605 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4606 | else |
| 4607 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4608 | |
Chris Lattner | 124569f | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4609 | if (DiagID) { |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4610 | Diag(Loc, DiagID) |
Chris Lattner | f350c6e | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4611 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 8b88b14 | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4612 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4613 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4614 | return ResultTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4615 | } |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4616 | // Handle block pointers. |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4617 | if (!isRelational && RHSIsNull |
| 4618 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4619 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4620 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4621 | } |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4622 | if (!isRelational && LHSIsNull |
| 4623 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4624 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 849ea9c | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4625 | return ResultTy; |
Steve Naroff | 4fea7b6 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4626 | } |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4627 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4630 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4631 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4632 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4633 | /// types. |
| 4634 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4635 | SourceLocation Loc, |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4636 | bool isRelational) { |
| 4637 | // Check to make sure we're operating on vectors of the same type and width, |
| 4638 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4639 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4640 | if (vType.isNull()) |
| 4641 | return vType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4642 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4643 | QualType lType = lex->getType(); |
| 4644 | QualType rType = rex->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4645 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4646 | // For non-floating point types, check for self-comparisons of the form |
| 4647 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4648 | // often indicate logic errors in the program. |
| 4649 | if (!lType->isFloatingType()) { |
| 4650 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4651 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4652 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4653 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4654 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4655 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4656 | // Check for comparisons of floating point operands using != and ==. |
| 4657 | if (!isRelational && lType->isFloatingType()) { |
| 4658 | assert (rType->isFloatingType()); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4659 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4660 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4661 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4662 | // Return the type for the comparison, which is the same as vector type for |
| 4663 | // integer vectors, or an integer type of identical size and number of |
| 4664 | // elements for floating point vectors. |
| 4665 | if (lType->isIntegerType()) |
| 4666 | return lType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4667 | |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4668 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4669 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4670 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4671 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 10687e3 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4672 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4673 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4674 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4675 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | d6d2f77 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4676 | "Unhandled vector element size in vector compare"); |
Nate Begeman | c5f0f65 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4677 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4678 | } |
| 4679 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4680 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4681 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4682 | { |
| 4683 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4684 | return CheckVectorOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4685 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4686 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4687 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4688 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4689 | return compType; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4690 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4694 | Expr *&lex, Expr *&rex, SourceLocation Loc) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4695 | { |
| 4696 | UsualUnaryConversions(lex); |
| 4697 | UsualUnaryConversions(rex); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4698 | |
Eli Friedman | bea3f84 | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 4699 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4700 | return Context.IntTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4701 | return InvalidOperands(Loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4704 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 4705 | /// is a read-only property; return true if so. A readonly property expression |
| 4706 | /// depends on various declarations and thus must be treated specially. |
| 4707 | /// |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4708 | static bool IsReadonlyProperty(Expr *E, Sema &S) |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4709 | { |
| 4710 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4711 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4712 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4713 | QualType BaseType = PropExpr->getBase()->getType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4714 | if (const ObjCObjectPointerType *OPT = |
| 4715 | BaseType->getAsObjCInterfacePointerType()) |
| 4716 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 4717 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4718 | return true; |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | return false; |
| 4722 | } |
| 4723 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4724 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4725 | /// emit an error and return true. If so, return false. |
| 4726 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4727 | SourceLocation OrigLoc = Loc; |
| 4728 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
| 4729 | &Loc); |
Fariborz Jahanian | f96ee9e | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4730 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4731 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4732 | if (IsLV == Expr::MLV_Valid) |
| 4733 | return false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4734 | |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4735 | unsigned Diag = 0; |
| 4736 | bool NeedType = false; |
| 4737 | switch (IsLV) { // C99 6.5.16p2 |
| 4738 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4739 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4740 | case Expr::MLV_ArrayType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4741 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4742 | NeedType = true; |
| 4743 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4744 | case Expr::MLV_NotObjectType: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4745 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4746 | NeedType = true; |
| 4747 | break; |
Chris Lattner | 37fb940 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4748 | case Expr::MLV_LValueCast: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4749 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4750 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4751 | case Expr::MLV_InvalidExpression: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4752 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4753 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4754 | case Expr::MLV_IncompleteType: |
| 4755 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | c84d893 | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4756 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | a21e787 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4757 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 4758 | << E->getSourceRange()); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4759 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4760 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4761 | break; |
Steve Naroff | 076d6cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4762 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4763 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4764 | break; |
Fariborz Jahanian | f18d4c8 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4765 | case Expr::MLV_ReadonlyProperty: |
| 4766 | Diag = diag::error_readonly_property_assignment; |
| 4767 | break; |
Fariborz Jahanian | c05da42 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4768 | case Expr::MLV_NoSetterProperty: |
| 4769 | Diag = diag::error_nosetter_property_assignment; |
| 4770 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4771 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4772 | |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4773 | SourceRange Assign; |
| 4774 | if (Loc != OrigLoc) |
| 4775 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4776 | if (NeedType) |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4777 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4778 | else |
Daniel Dunbar | f7fa9dc | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4779 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4780 | return true; |
| 4781 | } |
| 4782 | |
| 4783 | |
| 4784 | |
| 4785 | // C99 6.5.16.1 |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4786 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4787 | SourceLocation Loc, |
| 4788 | QualType CompoundType) { |
| 4789 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4790 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 4c2642c | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4791 | return QualType(); |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4792 | |
| 4793 | QualType LHSType = LHS->getType(); |
| 4794 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4795 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4796 | AssignConvertType ConvTy; |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4797 | if (CompoundType.isNull()) { |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4798 | // Simple assignment "x = y". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4799 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4800 | // Special case of NSObject attributes on c-style pointer types. |
| 4801 | if (ConvTy == IncompatiblePointer && |
| 4802 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | ad75bd2 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4803 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4804 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | ad75bd2 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4805 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 82f5496 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4806 | ConvTy = Compatible; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4807 | |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4808 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 4809 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 4810 | // instead of "x += 4". |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4811 | Expr *RHSCheck = RHS; |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4812 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 4813 | RHSCheck = ICE->getSubExpr(); |
| 4814 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 4815 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 4816 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4817 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4818 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4819 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 4820 | // And there is a space or other character before the subexpr of the |
| 4821 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | f1e5d4a | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 4822 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 4823 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4824 | Diag(Loc, diag::warn_not_compound_assign) |
| 4825 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 4826 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 55a1724 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4827 | } |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4828 | } |
| 4829 | } else { |
| 4830 | // Compound assignment "x += y" |
Eli Friedman | b653af4 | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 4831 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 34c8508 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4832 | } |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4833 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4834 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 4835 | RHS, "assigning")) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4836 | return QualType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4837 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4838 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 4839 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4840 | // it is the unqualified version of the type of the left operand. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4841 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 4842 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4843 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 6fcf2ca | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4844 | // operand. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4845 | return LHSType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4846 | } |
| 4847 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4848 | // C99 6.5.17 |
| 4849 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 03c430f | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 4850 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4851 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4852 | |
| 4853 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 4854 | // incomplete in C++). |
| 4855 | |
Chris Lattner | 1eafdea | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4856 | return RHS->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
| 4859 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 4860 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4861 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 4862 | bool isInc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4863 | if (Op->isTypeDependent()) |
| 4864 | return Context.DependentTy; |
| 4865 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4866 | QualType ResType = Op->getType(); |
| 4867 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4868 | |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4869 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 4870 | // Decrement of bool is not allowed. |
| 4871 | if (!isInc) { |
| 4872 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 4873 | return QualType(); |
| 4874 | } |
| 4875 | // Increment of bool sets it to true, but is deprecated. |
| 4876 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 4877 | } else if (ResType->isRealType()) { |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4878 | // OK! |
Steve Naroff | 79ae19a | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4879 | } else if (ResType->isAnyPointerType()) { |
| 4880 | QualType PointeeTy = ResType->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4881 | |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4882 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4883 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4884 | if (getLangOptions().CPlusPlus) { |
| 4885 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 4886 | << Op->getSourceRange(); |
| 4887 | return QualType(); |
| 4888 | } |
| 4889 | |
| 4890 | // Pointer to void is a GNU extension in C. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4891 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4892 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | b319324 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4893 | if (getLangOptions().CPlusPlus) { |
| 4894 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 4895 | << Op->getType() << Op->getSourceRange(); |
| 4896 | return QualType(); |
| 4897 | } |
| 4898 | |
| 4899 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4900 | << ResType << Op->getSourceRange(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4901 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | b5247af | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4902 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4903 | << Op->getSourceRange() |
| 4904 | << ResType)) |
Douglas Gregor | 46fe06e | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4905 | return QualType(); |
Fariborz Jahanian | 4738ac5 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 4906 | // Diagnose bad cases where we step over interface counts. |
| 4907 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4908 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 4909 | << PointeeTy << Op->getSourceRange(); |
| 4910 | return QualType(); |
| 4911 | } |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4912 | } else if (ResType->isComplexType()) { |
| 4913 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 4914 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4915 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4916 | } else { |
| 4917 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4918 | << ResType << Op->getSourceRange(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4919 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4920 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4921 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 4922 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4923 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4924 | return QualType(); |
Chris Lattner | e65182c | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4925 | return ResType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4926 | } |
| 4927 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4928 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4929 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4930 | /// where the declaration is needed for type checking. We only need to |
| 4931 | /// handle cases when the expression references a function designator |
| 4932 | /// or is an lvalue. Here are some examples: |
| 4933 | /// - &(x) => x |
| 4934 | /// - &*****f => f for f a function designator. |
| 4935 | /// - &s.xx => s |
| 4936 | /// - &s.zz[1].yy -> s, if zz is an array |
| 4937 | /// - *(x + 1) -> x, if x is an array |
| 4938 | /// - &"123"[2] -> 0 |
| 4939 | /// - & __real__ x -> x |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4940 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4941 | switch (E->getStmtClass()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4942 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 566782a | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 4943 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4944 | return cast<DeclRefExpr>(E)->getDecl(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4945 | case Stmt::MemberExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4946 | // If this is an arrow operator, the address is an offset from |
| 4947 | // the base's value, so the object the base refers to is |
| 4948 | // irrelevant. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4949 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4950 | return 0; |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4951 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4952 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4953 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4954 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 4955 | // promotion of register arrays earlier. |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4956 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 4957 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 4958 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 4959 | return getPrimaryDecl(ICE->getSubExpr()); |
| 4960 | } |
| 4961 | return 0; |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4962 | } |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4963 | case Stmt::UnaryOperatorClass: { |
| 4964 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4965 | |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4966 | switch(UO->getOpcode()) { |
Daniel Dunbar | b45f75c | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4967 | case UnaryOperator::Real: |
| 4968 | case UnaryOperator::Imag: |
| 4969 | case UnaryOperator::Extension: |
| 4970 | return getPrimaryDecl(UO->getSubExpr()); |
| 4971 | default: |
| 4972 | return 0; |
| 4973 | } |
| 4974 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4975 | case Stmt::ParenExprClass: |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4976 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4977 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4978 | // If the result of an implicit cast is an l-value, we care about |
| 4979 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 48d7f38 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4980 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4981 | default: |
| 4982 | return 0; |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4987 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4988 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4989 | /// Note: The usual conversions are *not* applied to the operand of the & |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4990 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4991 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4992 | /// we allow the '&' but retain the overloaded-function type. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 4993 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4994 | // Make sure to ignore parentheses in subsequent checks |
| 4995 | op = op->IgnoreParens(); |
| 4996 | |
Douglas Gregor | e6be68a | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 4997 | if (op->isTypeDependent()) |
| 4998 | return Context.DependentTy; |
| 4999 | |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5000 | if (getLangOptions().C99) { |
| 5001 | // Implement C99-only parts of addressof rules. |
| 5002 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5003 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5004 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5005 | // (assuming the deref expression is valid). |
| 5006 | return uOp->getSubExpr()->getType(); |
| 5007 | } |
| 5008 | // Technically, there should be a check for array subscript |
| 5009 | // expressions here, but the result of one is always an lvalue anyway. |
| 5010 | } |
Douglas Gregor | d2baafd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5011 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 25168a5 | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5012 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 1a68ecf | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5013 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5014 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5015 | // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5016 | // The operand must be either an l-value or a function designator |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5017 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5018 | // FIXME: emit more specific diag... |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5019 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5020 | << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5021 | return QualType(); |
| 5022 | } |
Douglas Gregor | 531434b | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5023 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5024 | // The operand cannot be a bit-field |
| 5025 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5026 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 82d4477 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5027 | return QualType(); |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5028 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5029 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 93ecce2 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5030 | // The operand cannot be an element of a vector |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5031 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a9187ab | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5032 | << "vector element" << op->getSourceRange(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5033 | return QualType(); |
Fariborz Jahanian | b35984a | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5034 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5035 | // cannot take address of a property expression. |
| 5036 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5037 | << "property expression" << op->getSourceRange(); |
| 5038 | return QualType(); |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5039 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5040 | // We have an lvalue with a decl. Make sure the decl is not declared |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5041 | // with the register storage-class specifier. |
| 5042 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5043 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5044 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5045 | << "register variable" << op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5046 | return QualType(); |
| 5047 | } |
Douglas Gregor | 62f7876 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5048 | } else if (isa<OverloadedFunctionDecl>(dcl) || |
| 5049 | isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 45014fd | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5050 | return Context.OverloadTy; |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5051 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 5b82d61 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5052 | // Okay: we can take the address of a field. |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5053 | // Could be a pointer to member, though, if there is an explicit |
| 5054 | // scope qualifier for the class. |
| 5055 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 5056 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5057 | if (Ctx && Ctx->isRecord()) { |
| 5058 | if (FD->getType()->isReferenceType()) { |
| 5059 | Diag(OpLoc, |
| 5060 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5061 | << FD->getDeclName() << FD->getType(); |
| 5062 | return QualType(); |
| 5063 | } |
| 5064 | |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5065 | return Context.getMemberPointerType(op->getType(), |
| 5066 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 6437147 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5067 | } |
Sebastian Redl | 0c9da21 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5068 | } |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5069 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | df23952 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5070 | // Okay: we can take the address of a function. |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5071 | // As above. |
Anders Carlsson | e9cc4c4 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5072 | if (isa<QualifiedDeclRefExpr>(op) && MD->isInstance()) |
| 5073 | return Context.getMemberPointerType(op->getType(), |
| 5074 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5075 | } else if (!isa<FunctionDecl>(dcl)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5076 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5077 | } |
Sebastian Redl | 7434fc3 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5078 | |
Eli Friedman | 14ab4c4 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5079 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5080 | // Taking the address of a void variable is technically illegal, but we |
| 5081 | // allow it in cases which are otherwise valid. |
| 5082 | // Example: "extern void x; void* y = &x;". |
| 5083 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5084 | } |
| 5085 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5086 | // If the operand has type "type", the result has type "pointer to type". |
| 5087 | return Context.getPointerType(op->getType()); |
| 5088 | } |
| 5089 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5090 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5091 | if (Op->isTypeDependent()) |
| 5092 | return Context.DependentTy; |
| 5093 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5094 | UsualUnaryConversions(Op); |
| 5095 | QualType Ty = Op->getType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5096 | |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5097 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5098 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5099 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5100 | // unlikely to catch any mistakes. |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5101 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5102 | return PT->getPointeeType(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5103 | |
Fariborz Jahanian | 81699d9 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5104 | if (const ObjCObjectPointerType *OPT = Ty->getAsObjCObjectPointerType()) |
| 5105 | return OPT->getPointeeType(); |
Steve Naroff | 329ec22 | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5106 | |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5107 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | da5c087 | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5108 | << Ty << Op->getSourceRange(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5109 | return QualType(); |
| 5110 | } |
| 5111 | |
| 5112 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5113 | tok::TokenKind Kind) { |
| 5114 | BinaryOperator::Opcode Opc; |
| 5115 | switch (Kind) { |
| 5116 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5117 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5118 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5119 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5120 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5121 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5122 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5123 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5124 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5125 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5126 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5127 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5128 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5129 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5130 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5131 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5132 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5133 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5134 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5135 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5136 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5137 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5138 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5139 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5140 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5141 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5142 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5143 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5144 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5145 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5146 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5147 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5148 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5149 | } |
| 5150 | return Opc; |
| 5151 | } |
| 5152 | |
| 5153 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5154 | tok::TokenKind Kind) { |
| 5155 | UnaryOperator::Opcode Opc; |
| 5156 | switch (Kind) { |
| 5157 | default: assert(0 && "Unknown unary op!"); |
| 5158 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5159 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5160 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5161 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5162 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5163 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5164 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5165 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5166 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5167 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 5168 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 5169 | } |
| 5170 | return Opc; |
| 5171 | } |
| 5172 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5173 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5174 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5175 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5176 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5177 | unsigned Op, |
| 5178 | Expr *lhs, Expr *rhs) { |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5179 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5180 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5181 | // The following two variables are used for compound assignment operators |
| 5182 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5183 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5184 | |
| 5185 | switch (Opc) { |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5186 | case BinaryOperator::Assign: |
| 5187 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5188 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5189 | case BinaryOperator::PtrMemD: |
| 5190 | case BinaryOperator::PtrMemI: |
| 5191 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5192 | Opc == BinaryOperator::PtrMemI); |
| 5193 | break; |
| 5194 | case BinaryOperator::Mul: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5195 | case BinaryOperator::Div: |
| 5196 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5197 | break; |
| 5198 | case BinaryOperator::Rem: |
| 5199 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5200 | break; |
| 5201 | case BinaryOperator::Add: |
| 5202 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5203 | break; |
| 5204 | case BinaryOperator::Sub: |
| 5205 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5206 | break; |
Sebastian Redl | 95216a6 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5207 | case BinaryOperator::Shl: |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5208 | case BinaryOperator::Shr: |
| 5209 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5210 | break; |
| 5211 | case BinaryOperator::LE: |
| 5212 | case BinaryOperator::LT: |
| 5213 | case BinaryOperator::GE: |
| 5214 | case BinaryOperator::GT: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5215 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5216 | break; |
| 5217 | case BinaryOperator::EQ: |
| 5218 | case BinaryOperator::NE: |
Douglas Gregor | 1f12c35 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5219 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5220 | break; |
| 5221 | case BinaryOperator::And: |
| 5222 | case BinaryOperator::Xor: |
| 5223 | case BinaryOperator::Or: |
| 5224 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5225 | break; |
| 5226 | case BinaryOperator::LAnd: |
| 5227 | case BinaryOperator::LOr: |
| 5228 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5229 | break; |
| 5230 | case BinaryOperator::MulAssign: |
| 5231 | case BinaryOperator::DivAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5232 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 5233 | CompLHSTy = CompResultTy; |
| 5234 | if (!CompResultTy.isNull()) |
| 5235 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5236 | break; |
| 5237 | case BinaryOperator::RemAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5238 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 5239 | CompLHSTy = CompResultTy; |
| 5240 | if (!CompResultTy.isNull()) |
| 5241 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5242 | break; |
| 5243 | case BinaryOperator::AddAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5244 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5245 | if (!CompResultTy.isNull()) |
| 5246 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5247 | break; |
| 5248 | case BinaryOperator::SubAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5249 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5250 | if (!CompResultTy.isNull()) |
| 5251 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5252 | break; |
| 5253 | case BinaryOperator::ShlAssign: |
| 5254 | case BinaryOperator::ShrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5255 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 5256 | CompLHSTy = CompResultTy; |
| 5257 | if (!CompResultTy.isNull()) |
| 5258 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5259 | break; |
| 5260 | case BinaryOperator::AndAssign: |
| 5261 | case BinaryOperator::XorAssign: |
| 5262 | case BinaryOperator::OrAssign: |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5263 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 5264 | CompLHSTy = CompResultTy; |
| 5265 | if (!CompResultTy.isNull()) |
| 5266 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5267 | break; |
| 5268 | case BinaryOperator::Comma: |
| 5269 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 5270 | break; |
| 5271 | } |
| 5272 | if (ResultTy.isNull()) |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5273 | return ExprError(); |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5274 | if (CompResultTy.isNull()) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5275 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 5276 | else |
| 5277 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | 3cd9288 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5278 | CompLHSTy, CompResultTy, |
| 5279 | OpLoc)); |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5282 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5283 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 5284 | tok::TokenKind Kind, |
| 5285 | ExprArg LHS, ExprArg RHS) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5286 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5287 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5288 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5289 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 5290 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5291 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5292 | if (getLangOptions().CPlusPlus && |
| 5293 | (lhs->getType()->isOverloadableType() || |
| 5294 | rhs->getType()->isOverloadableType())) { |
| 5295 | // Find all of the overloaded operators visible from this |
| 5296 | // point. We perform both an operator-name lookup from the local |
| 5297 | // scope and an argument-dependent lookup based on the types of |
| 5298 | // the arguments. |
Douglas Gregor | 3fc092f | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5299 | FunctionSet Functions; |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5300 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 5301 | if (OverOp != OO_None) { |
| 5302 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 5303 | Functions); |
| 5304 | Expr *Args[2] = { lhs, rhs }; |
| 5305 | DeclarationName OpName |
| 5306 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5307 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | 70d2612 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5308 | } |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5309 | |
Douglas Gregor | 00fe3f6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5310 | // Build the (potentially-overloaded, potentially-dependent) |
| 5311 | // binary operation. |
| 5312 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | 5457c5e | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5313 | } |
| 5314 | |
Douglas Gregor | d7f915e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5315 | // Build a built-in binary operation. |
| 5316 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5319 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| 5320 | unsigned OpcIn, |
| 5321 | ExprArg InputArg) { |
| 5322 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5323 | |
Mike Stump | e127ae3 | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5324 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5325 | Expr *Input = (Expr *)InputArg.get(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5326 | QualType resultType; |
| 5327 | switch (Opc) { |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5328 | case UnaryOperator::OffsetOf: |
| 5329 | assert(false && "Invalid unary operator"); |
| 5330 | break; |
| 5331 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5332 | case UnaryOperator::PreInc: |
| 5333 | case UnaryOperator::PreDec: |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5334 | case UnaryOperator::PostInc: |
| 5335 | case UnaryOperator::PostDec: |
Sebastian Redl | 0440c8c | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5336 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | 7934114 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5337 | Opc == UnaryOperator::PreInc || |
| 5338 | Opc == UnaryOperator::PostInc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5339 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5340 | case UnaryOperator::AddrOf: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5341 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 5342 | break; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5343 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 5344 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5345 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 5346 | break; |
| 5347 | case UnaryOperator::Plus: |
| 5348 | case UnaryOperator::Minus: |
| 5349 | UsualUnaryConversions(Input); |
| 5350 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5351 | if (resultType->isDependentType()) |
| 5352 | break; |
Douglas Gregor | 4f6904d | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5353 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 5354 | break; |
| 5355 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 5356 | resultType->isEnumeralType()) |
| 5357 | break; |
| 5358 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 5359 | Opc == UnaryOperator::Plus && |
| 5360 | resultType->isPointerType()) |
| 5361 | break; |
| 5362 | |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5363 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5364 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5365 | case UnaryOperator::Not: // bitwise complement |
| 5366 | UsualUnaryConversions(Input); |
| 5367 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5368 | if (resultType->isDependentType()) |
| 5369 | break; |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5370 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 5371 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 5372 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 77d52da | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5373 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | 4bfd223 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5374 | << resultType << Input->getSourceRange(); |
Chris Lattner | bd69502 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5375 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5376 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5377 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5378 | break; |
| 5379 | case UnaryOperator::LNot: // logical negation |
| 5380 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 5381 | DefaultFunctionArrayConversion(Input); |
| 5382 | resultType = Input->getType(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5383 | if (resultType->isDependentType()) |
| 5384 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5385 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5386 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5387 | << resultType << Input->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5388 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5389 | // In C++, it's bool. C++ 5.3.1p8 |
| 5390 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5391 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5392 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5393 | case UnaryOperator::Imag: |
Chris Lattner | 57e5f7e | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 5394 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5395 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5396 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5397 | resultType = Input->getType(); |
| 5398 | break; |
| 5399 | } |
| 5400 | if (resultType.isNull()) |
Sebastian Redl | 8b76997 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5401 | return ExprError(); |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5402 | |
| 5403 | InputArg.release(); |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5404 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
Douglas Gregor | c78182d | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5407 | // Unary Operators. 'Tok' is the token for the operator. |
| 5408 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5409 | tok::TokenKind Op, ExprArg input) { |
| 5410 | Expr *Input = (Expr*)input.get(); |
| 5411 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 5412 | |
| 5413 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 5414 | // Find all of the overloaded operators visible from this |
| 5415 | // point. We perform both an operator-name lookup from the local |
| 5416 | // scope and an argument-dependent lookup based on the types of |
| 5417 | // the arguments. |
| 5418 | FunctionSet Functions; |
| 5419 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 5420 | if (OverOp != OO_None) { |
| 5421 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 5422 | Functions); |
| 5423 | DeclarationName OpName |
| 5424 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5425 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 5426 | } |
| 5427 | |
| 5428 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 5429 | } |
| 5430 | |
| 5431 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 5432 | } |
| 5433 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 5434 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5435 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 5436 | SourceLocation LabLoc, |
| 5437 | IdentifierInfo *LabelII) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5438 | // Look up the record for this label identifier. |
Chris Lattner | 2616d8c | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 5439 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5440 | |
Daniel Dunbar | 879788d | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 5441 | // If we haven't seen this label yet, create a forward reference. It |
| 5442 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | b88d81c | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 5443 | if (LabelDecl == 0) |
Steve Naroff | 774e415 | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5444 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5445 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5446 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5447 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 5448 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5449 | } |
| 5450 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5451 | Sema::OwningExprResult |
| 5452 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 5453 | SourceLocation RPLoc) { // "({..})" |
| 5454 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5455 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 5456 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 5457 | |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5458 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | aa25759 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 5459 | if (isFileScope) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5460 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | bc941e1 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5461 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5462 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 5463 | // example, it is not possible to goto into a stmt expression apparently. |
| 5464 | // More semantic analysis is needed. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5466 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 5467 | // as the type of the stmtexpr. |
| 5468 | QualType Ty = Context.VoidTy; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5469 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5470 | if (!Compound->body_empty()) { |
| 5471 | Stmt *LastStmt = Compound->body_back(); |
| 5472 | // If LastStmt is a label, skip down through into the body. |
| 5473 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 5474 | LastStmt = Label->getSubStmt(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5475 | |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5476 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5477 | Ty = LastExpr->getType(); |
Chris Lattner | 200964f | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5478 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5479 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5480 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 5481 | // expressions are not lvalues. |
| 5482 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5483 | substmt.release(); |
| 5484 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 5485 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5486 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5487 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 5488 | SourceLocation BuiltinLoc, |
| 5489 | SourceLocation TypeLoc, |
| 5490 | TypeTy *argty, |
| 5491 | OffsetOfComponent *CompPtr, |
| 5492 | unsigned NumComponents, |
| 5493 | SourceLocation RPLoc) { |
| 5494 | // FIXME: This function leaks all expressions in the offset components on |
| 5495 | // error. |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5496 | // FIXME: Preserve type source info. |
| 5497 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5498 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5499 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5500 | bool Dependent = ArgTy->isDependentType(); |
| 5501 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5502 | // We must have at least one component that refers to the type, and the first |
| 5503 | // one is known to be a field designator. Verify that the ArgTy represents |
| 5504 | // a struct/union/class. |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5505 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5506 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5507 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5508 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 5509 | // with an incomplete type would be illegal. |
Douglas Gregor | 6e7c27c | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 5510 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5511 | // Otherwise, create a null pointer as the base, and iteratively process |
| 5512 | // the offsetof designators. |
| 5513 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 5514 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5515 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5516 | ArgTy, SourceLocation()); |
Eli Friedman | c67f86a | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 5517 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5518 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 5519 | // GCC extension, diagnose them. |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5520 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 5521 | // a system header! |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5522 | if (NumComponents != 1) |
Chris Lattner | 9d2cf08 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5523 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 5524 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5525 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5526 | if (!Dependent) { |
Eli Friedman | c24ae00 | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 5527 | bool DidWarnAboutNonPOD = false; |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5528 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5529 | // FIXME: Dependent case loses a lot of information here. And probably |
| 5530 | // leaks like a sieve. |
| 5531 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 5532 | const OffsetOfComponent &OC = CompPtr[i]; |
| 5533 | if (OC.isBrackets) { |
| 5534 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 5535 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 5536 | if (!AT) { |
| 5537 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5538 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 5539 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 5543 | |
Eli Friedman | 342d943 | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5544 | // Promote the array so it looks more like a normal array subscript |
| 5545 | // expression. |
| 5546 | DefaultFunctionArrayConversion(Res); |
| 5547 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5548 | // C99 6.5.2.1p1 |
| 5549 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5550 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5551 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5552 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 7264d21 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5553 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5554 | << Idx->getSourceRange()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5555 | |
| 5556 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5557 | OC.LocEnd); |
| 5558 | continue; |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5559 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5560 | |
Ted Kremenek | d00cd9e | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5561 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5562 | if (!RC) { |
| 5563 | Res->Destroy(Context); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5564 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5565 | << Res->getType()); |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5566 | } |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5567 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5568 | // Get the decl corresponding to this. |
| 5569 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5570 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5571 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | bbceaea | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5572 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5573 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5574 | << Res->getType()); |
Anders Carlsson | 68c926c | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5575 | DidWarnAboutNonPOD = true; |
| 5576 | } |
Anders Carlsson | 356946e | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5577 | } |
| 5578 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5579 | FieldDecl *MemberDecl |
| 5580 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 5581 | LookupMemberName) |
| 5582 | .getAsDecl()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5583 | // FIXME: Leaks Res |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5584 | if (!MemberDecl) |
Anders Carlsson | 4355a39 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 5585 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member_deprecated) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5586 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5587 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5588 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5589 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5590 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | c154a72 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5591 | Res = BuildAnonymousStructUnionMemberReference( |
| 5592 | SourceLocation(), MemberDecl, Res, SourceLocation()).takeAs<Expr>(); |
Eli Friedman | 35719da | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5593 | } else { |
| 5594 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5595 | // doesn't matter here. |
| 5596 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5597 | MemberDecl->getType().getNonReferenceType()); |
| 5598 | } |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5599 | } |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5600 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5601 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5602 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5603 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5604 | } |
| 5605 | |
| 5606 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5607 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5608 | TypeTy *arg1,TypeTy *arg2, |
| 5609 | SourceLocation RPLoc) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5610 | // FIXME: Preserve type source info. |
| 5611 | QualType argT1 = GetTypeFromParser(arg1); |
| 5612 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5613 | |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5614 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5615 | |
Douglas Gregor | e621150 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5616 | if (getLangOptions().CPlusPlus) { |
| 5617 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5618 | << SourceRange(BuiltinLoc, RPLoc); |
| 5619 | return ExprError(); |
| 5620 | } |
| 5621 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5622 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5623 | argT1, argT2, RPLoc)); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5626 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5627 | ExprArg cond, |
| 5628 | ExprArg expr1, ExprArg expr2, |
| 5629 | SourceLocation RPLoc) { |
| 5630 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5631 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5632 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5633 | |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5634 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5635 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5636 | QualType resType; |
Douglas Gregor | dd4ae3f | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5637 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5638 | resType = Context.DependentTy; |
| 5639 | } else { |
| 5640 | // The conditional expression is required to be a constant expression. |
| 5641 | llvm::APSInt condEval(32); |
| 5642 | SourceLocation ExpLoc; |
| 5643 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5644 | return ExprError(Diag(ExpLoc, |
| 5645 | diag::err_typecheck_choose_expr_requires_constant) |
| 5646 | << CondExpr->getSourceRange()); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5647 | |
Sebastian Redl | 6fdb28d | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5648 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5649 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 5650 | } |
| 5651 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5652 | cond.release(); expr1.release(); expr2.release(); |
| 5653 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 5654 | resType, RPLoc)); |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5655 | } |
| 5656 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5657 | //===----------------------------------------------------------------------===// |
| 5658 | // Clang Extensions. |
| 5659 | //===----------------------------------------------------------------------===// |
| 5660 | |
| 5661 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5662 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5663 | // Analyze block parameters. |
| 5664 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5665 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5666 | // Add BSI to CurBlock. |
| 5667 | BSI->PrevBlockInfo = CurBlock; |
| 5668 | CurBlock = BSI; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5669 | |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5670 | BSI->ReturnType = QualType(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5671 | BSI->TheScope = BlockScope; |
Mike Stump | ae93d65 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5672 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | c7ef2b9 | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 5673 | BSI->hasPrototype = false; |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5674 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5675 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5676 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5677 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 8acb727 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5678 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5679 | } |
| 5680 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5681 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | ea3d74e | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5682 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5683 | |
| 5684 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5685 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5686 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5687 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5688 | |
Mike Stump | 458287d | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5689 | if (T->isArrayType()) { |
| 5690 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5691 | diag::err_block_returns_array); |
| 5692 | return; |
| 5693 | } |
| 5694 | |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5695 | // The parameter list is optional, if there was none, assume (). |
| 5696 | if (!T->isFunctionType()) |
| 5697 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 5698 | |
| 5699 | CurBlock->hasPrototype = true; |
| 5700 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5701 | // Check for a valid sentinel attribute on this block. |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5702 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5703 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5704 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5705 | // FIXME: remove the attribute. |
| 5706 | } |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5707 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
| 5708 | |
| 5709 | // Do not allow returning a objc interface by-value. |
| 5710 | if (RetTy->isObjCInterfaceType()) { |
| 5711 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5712 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5713 | return; |
| 5714 | } |
Mike Stump | c1fddff | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5715 | return; |
| 5716 | } |
| 5717 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5718 | // Analyze arguments to block. |
| 5719 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 5720 | "Not a function declarator!"); |
| 5721 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5722 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5723 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 5724 | CurBlock->isVariadic = true; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5725 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5726 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 5727 | // no arguments, not a function that takes a single void argument. |
| 5728 | if (FTI.hasPrototype && |
| 5729 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5730 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 5731 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5732 | // empty arg list, don't push any params. |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5733 | CurBlock->isVariadic = false; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5734 | } else if (FTI.hasPrototype) { |
| 5735 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | 5261d0c | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5736 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5737 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5738 | } |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5739 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5740 | CurBlock->Params.size()); |
Fariborz Jahanian | 536f73d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 5741 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 2a2e040 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5742 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5743 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 5744 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 5745 | // If this has an identifier, add it to the scope stack. |
| 5746 | if ((*AI)->getIdentifier()) |
| 5747 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5748 | |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5749 | // Check for a valid sentinel attribute on this block. |
Douglas Gregor | 98da6ae | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 5750 | if (!CurBlock->isVariadic && |
Argiris Kirtzidis | fe5f973 | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5751 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5752 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 6dac16d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5753 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 157c426 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5754 | // FIXME: remove the attribute. |
| 5755 | } |
| 5756 | |
Chris Lattner | c65b8bb | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5757 | // Analyze the return type. |
| 5758 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5759 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
| 5760 | |
| 5761 | // Do not allow returning a objc interface by-value. |
| 5762 | if (RetTy->isObjCInterfaceType()) { |
| 5763 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5764 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5765 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5766 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5767 | } |
| 5768 | |
| 5769 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 5770 | /// is invoked to pop the information about the block from the action impl. |
| 5771 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 5772 | // Ensure that CurBlock is deleted. |
| 5773 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5774 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5775 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 5776 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5777 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | eb4d4a5 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 5778 | PopDeclContext(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5779 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5780 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 5784 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5785 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 5786 | StmtArg body, Scope *CurScope) { |
Chris Lattner | c14c7f0 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5787 | // If blocks are disabled, emit an error. |
| 5788 | if (!LangOpts.Blocks) |
| 5789 | Diag(CaretLoc, diag::err_blocks_disable); |
| 5790 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5791 | // Ensure that CurBlock is deleted. |
| 5792 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5793 | |
Steve Naroff | 5205938 | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5794 | PopDeclContext(); |
| 5795 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5796 | // Pop off CurBlock, handle nested blocks. |
| 5797 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5798 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5799 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 89942a0 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5800 | if (!BSI->ReturnType.isNull()) |
| 5801 | RetTy = BSI->ReturnType; |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5802 | |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5803 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 5804 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 5805 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5806 | |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5807 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5808 | QualType BlockTy; |
| 5809 | if (!BSI->hasPrototype) |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5810 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 5811 | NoReturn); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5812 | else |
Jay Foad | 9e6bef4 | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5813 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5814 | BSI->isVariadic, 0, false, false, 0, 0, |
| 5815 | NoReturn); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5816 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5817 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5818 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5819 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5820 | |
Chris Lattner | e7765e1 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5821 | // If needed, diagnose invalid gotos and switches in the block. |
| 5822 | if (CurFunctionNeedsScopeChecking) |
| 5823 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 5824 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
| 5825 | |
Anders Carlsson | 39ecdcf | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5826 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 8e288f4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5827 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5828 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 5829 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 52a81c0 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5832 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 5833 | ExprArg expr, TypeTy *type, |
| 5834 | SourceLocation RPLoc) { |
Argiris Kirtzidis | d6802ba | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5835 | QualType T = GetTypeFromParser(type); |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5836 | Expr *E = static_cast<Expr*>(expr.get()); |
| 5837 | Expr *OrigExpr = E; |
| 5838 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5839 | InitBuiltinVaListType(); |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5840 | |
| 5841 | // Get the va_list type |
| 5842 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5843 | if (VaListType->isArrayType()) { |
| 5844 | // Deal with implicit array decay; for example, on x86-64, |
| 5845 | // va_list is an array, but it's supposed to decay to |
| 5846 | // a pointer for va_arg. |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5847 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5848 | // Make sure the input expression also decays appropriately. |
| 5849 | UsualUnaryConversions(E); |
| 5850 | } else { |
| 5851 | // Otherwise, the va_list argument must be an l-value because |
| 5852 | // it is modified by va_arg. |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5853 | if (!E->isTypeDependent() && |
| 5854 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 6f6e892 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5855 | return ExprError(); |
| 5856 | } |
Eli Friedman | dd2b9af | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5857 | |
Douglas Gregor | 2599097 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5858 | if (!E->isTypeDependent() && |
| 5859 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5860 | return ExprError(Diag(E->getLocStart(), |
| 5861 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | da13948 | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5862 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 89a72c5 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 5863 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5864 | |
Eli Friedman | 2b12832 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5865 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5866 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5867 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5868 | expr.release(); |
| 5869 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 5870 | RPLoc)); |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5871 | } |
| 5872 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5873 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5874 | // The type of __null will be int or long, depending on the size of |
| 5875 | // pointers on the target. |
| 5876 | QualType Ty; |
| 5877 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 5878 | Ty = Context.IntTy; |
| 5879 | else |
| 5880 | Ty = Context.LongTy; |
| 5881 | |
Sebastian Redl | 76bb8ec | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5882 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | ad4b379 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5883 | } |
| 5884 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5885 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 5886 | SourceLocation Loc, |
| 5887 | QualType DstType, QualType SrcType, |
| 5888 | Expr *SrcExpr, const char *Flavor) { |
| 5889 | // Decode the result (notice that AST's are still created for extensions). |
| 5890 | bool isInvalid = false; |
| 5891 | unsigned DiagKind; |
| 5892 | switch (ConvTy) { |
| 5893 | default: assert(0 && "Unknown conversion type"); |
| 5894 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5895 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5896 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 5897 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5898 | case IntToPointer: |
| 5899 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 5900 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5901 | case IncompatiblePointer: |
| 5902 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 5903 | break; |
Eli Friedman | 6ca28cb | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5904 | case IncompatiblePointerSign: |
| 5905 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 5906 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5907 | case FunctionVoidPointer: |
| 5908 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 5909 | break; |
| 5910 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 1815b3b | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 5911 | // If the qualifiers lost were because we were applying the |
| 5912 | // (deprecated) C++ conversion from a string literal to a char* |
| 5913 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 5914 | // Ideally, this check would be performed in |
| 5915 | // CheckPointerTypesForAssignment. However, that would require a |
| 5916 | // bit of refactoring (so that the second argument is an |
| 5917 | // expression, rather than a type), which should be done as part |
| 5918 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 5919 | // C++ semantics. |
| 5920 | if (getLangOptions().CPlusPlus && |
| 5921 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 5922 | return false; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5923 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 5924 | break; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5925 | case IntToBlockPointer: |
| 5926 | DiagKind = diag::err_int_to_block_pointer; |
| 5927 | break; |
| 5928 | case IncompatibleBlockPointer: |
Mike Stump | d331e75 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 5929 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 3454b6c | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5930 | break; |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5931 | case IncompatibleObjCQualifiedId: |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5932 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 1960843 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5933 | // it can give a more specific diagnostic. |
| 5934 | DiagKind = diag::warn_incompatible_qualified_id; |
| 5935 | break; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5936 | case IncompatibleVectors: |
| 5937 | DiagKind = diag::warn_incompatible_vectors; |
| 5938 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5939 | case Incompatible: |
| 5940 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 5941 | isInvalid = true; |
| 5942 | break; |
| 5943 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5944 | |
Chris Lattner | 271d4c2 | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5945 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 5946 | << SrcExpr->getSourceRange(); |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5947 | return isInvalid; |
| 5948 | } |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5949 | |
Chris Lattner | eec8ae2 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 5950 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5951 | llvm::APSInt ICEResult; |
| 5952 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 5953 | if (Result) |
| 5954 | *Result = ICEResult; |
| 5955 | return false; |
| 5956 | } |
| 5957 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5958 | Expr::EvalResult EvalResult; |
| 5959 | |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5960 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5961 | EvalResult.HasSideEffects) { |
| 5962 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 5963 | |
| 5964 | if (EvalResult.Diag) { |
| 5965 | // We only show the note if it's not the usual "invalid subexpression" |
| 5966 | // or if it's actually in a subexpression. |
| 5967 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 5968 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 5969 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 5970 | } |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5971 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5972 | return true; |
| 5973 | } |
| 5974 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5975 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 5976 | E->getSourceRange(); |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5977 | |
Eli Friedman | ce32941 | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5978 | if (EvalResult.Diag && |
| 5979 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 5980 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 9afab10 | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5981 | |
Anders Carlsson | d5201b9 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5982 | if (Result) |
| 5983 | *Result = EvalResult.Val.getInt(); |
| 5984 | return false; |
| 5985 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5986 | |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5987 | Sema::ExpressionEvaluationContext |
| 5988 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
| 5989 | // Introduce a new set of potentially referenced declarations to the stack. |
| 5990 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 5991 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
| 5992 | |
| 5993 | std::swap(ExprEvalContext, NewContext); |
| 5994 | return NewContext; |
| 5995 | } |
| 5996 | |
| 5997 | void |
| 5998 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 5999 | ExpressionEvaluationContext NewContext) { |
| 6000 | ExprEvalContext = NewContext; |
| 6001 | |
| 6002 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 6003 | // Mark any remaining declarations in the current position of the stack |
| 6004 | // as "referenced". If they were not meant to be referenced, semantic |
| 6005 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6006 | PotentiallyReferencedDecls RemainingDecls; |
| 6007 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 6008 | PotentiallyReferencedDeclStack.pop_back(); |
| 6009 | |
| 6010 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 6011 | IEnd = RemainingDecls.end(); |
| 6012 | I != IEnd; ++I) |
| 6013 | MarkDeclarationReferenced(I->first, I->second); |
| 6014 | } |
| 6015 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6016 | |
| 6017 | /// \brief Note that the given declaration was referenced in the source code. |
| 6018 | /// |
| 6019 | /// This routine should be invoke whenever a given declaration is referenced |
| 6020 | /// in the source code, and where that reference occurred. If this declaration |
| 6021 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 6022 | /// C99 6.9p3), then the declaration will be marked as used. |
| 6023 | /// |
| 6024 | /// \param Loc the location where the declaration was referenced. |
| 6025 | /// |
| 6026 | /// \param D the declaration that has been referenced by the source code. |
| 6027 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 6028 | assert(D && "No declaration?"); |
| 6029 | |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6030 | if (D->isUsed()) |
| 6031 | return; |
| 6032 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6033 | // Mark a parameter declaration "used", regardless of whether we're in a |
| 6034 | // template or not. |
| 6035 | if (isa<ParmVarDecl>(D)) |
| 6036 | D->setUsed(true); |
| 6037 | |
| 6038 | // Do not mark anything as "used" within a dependent context; wait for |
| 6039 | // an instantiation. |
| 6040 | if (CurContext->isDependentContext()) |
| 6041 | return; |
| 6042 | |
Douglas Gregor | a8b2fbf | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6043 | switch (ExprEvalContext) { |
| 6044 | case Unevaluated: |
| 6045 | // We are in an expression that is not potentially evaluated; do nothing. |
| 6046 | return; |
| 6047 | |
| 6048 | case PotentiallyEvaluated: |
| 6049 | // We are in a potentially-evaluated expression, so this declaration is |
| 6050 | // "used"; handle this below. |
| 6051 | break; |
| 6052 | |
| 6053 | case PotentiallyPotentiallyEvaluated: |
| 6054 | // We are in an expression that may be potentially evaluated; queue this |
| 6055 | // declaration reference until we know whether the expression is |
| 6056 | // potentially evaluated. |
| 6057 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 6058 | return; |
| 6059 | } |
| 6060 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6061 | // Note that this declaration has been used. |
Fariborz Jahanian | 8915a3d | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 6062 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6063 | unsigned TypeQuals; |
Fariborz Jahanian | 2f5a0a3 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6064 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 6065 | if (!Constructor->isUsed()) |
| 6066 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 90fc78e | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6067 | } else if (Constructor->isImplicit() && |
| 6068 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 599778e | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6069 | if (!Constructor->isUsed()) |
| 6070 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 6071 | } |
Fariborz Jahanian | 368cc6c | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6072 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 6073 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 6074 | DefineImplicitDestructor(Loc, Destructor); |
| 6075 | |
Fariborz Jahanian | d67364c | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6076 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 6077 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 6078 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 6079 | if (!MethodDecl->isUsed()) |
| 6080 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 6081 | } |
| 6082 | } |
Fariborz Jahanian | b12bd43 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 6083 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6084 | // Implicit instantiation of function templates and member functions of |
| 6085 | // class templates. |
Argiris Kirtzidis | ccb9efe | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 6086 | if (!Function->getBody()) { |
Douglas Gregor | 6f5e054 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6087 | // FIXME: distinguish between implicit instantiations of function |
| 6088 | // templates and explicit specializations (the latter don't get |
| 6089 | // instantiated, naturally). |
| 6090 | if (Function->getInstantiatedFromMemberFunction() || |
| 6091 | Function->getPrimaryTemplate()) |
Douglas Gregor | dcdb384 | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 6092 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
| 6095 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6096 | // FIXME: keep track of references to static functions |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6097 | Function->setUsed(true); |
| 6098 | return; |
Douglas Gregor | cad27f6 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6099 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6100 | |
| 6101 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6102 | // Implicit instantiation of static data members of class templates. |
| 6103 | // FIXME: distinguish between implicit instantiations (which we need to |
| 6104 | // actually instantiate) and explicit specializations. |
| 6105 | if (Var->isStaticDataMember() && |
| 6106 | Var->getInstantiatedFromStaticDataMember()) |
| 6107 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
| 6108 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6109 | // FIXME: keep track of references to static data? |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6110 | |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6111 | D->setUsed(true); |
Douglas Gregor | 181fe79 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6112 | return; |
| 6113 | } |
Douglas Gregor | 9818926 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6114 | } |
| 6115 | |