Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 19 | #include "clang/AST/ExprObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 20 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 23 | #include "clang/Lex/LiteralSupport.h" |
| 24 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 25 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 26 | #include "clang/Parse/Designator.h" |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 27 | #include "clang/Parse/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 48f3bb9 | 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 | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 43 | // See if the decl is deprecated. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 44 | if (D->getAttr<DeprecatedAttr>()) { |
Douglas Gregor | 48f3bb9 | 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 | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 48 | bool isSilenced = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 50 | if (NamedDecl *ND = getCurFunctionOrMethodDecl()) { |
| 51 | // If this reference happens *in* a deprecated function or method, don't |
| 52 | // warn. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 53 | isSilenced = ND->getAttr<DeprecatedAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 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())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 63 | MD = Impl->getClassInterface()->getMethod(MD->getSelector(), |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 64 | MD->isInstanceMethod()); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 65 | isSilenced |= MD && MD->getAttr<DeprecatedAttr>(); |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | f15970c | 2009-02-16 19:35:30 +0000 | [diff] [blame] | 70 | if (!isSilenced) |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 71 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 72 | } |
| 73 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 74 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 75 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 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 | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 81 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 82 | |
| 83 | // See if the decl is unavailable |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 84 | if (D->getAttr<UnavailableAttr>()) { |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 85 | Diag(Loc, diag::warn_unavailable) << D->getDeclName(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 86 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 87 | } |
| 88 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 92 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 93 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 94 | /// attribute. It warns if call does not have the sentinel argument. |
| 95 | /// |
| 96 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 98 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 100 | return; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 101 | int sentinelPos = attr->getSentinel(); |
| 102 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 104 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 105 | // base class. Then we won't be needing two versions of the same code. |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 106 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 107 | bool warnNotEnoughArgs = false; |
| 108 | int isMethod = 0; |
| 109 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 110 | // skip over named parameters. |
| 111 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 112 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 113 | if (nullPos) |
| 114 | --nullPos; |
| 115 | else |
| 116 | ++i; |
| 117 | } |
| 118 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 119 | isMethod = 1; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 120 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 121 | // skip over named parameters. |
| 122 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 123 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 124 | if (nullPos) |
| 125 | --nullPos; |
| 126 | else |
| 127 | ++i; |
| 128 | } |
| 129 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 130 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 131 | // block or function pointer call. |
| 132 | QualType Ty = V->getType(); |
| 133 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | const FunctionType *FT = Ty->isFunctionPointerType() |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 135 | ? Ty->getAs<PointerType>()->getPointeeType()->getAsFunctionType() |
| 136 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAsFunctionType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 137 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 138 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 139 | unsigned k; |
| 140 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 141 | if (nullPos) |
| 142 | --nullPos; |
| 143 | else |
| 144 | ++i; |
| 145 | } |
| 146 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 147 | } |
| 148 | if (Ty->isBlockPointerType()) |
| 149 | isMethod = 2; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 150 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 151 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 152 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 153 | return; |
| 154 | |
| 155 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 156 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 157 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | int sentinel = i; |
| 161 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 162 | --sentinelPos; |
| 163 | ++i; |
| 164 | } |
| 165 | if (sentinelPos > 0) { |
| 166 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 167 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | while (i < NumArgs-1) { |
| 171 | ++i; |
| 172 | ++sentinel; |
| 173 | } |
| 174 | Expr *sentinelExpr = Args[sentinel]; |
| 175 | if (sentinelExpr && (!sentinelExpr->getType()->isPointerType() || |
| 176 | !sentinelExpr->isNullPointerConstant(Context))) { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 177 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 178 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 179 | } |
| 180 | return; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 183 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 184 | Expr *Ex = (Expr *)E; |
| 185 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 186 | } |
| 187 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
| 189 | // Standard Promotions and Conversions |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 192 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 193 | void Sema::DefaultFunctionArrayConversion(Expr *&E) { |
| 194 | QualType Ty = E->getType(); |
| 195 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 196 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 197 | if (Ty->isFunctionType()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | ImpCastExprToType(E, Context.getPointerType(Ty), |
Anders Carlsson | b633c4e | 2009-09-01 20:37:18 +0000 | [diff] [blame] | 199 | CastExpr::CK_FunctionToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 200 | else if (Ty->isArrayType()) { |
| 201 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 202 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 203 | // type 'array of type' is converted to an expression that has type 'pointer |
| 204 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 205 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 206 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 207 | // |
| 208 | // C++ 4.2p1: |
| 209 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 210 | // T" can be converted to an rvalue of type "pointer to T". |
| 211 | // |
| 212 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || |
| 213 | E->isLvalue(Context) == Expr::LV_Valid) |
Anders Carlsson | 112a0a8 | 2009-08-07 23:48:20 +0000 | [diff] [blame] | 214 | ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 215 | CastExpr::CK_ArrayToPointerDecay); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 216 | } |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 221 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 222 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 223 | /// In these instances, this routine should *not* be called. |
| 224 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 225 | QualType Ty = Expr->getType(); |
| 226 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 228 | // C99 6.3.1.1p2: |
| 229 | // |
| 230 | // The following may be used in an expression wherever an int or |
| 231 | // unsigned int may be used: |
| 232 | // - an object or expression with an integer type whose integer |
| 233 | // conversion rank is less than or equal to the rank of int |
| 234 | // and unsigned int. |
| 235 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 236 | // |
| 237 | // If an int can represent all values of the original type, the |
| 238 | // value is converted to an int; otherwise, it is converted to an |
| 239 | // unsigned int. These are called the integer promotions. All |
| 240 | // other types are unchanged by the integer promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 241 | QualType PTy = Context.isPromotableBitField(Expr); |
| 242 | if (!PTy.isNull()) { |
| 243 | ImpCastExprToType(Expr, PTy); |
| 244 | return Expr; |
| 245 | } |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 246 | if (Ty->isPromotableIntegerType()) { |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 247 | QualType PT = Context.getPromotedIntegerType(Ty); |
| 248 | ImpCastExprToType(Expr, PT); |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 249 | return Expr; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Douglas Gregor | fc24e44 | 2009-05-01 20:41:21 +0000 | [diff] [blame] | 252 | DefaultFunctionArrayConversion(Expr); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 253 | return Expr; |
| 254 | } |
| 255 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 256 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | /// do not have a prototype. Arguments that have type float are promoted to |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 258 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| 259 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 260 | QualType Ty = Expr->getType(); |
| 261 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 263 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| 264 | if (const BuiltinType *BT = Ty->getAsBuiltinType()) |
| 265 | if (BT->getKind() == BuiltinType::Float) |
| 266 | return ImpCastExprToType(Expr, Context.DoubleTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 267 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 268 | UsualUnaryConversions(Expr); |
| 269 | } |
| 270 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 271 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 272 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| 273 | /// interfaces passed by value. This returns true if the argument type is |
| 274 | /// completely illegal. |
| 275 | bool Sema::DefaultVariadicArgumentPromotion(Expr *&Expr, VariadicCallType CT) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 276 | DefaultArgumentPromotion(Expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 278 | if (Expr->getType()->isObjCInterfaceType()) { |
| 279 | Diag(Expr->getLocStart(), |
| 280 | diag::err_cannot_pass_objc_interface_to_vararg) |
| 281 | << Expr->getType() << CT; |
| 282 | return true; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 283 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 285 | if (!Expr->getType()->isPODType()) |
| 286 | Diag(Expr->getLocStart(), diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 287 | << Expr->getType() << CT; |
| 288 | |
| 289 | return false; |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 293 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 294 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 296 | /// responsible for emitting appropriate error diagnostics. |
| 297 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 298 | /// GCC. |
| 299 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 300 | bool isCompAssign) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 301 | if (!isCompAssign) |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 302 | UsualUnaryConversions(lhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 303 | |
| 304 | UsualUnaryConversions(rhsExpr); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 305 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 307 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 308 | QualType lhs = |
| 309 | Context.getCanonicalType(lhsExpr->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 310 | QualType rhs = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 311 | Context.getCanonicalType(rhsExpr->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 312 | |
| 313 | // If both types are identical, no conversion is needed. |
| 314 | if (lhs == rhs) |
| 315 | return lhs; |
| 316 | |
| 317 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 318 | // The caller can deal with this (e.g. pointer + int). |
| 319 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 320 | return lhs; |
| 321 | |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 322 | // Perform bitfield promotions. |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 323 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 324 | if (!LHSBitfieldPromoteTy.isNull()) |
| 325 | lhs = LHSBitfieldPromoteTy; |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 326 | QualType RHSBitfieldPromoteTy = Context.isPromotableBitField(rhsExpr); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 327 | if (!RHSBitfieldPromoteTy.isNull()) |
| 328 | rhs = RHSBitfieldPromoteTy; |
| 329 | |
Eli Friedman | a95d757 | 2009-08-19 07:44:53 +0000 | [diff] [blame] | 330 | QualType destType = Context.UsualArithmeticConversionsType(lhs, rhs); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 331 | if (!isCompAssign) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 332 | ImpCastExprToType(lhsExpr, destType); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 333 | ImpCastExprToType(rhsExpr, destType); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 334 | return destType; |
| 335 | } |
| 336 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 337 | //===----------------------------------------------------------------------===// |
| 338 | // Semantic Analysis for various Expression Types |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
| 341 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 342 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 344 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 345 | /// multiple tokens. However, the common case is that StringToks points to one |
| 346 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 347 | /// |
| 348 | Action::OwningExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 349 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 350 | assert(NumStringToks && "Must have at least one string!"); |
| 351 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 352 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 353 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 354 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | |
| 356 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 357 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 358 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 359 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 360 | QualType StrTy = Context.CharTy; |
Argyrios Kyrtzidis | 55f4b02 | 2008-08-09 17:20:01 +0000 | [diff] [blame] | 361 | if (Literal.AnyWide) StrTy = Context.getWCharType(); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 362 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 363 | |
| 364 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 365 | if (getLangOptions().CPlusPlus) |
| 366 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 367 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 368 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 369 | // the nul terminator character as well as the string length for pascal |
| 370 | // strings. |
| 371 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 372 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 373 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 377 | Literal.GetStringLength(), |
| 378 | Literal.AnyWide, StrTy, |
| 379 | &StringTokLocs[0], |
| 380 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 383 | /// ShouldSnapshotBlockValueReference - Return true if a reference inside of |
| 384 | /// CurBlock to VD should cause it to be snapshotted (as we do for auto |
| 385 | /// variables defined outside the block) or false if this is not needed (e.g. |
| 386 | /// for values inside the block or for globals). |
| 387 | /// |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 388 | /// This also keeps the 'hasBlockDeclRefExprs' in the BlockSemaInfo records |
| 389 | /// up-to-date. |
| 390 | /// |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 391 | static bool ShouldSnapshotBlockValueReference(BlockSemaInfo *CurBlock, |
| 392 | ValueDecl *VD) { |
| 393 | // If the value is defined inside the block, we couldn't snapshot it even if |
| 394 | // we wanted to. |
| 395 | if (CurBlock->TheDecl == VD->getDeclContext()) |
| 396 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 397 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 398 | // If this is an enum constant or function, it is constant, don't snapshot. |
| 399 | if (isa<EnumConstantDecl>(VD) || isa<FunctionDecl>(VD)) |
| 400 | return false; |
| 401 | |
| 402 | // If this is a reference to an extern, static, or global variable, no need to |
| 403 | // snapshot it. |
| 404 | // FIXME: What about 'const' variables in C++? |
| 405 | if (const VarDecl *Var = dyn_cast<VarDecl>(VD)) |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 406 | if (!Var->hasLocalStorage()) |
| 407 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 409 | // Blocks that have these can't be constant. |
| 410 | CurBlock->hasBlockDeclRefExprs = true; |
| 411 | |
| 412 | // If we have nested blocks, the decl may be declared in an outer block (in |
| 413 | // which case that outer block doesn't get "hasBlockDeclRefExprs") or it may |
| 414 | // be defined outside all of the current blocks (in which case the blocks do |
| 415 | // all get the bit). Walk the nesting chain. |
| 416 | for (BlockSemaInfo *NextBlock = CurBlock->PrevBlockInfo; NextBlock; |
| 417 | NextBlock = NextBlock->PrevBlockInfo) { |
| 418 | // If we found the defining block for the variable, don't mark the block as |
| 419 | // having a reference outside it. |
| 420 | if (NextBlock->TheDecl == VD->getDeclContext()) |
| 421 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 423 | // Otherwise, the DeclRef from the inner block causes the outer one to need |
| 424 | // a snapshot as well. |
| 425 | NextBlock->hasBlockDeclRefExprs = true; |
| 426 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 428 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 431 | |
| 432 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 433 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 434 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | 0d755ad | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 435 | /// identifier is used in a function call context. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 436 | /// SS is only used for a C++ qualified-id (foo::bar) to indicate the |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 437 | /// class or namespace that the identifier must be a member of. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 438 | Sema::OwningExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
| 439 | IdentifierInfo &II, |
| 440 | bool HasTrailingLParen, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 441 | const CXXScopeSpec *SS, |
| 442 | bool isAddressOfOperand) { |
| 443 | return ActOnDeclarationNameExpr(S, Loc, &II, HasTrailingLParen, SS, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 444 | isAddressOfOperand); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 447 | /// BuildDeclRefExpr - Build either a DeclRefExpr or a |
| 448 | /// QualifiedDeclRefExpr based on whether or not SS is a |
| 449 | /// nested-name-specifier. |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 450 | Sema::OwningExprResult |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 451 | Sema::BuildDeclRefExpr(NamedDecl *D, QualType Ty, SourceLocation Loc, |
| 452 | bool TypeDependent, bool ValueDependent, |
| 453 | const CXXScopeSpec *SS) { |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 454 | if (Context.getCanonicalType(Ty) == Context.UndeducedAutoTy) { |
| 455 | Diag(Loc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 456 | diag::err_auto_variable_cannot_appear_in_own_initializer) |
Anders Carlsson | e2bb224 | 2009-06-26 19:16:07 +0000 | [diff] [blame] | 457 | << D->getDeclName(); |
| 458 | return ExprError(); |
| 459 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 460 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 461 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 462 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 463 | if (const FunctionDecl *FD = MD->getParent()->isLocalClass()) { |
| 464 | if (VD->hasLocalStorage() && VD->getDeclContext() != CurContext) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | Diag(Loc, diag::err_reference_to_local_var_in_enclosing_function) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 466 | << D->getIdentifier() << FD->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 467 | Diag(D->getLocation(), diag::note_local_variable_declared_here) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 468 | << D->getIdentifier(); |
| 469 | return ExprError(); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 475 | MarkDeclarationReferenced(Loc, D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 477 | Expr *E; |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 478 | if (SS && !SS->isEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | E = new (Context) QualifiedDeclRefExpr(D, Ty, Loc, TypeDependent, |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 480 | ValueDependent, SS->getRange(), |
Douglas Gregor | 3507369 | 2009-03-26 23:56:24 +0000 | [diff] [blame] | 481 | static_cast<NestedNameSpecifier *>(SS->getScopeRep())); |
Douglas Gregor | bad3518 | 2009-03-19 03:51:16 +0000 | [diff] [blame] | 482 | } else |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 483 | E = new (Context) DeclRefExpr(D, Ty, Loc, TypeDependent, ValueDependent); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 485 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 488 | /// getObjectForAnonymousRecordDecl - Retrieve the (unnamed) field or |
| 489 | /// variable corresponding to the anonymous union or struct whose type |
| 490 | /// is Record. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 491 | static Decl *getObjectForAnonymousRecordDecl(ASTContext &Context, |
| 492 | RecordDecl *Record) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | assert(Record->isAnonymousStructOrUnion() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 494 | "Record must be an anonymous struct or union!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 496 | // FIXME: Once Decls are directly linked together, this will be an O(1) |
| 497 | // operation rather than a slow walk through DeclContext's vector (which |
| 498 | // itself will be eliminated). DeclGroups might make this even better. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 499 | DeclContext *Ctx = Record->getDeclContext(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | for (DeclContext::decl_iterator D = Ctx->decls_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 501 | DEnd = Ctx->decls_end(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 502 | D != DEnd; ++D) { |
| 503 | if (*D == Record) { |
| 504 | // The object for the anonymous struct/union directly |
| 505 | // follows its type in the list of declarations. |
| 506 | ++D; |
| 507 | assert(D != DEnd && "Missing object for anonymous record"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 508 | assert(!cast<NamedDecl>(*D)->getDeclName() && "Decl should be unnamed"); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 509 | return *D; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | assert(false && "Missing object for anonymous record"); |
| 514 | return 0; |
| 515 | } |
| 516 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 517 | /// \brief Given a field that represents a member of an anonymous |
| 518 | /// struct/union, build the path from that field's context to the |
| 519 | /// actual member. |
| 520 | /// |
| 521 | /// Construct the sequence of field member references we'll have to |
| 522 | /// perform to get to the field in the anonymous union/struct. The |
| 523 | /// list of members is built from the field outward, so traverse it |
| 524 | /// backwards to go from an object in the current context to the field |
| 525 | /// we found. |
| 526 | /// |
| 527 | /// \returns The variable from which the field access should begin, |
| 528 | /// for an anonymous struct/union that is not a member of another |
| 529 | /// class. Otherwise, returns NULL. |
| 530 | VarDecl *Sema::BuildAnonymousStructUnionMemberPath(FieldDecl *Field, |
| 531 | llvm::SmallVectorImpl<FieldDecl *> &Path) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 532 | assert(Field->getDeclContext()->isRecord() && |
| 533 | cast<RecordDecl>(Field->getDeclContext())->isAnonymousStructOrUnion() |
| 534 | && "Field must be stored inside an anonymous struct or union"); |
| 535 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 536 | Path.push_back(Field); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 537 | VarDecl *BaseObject = 0; |
| 538 | DeclContext *Ctx = Field->getDeclContext(); |
| 539 | do { |
| 540 | RecordDecl *Record = cast<RecordDecl>(Ctx); |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 541 | Decl *AnonObject = getObjectForAnonymousRecordDecl(Context, Record); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 542 | if (FieldDecl *AnonField = dyn_cast<FieldDecl>(AnonObject)) |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 543 | Path.push_back(AnonField); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 544 | else { |
| 545 | BaseObject = cast<VarDecl>(AnonObject); |
| 546 | break; |
| 547 | } |
| 548 | Ctx = Ctx->getParent(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | } while (Ctx->isRecord() && |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 550 | cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 551 | |
| 552 | return BaseObject; |
| 553 | } |
| 554 | |
| 555 | Sema::OwningExprResult |
| 556 | Sema::BuildAnonymousStructUnionMemberReference(SourceLocation Loc, |
| 557 | FieldDecl *Field, |
| 558 | Expr *BaseObjectExpr, |
| 559 | SourceLocation OpLoc) { |
| 560 | llvm::SmallVector<FieldDecl *, 4> AnonFields; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | VarDecl *BaseObject = BuildAnonymousStructUnionMemberPath(Field, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 562 | AnonFields); |
| 563 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 564 | // Build the expression that refers to the base object, from |
| 565 | // which we will build a sequence of member references to each |
| 566 | // of the anonymous union objects and, eventually, the field we |
| 567 | // found via name lookup. |
| 568 | bool BaseObjectIsPointer = false; |
| 569 | unsigned ExtraQuals = 0; |
| 570 | if (BaseObject) { |
| 571 | // BaseObject is an anonymous struct/union variable (and is, |
| 572 | // therefore, not part of another non-anonymous record). |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 573 | if (BaseObjectExpr) BaseObjectExpr->Destroy(Context); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 574 | MarkDeclarationReferenced(Loc, BaseObject); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 575 | BaseObjectExpr = new (Context) DeclRefExpr(BaseObject,BaseObject->getType(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 576 | SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | ExtraQuals |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 578 | = Context.getCanonicalType(BaseObject->getType()).getCVRQualifiers(); |
| 579 | } else if (BaseObjectExpr) { |
| 580 | // The caller provided the base object expression. Determine |
| 581 | // whether its a pointer and whether it adds any qualifiers to the |
| 582 | // anonymous struct/union fields we're looking into. |
| 583 | QualType ObjectType = BaseObjectExpr->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 584 | if (const PointerType *ObjectPtr = ObjectType->getAs<PointerType>()) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 585 | BaseObjectIsPointer = true; |
| 586 | ObjectType = ObjectPtr->getPointeeType(); |
| 587 | } |
| 588 | ExtraQuals = Context.getCanonicalType(ObjectType).getCVRQualifiers(); |
| 589 | } else { |
| 590 | // We've found a member of an anonymous struct/union that is |
| 591 | // inside a non-anonymous struct/union, so in a well-formed |
| 592 | // program our base object expression is "this". |
| 593 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 594 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | QualType AnonFieldType |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 596 | = Context.getTagDeclType( |
| 597 | cast<RecordDecl>(AnonFields.back()->getDeclContext())); |
| 598 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 599 | if ((Context.getCanonicalType(AnonFieldType) |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 600 | == Context.getCanonicalType(ThisType)) || |
| 601 | IsDerivedFrom(ThisType, AnonFieldType)) { |
| 602 | // Our base object expression is "this". |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 603 | BaseObjectExpr = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 604 | MD->getThisType(Context)); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 605 | BaseObjectIsPointer = true; |
| 606 | } |
| 607 | } else { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 608 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 609 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 610 | } |
| 611 | ExtraQuals = MD->getTypeQualifiers(); |
| 612 | } |
| 613 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | if (!BaseObjectExpr) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 615 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 616 | << Field->getDeclName()); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // Build the implicit member references to the field of the |
| 620 | // anonymous struct/union. |
| 621 | Expr *Result = BaseObjectExpr; |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 622 | unsigned BaseAddrSpace = BaseObjectExpr->getType().getAddressSpace(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 623 | for (llvm::SmallVector<FieldDecl *, 4>::reverse_iterator |
| 624 | FI = AnonFields.rbegin(), FIEnd = AnonFields.rend(); |
| 625 | FI != FIEnd; ++FI) { |
| 626 | QualType MemberType = (*FI)->getType(); |
| 627 | if (!(*FI)->isMutable()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | unsigned combinedQualifiers |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 629 | = MemberType.getCVRQualifiers() | ExtraQuals; |
| 630 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 631 | } |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 632 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 633 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 634 | MarkDeclarationReferenced(Loc, *FI); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 635 | // FIXME: Might this end up being a qualified name? |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 636 | Result = new (Context) MemberExpr(Result, BaseObjectIsPointer, *FI, |
| 637 | OpLoc, MemberType); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 638 | BaseObjectIsPointer = false; |
| 639 | ExtraQuals = Context.getCanonicalType(MemberType).getCVRQualifiers(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 642 | return Owned(Result); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 645 | /// ActOnDeclarationNameExpr - The parser has read some kind of name |
| 646 | /// (e.g., a C++ id-expression (C++ [expr.prim]p1)). This routine |
| 647 | /// performs lookup on that name and returns an expression that refers |
| 648 | /// to that name. This routine isn't directly called from the parser, |
| 649 | /// because the parser doesn't know about DeclarationName. Rather, |
| 650 | /// this routine is called by ActOnIdentifierExpr, |
| 651 | /// ActOnOperatorFunctionIdExpr, and ActOnConversionFunctionExpr, |
| 652 | /// which form the DeclarationName from the corresponding syntactic |
| 653 | /// forms. |
| 654 | /// |
| 655 | /// HasTrailingLParen indicates whether this identifier is used in a |
| 656 | /// function call context. LookupCtx is only used for a C++ |
| 657 | /// qualified-id (foo::bar) to indicate the class or namespace that |
| 658 | /// the identifier must be a member of. |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 659 | /// |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 660 | /// isAddressOfOperand means that this expression is the direct operand |
| 661 | /// of an address-of operator. This matters because this is the only |
| 662 | /// situation where a qualified name referencing a non-static member may |
| 663 | /// appear outside a member function of this class. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 664 | Sema::OwningExprResult |
| 665 | Sema::ActOnDeclarationNameExpr(Scope *S, SourceLocation Loc, |
| 666 | DeclarationName Name, bool HasTrailingLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | const CXXScopeSpec *SS, |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 668 | bool isAddressOfOperand) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 669 | // Could be enum-constant, value decl, instance variable, etc. |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 670 | if (SS && SS->isInvalid()) |
| 671 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 672 | |
| 673 | // C++ [temp.dep.expr]p3: |
| 674 | // An id-expression is type-dependent if it contains: |
| 675 | // -- a nested-name-specifier that contains a class-name that |
| 676 | // names a dependent type. |
Douglas Gregor | 00c4486 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 677 | // FIXME: Member of the current instantiation. |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 678 | if (SS && isDependentScopeSpecifier(*SS)) { |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 679 | return Owned(new (Context) UnresolvedDeclRefExpr(Name, Context.DependentTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | Loc, SS->getRange(), |
Anders Carlsson | 9b31df4 | 2009-07-09 00:05:08 +0000 | [diff] [blame] | 681 | static_cast<NestedNameSpecifier *>(SS->getScopeRep()), |
| 682 | isAddressOfOperand)); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 685 | LookupResult Lookup = LookupParsedName(S, SS, Name, LookupOrdinaryName, |
| 686 | false, true, Loc); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 687 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 688 | if (Lookup.isAmbiguous()) { |
| 689 | DiagnoseAmbiguousLookup(Lookup, Name, Loc, |
| 690 | SS && SS->isSet() ? SS->getRange() |
| 691 | : SourceRange()); |
| 692 | return ExprError(); |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 693 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 695 | NamedDecl *D = Lookup.getAsDecl(); |
Douglas Gregor | 5c37de7 | 2008-12-06 00:22:45 +0000 | [diff] [blame] | 696 | |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 697 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 698 | // well. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 699 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| 700 | if (II && getCurMethodDecl()) { |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 701 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 702 | // in which case we should look for an ivar. 2) scoped lookup could have |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | // found a decl, but that decl is outside the current instance method (i.e. |
| 704 | // a global variable). In these two cases, we do a lookup for an ivar with |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 705 | // this name, if the lookup sucedes, we replace it our current decl. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 706 | if (D == 0 || D->isDefinedOutsideFunctionOrMethod()) { |
Argyrios Kyrtzidis | 53d0ea5 | 2008-06-28 06:07:14 +0000 | [diff] [blame] | 707 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 708 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 709 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Chris Lattner | 553905d | 2009-02-16 17:19:12 +0000 | [diff] [blame] | 710 | // Check if referencing a field with __attribute__((deprecated)). |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 711 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 712 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 714 | // If we're referencing an invalid decl, just return this as a silent |
| 715 | // error node. The error diagnostic was already emitted on the decl. |
| 716 | if (IV->isInvalidDecl()) |
| 717 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 719 | bool IsClsMethod = getCurMethodDecl()->isClassMethod(); |
| 720 | // If a class method attemps to use a free standing ivar, this is |
| 721 | // an error. |
| 722 | if (IsClsMethod && D && !D->isDefinedOutsideFunctionOrMethod()) |
| 723 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 724 | << IV->getDeclName()); |
| 725 | // If a class method uses a global variable, even if an ivar with |
| 726 | // same name exists, use the global. |
| 727 | if (!IsClsMethod) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 728 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 729 | ClassDeclared != IFace) |
| 730 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 731 | // FIXME: This should use a new expr for a direct reference, don't |
| 732 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 733 | IdentifierInfo &II = Context.Idents.get("self"); |
Argyrios Kyrtzidis | ecd1bae | 2009-07-18 08:49:37 +0000 | [diff] [blame] | 734 | OwningExprResult SelfExpr = ActOnIdentifierExpr(S, SourceLocation(), |
| 735 | II, false); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 736 | MarkDeclarationReferenced(Loc, IV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | return Owned(new (Context) |
| 738 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 739 | SelfExpr.takeAs<Expr>(), true, true)); |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 740 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 741 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 742 | } else if (getCurMethodDecl()->isInstanceMethod()) { |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 743 | // We should warn if a local variable hides an ivar. |
| 744 | ObjCInterfaceDecl *IFace = getCurMethodDecl()->getClassInterface(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 745 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 746 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 747 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 748 | IFace == ClassDeclared) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 749 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 750 | } |
Fariborz Jahanian | 077c1e7 | 2009-03-02 21:55:29 +0000 | [diff] [blame] | 751 | } |
Steve Naroff | 76de9d7 | 2008-08-10 19:10:41 +0000 | [diff] [blame] | 752 | // Needed to implement property "super.method" notation. |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 753 | if (D == 0 && II->isStr("super")) { |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 754 | QualType T; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 756 | if (getCurMethodDecl()->isInstanceMethod()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 757 | T = Context.getObjCObjectPointerType(Context.getObjCInterfaceType( |
| 758 | getCurMethodDecl()->getClassInterface())); |
Steve Naroff | dd53eb5 | 2009-03-05 20:12:00 +0000 | [diff] [blame] | 759 | else |
| 760 | T = Context.getObjCClassType(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 761 | return Owned(new (Context) ObjCSuperExpr(Loc, T)); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 763 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 764 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 765 | // Determine whether this name might be a candidate for |
| 766 | // argument-dependent lookup. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 768 | HasTrailingLParen; |
| 769 | |
| 770 | if (ADL && D == 0) { |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 771 | // We've seen something of the form |
| 772 | // |
| 773 | // identifier( |
| 774 | // |
| 775 | // and we did not find any entity by the name |
| 776 | // "identifier". However, this identifier is still subject to |
| 777 | // argument-dependent lookup, so keep track of the name. |
| 778 | return Owned(new (Context) UnresolvedFunctionNameExpr(Name, |
| 779 | Context.OverloadTy, |
| 780 | Loc)); |
| 781 | } |
| 782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 783 | if (D == 0) { |
| 784 | // Otherwise, this could be an implicitly declared function reference (legal |
| 785 | // in C90, extension in C99). |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 786 | if (HasTrailingLParen && II && |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 787 | !getLangOptions().CPlusPlus) // Not in C++. |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 788 | D = ImplicitlyDefineFunction(Loc, *II, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | else { |
| 790 | // If this name wasn't predeclared and if this is not a function call, |
| 791 | // diagnose the problem. |
Anders Carlsson | f4d84b6 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 792 | if (SS && !SS->isEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 793 | DiagnoseMissingMember(Loc, Name, |
Anders Carlsson | f4d84b6 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 794 | (NestedNameSpecifier *)SS->getScopeRep(), |
| 795 | SS->getRange()); |
| 796 | return ExprError(); |
| 797 | } else if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 798 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 799 | return ExprError(Diag(Loc, diag::err_undeclared_use) |
| 800 | << Name.getAsString()); |
Argyrios Kyrtzidis | ef6e647 | 2008-11-08 17:17:31 +0000 | [diff] [blame] | 801 | else |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 802 | return ExprError(Diag(Loc, diag::err_undeclared_var_use) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 806 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 807 | // Warn about constructs like: |
| 808 | // if (void *X = foo()) { ... } else { X }. |
| 809 | // In the else block, the pointer is always false. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 811 | // FIXME: In a template instantiation, we don't have scope |
| 812 | // information to check this property. |
| 813 | if (Var->isDeclaredInCondition() && Var->getType()->isScalarType()) { |
| 814 | Scope *CheckS = S; |
| 815 | while (CheckS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | if (CheckS->isWithinElse() && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 817 | CheckS->getControlParent()->isDeclScope(DeclPtrTy::make(Var))) { |
| 818 | if (Var->getType()->isBooleanType()) |
| 819 | ExprError(Diag(Loc, diag::warn_value_always_false) |
| 820 | << Var->getDeclName()); |
| 821 | else |
| 822 | ExprError(Diag(Loc, diag::warn_value_always_zero) |
| 823 | << Var->getDeclName()); |
| 824 | break; |
| 825 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 826 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 827 | // Move up one more control parent to check again. |
| 828 | CheckS = CheckS->getControlParent(); |
| 829 | if (CheckS) |
| 830 | CheckS = CheckS->getParent(); |
| 831 | } |
| 832 | } |
| 833 | } else if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D)) { |
| 834 | if (!getLangOptions().CPlusPlus && !Func->hasPrototype()) { |
| 835 | // C99 DR 316 says that, if a function type comes from a |
| 836 | // function definition (without a prototype), that type is only |
| 837 | // used for checking compatibility. Therefore, when referencing |
| 838 | // the function, we pretend that we don't have the full function |
| 839 | // type. |
| 840 | if (DiagnoseUseOfDecl(Func, Loc)) |
| 841 | return ExprError(); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 843 | QualType T = Func->getType(); |
| 844 | QualType NoProtoType = T; |
| 845 | if (const FunctionProtoType *Proto = T->getAsFunctionProtoType()) |
| 846 | NoProtoType = Context.getFunctionNoProtoType(Proto->getResultType()); |
| 847 | return BuildDeclRefExpr(Func, NoProtoType, Loc, false, false, SS); |
| 848 | } |
| 849 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 851 | return BuildDeclarationNameExpr(Loc, D, HasTrailingLParen, SS, isAddressOfOperand); |
| 852 | } |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 853 | /// \brief Cast member's object to its own class if necessary. |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 854 | bool |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 855 | Sema::PerformObjectMemberConversion(Expr *&From, NamedDecl *Member) { |
| 856 | if (FieldDecl *FD = dyn_cast<FieldDecl>(Member)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | if (CXXRecordDecl *RD = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 858 | dyn_cast<CXXRecordDecl>(FD->getDeclContext())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 859 | QualType DestType = |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 860 | Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 861 | if (DestType->isDependentType() || From->getType()->isDependentType()) |
| 862 | return false; |
| 863 | QualType FromRecordType = From->getType(); |
| 864 | QualType DestRecordType = DestType; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 865 | if (FromRecordType->getAs<PointerType>()) { |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 866 | DestType = Context.getPointerType(DestType); |
| 867 | FromRecordType = FromRecordType->getPointeeType(); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 868 | } |
Fariborz Jahanian | 96e2fa9 | 2009-07-29 20:41:46 +0000 | [diff] [blame] | 869 | if (!Context.hasSameUnqualifiedType(FromRecordType, DestRecordType) && |
| 870 | CheckDerivedToBaseConversion(FromRecordType, |
| 871 | DestRecordType, |
| 872 | From->getSourceRange().getBegin(), |
| 873 | From->getSourceRange())) |
| 874 | return true; |
Anders Carlsson | 3503d04 | 2009-07-31 01:23:52 +0000 | [diff] [blame] | 875 | ImpCastExprToType(From, DestType, CastExpr::CK_DerivedToBase, |
| 876 | /*isLvalue=*/true); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 877 | } |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 878 | return false; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 879 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 881 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
| 883 | const CXXScopeSpec *SS, NamedDecl *Member, |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 884 | SourceLocation Loc, QualType Ty) { |
| 885 | if (SS && SS->isSet()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | return MemberExpr::Create(C, Base, isArrow, |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 887 | (NestedNameSpecifier *)SS->getScopeRep(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 888 | SS->getRange(), Member, Loc, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 889 | // FIXME: Explicit template argument lists |
| 890 | false, SourceLocation(), 0, 0, SourceLocation(), |
| 891 | Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 893 | return new (C) MemberExpr(Base, isArrow, Member, Loc, Ty); |
| 894 | } |
| 895 | |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 896 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| 897 | Sema::OwningExprResult |
| 898 | Sema::BuildDeclarationNameExpr(SourceLocation Loc, NamedDecl *D, |
| 899 | bool HasTrailingLParen, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 900 | const CXXScopeSpec *SS, |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 901 | bool isAddressOfOperand) { |
| 902 | assert(D && "Cannot refer to a NULL declaration"); |
| 903 | DeclarationName Name = D->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 904 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 905 | // If this is an expression of the form &Class::member, don't build an |
| 906 | // implicit member ref, because we want a pointer to the member in general, |
| 907 | // not any specific instance's member. |
| 908 | if (isAddressOfOperand && SS && !SS->isEmpty() && !HasTrailingLParen) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 909 | DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 910 | if (D && isa<CXXRecordDecl>(DC)) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 911 | QualType DType; |
| 912 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 913 | DType = FD->getType().getNonReferenceType(); |
| 914 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 915 | DType = Method->getType(); |
| 916 | } else if (isa<OverloadedFunctionDecl>(D)) { |
| 917 | DType = Context.OverloadTy; |
| 918 | } |
| 919 | // Could be an inner type. That's diagnosed below, so ignore it here. |
| 920 | if (!DType.isNull()) { |
| 921 | // The pointer is type- and value-dependent if it points into something |
| 922 | // dependent. |
Douglas Gregor | 00c4486 | 2009-05-29 14:49:33 +0000 | [diff] [blame] | 923 | bool Dependent = DC->isDependentContext(); |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 924 | return BuildDeclRefExpr(D, DType, Loc, Dependent, Dependent, SS); |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | } |
| 928 | |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 929 | // We may have found a field within an anonymous union or struct |
| 930 | // (C++ [class.union]). |
| 931 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) |
| 932 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
| 933 | return BuildAnonymousStructUnionMemberReference(Loc, FD); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 935 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 936 | if (!MD->isStatic()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 937 | // C++ [class.mfct.nonstatic]p2: |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 938 | // [...] if name lookup (3.4.1) resolves the name in the |
| 939 | // id-expression to a nonstatic nontype member of class X or of |
| 940 | // a base class of X, the id-expression is transformed into a |
| 941 | // class member access expression (5.2.5) using (*this) (9.3.2) |
| 942 | // as the postfix-expression to the left of the '.' operator. |
| 943 | DeclContext *Ctx = 0; |
| 944 | QualType MemberType; |
| 945 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 946 | Ctx = FD->getDeclContext(); |
| 947 | MemberType = FD->getType(); |
| 948 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 949 | if (const ReferenceType *RefType = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 950 | MemberType = RefType->getPointeeType(); |
| 951 | else if (!FD->isMutable()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | unsigned combinedQualifiers |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 953 | = MemberType.getCVRQualifiers() | MD->getTypeQualifiers(); |
| 954 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 955 | } |
| 956 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(D)) { |
| 957 | if (!Method->isStatic()) { |
| 958 | Ctx = Method->getParent(); |
| 959 | MemberType = Method->getType(); |
| 960 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | } else if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 962 | = dyn_cast<FunctionTemplateDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 963 | if (CXXMethodDecl *Method |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 964 | = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 965 | if (!Method->isStatic()) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 966 | Ctx = Method->getParent(); |
| 967 | MemberType = Context.OverloadTy; |
| 968 | } |
| 969 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | } else if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 971 | = dyn_cast<OverloadedFunctionDecl>(D)) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 972 | // FIXME: We need an abstraction for iterating over one or more function |
| 973 | // templates or functions. This code is far too repetitive! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 974 | for (OverloadedFunctionDecl::function_iterator |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 975 | Func = Ovl->function_begin(), |
| 976 | FuncEnd = Ovl->function_end(); |
| 977 | Func != FuncEnd; ++Func) { |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 978 | CXXMethodDecl *DMethod = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 980 | = dyn_cast<FunctionTemplateDecl>(*Func)) |
| 981 | DMethod = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); |
| 982 | else |
| 983 | DMethod = dyn_cast<CXXMethodDecl>(*Func); |
| 984 | |
| 985 | if (DMethod && !DMethod->isStatic()) { |
| 986 | Ctx = DMethod->getDeclContext(); |
| 987 | MemberType = Context.OverloadTy; |
| 988 | break; |
| 989 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 992 | |
| 993 | if (Ctx && Ctx->isRecord()) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 994 | QualType CtxType = Context.getTagDeclType(cast<CXXRecordDecl>(Ctx)); |
| 995 | QualType ThisType = Context.getTagDeclType(MD->getParent()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | if ((Context.getCanonicalType(CtxType) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 997 | == Context.getCanonicalType(ThisType)) || |
| 998 | IsDerivedFrom(ThisType, CtxType)) { |
| 999 | // Build the implicit member access expression. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1000 | Expr *This = new (Context) CXXThisExpr(SourceLocation(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1001 | MD->getThisType(Context)); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1002 | MarkDeclarationReferenced(Loc, D); |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 1003 | if (PerformObjectMemberConversion(This, D)) |
| 1004 | return ExprError(); |
Anders Carlsson | 0f44b5a | 2009-08-08 16:55:18 +0000 | [diff] [blame] | 1005 | if (DiagnoseUseOfDecl(D, Loc)) |
| 1006 | return ExprError(); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 1007 | return Owned(BuildMemberExpr(Context, This, true, SS, D, |
| 1008 | Loc, MemberType)); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | } |
| 1013 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1014 | if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1015 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) { |
| 1016 | if (MD->isStatic()) |
| 1017 | // "invalid use of member 'x' in static member function" |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1018 | return ExprError(Diag(Loc,diag::err_invalid_member_use_in_static_method) |
| 1019 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1022 | // Any other ways we could have found the field in a well-formed |
| 1023 | // program would have been turned into implicit member expressions |
| 1024 | // above. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1025 | return ExprError(Diag(Loc, diag::err_invalid_non_static_member_use) |
| 1026 | << FD->getDeclName()); |
Argyrios Kyrtzidis | 0795232 | 2008-07-01 10:37:29 +0000 | [diff] [blame] | 1027 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1028 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | if (isa<TypedefDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1030 | return ExprError(Diag(Loc, diag::err_unexpected_typedef) << Name); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1031 | if (isa<ObjCInterfaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1032 | return ExprError(Diag(Loc, diag::err_unexpected_interface) << Name); |
Argyrios Kyrtzidis | 2d1c5d3 | 2008-04-27 13:50:30 +0000 | [diff] [blame] | 1033 | if (isa<NamespaceDecl>(D)) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1034 | return ExprError(Diag(Loc, diag::err_unexpected_namespace) << Name); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1035 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1036 | // Make the DeclRefExpr or BlockDeclRefExpr for the decl. |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 1037 | if (OverloadedFunctionDecl *Ovl = dyn_cast<OverloadedFunctionDecl>(D)) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1038 | return BuildDeclRefExpr(Ovl, Context.OverloadTy, Loc, |
| 1039 | false, false, SS); |
Douglas Gregor | c15cb38 | 2009-02-09 23:23:08 +0000 | [diff] [blame] | 1040 | else if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1041 | return BuildDeclRefExpr(Template, Context.OverloadTy, Loc, |
| 1042 | false, false, SS); |
Anders Carlsson | 598da5b | 2009-08-29 01:06:32 +0000 | [diff] [blame] | 1043 | else if (UnresolvedUsingDecl *UD = dyn_cast<UnresolvedUsingDecl>(D)) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | return BuildDeclRefExpr(UD, Context.DependentTy, Loc, |
| 1045 | /*TypeDependent=*/true, |
Anders Carlsson | 598da5b | 2009-08-29 01:06:32 +0000 | [diff] [blame] | 1046 | /*ValueDependent=*/true, SS); |
| 1047 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1048 | ValueDecl *VD = cast<ValueDecl>(D); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1049 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1050 | // Check whether this declaration can be used. Note that we suppress |
| 1051 | // this check when we're going to perform argument-dependent lookup |
| 1052 | // on this function name, because this might not be the function |
| 1053 | // that overload resolution actually selects. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1054 | bool ADL = getLangOptions().CPlusPlus && (!SS || !SS->isSet()) && |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1055 | HasTrailingLParen; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1056 | if (!(ADL && isa<FunctionDecl>(VD)) && DiagnoseUseOfDecl(VD, Loc)) |
| 1057 | return ExprError(); |
| 1058 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1059 | // Only create DeclRefExpr's for valid Decl's. |
| 1060 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1061 | return ExprError(); |
| 1062 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1063 | // If the identifier reference is inside a block, and it refers to a value |
| 1064 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 1065 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 1066 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 1067 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1068 | // We do not do this for things like enum constants, global variables, etc, |
| 1069 | // as they do not get snapshotted. |
| 1070 | // |
| 1071 | if (CurBlock && ShouldSnapshotBlockValueReference(CurBlock, VD)) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1072 | MarkDeclarationReferenced(Loc, VD); |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1073 | QualType ExprTy = VD->getType().getNonReferenceType(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1074 | // The BlocksAttr indicates the variable is bound by-reference. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1075 | if (VD->getAttr<BlocksAttr>()) |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1076 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, true)); |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1077 | // This is to record that a 'const' was actually synthesize and added. |
| 1078 | bool constAdded = !ExprTy.isConstQualified(); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1079 | // Variable will be bound by-copy, make it const within the closure. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | |
Eli Friedman | 5fdeae1 | 2009-03-22 23:00:19 +0000 | [diff] [blame] | 1081 | ExprTy.addConst(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1082 | return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false, |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 1083 | constAdded)); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 1084 | } |
| 1085 | // If this reference is not in a block or if the referenced variable is |
| 1086 | // within the block, create a normal DeclRefExpr. |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1088 | bool TypeDependent = false; |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1089 | bool ValueDependent = false; |
| 1090 | if (getLangOptions().CPlusPlus) { |
| 1091 | // C++ [temp.dep.expr]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1093 | // - an identifier that was declared with a dependent type, |
| 1094 | if (VD->getType()->isDependentType()) |
| 1095 | TypeDependent = true; |
| 1096 | // - FIXME: a template-id that is dependent, |
| 1097 | // - a conversion-function-id that specifies a dependent type, |
| 1098 | else if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1099 | Name.getCXXNameType()->isDependentType()) |
| 1100 | TypeDependent = true; |
| 1101 | // - a nested-name-specifier that contains a class-name that |
| 1102 | // names a dependent type. |
| 1103 | else if (SS && !SS->isEmpty()) { |
Douglas Gregor | e4e5b05 | 2009-03-19 00:18:19 +0000 | [diff] [blame] | 1104 | for (DeclContext *DC = computeDeclContext(*SS); |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1105 | DC; DC = DC->getParent()) { |
| 1106 | // FIXME: could stop early at namespace scope. |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1107 | if (DC->isRecord()) { |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1108 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DC); |
| 1109 | if (Context.getTypeDeclType(Record)->isDependentType()) { |
| 1110 | TypeDependent = true; |
| 1111 | break; |
| 1112 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1116 | |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1117 | // C++ [temp.dep.constexpr]p2: |
| 1118 | // |
| 1119 | // An identifier is value-dependent if it is: |
| 1120 | // - a name declared with a dependent type, |
| 1121 | if (TypeDependent) |
| 1122 | ValueDependent = true; |
| 1123 | // - the name of a non-type template parameter, |
| 1124 | else if (isa<NonTypeTemplateParmDecl>(VD)) |
| 1125 | ValueDependent = true; |
| 1126 | // - a constant with integral or enumeration type and is |
| 1127 | // initialized with an expression that is value-dependent |
Eli Friedman | c149412 | 2009-06-11 01:11:20 +0000 | [diff] [blame] | 1128 | else if (const VarDecl *Dcl = dyn_cast<VarDecl>(VD)) { |
| 1129 | if (Dcl->getType().getCVRQualifiers() == QualType::Const && |
| 1130 | Dcl->getInit()) { |
| 1131 | ValueDependent = Dcl->getInit()->isValueDependent(); |
| 1132 | } |
| 1133 | } |
Douglas Gregor | 83f96f6 | 2008-12-10 20:57:37 +0000 | [diff] [blame] | 1134 | } |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1135 | |
Anders Carlsson | e41590d | 2009-06-24 00:10:43 +0000 | [diff] [blame] | 1136 | return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(), Loc, |
| 1137 | TypeDependent, ValueDependent, SS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1140 | Sema::OwningExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, |
| 1141 | tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1142 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1143 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1144 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1145 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1146 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 1147 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 1148 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1149 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1150 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 1151 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 1152 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1154 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| 1155 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1156 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1157 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 1158 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1159 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 1160 | unsigned Length = |
| 1161 | PredefinedExpr::ComputeName(Context, IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1163 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 1164 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | 8f978d5 | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 1165 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1166 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1169 | Sema::OwningExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1170 | llvm::SmallString<16> CharBuffer; |
| 1171 | CharBuffer.resize(Tok.getLength()); |
| 1172 | const char *ThisTokBegin = &CharBuffer[0]; |
| 1173 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1175 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 1176 | Tok.getLocation(), PP); |
| 1177 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1178 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 1179 | |
| 1180 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 1181 | |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1182 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 1183 | Literal.isWide(), |
| 1184 | type, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1187 | Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| 1188 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 1190 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 1191 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 1192 | unsigned IntSize = Context.Target.getIntWidth(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1193 | return Owned(new (Context) IntegerLiteral(llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 1194 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 1196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1197 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 1198 | // Add padding so that NumericLiteralParser can overread by one character. |
| 1199 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 1203 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1204 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | Tok.getLocation(), PP); |
| 1207 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1208 | return ExprError(); |
| 1209 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1210 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1211 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1212 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1213 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1214 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1215 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1216 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 1217 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1218 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 1219 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 1220 | |
| 1221 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 1222 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 1223 | // isExact will be set by GetFloatValue(). |
| 1224 | bool isExact = false; |
Chris Lattner | 001d64d | 2009-06-29 17:34:55 +0000 | [diff] [blame] | 1225 | llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact); |
| 1226 | Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1227 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1228 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1229 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1230 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1231 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1233 | // long long is a C99 feature. |
| 1234 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 1235 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 1236 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 1237 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1238 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1239 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1241 | if (Literal.GetIntegerValue(ResultVal)) { |
| 1242 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 1243 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1244 | Ty = Context.UnsignedLongLongTy; |
| 1245 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1246 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1247 | } else { |
| 1248 | // If this value fits into a ULL, try to figure out what else it fits into |
| 1249 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1250 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1251 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 1252 | // be an unsigned int. |
| 1253 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 1254 | |
| 1255 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1256 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 1257 | if (!Literal.isLong && !Literal.isLongLong) { |
| 1258 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1259 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | // Does it fit in a unsigned int? |
| 1262 | if (ResultVal.isIntN(IntSize)) { |
| 1263 | // Does it fit in a signed int? |
| 1264 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1265 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1266 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1267 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1268 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1269 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1272 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1273 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1274 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1276 | // Does it fit in a unsigned long? |
| 1277 | if (ResultVal.isIntN(LongSize)) { |
| 1278 | // Does it fit in a signed long? |
| 1279 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1280 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1282 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1283 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1288 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1289 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1290 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1291 | // Does it fit in a unsigned long long? |
| 1292 | if (ResultVal.isIntN(LongLongSize)) { |
| 1293 | // Does it fit in a signed long long? |
| 1294 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1295 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1297 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1298 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | } |
| 1300 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1301 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1302 | // If we still couldn't decide a type, we probably have something that |
| 1303 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1304 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1306 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1307 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1308 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1309 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 1310 | if (ResultVal.getBitWidth() != Width) |
| 1311 | ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1312 | } |
Sebastian Redl | e91b3bc | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 1313 | Res = new (Context) IntegerLiteral(ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1315 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1316 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 1317 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1318 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1319 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1320 | |
| 1321 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1324 | Action::OwningExprResult Sema::ActOnParenExpr(SourceLocation L, |
| 1325 | SourceLocation R, ExprArg Val) { |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 1326 | Expr *E = Val.takeAs<Expr>(); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 1327 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1328 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 1332 | /// See C99 6.3.2.1p[2-4] for more details. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1333 | bool Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1334 | SourceLocation OpLoc, |
| 1335 | const SourceRange &ExprRange, |
| 1336 | bool isSizeof) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1337 | if (exprType->isDependentType()) |
| 1338 | return false; |
| 1339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1340 | // C99 6.5.3.4p1: |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1341 | if (isa<FunctionType>(exprType)) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1342 | // alignof(function) is allowed as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1343 | if (isSizeof) |
| 1344 | Diag(OpLoc, diag::ext_sizeof_function_type) << ExprRange; |
| 1345 | return false; |
| 1346 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1348 | // Allow sizeof(void)/alignof(void) as an extension. |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1349 | if (exprType->isVoidType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1350 | Diag(OpLoc, diag::ext_sizeof_void_type) |
| 1351 | << (isSizeof ? "sizeof" : "__alignof") << ExprRange; |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 1352 | return false; |
| 1353 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1355 | if (RequireCompleteType(OpLoc, exprType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | isSizeof ? diag::err_sizeof_incomplete_type : |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1357 | PDiag(diag::err_alignof_incomplete_type) |
| 1358 | << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1359 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1361 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
Fariborz Jahanian | ced1e28 | 2009-04-24 17:34:33 +0000 | [diff] [blame] | 1362 | if (LangOpts.ObjCNonFragileABI && exprType->isObjCInterfaceType()) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1363 | Diag(OpLoc, diag::err_sizeof_nonfragile_interface) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 1364 | << exprType << isSizeof << ExprRange; |
| 1365 | return true; |
Chris Lattner | ca79092 | 2009-04-21 19:55:16 +0000 | [diff] [blame] | 1366 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1367 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1368 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1371 | bool Sema::CheckAlignOfExpr(Expr *E, SourceLocation OpLoc, |
| 1372 | const SourceRange &ExprRange) { |
| 1373 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1374 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1376 | if (isa<DeclRefExpr>(E)) |
| 1377 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1378 | |
| 1379 | // Cannot know anything else if the expression is dependent. |
| 1380 | if (E->isTypeDependent()) |
| 1381 | return false; |
| 1382 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1383 | if (E->getBitField()) { |
| 1384 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 1 << ExprRange; |
| 1385 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1386 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1387 | |
| 1388 | // Alignment of a field access is always okay, so long as it isn't a |
| 1389 | // bit-field. |
| 1390 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 1391 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1392 | return false; |
| 1393 | |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 1394 | return CheckSizeOfAlignOfOperand(E->getType(), OpLoc, ExprRange, false); |
| 1395 | } |
| 1396 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1397 | /// \brief Build a sizeof or alignof expression given a type operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | Action::OwningExprResult |
| 1399 | Sema::CreateSizeOfAlignOfExpr(QualType T, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1400 | bool isSizeOf, SourceRange R) { |
| 1401 | if (T.isNull()) |
| 1402 | return ExprError(); |
| 1403 | |
| 1404 | if (!T->isDependentType() && |
| 1405 | CheckSizeOfAlignOfOperand(T, OpLoc, R, isSizeOf)) |
| 1406 | return ExprError(); |
| 1407 | |
| 1408 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1409 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, T, |
| 1410 | Context.getSizeType(), OpLoc, |
| 1411 | R.getEnd())); |
| 1412 | } |
| 1413 | |
| 1414 | /// \brief Build a sizeof or alignof expression given an expression |
| 1415 | /// operand. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | Action::OwningExprResult |
| 1417 | Sema::CreateSizeOfAlignOfExpr(Expr *E, SourceLocation OpLoc, |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1418 | bool isSizeOf, SourceRange R) { |
| 1419 | // Verify that the operand is valid. |
| 1420 | bool isInvalid = false; |
| 1421 | if (E->isTypeDependent()) { |
| 1422 | // Delay type-checking for type-dependent expressions. |
| 1423 | } else if (!isSizeOf) { |
| 1424 | isInvalid = CheckAlignOfExpr(E, OpLoc, R); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1425 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1426 | Diag(OpLoc, diag::err_sizeof_alignof_bitfield) << 0; |
| 1427 | isInvalid = true; |
| 1428 | } else { |
| 1429 | isInvalid = CheckSizeOfAlignOfOperand(E->getType(), OpLoc, R, true); |
| 1430 | } |
| 1431 | |
| 1432 | if (isInvalid) |
| 1433 | return ExprError(); |
| 1434 | |
| 1435 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 1436 | return Owned(new (Context) SizeOfAlignOfExpr(isSizeOf, E, |
| 1437 | Context.getSizeType(), OpLoc, |
| 1438 | R.getEnd())); |
| 1439 | } |
| 1440 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1441 | /// ActOnSizeOfAlignOfExpr - Handle @c sizeof(type) and @c sizeof @c expr and |
| 1442 | /// the same for @c alignof and @c __alignof |
| 1443 | /// Note that the ArgRange is invalid if isType is false. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1444 | Action::OwningExprResult |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1445 | Sema::ActOnSizeOfAlignOfExpr(SourceLocation OpLoc, bool isSizeof, bool isType, |
| 1446 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1447 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1448 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1450 | if (isType) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1451 | // FIXME: Preserve type source info. |
| 1452 | QualType ArgTy = GetTypeFromParser(TyOrEx); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1453 | return CreateSizeOfAlignOfExpr(ArgTy, OpLoc, isSizeof, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 1456 | // Get the end location. |
| 1457 | Expr *ArgEx = (Expr *)TyOrEx; |
| 1458 | Action::OwningExprResult Result |
| 1459 | = CreateSizeOfAlignOfExpr(ArgEx, OpLoc, isSizeof, ArgEx->getSourceRange()); |
| 1460 | |
| 1461 | if (Result.isInvalid()) |
| 1462 | DeleteExpr(ArgEx); |
| 1463 | |
| 1464 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1467 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc, bool isReal) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1468 | if (V->isTypeDependent()) |
| 1469 | return Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1471 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1472 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 1473 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1475 | // Otherwise they pass through real integer and floating point types here. |
| 1476 | if (V->getType()->isArithmeticType()) |
| 1477 | return V->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1479 | // Reject anything else. |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 1480 | Diag(Loc, diag::err_realimag_invalid_type) << V->getType() |
| 1481 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 1482 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1487 | Action::OwningExprResult |
| 1488 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| 1489 | tok::TokenKind Kind, ExprArg Input) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1490 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1491 | Input = MaybeConvertParenListExprToParenExpr(S, move(Input)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1492 | Expr *Arg = (Expr *)Input.get(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1493 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1494 | UnaryOperator::Opcode Opc; |
| 1495 | switch (Kind) { |
| 1496 | default: assert(0 && "Unknown unary op!"); |
| 1497 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 1498 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 1499 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1500 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1501 | if (getLangOptions().CPlusPlus && |
| 1502 | (Arg->getType()->isRecordType() || Arg->getType()->isEnumeralType())) { |
| 1503 | // Which overloaded operator? |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1504 | OverloadedOperatorKind OverOp = |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1505 | (Opc == UnaryOperator::PostInc)? OO_PlusPlus : OO_MinusMinus; |
| 1506 | |
| 1507 | // C++ [over.inc]p1: |
| 1508 | // |
| 1509 | // [...] If the function is a member function with one |
| 1510 | // parameter (which shall be of type int) or a non-member |
| 1511 | // function with two parameters (the second of which shall be |
| 1512 | // of type int), it defines the postfix increment operator ++ |
| 1513 | // for objects of that type. When the postfix increment is |
| 1514 | // called as a result of using the ++ operator, the int |
| 1515 | // argument will have value zero. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1516 | Expr *Args[2] = { |
| 1517 | Arg, |
| 1518 | new (Context) IntegerLiteral(llvm::APInt(Context.Target.getIntWidth(), 0, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1519 | /*isSigned=*/true), Context.IntTy, SourceLocation()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1520 | }; |
| 1521 | |
| 1522 | // Build the candidate set for overloading |
| 1523 | OverloadCandidateSet CandidateSet; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1524 | AddOperatorCandidates(OverOp, S, OpLoc, Args, 2, CandidateSet); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1525 | |
| 1526 | // Perform overload resolution. |
| 1527 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1528 | switch (BestViableFunction(CandidateSet, OpLoc, Best)) { |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1529 | case OR_Success: { |
| 1530 | // We found a built-in operator or an overloaded operator. |
| 1531 | FunctionDecl *FnDecl = Best->Function; |
| 1532 | |
| 1533 | if (FnDecl) { |
| 1534 | // We matched an overloaded operator. Build a call to that |
| 1535 | // operator. |
| 1536 | |
| 1537 | // Convert the arguments. |
| 1538 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1539 | if (PerformObjectArgumentInitialization(Arg, Method)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1540 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1541 | } else { |
| 1542 | // Convert the arguments. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1543 | if (PerformCopyInitialization(Arg, |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1544 | FnDecl->getParamDecl(0)->getType(), |
| 1545 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1546 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1550 | QualType ResultTy |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1551 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1552 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1554 | // Build the actual expression node. |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1555 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
Mike Stump | 488e25b | 2009-02-19 02:54:59 +0000 | [diff] [blame] | 1556 | SourceLocation()); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1557 | UsualUnaryConversions(FnExpr); |
| 1558 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1559 | Input.release(); |
Douglas Gregor | 7ff6926 | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1560 | Args[0] = Arg; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1561 | return Owned(new (Context) CXXOperatorCallExpr(Context, OverOp, FnExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1562 | Args, 2, ResultTy, |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1563 | OpLoc)); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1564 | } else { |
| 1565 | // We matched a built-in operator. Convert the arguments, then |
| 1566 | // break out so that we will build the appropriate built-in |
| 1567 | // operator node. |
| 1568 | if (PerformCopyInitialization(Arg, Best->BuiltinTypes.ParamTypes[0], |
| 1569 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1570 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1571 | |
| 1572 | break; |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1573 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | case OR_No_Viable_Function: |
| 1577 | // No viable function; fall through to handling this as a |
| 1578 | // built-in operator, which will produce an error message for us. |
| 1579 | break; |
| 1580 | |
| 1581 | case OR_Ambiguous: |
| 1582 | Diag(OpLoc, diag::err_ovl_ambiguous_oper) |
| 1583 | << UnaryOperator::getOpcodeStr(Opc) |
| 1584 | << Arg->getSourceRange(); |
| 1585 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1586 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1587 | |
| 1588 | case OR_Deleted: |
| 1589 | Diag(OpLoc, diag::err_ovl_deleted_oper) |
| 1590 | << Best->Function->isDeleted() |
| 1591 | << UnaryOperator::getOpcodeStr(Opc) |
| 1592 | << Arg->getSourceRange(); |
| 1593 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1594 | return ExprError(); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | // Either we found no viable overloaded operator or we matched a |
| 1598 | // built-in operator. In either case, fall through to trying to |
| 1599 | // build a built-in operation. |
| 1600 | } |
| 1601 | |
Eli Friedman | 6016cb2 | 2009-07-22 23:24:42 +0000 | [diff] [blame] | 1602 | Input.release(); |
| 1603 | Input = Arg; |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 1604 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(Input)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1607 | Action::OwningExprResult |
| 1608 | Sema::ActOnArraySubscriptExpr(Scope *S, ExprArg Base, SourceLocation LLoc, |
| 1609 | ExprArg Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1610 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1611 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1612 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1613 | Expr *LHSExp = static_cast<Expr*>(Base.get()), |
| 1614 | *RHSExp = static_cast<Expr*>(Idx.get()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1615 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1616 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 1617 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| 1618 | Base.release(); |
| 1619 | Idx.release(); |
| 1620 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| 1621 | Context.DependentTy, RLoc)); |
| 1622 | } |
| 1623 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1625 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 1626 | LHSExp->getType()->isEnumeralType() || |
| 1627 | RHSExp->getType()->isRecordType() || |
| 1628 | RHSExp->getType()->isEnumeralType())) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | // Add the appropriate overloaded operators (C++ [over.match.oper]) |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1630 | // to the candidate set. |
| 1631 | OverloadCandidateSet CandidateSet; |
| 1632 | Expr *Args[2] = { LHSExp, RHSExp }; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1633 | AddOperatorCandidates(OO_Subscript, S, LLoc, Args, 2, CandidateSet, |
| 1634 | SourceRange(LLoc, RLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1635 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1636 | // Perform overload resolution. |
| 1637 | OverloadCandidateSet::iterator Best; |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 1638 | switch (BestViableFunction(CandidateSet, LLoc, Best)) { |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1639 | case OR_Success: { |
| 1640 | // We found a built-in operator or an overloaded operator. |
| 1641 | FunctionDecl *FnDecl = Best->Function; |
| 1642 | |
| 1643 | if (FnDecl) { |
| 1644 | // We matched an overloaded operator. Build a call to that |
| 1645 | // operator. |
| 1646 | |
| 1647 | // Convert the arguments. |
| 1648 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(FnDecl)) { |
| 1649 | if (PerformObjectArgumentInitialization(LHSExp, Method) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | PerformCopyInitialization(RHSExp, |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1651 | FnDecl->getParamDecl(0)->getType(), |
| 1652 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1653 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1654 | } else { |
| 1655 | // Convert the arguments. |
| 1656 | if (PerformCopyInitialization(LHSExp, |
| 1657 | FnDecl->getParamDecl(0)->getType(), |
| 1658 | "passing") || |
| 1659 | PerformCopyInitialization(RHSExp, |
| 1660 | FnDecl->getParamDecl(1)->getType(), |
| 1661 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1662 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | // Determine the result type |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1666 | QualType ResultTy |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1667 | = FnDecl->getType()->getAsFunctionType()->getResultType(); |
| 1668 | ResultTy = ResultTy.getNonReferenceType(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1669 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1670 | // Build the actual expression node. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1671 | Expr *FnExpr = new (Context) DeclRefExpr(FnDecl, FnDecl->getType(), |
| 1672 | SourceLocation()); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1673 | UsualUnaryConversions(FnExpr); |
| 1674 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1675 | Base.release(); |
| 1676 | Idx.release(); |
Douglas Gregor | 7ff6926 | 2009-05-27 05:00:47 +0000 | [diff] [blame] | 1677 | Args[0] = LHSExp; |
| 1678 | Args[1] = RHSExp; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1679 | return Owned(new (Context) CXXOperatorCallExpr(Context, OO_Subscript, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | FnExpr, Args, 2, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1681 | ResultTy, LLoc)); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1682 | } else { |
| 1683 | // We matched a built-in operator. Convert the arguments, then |
| 1684 | // break out so that we will build the appropriate built-in |
| 1685 | // operator node. |
| 1686 | if (PerformCopyInitialization(LHSExp, Best->BuiltinTypes.ParamTypes[0], |
| 1687 | "passing") || |
| 1688 | PerformCopyInitialization(RHSExp, Best->BuiltinTypes.ParamTypes[1], |
| 1689 | "passing")) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1690 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1691 | |
| 1692 | break; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | case OR_No_Viable_Function: |
| 1697 | // No viable function; fall through to handling this as a |
| 1698 | // built-in operator, which will produce an error message for us. |
| 1699 | break; |
| 1700 | |
| 1701 | case OR_Ambiguous: |
| 1702 | Diag(LLoc, diag::err_ovl_ambiguous_oper) |
| 1703 | << "[]" |
| 1704 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1705 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1706 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1707 | |
| 1708 | case OR_Deleted: |
| 1709 | Diag(LLoc, diag::err_ovl_deleted_oper) |
| 1710 | << Best->Function->isDeleted() |
| 1711 | << "[]" |
| 1712 | << LHSExp->getSourceRange() << RHSExp->getSourceRange(); |
| 1713 | PrintOverloadCandidates(CandidateSet, /*OnlyViable=*/true); |
| 1714 | return ExprError(); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | // Either we found no viable overloaded operator or we matched a |
| 1718 | // built-in operator. In either case, fall through to trying to |
| 1719 | // build a built-in operation. |
| 1720 | } |
| 1721 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1722 | // Perform default conversions. |
| 1723 | DefaultFunctionArrayConversion(LHSExp); |
| 1724 | DefaultFunctionArrayConversion(RHSExp); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1725 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1726 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1727 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1729 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1730 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1731 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1732 | Expr *BaseExpr, *IndexExpr; |
| 1733 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 1734 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 1735 | BaseExpr = LHSExp; |
| 1736 | IndexExpr = RHSExp; |
| 1737 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1738 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1739 | BaseExpr = LHSExp; |
| 1740 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1741 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1742 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 1743 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1744 | BaseExpr = RHSExp; |
| 1745 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1746 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1747 | } else if (const ObjCObjectPointerType *PTy = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1748 | LHSTy->getAsObjCObjectPointerType()) { |
| 1749 | BaseExpr = LHSExp; |
| 1750 | IndexExpr = RHSExp; |
| 1751 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | } else if (const ObjCObjectPointerType *PTy = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1753 | RHSTy->getAsObjCObjectPointerType()) { |
| 1754 | // Handle the uncommon case of "123[Ptr]". |
| 1755 | BaseExpr = RHSExp; |
| 1756 | IndexExpr = LHSExp; |
| 1757 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 1758 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 1759 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1760 | IndexExpr = RHSExp; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 1761 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 1762 | // FIXME: need to deal with const... |
| 1763 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1764 | } else if (LHSTy->isArrayType()) { |
| 1765 | // If we see an array that wasn't promoted by |
| 1766 | // DefaultFunctionArrayConversion, it must be an array that |
| 1767 | // wasn't promoted because of the C90 rule that doesn't |
| 1768 | // allow promoting non-lvalue arrays. Warn, then |
| 1769 | // force the promotion here. |
| 1770 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1771 | LHSExp->getSourceRange(); |
| 1772 | ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy)); |
| 1773 | LHSTy = LHSExp->getType(); |
| 1774 | |
| 1775 | BaseExpr = LHSExp; |
| 1776 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1777 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 1778 | } else if (RHSTy->isArrayType()) { |
| 1779 | // Same as previous, except for 123[f().a] case |
| 1780 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 1781 | RHSExp->getSourceRange(); |
| 1782 | ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy)); |
| 1783 | RHSTy = RHSExp->getType(); |
| 1784 | |
| 1785 | BaseExpr = RHSExp; |
| 1786 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1787 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1788 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1789 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 1790 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1791 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1792 | // C99 6.5.2.1p1 |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1793 | if (!(IndexExpr->getType()->isIntegerType() && |
| 1794 | IndexExpr->getType()->isScalarType()) && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 1795 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 1796 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1798 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1799 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 1800 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1801 | // incomplete types are not object types. |
| 1802 | if (ResultType->isFunctionType()) { |
| 1803 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 1804 | << ResultType << BaseExpr->getSourceRange(); |
| 1805 | return ExprError(); |
| 1806 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1808 | if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 1810 | PDiag(diag::err_subscript_incomplete_type) |
| 1811 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 1812 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 1814 | // Diagnose bad cases where we step over interface counts. |
| 1815 | if (ResultType->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 1816 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 1817 | << ResultType << BaseExpr->getSourceRange(); |
| 1818 | return ExprError(); |
| 1819 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1821 | Base.release(); |
| 1822 | Idx.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1823 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 1824 | ResultType, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1825 | } |
| 1826 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1827 | QualType Sema:: |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1828 | CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1829 | const IdentifierInfo *CompName, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1830 | SourceLocation CompLoc) { |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1831 | const ExtVectorType *vecType = baseType->getAsExtVectorType(); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1832 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1833 | // The vector accessor can't exceed the number of elements. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1834 | const char *compStr = CompName->getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1835 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1836 | // This flag determines whether or not the component is one of the four |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1837 | // special names that indicate a subset of exactly half the elements are |
| 1838 | // to be selected. |
| 1839 | bool HalvingSwizzle = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1840 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1841 | // This flag determines whether or not CompName has an 's' char prefix, |
| 1842 | // indicating that it is a string of hex values to be used as vector indices. |
Nate Begeman | 131f465 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 1843 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1844 | |
| 1845 | // Check that we've found one of the special components, or that the component |
| 1846 | // names must come from the same set. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1847 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1848 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 1849 | HalvingSwizzle = true; |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1850 | } else if (vecType->getPointAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1851 | do |
| 1852 | compStr++; |
| 1853 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1854 | } else if (HexSwizzle || vecType->getNumericAccessorIdx(*compStr) != -1) { |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1855 | do |
| 1856 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1857 | while (*compStr && vecType->getNumericAccessorIdx(*compStr) != -1); |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 1858 | } |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1859 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1860 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1861 | // We didn't get to the end of the string. This means the component names |
| 1862 | // didn't come from the same set *or* we encountered an illegal name. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1863 | Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| 1864 | << std::string(compStr,compStr+1) << SourceRange(CompLoc); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1865 | return QualType(); |
| 1866 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1867 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1868 | // Ensure no component accessor exceeds the width of the vector type it |
| 1869 | // operates on. |
| 1870 | if (!HalvingSwizzle) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1871 | compStr = CompName->getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1872 | |
| 1873 | if (HexSwizzle) |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1874 | compStr++; |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1875 | |
| 1876 | while (*compStr) { |
| 1877 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| 1878 | Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| 1879 | << baseType << SourceRange(CompLoc); |
| 1880 | return QualType(); |
| 1881 | } |
| 1882 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1883 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1884 | |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1885 | // If this is a halving swizzle, verify that the base type has an even |
| 1886 | // number of elements. |
| 1887 | if (HalvingSwizzle && (vecType->getNumElements() & 1U)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1888 | Diag(OpLoc, diag::err_ext_vector_component_requires_even) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 1889 | << baseType << SourceRange(CompLoc); |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1890 | return QualType(); |
| 1891 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1892 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1893 | // The component accessor looks fine - now we need to compute the actual type. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1894 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1895 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1896 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1897 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1898 | unsigned CompSize = HalvingSwizzle ? vecType->getNumElements() / 2 |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1899 | : CompName->getLength(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1900 | if (HexSwizzle) |
| 1901 | CompSize--; |
| 1902 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1903 | if (CompSize == 1) |
| 1904 | return vecType->getElementType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1905 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1906 | QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 1907 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1908 | // diagostics look bad. We want extended vector types to appear built-in. |
| 1909 | for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) { |
| 1910 | if (ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 1911 | return Context.getTypedefType(ExtVectorDecls[i]); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 1912 | } |
| 1913 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1916 | static Decl *FindGetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1917 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1918 | const Selector &Sel, |
| 1919 | ASTContext &Context) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1921 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1922 | return PD; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1923 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1924 | return OMD; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1925 | |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1926 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 1927 | E = PDecl->protocol_end(); I != E; ++I) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1928 | if (Decl *D = FindGetterNameDeclFromProtocolList(*I, Member, Sel, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1929 | Context)) |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1930 | return D; |
| 1931 | } |
| 1932 | return 0; |
| 1933 | } |
| 1934 | |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1935 | static Decl *FindGetterNameDecl(const ObjCObjectPointerType *QIdTy, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1936 | IdentifierInfo *Member, |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1937 | const Selector &Sel, |
| 1938 | ASTContext &Context) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1939 | // Check protocols on qualified interfaces. |
| 1940 | Decl *GDecl = 0; |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1941 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1942 | E = QIdTy->qual_end(); I != E; ++I) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1943 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1944 | GDecl = PD; |
| 1945 | break; |
| 1946 | } |
| 1947 | // Also must look for a getter name which uses property syntax. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1948 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1949 | GDecl = OMD; |
| 1950 | break; |
| 1951 | } |
| 1952 | } |
| 1953 | if (!GDecl) { |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1954 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1955 | E = QIdTy->qual_end(); I != E; ++I) { |
| 1956 | // Search in the protocol-qualifier list of current protocol. |
Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1957 | GDecl = FindGetterNameDeclFromProtocolList(*I, Member, Sel, Context); |
Fariborz Jahanian | 2ce1be0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 1958 | if (GDecl) |
| 1959 | return GDecl; |
| 1960 | } |
| 1961 | } |
| 1962 | return GDecl; |
| 1963 | } |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 1964 | |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1965 | /// FindMethodInNestedImplementations - Look up a method in current and |
| 1966 | /// all base class implementations. |
| 1967 | /// |
| 1968 | ObjCMethodDecl *Sema::FindMethodInNestedImplementations( |
| 1969 | const ObjCInterfaceDecl *IFace, |
| 1970 | const Selector &Sel) { |
| 1971 | ObjCMethodDecl *Method = 0; |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1972 | if (ObjCImplementationDecl *ImpDecl = IFace->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1973 | Method = ImpDecl->getInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 1975 | if (!Method && IFace->getSuperClass()) |
| 1976 | return FindMethodInNestedImplementations(IFace->getSuperClass(), Sel); |
| 1977 | return Method; |
| 1978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
| 1980 | Action::OwningExprResult |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 1981 | Sema::BuildMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 1982 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | DeclarationName MemberName, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 1984 | bool HasExplicitTemplateArgs, |
| 1985 | SourceLocation LAngleLoc, |
| 1986 | const TemplateArgument *ExplicitTemplateArgs, |
| 1987 | unsigned NumExplicitTemplateArgs, |
| 1988 | SourceLocation RAngleLoc, |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 1989 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS, |
| 1990 | NamedDecl *FirstQualifierInScope) { |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 1991 | if (SS && SS->isInvalid()) |
| 1992 | return ExprError(); |
| 1993 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1994 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 1995 | Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); |
| 1996 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 1997 | Expr *BaseExpr = Base.takeAs<Expr>(); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 1998 | assert(BaseExpr && "no base expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Steve Naroff | 3cc4af8 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 2000 | // Perform default conversions. |
| 2001 | DefaultFunctionArrayConversion(BaseExpr); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2002 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2003 | QualType BaseType = BaseExpr->getType(); |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2004 | // If this is an Objective-C pseudo-builtin and a definition is provided then |
| 2005 | // use that. |
| 2006 | if (BaseType->isObjCIdType()) { |
| 2007 | // We have an 'id' type. Rather than fall through, we check if this |
| 2008 | // is a reference to 'isa'. |
| 2009 | if (BaseType != Context.ObjCIdRedefinitionType) { |
| 2010 | BaseType = Context.ObjCIdRedefinitionType; |
| 2011 | ImpCastExprToType(BaseExpr, BaseType); |
| 2012 | } |
| 2013 | } else if (BaseType->isObjCClassType() && |
Douglas Gregor | f328a28 | 2009-08-31 21:16:32 +0000 | [diff] [blame] | 2014 | BaseType != Context.ObjCClassRedefinitionType) { |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 2015 | BaseType = Context.ObjCClassRedefinitionType; |
| 2016 | ImpCastExprToType(BaseExpr, BaseType); |
| 2017 | } |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2018 | assert(!BaseType.isNull() && "no type for member expression"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2019 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2020 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 2021 | // must have pointer type, and the accessed type is the pointee. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2022 | if (OpKind == tok::arrow) { |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2023 | if (BaseType->isDependentType()) { |
| 2024 | NestedNameSpecifier *Qualifier = 0; |
| 2025 | if (SS) { |
| 2026 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 2027 | if (!FirstQualifierInScope) |
| 2028 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 2029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
| 2031 | return Owned(CXXUnresolvedMemberExpr::Create(Context, BaseExpr, true, |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2032 | OpLoc, Qualifier, |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2033 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2034 | FirstQualifierInScope, |
| 2035 | MemberName, |
| 2036 | MemberLoc, |
| 2037 | HasExplicitTemplateArgs, |
| 2038 | LAngleLoc, |
| 2039 | ExplicitTemplateArgs, |
| 2040 | NumExplicitTemplateArgs, |
| 2041 | RAngleLoc)); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2042 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2043 | else if (const PointerType *PT = BaseType->getAs<PointerType>()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2044 | BaseType = PT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2045 | else if (BaseType->isObjCObjectPointerType()) |
| 2046 | ; |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2047 | else |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2048 | return ExprError(Diag(MemberLoc, |
| 2049 | diag::err_typecheck_member_reference_arrow) |
| 2050 | << BaseType << BaseExpr->getSourceRange()); |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 2051 | } else { |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2052 | if (BaseType->isDependentType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | // Require that the base type isn't a pointer type |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2054 | // (so we'll report an error for) |
| 2055 | // T* t; |
| 2056 | // t.f; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | // |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2058 | // In Obj-C++, however, the above expression is valid, since it could be |
| 2059 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 2060 | // allows this, while still reporting an error if T is a struct pointer. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2061 | const PointerType *PT = BaseType->getAs<PointerType>(); |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2062 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2063 | if (!PT || (getLangOptions().ObjC1 && |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2064 | !PT->getPointeeType()->isRecordType())) { |
| 2065 | NestedNameSpecifier *Qualifier = 0; |
| 2066 | if (SS) { |
| 2067 | Qualifier = static_cast<NestedNameSpecifier *>(SS->getScopeRep()); |
| 2068 | if (!FirstQualifierInScope) |
| 2069 | FirstQualifierInScope = FindFirstQualifierInScope(S, Qualifier); |
| 2070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2072 | return Owned(CXXUnresolvedMemberExpr::Create(Context, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | BaseExpr, false, |
| 2074 | OpLoc, |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2075 | Qualifier, |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2076 | SS? SS->getRange() : SourceRange(), |
Douglas Gregor | 3b6afbb | 2009-09-09 00:23:06 +0000 | [diff] [blame] | 2077 | FirstQualifierInScope, |
| 2078 | MemberName, |
| 2079 | MemberLoc, |
| 2080 | HasExplicitTemplateArgs, |
| 2081 | LAngleLoc, |
| 2082 | ExplicitTemplateArgs, |
| 2083 | NumExplicitTemplateArgs, |
| 2084 | RAngleLoc)); |
Douglas Gregor | c68afe2 | 2009-09-03 21:38:09 +0000 | [diff] [blame] | 2085 | } |
Anders Carlsson | 4ef2770 | 2009-05-16 20:31:20 +0000 | [diff] [blame] | 2086 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2087 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2088 | |
Chris Lattner | 68a057b | 2008-07-21 04:36:39 +0000 | [diff] [blame] | 2089 | // Handle field access to simple records. This also handles access to fields |
| 2090 | // of the ObjC 'id' struct. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2091 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2092 | RecordDecl *RDecl = RTy->getDecl(); |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 2093 | if (RequireCompleteType(OpLoc, BaseType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2094 | PDiag(diag::err_typecheck_incomplete_tag) |
| 2095 | << BaseExpr->getSourceRange())) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 2096 | return ExprError(); |
| 2097 | |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2098 | DeclContext *DC = RDecl; |
| 2099 | if (SS && SS->isSet()) { |
| 2100 | // If the member name was a qualified-id, look into the |
| 2101 | // nested-name-specifier. |
| 2102 | DC = computeDeclContext(*SS, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2103 | |
| 2104 | // FIXME: If DC is not computable, we should build a |
Douglas Gregor | fe85ced | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 2105 | // CXXUnresolvedMemberExpr. |
| 2106 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| 2107 | } |
| 2108 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 2109 | // The record definition is complete, now make sure the member is valid. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2110 | LookupResult Result |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2111 | = LookupQualifiedName(DC, MemberName, LookupMemberName, false); |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 7176fff | 2009-01-15 00:26:24 +0000 | [diff] [blame] | 2113 | if (!Result) |
Anders Carlsson | f4d84b6 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 2114 | return ExprError(Diag(MemberLoc, diag::err_typecheck_no_member_deprecated) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2115 | << MemberName << BaseExpr->getSourceRange()); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2116 | if (Result.isAmbiguous()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2117 | DiagnoseAmbiguousLookup(Result, MemberName, MemberLoc, |
| 2118 | BaseExpr->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2119 | return ExprError(); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2122 | if (SS && SS->isSet()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | QualType BaseTypeCanon |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2124 | = Context.getCanonicalType(BaseType).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2125 | QualType MemberTypeCanon |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2126 | = Context.getCanonicalType( |
| 2127 | Context.getTypeDeclType( |
| 2128 | dyn_cast<TypeDecl>(Result.getAsDecl()->getDeclContext()))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | a38c687 | 2009-09-03 16:14:30 +0000 | [diff] [blame] | 2130 | if (BaseTypeCanon != MemberTypeCanon && |
| 2131 | !IsDerivedFrom(BaseTypeCanon, MemberTypeCanon)) |
| 2132 | return ExprError(Diag(SS->getBeginLoc(), |
| 2133 | diag::err_not_direct_base_or_virtual) |
| 2134 | << MemberTypeCanon << BaseTypeCanon); |
| 2135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2136 | |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2137 | NamedDecl *MemberDecl = Result; |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2138 | |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2139 | // If the decl being referenced had an error, return an error for this |
| 2140 | // sub-expr without emitting another error, in order to avoid cascading |
| 2141 | // error cases. |
| 2142 | if (MemberDecl->isInvalidDecl()) |
| 2143 | return ExprError(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2145 | // Check the use of this field |
| 2146 | if (DiagnoseUseOfDecl(MemberDecl, MemberLoc)) |
| 2147 | return ExprError(); |
Chris Lattner | 56cd21b | 2009-02-13 22:08:30 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2149 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) { |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2150 | // We may have found a field within an anonymous union or struct |
| 2151 | // (C++ [class.union]). |
| 2152 | if (cast<RecordDecl>(FD->getDeclContext())->isAnonymousStructOrUnion()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2153 | return BuildAnonymousStructUnionMemberReference(MemberLoc, FD, |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2154 | BaseExpr, OpLoc); |
Douglas Gregor | bcbffc4 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2155 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2156 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2157 | QualType MemberType = FD->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2158 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2159 | MemberType = Ref->getPointeeType(); |
| 2160 | else { |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2161 | unsigned BaseAddrSpace = BaseType.getAddressSpace(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2162 | unsigned combinedQualifiers = |
| 2163 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Douglas Gregor | 3fc749d | 2008-12-23 00:26:44 +0000 | [diff] [blame] | 2164 | if (FD->isMutable()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2165 | combinedQualifiers &= ~QualType::Const; |
| 2166 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
Mon P Wang | c6a38a4 | 2009-07-22 03:08:17 +0000 | [diff] [blame] | 2167 | if (BaseAddrSpace != MemberType.getAddressSpace()) |
| 2168 | MemberType = Context.getAddrSpaceQualType(MemberType, BaseAddrSpace); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2169 | } |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2171 | MarkDeclarationReferenced(MemberLoc, FD); |
Fariborz Jahanian | f3e53d3 | 2009-07-29 19:40:11 +0000 | [diff] [blame] | 2172 | if (PerformObjectMemberConversion(BaseExpr, FD)) |
| 2173 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2174 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2175 | FD, MemberLoc, MemberType)); |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2176 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2178 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 2179 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2180 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2181 | Var, MemberLoc, |
| 2182 | Var->getType().getNonReferenceType())); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2183 | } |
| 2184 | if (FunctionDecl *MemberFn = dyn_cast<FunctionDecl>(MemberDecl)) { |
| 2185 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2186 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2187 | MemberFn, MemberLoc, |
| 2188 | MemberFn->getType())); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | if (FunctionTemplateDecl *FunTmpl |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2191 | = dyn_cast<FunctionTemplateDecl>(MemberDecl)) { |
| 2192 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2193 | |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2194 | if (HasExplicitTemplateArgs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2196 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2197 | SS? SS->getRange() : SourceRange(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | FunTmpl, MemberLoc, true, |
| 2199 | LAngleLoc, ExplicitTemplateArgs, |
| 2200 | NumExplicitTemplateArgs, RAngleLoc, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2201 | Context.OverloadTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2203 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2204 | FunTmpl, MemberLoc, |
| 2205 | Context.OverloadTy)); |
Douglas Gregor | 6b90686 | 2009-08-21 00:16:32 +0000 | [diff] [blame] | 2206 | } |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2207 | if (OverloadedFunctionDecl *Ovl |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2208 | = dyn_cast<OverloadedFunctionDecl>(MemberDecl)) { |
| 2209 | if (HasExplicitTemplateArgs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | return Owned(MemberExpr::Create(Context, BaseExpr, OpKind == tok::arrow, |
| 2211 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2212 | SS? SS->getRange() : SourceRange(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | Ovl, MemberLoc, true, |
| 2214 | LAngleLoc, ExplicitTemplateArgs, |
| 2215 | NumExplicitTemplateArgs, RAngleLoc, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2216 | Context.OverloadTy)); |
| 2217 | |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2218 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2219 | Ovl, MemberLoc, Context.OverloadTy)); |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 2220 | } |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2221 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 2222 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
Douglas Gregor | bd4c4ae | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2223 | return Owned(BuildMemberExpr(Context, BaseExpr, OpKind == tok::arrow, SS, |
| 2224 | Enum, MemberLoc, Enum->getType())); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 2225 | } |
Chris Lattner | a3d2524 | 2009-03-31 08:18:48 +0000 | [diff] [blame] | 2226 | if (isa<TypeDecl>(MemberDecl)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2227 | return ExprError(Diag(MemberLoc,diag::err_typecheck_member_reference_type) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2228 | << MemberName << int(OpKind == tok::arrow)); |
Eli Friedman | 5101907 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2229 | |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2230 | // We found a declaration kind that we didn't expect. This is a |
| 2231 | // generic error message that tells the user that she can't refer |
| 2232 | // to this member with '.' or '->'. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2233 | return ExprError(Diag(MemberLoc, |
| 2234 | diag::err_typecheck_member_reference_unknown) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2235 | << MemberName << int(OpKind == tok::arrow)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2236 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2237 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2238 | // Handle pseudo-destructors (C++ [expr.pseudo]). Since anything referring |
| 2239 | // into a record type was handled above, any destructor we see here is a |
| 2240 | // pseudo-destructor. |
| 2241 | if (MemberName.getNameKind() == DeclarationName::CXXDestructorName) { |
| 2242 | // C++ [expr.pseudo]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | // The left hand side of the dot operator shall be of scalar type. The |
| 2244 | // left hand side of the arrow operator shall be of pointer to scalar |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2245 | // type. |
| 2246 | if (!BaseType->isScalarType()) |
| 2247 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_base_not_scalar) |
| 2248 | << BaseType << BaseExpr->getSourceRange()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2250 | // [...] The type designated by the pseudo-destructor-name shall be the |
| 2251 | // same as the object type. |
| 2252 | if (!MemberName.getCXXNameType()->isDependentType() && |
| 2253 | !Context.hasSameUnqualifiedType(BaseType, MemberName.getCXXNameType())) |
| 2254 | return Owned(Diag(OpLoc, diag::err_pseudo_dtor_type_mismatch) |
| 2255 | << BaseType << MemberName.getCXXNameType() |
| 2256 | << BaseExpr->getSourceRange() << SourceRange(MemberLoc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | |
| 2258 | // [...] Furthermore, the two type-names in a pseudo-destructor-name of |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2259 | // the form |
| 2260 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2261 | // ::[opt] nested-name-specifier[opt] type-name :: ̃ type-name |
| 2262 | // |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2263 | // shall designate the same scalar type. |
| 2264 | // |
| 2265 | // FIXME: DPG can't see any way to trigger this particular clause, so it |
| 2266 | // isn't checked here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2267 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2268 | // FIXME: We've lost the precise spelling of the type by going through |
| 2269 | // DeclarationName. Can we do better? |
| 2270 | return Owned(new (Context) CXXPseudoDestructorExpr(Context, BaseExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2271 | OpKind == tok::arrow, |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2272 | OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | (NestedNameSpecifier *)(SS? SS->getScopeRep() : 0), |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2274 | SS? SS->getRange() : SourceRange(), |
| 2275 | MemberName.getCXXNameType(), |
| 2276 | MemberLoc)); |
| 2277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2278 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2279 | // Handle properties on ObjC 'Class' types. |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 2280 | if (OpKind == tok::period && BaseType->isObjCClassType()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2281 | // Also must look for a getter name which uses property syntax. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2282 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2283 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2284 | if (ObjCMethodDecl *MD = getCurMethodDecl()) { |
| 2285 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 2286 | ObjCMethodDecl *Getter; |
| 2287 | // FIXME: need to also look locally in the implementation. |
| 2288 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 2289 | // Check the use of this method. |
| 2290 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2291 | return ExprError(); |
| 2292 | } |
| 2293 | // If we found a getter then this may be a valid dot-reference, we |
| 2294 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | Selector SetterSel = |
| 2296 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2297 | PP.getSelectorTable(), Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2298 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 2299 | if (!Setter) { |
| 2300 | // If this reference is in an @implementation, also check for 'private' |
| 2301 | // methods. |
| 2302 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
| 2303 | } |
| 2304 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2305 | if (!Setter) |
| 2306 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2307 | |
| 2308 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2309 | return ExprError(); |
| 2310 | |
| 2311 | if (Getter || Setter) { |
| 2312 | QualType PType; |
| 2313 | |
| 2314 | if (Getter) |
| 2315 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2316 | else |
| 2317 | // Get the expression type from Setter's incoming parameter. |
| 2318 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2319 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2320 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2321 | Setter, MemberLoc, BaseExpr)); |
| 2322 | } |
| 2323 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2324 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2325 | } |
| 2326 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2327 | // Handle access to Objective-C instance variables, such as "Obj->ivar" and |
| 2328 | // (*Obj).ivar. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2329 | if ((OpKind == tok::arrow && BaseType->isObjCObjectPointerType()) || |
| 2330 | (OpKind == tok::period && BaseType->isObjCInterfaceType())) { |
| 2331 | const ObjCObjectPointerType *OPT = BaseType->getAsObjCObjectPointerType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | const ObjCInterfaceType *IFaceT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2333 | OPT ? OPT->getInterfaceType() : BaseType->getAsObjCInterfaceType(); |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2334 | if (IFaceT) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2335 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 2336 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2337 | ObjCInterfaceDecl *IDecl = IFaceT->getDecl(); |
| 2338 | ObjCInterfaceDecl *ClassDeclared; |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2339 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2340 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2341 | if (IV) { |
| 2342 | // If the decl being referenced had an error, return an error for this |
| 2343 | // sub-expr without emitting another error, in order to avoid cascading |
| 2344 | // error cases. |
| 2345 | if (IV->isInvalidDecl()) |
| 2346 | return ExprError(); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2347 | |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2348 | // Check whether we can reference this field. |
| 2349 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 2350 | return ExprError(); |
| 2351 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 2352 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 2353 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 2354 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 2355 | ClassOfMethodDecl = MD->getClassInterface(); |
| 2356 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 2357 | // Case of a c-function declared inside an objc implementation. |
| 2358 | // FIXME: For a c-style function nested inside an objc implementation |
| 2359 | // class, there is no implementation context available, so we pass |
| 2360 | // down the context as argument to this routine. Ideally, this context |
| 2361 | // need be passed down in the AST node and somehow calculated from the |
| 2362 | // AST for a function decl. |
| 2363 | Decl *ImplDecl = ObjCImpDecl.getAs<Decl>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2364 | if (ObjCImplementationDecl *IMPD = |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2365 | dyn_cast<ObjCImplementationDecl>(ImplDecl)) |
| 2366 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 2367 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 2368 | dyn_cast<ObjCCategoryImplDecl>(ImplDecl)) |
| 2369 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 2370 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2371 | |
| 2372 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 2373 | if (ClassDeclared != IDecl || |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2374 | ClassOfMethodDecl != ClassDeclared) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2375 | Diag(MemberLoc, diag::error_private_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2376 | << IV->getDeclName(); |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2377 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 2378 | // @protected |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2379 | Diag(MemberLoc, diag::error_protected_ivar_access) |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2380 | << IV->getDeclName(); |
Steve Naroff | b06d875 | 2009-03-04 18:34:24 +0000 | [diff] [blame] | 2381 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2382 | |
| 2383 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| 2384 | MemberLoc, BaseExpr, |
| 2385 | OpKind == tok::arrow)); |
Fariborz Jahanian | 935fd76 | 2009-03-03 01:21:12 +0000 | [diff] [blame] | 2386 | } |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2387 | return ExprError(Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2388 | << IDecl->getDeclName() << MemberName |
Steve Naroff | c70e8d9 | 2009-07-16 00:25:06 +0000 | [diff] [blame] | 2389 | << BaseExpr->getSourceRange()); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 2390 | } |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2391 | } |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2392 | // Handle properties on 'id' and qualified "id". |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | if (OpKind == tok::period && (BaseType->isObjCIdType() || |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 2394 | BaseType->isObjCQualifiedIdType())) { |
| 2395 | const ObjCObjectPointerType *QIdTy = BaseType->getAsObjCObjectPointerType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2396 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2398 | // Check protocols on qualified interfaces. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2399 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2400 | if (Decl *PMDecl = FindGetterNameDecl(QIdTy, Member, Sel, Context)) { |
| 2401 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 2402 | // Check the use of this declaration |
| 2403 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2404 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2405 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2406 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2407 | MemberLoc, BaseExpr)); |
| 2408 | } |
| 2409 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 2410 | // Check the use of this method. |
| 2411 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 2412 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2413 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2414 | return Owned(new (Context) ObjCMessageExpr(BaseExpr, Sel, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | OMD->getResultType(), |
| 2416 | OMD, OpLoc, MemberLoc, |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2417 | NULL, 0)); |
| 2418 | } |
| 2419 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2420 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2421 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2422 | << MemberName << BaseType); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2423 | } |
Chris Lattner | a38e6b1 | 2008-07-21 04:59:05 +0000 | [diff] [blame] | 2424 | // Handle Objective-C property access, which is "Obj.property" where Obj is a |
| 2425 | // pointer to a (potentially qualified) interface type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2426 | const ObjCObjectPointerType *OPT; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | if (OpKind == tok::period && |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2428 | (OPT = BaseType->getAsObjCInterfacePointerType())) { |
| 2429 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 2430 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2431 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2432 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2433 | // Search for a declared property first. |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2434 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2435 | // Check whether we can reference this property. |
| 2436 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2437 | return ExprError(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2438 | QualType ResTy = PD->getType(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2439 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2440 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | c001e89 | 2009-05-08 20:20:55 +0000 | [diff] [blame] | 2441 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
| 2442 | ResTy = Getter->getResultType(); |
Fariborz Jahanian | 4c2743f | 2009-05-08 19:36:34 +0000 | [diff] [blame] | 2443 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2444 | MemberLoc, BaseExpr)); |
| 2445 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2446 | // Check protocols on qualified interfaces. |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 2447 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2448 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2449 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2450 | // Check whether we can reference this property. |
| 2451 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2452 | return ExprError(); |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2453 | |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2454 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
Chris Lattner | 7eba82e | 2009-02-16 18:35:08 +0000 | [diff] [blame] | 2455 | MemberLoc, BaseExpr)); |
| 2456 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2457 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 2458 | E = OPT->qual_end(); I != E; ++I) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2459 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2460 | // Check whether we can reference this property. |
| 2461 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 2462 | return ExprError(); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2463 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2464 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 2465 | MemberLoc, BaseExpr)); |
| 2466 | } |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2467 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 2468 | // selector is implemented. |
| 2469 | |
| 2470 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 2471 | // shared with the code in ActOnInstanceMessage. |
| 2472 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2473 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2474 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2475 | |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2476 | // If this reference is in an @implementation, check for 'private' methods. |
| 2477 | if (!Getter) |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2478 | Getter = FindMethodInNestedImplementations(IFace, Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2479 | |
Steve Naroff | 7692ed6 | 2008-10-22 19:16:27 +0000 | [diff] [blame] | 2480 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2481 | if (!Getter) |
| 2482 | Getter = IFace->getCategoryInstanceMethod(Sel); |
Daniel Dunbar | 2307d31 | 2008-09-03 01:05:41 +0000 | [diff] [blame] | 2483 | if (Getter) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2484 | // Check if we can reference this property. |
| 2485 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 2486 | return ExprError(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2487 | } |
| 2488 | // If we found a getter then this may be a valid dot-reference, we |
| 2489 | // will look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | Selector SetterSel = |
| 2491 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2492 | PP.getSelectorTable(), Member); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2493 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2494 | if (!Setter) { |
| 2495 | // If this reference is in an @implementation, also check for 'private' |
| 2496 | // methods. |
Fariborz Jahanian | ef79bc9 | 2009-04-07 18:28:06 +0000 | [diff] [blame] | 2497 | Setter = FindMethodInNestedImplementations(IFace, SetterSel); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2498 | } |
| 2499 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2500 | if (!Setter) |
| 2501 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2502 | |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2503 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 2504 | return ExprError(); |
| 2505 | |
| 2506 | if (Getter || Setter) { |
| 2507 | QualType PType; |
| 2508 | |
| 2509 | if (Getter) |
| 2510 | PType = Getter->getResultType(); |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2511 | else |
| 2512 | // Get the expression type from Setter's incoming parameter. |
| 2513 | PType = (*(Setter->param_end() -1))->getType(); |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2514 | // FIXME: we must check that the setter has property type. |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2515 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
Steve Naroff | 1ca6694 | 2009-03-11 13:48:17 +0000 | [diff] [blame] | 2516 | Setter, MemberLoc, BaseExpr)); |
| 2517 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2518 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2519 | << MemberName << BaseType); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 2520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2522 | // Handle the following exceptional case (*Obj).isa. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | if (OpKind == tok::period && |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2524 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCId) && |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2525 | MemberName.getAsIdentifierInfo()->isStr("isa")) |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2526 | return Owned(new (Context) ObjCIsaExpr(BaseExpr, false, MemberLoc, |
| 2527 | Context.getObjCIdType())); |
| 2528 | |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2529 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 73525de | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 2530 | if (BaseType->isExtVectorType()) { |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2531 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2532 | QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 2533 | if (ret.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2534 | return ExprError(); |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2535 | return Owned(new (Context) ExtVectorElementExpr(ret, BaseExpr, *Member, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2536 | MemberLoc)); |
Chris Lattner | fb173ec | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 2537 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2538 | |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2539 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| 2540 | << BaseType << BaseExpr->getSourceRange(); |
| 2541 | |
| 2542 | // If the user is trying to apply -> or . to a function or function |
| 2543 | // pointer, it's probably because they forgot parentheses to call |
| 2544 | // the function. Suggest the addition of those parentheses. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2545 | if (BaseType == Context.OverloadTy || |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2546 | BaseType->isFunctionType() || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | (BaseType->isPointerType() && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2548 | BaseType->getAs<PointerType>()->isFunctionType())) { |
Douglas Gregor | 214f31a | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 2549 | SourceLocation Loc = PP.getLocForEndOfToken(BaseExpr->getLocEnd()); |
| 2550 | Diag(Loc, diag::note_member_reference_needs_call) |
| 2551 | << CodeModificationHint::CreateInsertion(Loc, "()"); |
| 2552 | } |
| 2553 | |
| 2554 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2557 | Action::OwningExprResult |
| 2558 | Sema::ActOnMemberReferenceExpr(Scope *S, ExprArg Base, SourceLocation OpLoc, |
| 2559 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 2560 | IdentifierInfo &Member, |
| 2561 | DeclPtrTy ObjCImpDecl, const CXXScopeSpec *SS) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | return BuildMemberReferenceExpr(S, move(Base), OpLoc, OpKind, MemberLoc, |
Anders Carlsson | 8f28f99 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 2563 | DeclarationName(&Member), ObjCImpDecl, SS); |
| 2564 | } |
| 2565 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2566 | Sema::OwningExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| 2567 | FunctionDecl *FD, |
| 2568 | ParmVarDecl *Param) { |
| 2569 | if (Param->hasUnparsedDefaultArg()) { |
| 2570 | Diag (CallLoc, |
| 2571 | diag::err_use_of_default_argument_to_function_declared_later) << |
| 2572 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2573 | Diag(UnparsedDefaultArgLocs[Param], |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2574 | diag::note_default_argument_declared_here); |
| 2575 | } else { |
| 2576 | if (Param->hasUninstantiatedDefaultArg()) { |
| 2577 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| 2578 | |
| 2579 | // Instantiate the expression. |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2580 | MultiLevelTemplateArgumentList ArgList = getTemplateInstantiationArgs(FD); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 2581 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 2583 | ArgList.getInnermost().getFlatArgumentList(), |
Douglas Gregor | d6350ae | 2009-08-28 20:31:08 +0000 | [diff] [blame] | 2584 | ArgList.getInnermost().flat_size()); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2585 | |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 2586 | OwningExprResult Result = SubstExpr(UninstExpr, ArgList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | if (Result.isInvalid()) |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2588 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | |
| 2590 | if (SetParamDefaultArgument(Param, move(Result), |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2591 | /*FIXME:EqualLoc*/ |
| 2592 | UninstExpr->getSourceRange().getBegin())) |
| 2593 | return ExprError(); |
| 2594 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2596 | Expr *DefaultExpr = Param->getDefaultArg(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2597 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2598 | // If the default expression creates temporaries, we need to |
| 2599 | // push them to the current stack of expression temporaries so they'll |
| 2600 | // be properly destroyed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2601 | if (CXXExprWithTemporaries *E |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2602 | = dyn_cast_or_null<CXXExprWithTemporaries>(DefaultExpr)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2603 | assert(!E->shouldDestroyTemporaries() && |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2604 | "Can't destroy temporaries in a default argument expr!"); |
| 2605 | for (unsigned I = 0, N = E->getNumTemporaries(); I != N; ++I) |
| 2606 | ExprTemporaries.push_back(E->getTemporary(I)); |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | // We already type-checked the argument, so we know it works. |
| 2611 | return Owned(CXXDefaultArgExpr::Create(Context, Param)); |
| 2612 | } |
| 2613 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2614 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 2615 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 2616 | /// function prototype Proto. Call is the call expression itself, and |
| 2617 | /// Fn is the function expression. For a C++ member function, this |
| 2618 | /// routine does not attempt to convert the object argument. Returns |
| 2619 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2620 | bool |
| 2621 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2622 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2623 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2624 | Expr **Args, unsigned NumArgs, |
| 2625 | SourceLocation RParenLoc) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2626 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2627 | // assignment, to the types of the corresponding parameter, ... |
| 2628 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 2629 | unsigned NumArgsToCheck = NumArgs; |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2630 | bool Invalid = false; |
| 2631 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2632 | // If too few arguments are available (and we don't have default |
| 2633 | // arguments for the remaining parameters), don't make the call. |
| 2634 | if (NumArgs < NumArgsInProto) { |
| 2635 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 2636 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| 2637 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange(); |
| 2638 | // Use default arguments for missing arguments |
| 2639 | NumArgsToCheck = NumArgsInProto; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2640 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | // If too many are passed and not variadic, error on the extras and drop |
| 2644 | // them. |
| 2645 | if (NumArgs > NumArgsInProto) { |
| 2646 | if (!Proto->isVariadic()) { |
| 2647 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 2648 | diag::err_typecheck_call_too_many_args) |
| 2649 | << Fn->getType()->isBlockPointerType() << Fn->getSourceRange() |
| 2650 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 2651 | Args[NumArgs-1]->getLocEnd()); |
| 2652 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2653 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2654 | Invalid = true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2655 | } |
| 2656 | NumArgsToCheck = NumArgsInProto; |
| 2657 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2658 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2659 | // Continue to check argument types (even if we have too few/many args). |
| 2660 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 2661 | QualType ProtoArgType = Proto->getArgType(i); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2662 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2663 | Expr *Arg; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2664 | if (i < NumArgs) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2665 | Arg = Args[i]; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2666 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2667 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2668 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2669 | PDiag(diag::err_call_incomplete_argument) |
| 2670 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2671 | return true; |
| 2672 | |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 2673 | // Pass the argument. |
| 2674 | if (PerformCopyInitialization(Arg, ProtoArgType, "passing")) |
| 2675 | return true; |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2676 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 2677 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | |
| 2679 | OwningExprResult ArgExpr = |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2680 | BuildCXXDefaultArgExpr(Call->getSourceRange().getBegin(), |
| 2681 | FDecl, Param); |
| 2682 | if (ArgExpr.isInvalid()) |
| 2683 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 2685 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 2686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2688 | Call->setArg(i, Arg); |
| 2689 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2690 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2691 | // If this is a variadic call, handle args passed through "...". |
| 2692 | if (Proto->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 2693 | VariadicCallType CallType = VariadicFunction; |
| 2694 | if (Fn->getType()->isBlockPointerType()) |
| 2695 | CallType = VariadicBlock; // Block |
| 2696 | else if (isa<MemberExpr>(Fn)) |
| 2697 | CallType = VariadicMethod; |
| 2698 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2699 | // Promote the arguments (C99 6.5.2.2p7). |
| 2700 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 2701 | Expr *Arg = Args[i]; |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 2702 | Invalid |= DefaultVariadicArgumentPromotion(Arg, CallType); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2703 | Call->setArg(i, Arg); |
| 2704 | } |
| 2705 | } |
| 2706 | |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 2707 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2708 | } |
| 2709 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2710 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2711 | /// This provides the location of the left/right parens and a list of comma |
| 2712 | /// locations. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2713 | Action::OwningExprResult |
| 2714 | Sema::ActOnCallExpr(Scope *S, ExprArg fn, SourceLocation LParenLoc, |
| 2715 | MultiExprArg args, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2716 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2717 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2718 | |
| 2719 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| 2720 | fn = MaybeConvertParenListExprToParenExpr(S, move(fn)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2721 | |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 2722 | Expr *Fn = fn.takeAs<Expr>(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2723 | Expr **Args = reinterpret_cast<Expr**>(args.release()); |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 2724 | assert(Fn && "no function call expression"); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2725 | FunctionDecl *FDecl = NULL; |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2726 | NamedDecl *NDecl = NULL; |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2727 | DeclarationName UnqualifiedName; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2729 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2730 | // If this is a pseudo-destructor expression, build the call immediately. |
| 2731 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 2732 | if (NumArgs > 0) { |
| 2733 | // Pseudo-destructor calls should not have any arguments. |
| 2734 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| 2735 | << CodeModificationHint::CreateRemoval( |
| 2736 | SourceRange(Args[0]->getLocStart(), |
| 2737 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2739 | for (unsigned I = 0; I != NumArgs; ++I) |
| 2740 | Args[I]->Destroy(Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2741 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2742 | NumArgs = 0; |
| 2743 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2744 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 2745 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| 2746 | RParenLoc)); |
| 2747 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2749 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2750 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2751 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 2752 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2753 | bool Dependent = false; |
| 2754 | if (Fn->isTypeDependent()) |
| 2755 | Dependent = true; |
| 2756 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 2757 | Dependent = true; |
| 2758 | |
| 2759 | if (Dependent) |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2760 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2761 | Context.DependentTy, RParenLoc)); |
| 2762 | |
| 2763 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 2764 | if (Fn->getType()->isRecordType()) |
| 2765 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| 2766 | CommaLocs, RParenLoc)); |
| 2767 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2768 | // Determine whether this is a call to a member function. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2769 | if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(Fn->IgnoreParens())) { |
| 2770 | NamedDecl *MemDecl = MemExpr->getMemberDecl(); |
| 2771 | if (isa<OverloadedFunctionDecl>(MemDecl) || |
| 2772 | isa<CXXMethodDecl>(MemDecl) || |
| 2773 | (isa<FunctionTemplateDecl>(MemDecl) && |
| 2774 | isa<CXXMethodDecl>( |
| 2775 | cast<FunctionTemplateDecl>(MemDecl)->getTemplatedDecl()))) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2776 | return Owned(BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| 2777 | CommaLocs, RParenLoc)); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2778 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2781 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | // Also, in C++, keep track of whether we should perform argument-dependent |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2783 | // lookup and whether there were any explicitly-specified template arguments. |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2784 | Expr *FnExpr = Fn; |
| 2785 | bool ADL = true; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2786 | bool HasExplicitTemplateArgs = 0; |
| 2787 | const TemplateArgument *ExplicitTemplateArgs = 0; |
| 2788 | unsigned NumExplicitTemplateArgs = 0; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2789 | while (true) { |
| 2790 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(FnExpr)) |
| 2791 | FnExpr = IcExpr->getSubExpr(); |
| 2792 | else if (ParenExpr *PExpr = dyn_cast<ParenExpr>(FnExpr)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2793 | // Parentheses around a function disable ADL |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2794 | // (C++0x [basic.lookup.argdep]p1). |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2795 | ADL = false; |
| 2796 | FnExpr = PExpr->getSubExpr(); |
| 2797 | } else if (isa<UnaryOperator>(FnExpr) && |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2798 | cast<UnaryOperator>(FnExpr)->getOpcode() |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2799 | == UnaryOperator::AddrOf) { |
| 2800 | FnExpr = cast<UnaryOperator>(FnExpr)->getSubExpr(); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2801 | } else if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(FnExpr)) { |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2802 | // Qualified names disable ADL (C++0x [basic.lookup.argdep]p1). |
| 2803 | ADL &= !isa<QualifiedDeclRefExpr>(DRExpr); |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2804 | NDecl = dyn_cast<NamedDecl>(DRExpr->getDecl()); |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2805 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2806 | } else if (UnresolvedFunctionNameExpr *DepName |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2807 | = dyn_cast<UnresolvedFunctionNameExpr>(FnExpr)) { |
| 2808 | UnqualifiedName = DepName->getName(); |
| 2809 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | } else if (TemplateIdRefExpr *TemplateIdRef |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2811 | = dyn_cast<TemplateIdRefExpr>(FnExpr)) { |
| 2812 | NDecl = TemplateIdRef->getTemplateName().getAsTemplateDecl(); |
Douglas Gregor | d99cbe6 | 2009-07-29 18:26:50 +0000 | [diff] [blame] | 2813 | if (!NDecl) |
| 2814 | NDecl = TemplateIdRef->getTemplateName().getAsOverloadedFunctionDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2815 | HasExplicitTemplateArgs = true; |
| 2816 | ExplicitTemplateArgs = TemplateIdRef->getTemplateArgs(); |
| 2817 | NumExplicitTemplateArgs = TemplateIdRef->getNumTemplateArgs(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2819 | // C++ [temp.arg.explicit]p6: |
| 2820 | // [Note: For simple function names, argument dependent lookup (3.4.2) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2821 | // applies even when the function name is not visible within the |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2822 | // scope of the call. This is because the call still has the syntactic |
| 2823 | // form of a function call (3.4.1). But when a function template with |
| 2824 | // explicit template arguments is used, the call does not have the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | // correct syntactic form unless there is a function template with |
| 2826 | // that name visible at the point of the call. If no such name is |
| 2827 | // visible, the call is not syntactically well-formed and |
| 2828 | // argument-dependent lookup does not apply. If some such name is |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2829 | // visible, argument dependent lookup applies and additional function |
| 2830 | // templates may be found in other namespaces. |
| 2831 | // |
| 2832 | // The summary of this paragraph is that, if we get to this point and the |
| 2833 | // template-id was not a qualified name, then argument-dependent lookup |
| 2834 | // is still possible. |
| 2835 | if (TemplateIdRef->getQualifier()) |
| 2836 | ADL = false; |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2837 | break; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2838 | } else { |
Chris Lattner | 90e150d | 2009-02-14 07:22:29 +0000 | [diff] [blame] | 2839 | // Any kind of name that does not refer to a declaration (or |
| 2840 | // set of declarations) disables ADL (C++0x [basic.lookup.argdep]p3). |
| 2841 | ADL = false; |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2842 | break; |
| 2843 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2844 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2845 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2846 | OverloadedFunctionDecl *Ovl = 0; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2847 | FunctionTemplateDecl *FunctionTemplate = 0; |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2848 | if (NDecl) { |
| 2849 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2850 | if ((FunctionTemplate = dyn_cast<FunctionTemplateDecl>(NDecl))) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2851 | FDecl = FunctionTemplate->getTemplatedDecl(); |
| 2852 | else |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2853 | FDecl = dyn_cast<FunctionDecl>(NDecl); |
| 2854 | Ovl = dyn_cast<OverloadedFunctionDecl>(NDecl); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 2855 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2856 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2857 | if (Ovl || FunctionTemplate || |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2858 | (getLangOptions().CPlusPlus && (FDecl || UnqualifiedName))) { |
Douglas Gregor | 3e41d60 | 2009-02-13 23:20:09 +0000 | [diff] [blame] | 2859 | // We don't perform ADL for implicit declarations of builtins. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 2860 | if (FDecl && FDecl->getBuiltinID(Context) && FDecl->isImplicit()) |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2861 | ADL = false; |
| 2862 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2863 | // We don't perform ADL in C. |
| 2864 | if (!getLangOptions().CPlusPlus) |
| 2865 | ADL = false; |
| 2866 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2867 | if (Ovl || FunctionTemplate || ADL) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2868 | FDecl = ResolveOverloadedCallFn(Fn, NDecl, UnqualifiedName, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2869 | HasExplicitTemplateArgs, |
| 2870 | ExplicitTemplateArgs, |
| 2871 | NumExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2872 | LParenLoc, Args, NumArgs, CommaLocs, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2873 | RParenLoc, ADL); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2874 | if (!FDecl) |
| 2875 | return ExprError(); |
| 2876 | |
| 2877 | // Update Fn to refer to the actual function selected. |
| 2878 | Expr *NewFn = 0; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2879 | if (QualifiedDeclRefExpr *QDRExpr |
Douglas Gregor | edce4dd | 2009-06-30 22:34:41 +0000 | [diff] [blame] | 2880 | = dyn_cast<QualifiedDeclRefExpr>(FnExpr)) |
Douglas Gregor | ab452ba | 2009-03-26 23:50:42 +0000 | [diff] [blame] | 2881 | NewFn = new (Context) QualifiedDeclRefExpr(FDecl, FDecl->getType(), |
| 2882 | QDRExpr->getLocation(), |
| 2883 | false, false, |
| 2884 | QDRExpr->getQualifierRange(), |
| 2885 | QDRExpr->getQualifier()); |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2886 | else |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2887 | NewFn = new (Context) DeclRefExpr(FDecl, FDecl->getType(), |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 2888 | Fn->getSourceRange().getBegin()); |
| 2889 | Fn->Destroy(Context); |
| 2890 | Fn = NewFn; |
| 2891 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 2892 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2893 | |
| 2894 | // Promote the function operand. |
| 2895 | UsualUnaryConversions(Fn); |
| 2896 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2897 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 2898 | // of arguments and function on error. |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 2899 | ExprOwningPtr<CallExpr> TheCall(this, new (Context) CallExpr(Context, Fn, |
| 2900 | Args, NumArgs, |
| 2901 | Context.BoolTy, |
| 2902 | RParenLoc)); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2903 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2904 | const FunctionType *FuncT; |
| 2905 | if (!Fn->getType()->isBlockPointerType()) { |
| 2906 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 2907 | // have type pointer to function". |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2908 | const PointerType *PT = Fn->getType()->getAs<PointerType>(); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2909 | if (PT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2910 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2911 | << Fn->getType() << Fn->getSourceRange()); |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2912 | FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 2913 | } else { // This is a block call. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2914 | FuncT = Fn->getType()->getAs<BlockPointerType>()->getPointeeType()-> |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2915 | getAsFunctionType(); |
| 2916 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2917 | if (FuncT == 0) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2918 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 2919 | << Fn->getType() << Fn->getSourceRange()); |
| 2920 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2921 | // Check for a valid return type |
| 2922 | if (!FuncT->getResultType()->isVoidType() && |
| 2923 | RequireCompleteType(Fn->getSourceRange().getBegin(), |
| 2924 | FuncT->getResultType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2925 | PDiag(diag::err_call_incomplete_return) |
| 2926 | << TheCall->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2927 | return ExprError(); |
| 2928 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2929 | // We know the result type of the call, set it. |
Douglas Gregor | 15da57e | 2008-10-29 02:00:59 +0000 | [diff] [blame] | 2930 | TheCall->setType(FuncT->getResultType().getNonReferenceType()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2932 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 2933 | if (ConvertArgumentsForCall(&*TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2934 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2935 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2936 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 2937 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2938 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2939 | if (FDecl) { |
| 2940 | // Check if we have too few/too many template arguments, based |
| 2941 | // on our knowledge of the function definition. |
| 2942 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 2943 | if (FDecl->getBody(Def) && NumArgs != Def->param_size()) { |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 2944 | const FunctionProtoType *Proto = |
| 2945 | Def->getType()->getAsFunctionProtoType(); |
| 2946 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) { |
| 2947 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 2948 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| 2949 | } |
| 2950 | } |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2953 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2954 | for (unsigned i = 0; i != NumArgs; i++) { |
| 2955 | Expr *Arg = Args[i]; |
| 2956 | DefaultArgumentPromotion(Arg); |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2957 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 2958 | Arg->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 2959 | PDiag(diag::err_call_incomplete_argument) |
| 2960 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 2961 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2962 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 2963 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2964 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2966 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 2967 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2968 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 2969 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2970 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2971 | // Check for sentinels |
| 2972 | if (NDecl) |
| 2973 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2975 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2976 | if (FDecl) { |
| 2977 | if (CheckFunctionCall(FDecl, TheCall.get())) |
| 2978 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2979 | |
| 2980 | if (unsigned BuiltinID = FDecl->getBuiltinID(Context)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 2981 | return CheckBuiltinFunctionCall(BuiltinID, TheCall.take()); |
| 2982 | } else if (NDecl) { |
| 2983 | if (CheckBlockCall(NDecl, TheCall.get())) |
| 2984 | return ExprError(); |
| 2985 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 2986 | |
Anders Carlsson | ec74c59 | 2009-08-16 03:06:32 +0000 | [diff] [blame] | 2987 | return MaybeBindToTemporary(TheCall.take()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2990 | Action::OwningExprResult |
| 2991 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
| 2992 | SourceLocation RParenLoc, ExprArg InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2993 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 2994 | //FIXME: Preserve type source info. |
| 2995 | QualType literalType = GetTypeFromParser(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 2996 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 2997 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 2998 | Expr *literalExpr = static_cast<Expr*>(InitExpr.get()); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 2999 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3000 | if (literalType->isArrayType()) { |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3001 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3002 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3003 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3004 | } else if (!literalType->isDependentType() && |
| 3005 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3006 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3008 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3009 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3010 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3011 | if (CheckInitializerTypes(literalExpr, literalType, LParenLoc, |
Douglas Gregor | 09f41cf | 2009-01-14 15:45:31 +0000 | [diff] [blame] | 3012 | DeclarationName(), /*FIXME:DirectInit=*/false)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3013 | return ExprError(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3014 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3015 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3016 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3017 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3018 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3019 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3020 | InitExpr.release(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3021 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, literalType, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3022 | literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3025 | Action::OwningExprResult |
| 3026 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3027 | SourceLocation RBraceLoc) { |
| 3028 | unsigned NumInit = initlist.size(); |
| 3029 | Expr **InitList = reinterpret_cast<Expr**>(initlist.release()); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3030 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3031 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3032 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3033 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3034 | InitListExpr *E = new (Context) InitListExpr(LBraceLoc, InitList, NumInit, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 3035 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3036 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3037 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3040 | /// CheckCastTypes - Check type constraints for casting between types. |
Sebastian Redl | ef0cb8e | 2009-07-29 13:50:23 +0000 | [diff] [blame] | 3041 | bool Sema::CheckCastTypes(SourceRange TyR, QualType castType, Expr *&castExpr, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | CastExpr::CastKind& Kind, |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3043 | CXXMethodDecl *& ConversionDecl, |
| 3044 | bool FunctionalStyle) { |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3045 | if (getLangOptions().CPlusPlus) |
Fariborz Jahanian | e9f4208 | 2009-08-26 18:55:36 +0000 | [diff] [blame] | 3046 | return CXXCheckCStyleCast(TyR, castType, castExpr, Kind, FunctionalStyle, |
| 3047 | ConversionDecl); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3048 | |
Eli Friedman | 199ea95 | 2009-08-15 19:02:19 +0000 | [diff] [blame] | 3049 | DefaultFunctionArrayConversion(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3050 | |
| 3051 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3052 | // type needs to be scalar. |
| 3053 | if (castType->isVoidType()) { |
| 3054 | // Cast to void allows any expr type. |
| 3055 | } else if (!castType->isScalarType() && !castType->isVectorType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3056 | if (Context.getCanonicalType(castType).getUnqualifiedType() == |
| 3057 | Context.getCanonicalType(castExpr->getType().getUnqualifiedType()) && |
| 3058 | (castType->isStructureType() || castType->isUnionType())) { |
| 3059 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3060 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3061 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3062 | << castType << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3063 | Kind = CastExpr::CK_NoOp; |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3064 | } else if (castType->isUnionType()) { |
| 3065 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3066 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3067 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3068 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3069 | Field != FieldEnd; ++Field) { |
| 3070 | if (Context.getCanonicalType(Field->getType()).getUnqualifiedType() == |
| 3071 | Context.getCanonicalType(castExpr->getType()).getUnqualifiedType()) { |
| 3072 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 3073 | << castExpr->getSourceRange(); |
| 3074 | break; |
| 3075 | } |
| 3076 | } |
| 3077 | if (Field == FieldEnd) |
| 3078 | return Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| 3079 | << castExpr->getType() << castExpr->getSourceRange(); |
Anders Carlsson | 4d8673b | 2009-08-07 23:22:37 +0000 | [diff] [blame] | 3080 | Kind = CastExpr::CK_ToUnion; |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3081 | } else { |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3082 | // Reject any other conversions to non-scalar types. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3083 | return Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3084 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3085 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3086 | } else if (!castExpr->getType()->isScalarType() && |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3087 | !castExpr->getType()->isVectorType()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3088 | return Diag(castExpr->getLocStart(), |
| 3089 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3090 | << castExpr->getType() << castExpr->getSourceRange(); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3091 | } else if (castType->isExtVectorType()) { |
| 3092 | if (CheckExtVectorCast(TyR, castType, castExpr->getType())) |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3093 | return true; |
| 3094 | } else if (castType->isVectorType()) { |
| 3095 | if (CheckVectorCast(TyR, castType, castExpr->getType())) |
| 3096 | return true; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3097 | } else if (castExpr->getType()->isVectorType()) { |
| 3098 | if (CheckVectorCast(TyR, castExpr->getType(), castType)) |
| 3099 | return true; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 3100 | } else if (getLangOptions().ObjC1 && isa<ObjCSuperExpr>(castExpr)) { |
Steve Naroff | a0c3e9c | 2009-04-08 23:52:26 +0000 | [diff] [blame] | 3101 | return Diag(castExpr->getLocStart(), diag::err_illegal_super_cast) << TyR; |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 3102 | } else if (!castType->isArithmeticType()) { |
| 3103 | QualType castExprType = castExpr->getType(); |
| 3104 | if (!castExprType->isIntegralType() && castExprType->isArithmeticType()) |
| 3105 | return Diag(castExpr->getLocStart(), |
| 3106 | diag::err_cast_pointer_from_non_pointer_int) |
| 3107 | << castExprType << castExpr->getSourceRange(); |
| 3108 | } else if (!castExpr->getType()->isArithmeticType()) { |
| 3109 | if (!castType->isIntegralType() && castType->isArithmeticType()) |
| 3110 | return Diag(castExpr->getLocStart(), |
| 3111 | diag::err_cast_pointer_to_non_pointer_int) |
| 3112 | << castType << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3113 | } |
Fariborz Jahanian | b5ff6bf | 2009-05-22 21:42:52 +0000 | [diff] [blame] | 3114 | if (isa<ObjCSelectorExpr>(castExpr)) |
| 3115 | return Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3116 | return false; |
| 3117 | } |
| 3118 | |
Chris Lattner | fe23e21 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 3119 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3120 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3121 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3122 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 3123 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3124 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3125 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3126 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3127 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3128 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3129 | } else |
| 3130 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3131 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 3132 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3133 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 3134 | return false; |
| 3135 | } |
| 3136 | |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3137 | bool Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, QualType SrcTy) { |
| 3138 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 3140 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 3141 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3142 | if (SrcTy->isVectorType()) { |
| 3143 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) |
| 3144 | return Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| 3145 | << DestTy << SrcTy << R; |
| 3146 | return false; |
| 3147 | } |
| 3148 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3149 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3150 | // conversion will take place first from scalar to elt type, and then |
| 3151 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3152 | if (SrcTy->isPointerType()) |
| 3153 | return Diag(R.getBegin(), |
| 3154 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 3155 | << DestTy << SrcTy << R; |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 3156 | return false; |
| 3157 | } |
| 3158 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3159 | Action::OwningExprResult |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3160 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, TypeTy *Ty, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3161 | SourceLocation RParenLoc, ExprArg Op) { |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3162 | CastExpr::CastKind Kind = CastExpr::CK_Unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3163 | |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3164 | assert((Ty != 0) && (Op.get() != 0) && |
| 3165 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 3166 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3167 | Expr *castExpr = (Expr *)Op.get(); |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 3168 | //FIXME: Preserve type source info. |
| 3169 | QualType castType = GetTypeFromParser(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3171 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 3172 | if (isa<ParenListExpr>(castExpr)) |
| 3173 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, move(Op),castType); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame^] | 3174 | CXXMethodDecl *Method = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | if (CheckCastTypes(SourceRange(LParenLoc, RParenLoc), castType, castExpr, |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame^] | 3176 | Kind, Method)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3177 | return ExprError(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame^] | 3178 | |
| 3179 | if (Method) { |
| 3180 | OwningExprResult CastArg = BuildCXXCastArgument(LParenLoc, castType, Kind, |
| 3181 | Method, move(Op)); |
| 3182 | |
| 3183 | if (CastArg.isInvalid()) |
| 3184 | return ExprError(); |
| 3185 | |
| 3186 | castExpr = CastArg.takeAs<Expr>(); |
| 3187 | } else { |
| 3188 | Op.release(); |
Fariborz Jahanian | 3197659 | 2009-08-29 19:15:16 +0000 | [diff] [blame] | 3189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3190 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3191 | return Owned(new (Context) CStyleCastExpr(castType.getNonReferenceType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3192 | Kind, castExpr, castType, |
Anders Carlsson | cdb6197 | 2009-08-07 22:21:05 +0000 | [diff] [blame] | 3193 | LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3194 | } |
| 3195 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3196 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 3197 | /// of comma binary operators. |
| 3198 | Action::OwningExprResult |
| 3199 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, ExprArg EA) { |
| 3200 | Expr *expr = EA.takeAs<Expr>(); |
| 3201 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 3202 | if (!E) |
| 3203 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3205 | OwningExprResult Result(*this, E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3206 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3207 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| 3208 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, move(Result), |
| 3209 | Owned(E->getExpr(i))); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3210 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3211 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), move(Result)); |
| 3212 | } |
| 3213 | |
| 3214 | Action::OwningExprResult |
| 3215 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| 3216 | SourceLocation RParenLoc, ExprArg Op, |
| 3217 | QualType Ty) { |
| 3218 | ParenListExpr *PE = (ParenListExpr *)Op.get(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
| 3220 | // If this is an altivec initializer, '(' type ')' '(' init, ..., init ')' |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3221 | // then handle it as such. |
| 3222 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 3223 | if (PE->getNumExprs() == 0) { |
| 3224 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 3225 | return ExprError(); |
| 3226 | } |
| 3227 | |
| 3228 | llvm::SmallVector<Expr *, 8> initExprs; |
| 3229 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 3230 | initExprs.push_back(PE->getExpr(i)); |
| 3231 | |
| 3232 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 3233 | // braces instead of the original commas. |
| 3234 | Op.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3235 | InitListExpr *E = new (Context) InitListExpr(LParenLoc, &initExprs[0], |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3236 | initExprs.size(), RParenLoc); |
| 3237 | E->setType(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3238 | return ActOnCompoundLiteral(LParenLoc, Ty.getAsOpaquePtr(), RParenLoc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3239 | Owned(E)); |
| 3240 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3241 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3242 | // sequence of BinOp comma operators. |
| 3243 | Op = MaybeConvertParenListExprToParenExpr(S, move(Op)); |
| 3244 | return ActOnCastExpr(S, LParenLoc, Ty.getAsOpaquePtr(), RParenLoc,move(Op)); |
| 3245 | } |
| 3246 | } |
| 3247 | |
| 3248 | Action::OwningExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 3249 | SourceLocation R, |
| 3250 | MultiExprArg Val) { |
| 3251 | unsigned nexprs = Val.size(); |
| 3252 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| 3253 | assert((exprs != 0) && "ActOnParenListExpr() missing expr list"); |
| 3254 | Expr *expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| 3255 | return Owned(expr); |
| 3256 | } |
| 3257 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3258 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 3259 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 3260 | /// C99 6.5.15 |
| 3261 | QualType Sema::CheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, |
| 3262 | SourceLocation QuestionLoc) { |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3263 | // C++ is sufficiently different to merit its own checker. |
| 3264 | if (getLangOptions().CPlusPlus) |
| 3265 | return CXXCheckConditionalOperands(Cond, LHS, RHS, QuestionLoc); |
| 3266 | |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3267 | UsualUnaryConversions(Cond); |
| 3268 | UsualUnaryConversions(LHS); |
| 3269 | UsualUnaryConversions(RHS); |
| 3270 | QualType CondTy = Cond->getType(); |
| 3271 | QualType LHSTy = LHS->getType(); |
| 3272 | QualType RHSTy = RHS->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 3273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3274 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 3275 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| 3276 | Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 3277 | << CondTy; |
| 3278 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3279 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3280 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3281 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3282 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 3283 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 3284 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3285 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 3286 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3287 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 3288 | UsualArithmeticConversions(LHS, RHS); |
| 3289 | return LHS->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 3290 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3291 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3292 | // If both operands are the same structure or union type, the result is that |
| 3293 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3294 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 3295 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3296 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3297 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3298 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3299 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3300 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3301 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3302 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3303 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3304 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3305 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 3306 | if (!LHSTy->isVoidType()) |
| 3307 | Diag(RHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3308 | << RHS->getSourceRange(); |
| 3309 | if (!RHSTy->isVoidType()) |
| 3310 | Diag(LHS->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 3311 | << LHS->getSourceRange(); |
| 3312 | ImpCastExprToType(LHS, Context.VoidTy); |
| 3313 | ImpCastExprToType(RHS, Context.VoidTy); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 3314 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 3315 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3316 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 3317 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3318 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3319 | RHS->isNullPointerConstant(Context)) { |
| 3320 | ImpCastExprToType(RHS, LHSTy); // promote the null to a pointer. |
| 3321 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3322 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 3323 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3324 | LHS->isNullPointerConstant(Context)) { |
| 3325 | ImpCastExprToType(LHS, RHSTy); // promote the null to a pointer. |
| 3326 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 3327 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3328 | // Handle things like Class and struct objc_class*. Here we case the result |
| 3329 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 3330 | // redefinition type if an attempt is made to access its fields. |
| 3331 | if (LHSTy->isObjCClassType() && |
| 3332 | (RHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3333 | ImpCastExprToType(RHS, LHSTy); |
| 3334 | return LHSTy; |
| 3335 | } |
| 3336 | if (RHSTy->isObjCClassType() && |
| 3337 | (LHSTy.getDesugaredType() == Context.ObjCClassRedefinitionType)) { |
| 3338 | ImpCastExprToType(LHS, RHSTy); |
| 3339 | return RHSTy; |
| 3340 | } |
| 3341 | // And the same for struct objc_object* / id |
| 3342 | if (LHSTy->isObjCIdType() && |
| 3343 | (RHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3344 | ImpCastExprToType(RHS, LHSTy); |
| 3345 | return LHSTy; |
| 3346 | } |
| 3347 | if (RHSTy->isObjCIdType() && |
| 3348 | (LHSTy.getDesugaredType() == Context.ObjCIdRedefinitionType)) { |
| 3349 | ImpCastExprToType(LHS, RHSTy); |
| 3350 | return RHSTy; |
| 3351 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3352 | // Handle block pointer types. |
| 3353 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 3354 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 3355 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 3356 | QualType destType = Context.getPointerType(Context.VoidTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3357 | ImpCastExprToType(LHS, destType); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3358 | ImpCastExprToType(RHS, destType); |
| 3359 | return destType; |
| 3360 | } |
| 3361 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3362 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3363 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3364 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3365 | // We have 2 block pointer types. |
| 3366 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3367 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3368 | return LHSTy; |
| 3369 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3370 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3371 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 3372 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3373 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3374 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3375 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 3376 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3377 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3378 | // In this situation, we assume void* type. No especially good |
| 3379 | // reason, but this is what gcc does, and we do have to pick |
| 3380 | // to get a consistent AST. |
| 3381 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3382 | ImpCastExprToType(LHS, incompatTy); |
| 3383 | ImpCastExprToType(RHS, incompatTy); |
| 3384 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3385 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3386 | // The block pointer types are compatible. |
| 3387 | ImpCastExprToType(LHS, LHSTy); |
| 3388 | ImpCastExprToType(RHS, LHSTy); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 3389 | return LHSTy; |
| 3390 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3391 | // Check constraints for Objective-C object pointers types. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3392 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3394 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3395 | // Two identical object pointer types are always compatible. |
| 3396 | return LHSTy; |
| 3397 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3398 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAsObjCObjectPointerType(); |
| 3399 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAsObjCObjectPointerType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3400 | QualType compositeType = LHSTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3401 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3402 | // If both operands are interfaces and either operand can be |
| 3403 | // assigned to the other, use that type as the composite |
| 3404 | // type. This allows |
| 3405 | // xxx ? (A*) a : (B*) b |
| 3406 | // where B is a subclass of A. |
| 3407 | // |
| 3408 | // Additionally, as for assignment, if either type is 'id' |
| 3409 | // allow silent coercion. Finally, if the types are |
| 3410 | // incompatible then make sure to use 'id' as the composite |
| 3411 | // type so the result is acceptable for sending messages to. |
| 3412 | |
| 3413 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 3414 | // It could return the composite type. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3415 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
Fariborz Jahanian | 9ec22a3 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3416 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3417 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
Fariborz Jahanian | 9ec22a3 | 2009-08-22 22:27:17 +0000 | [diff] [blame] | 3418 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | } else if ((LHSTy->isObjCQualifiedIdType() || |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3420 | RHSTy->isObjCQualifiedIdType()) && |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3421 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | // Need to handle "id<xx>" explicitly. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3423 | // GCC allows qualified id and any Objective-C type to devolve to |
| 3424 | // id. Currently localizing to here until clear this should be |
| 3425 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 3426 | compositeType = Context.getObjCIdType(); |
| 3427 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3428 | compositeType = Context.getObjCIdType(); |
| 3429 | } else { |
| 3430 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 3431 | << LHSTy << RHSTy |
| 3432 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3433 | QualType incompatTy = Context.getObjCIdType(); |
| 3434 | ImpCastExprToType(LHS, incompatTy); |
| 3435 | ImpCastExprToType(RHS, incompatTy); |
| 3436 | return incompatTy; |
| 3437 | } |
| 3438 | // The object pointer types are compatible. |
| 3439 | ImpCastExprToType(LHS, compositeType); |
| 3440 | ImpCastExprToType(RHS, compositeType); |
| 3441 | return compositeType; |
| 3442 | } |
Steve Naroff | c715e78 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3443 | // Check Objective-C object pointer types and 'void *' |
| 3444 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3445 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | c715e78 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3446 | QualType rhptee = RHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
| 3447 | QualType destPointee = lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3448 | QualType destType = Context.getPointerType(destPointee); |
| 3449 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3450 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3451 | return destType; |
| 3452 | } |
| 3453 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 3454 | QualType lhptee = LHSTy->getAsObjCObjectPointerType()->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3455 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | c715e78 | 2009-07-29 15:09:39 +0000 | [diff] [blame] | 3456 | QualType destPointee = rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3457 | QualType destType = Context.getPointerType(destPointee); |
| 3458 | ImpCastExprToType(RHS, destType); // add qualifiers if necessary |
| 3459 | ImpCastExprToType(LHS, destType); // promote to void* |
| 3460 | return destType; |
| 3461 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3462 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 3463 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 3464 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3465 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 3466 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3467 | |
| 3468 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 3469 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 3470 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 3471 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
| 3472 | QualType destType = Context.getPointerType(destPointee); |
| 3473 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3474 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3475 | return destType; |
| 3476 | } |
| 3477 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 3478 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
| 3479 | QualType destType = Context.getPointerType(destPointee); |
| 3480 | ImpCastExprToType(LHS, destType); // add qualifiers if necessary |
| 3481 | ImpCastExprToType(RHS, destType); // promote to void* |
| 3482 | return destType; |
| 3483 | } |
| 3484 | |
| 3485 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 3486 | // Two identical pointer types are always compatible. |
| 3487 | return LHSTy; |
| 3488 | } |
| 3489 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 3490 | rhptee.getUnqualifiedType())) { |
| 3491 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| 3492 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3493 | // In this situation, we assume void* type. No especially good |
| 3494 | // reason, but this is what gcc does, and we do have to pick |
| 3495 | // to get a consistent AST. |
| 3496 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| 3497 | ImpCastExprToType(LHS, incompatTy); |
| 3498 | ImpCastExprToType(RHS, incompatTy); |
| 3499 | return incompatTy; |
| 3500 | } |
| 3501 | // The pointer types are compatible. |
| 3502 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 3503 | // differently qualified versions of compatible types, the result type is |
| 3504 | // a pointer to an appropriately qualified version of the *composite* |
| 3505 | // type. |
| 3506 | // FIXME: Need to calculate the composite type. |
| 3507 | // FIXME: Need to add qualifiers |
| 3508 | ImpCastExprToType(LHS, LHSTy); |
| 3509 | ImpCastExprToType(RHS, LHSTy); |
| 3510 | return LHSTy; |
| 3511 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3512 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 3513 | // GCC compatibility: soften pointer/integer mismatch. |
| 3514 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 3515 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3516 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3517 | ImpCastExprToType(LHS, RHSTy); // promote the integer to a pointer. |
| 3518 | return RHSTy; |
| 3519 | } |
| 3520 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 3521 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 3522 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
| 3523 | ImpCastExprToType(RHS, LHSTy); // promote the integer to a pointer. |
| 3524 | return LHSTy; |
| 3525 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 3526 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 3527 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 3528 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| 3529 | << LHSTy << RHSTy << LHS->getSourceRange() << RHS->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3530 | return QualType(); |
| 3531 | } |
| 3532 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3533 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3534 | /// in the case of a the GNU conditional expr extension. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3535 | Action::OwningExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| 3536 | SourceLocation ColonLoc, |
| 3537 | ExprArg Cond, ExprArg LHS, |
| 3538 | ExprArg RHS) { |
| 3539 | Expr *CondExpr = (Expr *) Cond.get(); |
| 3540 | Expr *LHSExpr = (Expr *) LHS.get(), *RHSExpr = (Expr *) RHS.get(); |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 3541 | |
| 3542 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 3543 | // was the condition. |
| 3544 | bool isLHSNull = LHSExpr == 0; |
| 3545 | if (isLHSNull) |
| 3546 | LHSExpr = CondExpr; |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3547 | |
| 3548 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 3549 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3550 | if (result.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3551 | return ExprError(); |
| 3552 | |
| 3553 | Cond.release(); |
| 3554 | LHS.release(); |
| 3555 | RHS.release(); |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3556 | return Owned(new (Context) ConditionalOperator(CondExpr, QuestionLoc, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3557 | isLHSNull ? 0 : LHSExpr, |
Douglas Gregor | 47e1f7c | 2009-08-26 14:37:04 +0000 | [diff] [blame] | 3558 | ColonLoc, RHSExpr, result)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3561 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3562 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3563 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 3564 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 3565 | // FIXME: add a couple examples in this comment. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3566 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3567 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 3568 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3569 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3570 | if ((lhsType->isObjCClassType() && |
| 3571 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3572 | (rhsType->isObjCClassType() && |
| 3573 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3574 | return Compatible; |
| 3575 | } |
| 3576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3577 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3578 | lhptee = lhsType->getAs<PointerType>()->getPointeeType(); |
| 3579 | rhptee = rhsType->getAs<PointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3580 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3581 | // make sure we operate on the canonical type |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3582 | lhptee = Context.getCanonicalType(lhptee); |
| 3583 | rhptee = Context.getCanonicalType(rhptee); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3584 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3585 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3586 | |
| 3587 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 3588 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 3589 | // qualifiers of the type *pointed to* by the right; |
Fariborz Jahanian | f11284a | 2009-02-17 18:27:45 +0000 | [diff] [blame] | 3590 | // FIXME: Handle ExtQualType |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3591 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3592 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3593 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3594 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 3595 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3596 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3597 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3598 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3599 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3600 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3601 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3602 | assert(rhptee->isFunctionType()); |
| 3603 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3604 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3605 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3606 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3607 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3608 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3609 | |
| 3610 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 3611 | assert(lhptee->isFunctionType()); |
| 3612 | return FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 3613 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3614 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3615 | // unqualified versions of compatible types, ... |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3616 | lhptee = lhptee.getUnqualifiedType(); |
| 3617 | rhptee = rhptee.getUnqualifiedType(); |
| 3618 | if (!Context.typesAreCompatible(lhptee, rhptee)) { |
| 3619 | // Check if the pointee types are compatible ignoring the sign. |
| 3620 | // We explicitly check for char so that we catch "char" vs |
| 3621 | // "unsigned char" on systems where "char" is unsigned. |
| 3622 | if (lhptee->isCharType()) { |
| 3623 | lhptee = Context.UnsignedCharTy; |
| 3624 | } else if (lhptee->isSignedIntegerType()) { |
| 3625 | lhptee = Context.getCorrespondingUnsignedType(lhptee); |
| 3626 | } |
| 3627 | if (rhptee->isCharType()) { |
| 3628 | rhptee = Context.UnsignedCharTy; |
| 3629 | } else if (rhptee->isSignedIntegerType()) { |
| 3630 | rhptee = Context.getCorrespondingUnsignedType(rhptee); |
| 3631 | } |
| 3632 | if (lhptee == rhptee) { |
| 3633 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 3634 | // takes priority over sign incompatibility because the sign |
| 3635 | // warning can be disabled. |
| 3636 | if (ConvTy != Compatible) |
| 3637 | return ConvTy; |
| 3638 | return IncompatiblePointerSign; |
| 3639 | } |
| 3640 | // General pointer incompatibility takes priority over qualifiers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | return IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 3642 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3643 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3646 | /// CheckBlockPointerTypesForAssignment - This routine determines whether two |
| 3647 | /// block pointer types are compatible or whether a block and normal pointer |
| 3648 | /// are compatible. It is more restrict than comparing two function pointer |
| 3649 | // types. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3650 | Sema::AssignConvertType |
| 3651 | Sema::CheckBlockPointerTypesForAssignment(QualType lhsType, |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3652 | QualType rhsType) { |
| 3653 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3654 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3655 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3656 | lhptee = lhsType->getAs<BlockPointerType>()->getPointeeType(); |
| 3657 | rhptee = rhsType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3658 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3659 | // make sure we operate on the canonical type |
| 3660 | lhptee = Context.getCanonicalType(lhptee); |
| 3661 | rhptee = Context.getCanonicalType(rhptee); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3662 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3663 | AssignConvertType ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3664 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3665 | // For blocks we enforce that qualifiers are identical. |
| 3666 | if (lhptee.getCVRQualifiers() != rhptee.getCVRQualifiers()) |
| 3667 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3668 | |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 3669 | if (!Context.typesAreCompatible(lhptee, rhptee)) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3670 | return IncompatibleBlockPointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3671 | return ConvTy; |
| 3672 | } |
| 3673 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3674 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 3675 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3676 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 3677 | /// |
| 3678 | /// int a, *pint; |
| 3679 | /// short *pshort; |
| 3680 | /// struct foo *pfoo; |
| 3681 | /// |
| 3682 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 3683 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 3684 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 3685 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 3686 | /// |
| 3687 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3688 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3689 | /// |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3690 | Sema::AssignConvertType |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3691 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3692 | // Get canonical types. We're not formatting these types, just comparing |
| 3693 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3694 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 3695 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3696 | |
| 3697 | if (lhsType == rhsType) |
Chris Lattner | d2656dd | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 3698 | return Compatible; // Common case: fast path an exact match. |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 3699 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 3700 | if ((lhsType->isObjCClassType() && |
| 3701 | (rhsType.getDesugaredType() == Context.ObjCClassRedefinitionType)) || |
| 3702 | (rhsType->isObjCClassType() && |
| 3703 | (lhsType.getDesugaredType() == Context.ObjCClassRedefinitionType))) { |
| 3704 | return Compatible; |
| 3705 | } |
| 3706 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3707 | // If the left-hand side is a reference type, then we are in a |
| 3708 | // (rare!) case where we've allowed the use of references in C, |
| 3709 | // e.g., as a parameter type in a built-in function. In this case, |
| 3710 | // just make sure that the type referenced is compatible with the |
| 3711 | // right-hand side type. The caller is responsible for adjusting |
| 3712 | // lhsType so that the resulting expression does not have reference |
| 3713 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3714 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3715 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 3716 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3717 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 3718 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 3719 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 3720 | // to the same ExtVector type. |
| 3721 | if (lhsType->isExtVectorType()) { |
| 3722 | if (rhsType->isExtVectorType()) |
| 3723 | return lhsType == rhsType ? Compatible : Incompatible; |
| 3724 | if (!rhsType->isVectorType() && rhsType->isArithmeticType()) |
| 3725 | return Compatible; |
| 3726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3727 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3728 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3729 | // If we are allowing lax vector conversions, and LHS and RHS are both |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3730 | // vectors, the total size only needs to be the same. This is a bitcast; |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3731 | // no bits are changed but the result type is different. |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3732 | if (getLangOptions().LaxVectorConversions && |
| 3733 | lhsType->isVectorType() && rhsType->isVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3734 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3735 | return IncompatibleVectors; |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3736 | } |
| 3737 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3738 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3739 | |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 3740 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3741 | return Compatible; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3742 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3743 | if (isa<PointerType>(lhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3744 | if (rhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3745 | return IntToPointer; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3746 | |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3747 | if (isa<PointerType>(rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3748 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3749 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3750 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3751 | if (isa<ObjCObjectPointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3752 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3753 | return Compatible; |
| 3754 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3755 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3756 | if (rhsType->getAs<BlockPointerType>()) { |
| 3757 | if (lhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3758 | return Compatible; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3759 | |
| 3760 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3761 | if (getLangOptions().ObjC1 && lhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3762 | return Compatible; |
| 3763 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3764 | return Incompatible; |
| 3765 | } |
| 3766 | |
| 3767 | if (isa<BlockPointerType>(lhsType)) { |
| 3768 | if (rhsType->isIntegerType()) |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 3769 | return IntToBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3770 | |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3771 | // Treat block pointers as objects. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3772 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 3773 | return Compatible; |
| 3774 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3775 | if (rhsType->isBlockPointerType()) |
| 3776 | return CheckBlockPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3777 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3778 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3779 | if (RHSPT->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3780 | return Compatible; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 3781 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3782 | return Incompatible; |
| 3783 | } |
| 3784 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3785 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| 3786 | if (rhsType->isIntegerType()) |
| 3787 | return IntToPointer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3789 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3790 | if (isa<PointerType>(rhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3791 | if (rhsType->isVoidPointerType()) // an exception to the rule. |
| 3792 | return Compatible; |
| 3793 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3794 | } |
| 3795 | if (rhsType->isObjCObjectPointerType()) { |
Steve Naroff | de2e22d | 2009-07-15 18:40:39 +0000 | [diff] [blame] | 3796 | if (lhsType->isObjCBuiltinType() || rhsType->isObjCBuiltinType()) |
| 3797 | return Compatible; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3798 | if (Context.typesAreCompatible(lhsType, rhsType)) |
| 3799 | return Compatible; |
Steve Naroff | 4084c30 | 2009-07-23 01:01:38 +0000 | [diff] [blame] | 3800 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| 3801 | return IncompatibleObjCQualifiedId; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3802 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3803 | } |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3804 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3805 | if (RHSPT->getPointeeType()->isVoidType()) |
| 3806 | return Compatible; |
| 3807 | } |
| 3808 | // Treat block pointers as objects. |
| 3809 | if (rhsType->isBlockPointerType()) |
| 3810 | return Compatible; |
| 3811 | return Incompatible; |
| 3812 | } |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3813 | if (isa<PointerType>(rhsType)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3814 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3815 | if (lhsType == Context.BoolTy) |
| 3816 | return Compatible; |
| 3817 | |
| 3818 | if (lhsType->isIntegerType()) |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 3819 | return PointerToInt; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3820 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3821 | if (isa<PointerType>(lhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3822 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3823 | |
| 3824 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3825 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 3826 | return Compatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3827 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3828 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3829 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 3830 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 3831 | if (lhsType == Context.BoolTy) |
| 3832 | return Compatible; |
| 3833 | |
| 3834 | if (lhsType->isIntegerType()) |
| 3835 | return PointerToInt; |
| 3836 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3837 | // In general, C pointers are not compatible with ObjC object pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3838 | if (isa<PointerType>(lhsType)) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 3839 | if (lhsType->isVoidPointerType()) // an exception to the rule. |
| 3840 | return Compatible; |
| 3841 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3842 | } |
| 3843 | if (isa<BlockPointerType>(lhsType) && |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3844 | rhsType->getAs<PointerType>()->getPointeeType()->isVoidType()) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3845 | return Compatible; |
| 3846 | return Incompatible; |
| 3847 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 3848 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 3849 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 3850 | if (Context.typesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3851 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3852 | } |
| 3853 | return Incompatible; |
| 3854 | } |
| 3855 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3856 | /// \brief Constructs a transparent union from an expression that is |
| 3857 | /// used to initialize the transparent union. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3858 | static void ConstructTransparentUnion(ASTContext &C, Expr *&E, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3859 | QualType UnionType, FieldDecl *Field) { |
| 3860 | // Build an initializer list that designates the appropriate member |
| 3861 | // of the transparent union. |
| 3862 | InitListExpr *Initializer = new (C) InitListExpr(SourceLocation(), |
| 3863 | &E, 1, |
| 3864 | SourceLocation()); |
| 3865 | Initializer->setType(UnionType); |
| 3866 | Initializer->setInitializedFieldInUnion(Field); |
| 3867 | |
| 3868 | // Build a compound literal constructing a value of the transparent |
| 3869 | // union type from this initializer list. |
| 3870 | E = new (C) CompoundLiteralExpr(SourceLocation(), UnionType, Initializer, |
| 3871 | false); |
| 3872 | } |
| 3873 | |
| 3874 | Sema::AssignConvertType |
| 3875 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, Expr *&rExpr) { |
| 3876 | QualType FromType = rExpr->getType(); |
| 3877 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3878 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3879 | // transparent_union GCC extension. |
| 3880 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 3881 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3882 | return Incompatible; |
| 3883 | |
| 3884 | // The field to initialize within the transparent union. |
| 3885 | RecordDecl *UD = UT->getDecl(); |
| 3886 | FieldDecl *InitField = 0; |
| 3887 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3888 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 3889 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3890 | it != itend; ++it) { |
| 3891 | if (it->getType()->isPointerType()) { |
| 3892 | // If the transparent union contains a pointer type, we allow: |
| 3893 | // 1) void pointer |
| 3894 | // 2) null pointer constant |
| 3895 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3896 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3897 | ImpCastExprToType(rExpr, it->getType()); |
| 3898 | InitField = *it; |
| 3899 | break; |
| 3900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3902 | if (rExpr->isNullPointerConstant(Context)) { |
| 3903 | ImpCastExprToType(rExpr, it->getType()); |
| 3904 | InitField = *it; |
| 3905 | break; |
| 3906 | } |
| 3907 | } |
| 3908 | |
| 3909 | if (CheckAssignmentConstraints(it->getType(), rExpr->getType()) |
| 3910 | == Compatible) { |
| 3911 | InitField = *it; |
| 3912 | break; |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | if (!InitField) |
| 3917 | return Incompatible; |
| 3918 | |
| 3919 | ConstructTransparentUnion(Context, rExpr, ArgType, InitField); |
| 3920 | return Compatible; |
| 3921 | } |
| 3922 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3923 | Sema::AssignConvertType |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3924 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3925 | if (getLangOptions().CPlusPlus) { |
| 3926 | if (!lhsType->isRecordType()) { |
| 3927 | // C++ 5.17p3: If the left operand is not of class type, the |
| 3928 | // expression is implicitly converted (C++ 4) to the |
| 3929 | // cv-unqualified type of the left operand. |
Douglas Gregor | 45920e8 | 2008-12-19 17:40:08 +0000 | [diff] [blame] | 3930 | if (PerformImplicitConversion(rExpr, lhsType.getUnqualifiedType(), |
| 3931 | "assigning")) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3932 | return Incompatible; |
Chris Lattner | 2c4463f | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 3933 | return Compatible; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
| 3936 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 3937 | // structures. |
| 3938 | } |
| 3939 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3940 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 3941 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3942 | if ((lhsType->isPointerType() || |
| 3943 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3944 | lhsType->isBlockPointerType()) |
Fariborz Jahanian | 9d3185e | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 3945 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 3946 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 3947 | return Compatible; |
| 3948 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3949 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3950 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3951 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3952 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3953 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3954 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3955 | // Suppress this for references: C++ 8.5.3p5. |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 3956 | if (!lhsType->isReferenceType()) |
| 3957 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3958 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 3959 | Sema::AssignConvertType result = |
| 3960 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3961 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3962 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 3963 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3964 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 3965 | // so that we can use references in built-in functions even in C. |
| 3966 | // The getNonReferenceType() call makes sure that the resulting expression |
| 3967 | // does not have reference type. |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3968 | if (result != Incompatible && rExpr->getType() != lhsType) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 3969 | ImpCastExprToType(rExpr, lhsType.getNonReferenceType()); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 3970 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 3971 | } |
| 3972 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 3973 | QualType Sema::InvalidOperands(SourceLocation Loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3974 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 3975 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 3976 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 3977 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3980 | inline QualType Sema::CheckVectorOperands(SourceLocation Loc, Expr *&lex, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 3981 | Expr *&rex) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3982 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3983 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 3984 | QualType lhsType = |
| 3985 | Context.getCanonicalType(lex->getType()).getUnqualifiedType(); |
| 3986 | QualType rhsType = |
| 3987 | Context.getCanonicalType(rex->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3988 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3989 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 3990 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3991 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 3992 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3993 | // Handle the case of a vector & extvector type of the same size and element |
| 3994 | // type. It would be nice if we only had one vector type someday. |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 3995 | if (getLangOptions().LaxVectorConversions) { |
| 3996 | // FIXME: Should we warn here? |
| 3997 | if (const VectorType *LV = lhsType->getAsVectorType()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 3998 | if (const VectorType *RV = rhsType->getAsVectorType()) |
| 3999 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4000 | LV->getNumElements() == RV->getNumElements()) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4001 | return lhsType->isExtVectorType() ? lhsType : rhsType; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 4002 | } |
| 4003 | } |
| 4004 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4005 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4006 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 4007 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 4008 | bool swapped = false; |
| 4009 | if (rhsType->isExtVectorType()) { |
| 4010 | swapped = true; |
| 4011 | std::swap(rex, lex); |
| 4012 | std::swap(rhsType, lhsType); |
| 4013 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4014 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4015 | // Handle the case of an ext vector and scalar. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4016 | if (const ExtVectorType *LV = lhsType->getAsExtVectorType()) { |
| 4017 | QualType EltTy = LV->getElementType(); |
| 4018 | if (EltTy->isIntegralType() && rhsType->isIntegralType()) { |
| 4019 | if (Context.getIntegerTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4020 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4021 | if (swapped) std::swap(rex, lex); |
| 4022 | return lhsType; |
| 4023 | } |
| 4024 | } |
| 4025 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 4026 | rhsType->isRealFloatingType()) { |
| 4027 | if (Context.getFloatingTypeOrder(EltTy, rhsType) >= 0) { |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4028 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4029 | if (swapped) std::swap(rex, lex); |
| 4030 | return lhsType; |
| 4031 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 4032 | } |
| 4033 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 4035 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4036 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4037 | << lex->getType() << rex->getType() |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4038 | << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4039 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 4040 | } |
| 4041 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | inline QualType Sema::CheckMultiplyDivideOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4043 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 69d1d00 | 2009-01-05 22:42:10 +0000 | [diff] [blame] | 4044 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4045 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4046 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4047 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4048 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4049 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4050 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4051 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | inline QualType Sema::CheckRemainderOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4055 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 4056 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4057 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 4058 | return CheckVectorOperands(Loc, lex, rex); |
| 4059 | return InvalidOperands(Loc, lex, rex); |
| 4060 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4061 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4062 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4063 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4064 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4065 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4066 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | Expr *&lex, Expr *&rex, SourceLocation Loc, QualType* CompLHSTy) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4071 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4072 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4073 | if (CompLHSTy) *CompLHSTy = compType; |
| 4074 | return compType; |
| 4075 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4076 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4077 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4078 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4079 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4080 | if (lex->getType()->isArithmeticType() && |
| 4081 | rex->getType()->isArithmeticType()) { |
| 4082 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4083 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4084 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4086 | // Put any potential pointer into PExp |
| 4087 | Expr* PExp = lex, *IExp = rex; |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4088 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4089 | std::swap(PExp, IExp); |
| 4090 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4091 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4093 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4094 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4096 | // Check for arithmetic on pointers to incomplete types. |
| 4097 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4098 | if (getLangOptions().CPlusPlus) { |
| 4099 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4100 | << lex->getSourceRange() << rex->getSourceRange(); |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4101 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4102 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4103 | |
| 4104 | // GNU extension: arithmetic on pointer to void |
| 4105 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4106 | << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4107 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4108 | if (getLangOptions().CPlusPlus) { |
| 4109 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| 4110 | << lex->getType() << lex->getSourceRange(); |
| 4111 | return QualType(); |
| 4112 | } |
| 4113 | |
| 4114 | // GNU extension: arithmetic on pointer to function |
| 4115 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| 4116 | << lex->getType() << lex->getSourceRange(); |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4117 | } else { |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4118 | // Check if we require a complete type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4119 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | 9deaeca | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 4120 | !PExp->getType()->isDependentType()) || |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4121 | PExp->getType()->isObjCObjectPointerType()) && |
| 4122 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 4124 | << PExp->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4125 | << PExp->getType())) |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 4126 | return QualType(); |
| 4127 | } |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4128 | // Diagnose bad cases where we step over interface counts. |
| 4129 | if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4130 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4131 | << PointeeTy << PExp->getSourceRange(); |
| 4132 | return QualType(); |
| 4133 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4135 | if (CompLHSTy) { |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4136 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4137 | if (LHSTy.isNull()) { |
| 4138 | LHSTy = lex->getType(); |
| 4139 | if (LHSTy->isPromotableIntegerType()) |
| 4140 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4141 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4142 | *CompLHSTy = LHSTy; |
| 4143 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 4144 | return PExp->getType(); |
| 4145 | } |
| 4146 | } |
| 4147 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4148 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4151 | // C99 6.5.6 |
| 4152 | QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4153 | SourceLocation Loc, QualType* CompLHSTy) { |
| 4154 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) { |
| 4155 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 4156 | if (CompLHSTy) *CompLHSTy = compType; |
| 4157 | return compType; |
| 4158 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4159 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4160 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4161 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4162 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4163 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4164 | // Handle the common case first (both operands are arithmetic). |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4165 | if (lex->getType()->isArithmeticType() |
| 4166 | && rex->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4167 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4168 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4170 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4171 | // Either ptr - int or ptr - ptr. |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4172 | if (lex->getType()->isAnyPointerType()) { |
Steve Naroff | 430ee5a | 2009-07-13 17:19:15 +0000 | [diff] [blame] | 4173 | QualType lpointee = lex->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4175 | // The LHS must be an completely-defined object type. |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4176 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4177 | bool ComplainAboutVoid = false; |
| 4178 | Expr *ComplainAboutFunc = 0; |
| 4179 | if (lpointee->isVoidType()) { |
| 4180 | if (getLangOptions().CPlusPlus) { |
| 4181 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4182 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4183 | return QualType(); |
| 4184 | } |
| 4185 | |
| 4186 | // GNU C extension: arithmetic on pointer to void |
| 4187 | ComplainAboutVoid = true; |
| 4188 | } else if (lpointee->isFunctionType()) { |
| 4189 | if (getLangOptions().CPlusPlus) { |
| 4190 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4191 | << lex->getType() << lex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4192 | return QualType(); |
| 4193 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4194 | |
| 4195 | // GNU C extension: arithmetic on pointer to function |
| 4196 | ComplainAboutFunc = lex; |
| 4197 | } else if (!lpointee->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4198 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4199 | PDiag(diag::err_typecheck_sub_ptr_object) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4200 | << lex->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4201 | << lex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4202 | return QualType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4203 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 4204 | // Diagnose bad cases where we step over interface counts. |
| 4205 | if (lpointee->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4206 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 4207 | << lpointee << lex->getSourceRange(); |
| 4208 | return QualType(); |
| 4209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4210 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4211 | // The result type of a pointer-int computation is the pointer type. |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4212 | if (rex->getType()->isIntegerType()) { |
| 4213 | if (ComplainAboutVoid) |
| 4214 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4215 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4216 | if (ComplainAboutFunc) |
| 4217 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4218 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4219 | << ComplainAboutFunc->getSourceRange(); |
| 4220 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4221 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4222 | return lex->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4223 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4224 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4225 | // Handle pointer-pointer subtractions. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4226 | if (const PointerType *RHSPTy = rex->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 4227 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4228 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4229 | // RHS must be a completely-type object type. |
| 4230 | // Handle the GNU void* extension. |
| 4231 | if (rpointee->isVoidType()) { |
| 4232 | if (getLangOptions().CPlusPlus) { |
| 4233 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| 4234 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4235 | return QualType(); |
| 4236 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4237 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4238 | ComplainAboutVoid = true; |
| 4239 | } else if (rpointee->isFunctionType()) { |
| 4240 | if (getLangOptions().CPlusPlus) { |
| 4241 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4242 | << rex->getType() << rex->getSourceRange(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4243 | return QualType(); |
| 4244 | } |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4245 | |
| 4246 | // GNU extension: arithmetic on pointer to function |
| 4247 | if (!ComplainAboutFunc) |
| 4248 | ComplainAboutFunc = rex; |
| 4249 | } else if (!rpointee->isDependentType() && |
| 4250 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4251 | PDiag(diag::err_typecheck_sub_ptr_object) |
| 4252 | << rex->getSourceRange() |
| 4253 | << rex->getType())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4254 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4255 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 4256 | if (getLangOptions().CPlusPlus) { |
| 4257 | // Pointee types must be the same: C++ [expr.add] |
| 4258 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 4259 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4260 | << lex->getType() << rex->getType() |
| 4261 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4262 | return QualType(); |
| 4263 | } |
| 4264 | } else { |
| 4265 | // Pointee types must be compatible C99 6.5.6p3 |
| 4266 | if (!Context.typesAreCompatible( |
| 4267 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 4268 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 4269 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 4270 | << lex->getType() << rex->getType() |
| 4271 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4272 | return QualType(); |
| 4273 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4274 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4275 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4276 | if (ComplainAboutVoid) |
| 4277 | Diag(Loc, diag::ext_gnu_void_ptr) |
| 4278 | << lex->getSourceRange() << rex->getSourceRange(); |
| 4279 | if (ComplainAboutFunc) |
| 4280 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4281 | << ComplainAboutFunc->getType() |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 4282 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4283 | |
| 4284 | if (CompLHSTy) *CompLHSTy = lex->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 4285 | return Context.getPointerDiffType(); |
| 4286 | } |
| 4287 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4288 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4289 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4290 | } |
| 4291 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4292 | // C99 6.5.7 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4293 | QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 4294 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4295 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 4296 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4297 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4298 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4299 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 4300 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 4301 | QualType LHSTy = Context.isPromotableBitField(lex); |
| 4302 | if (LHSTy.isNull()) { |
| 4303 | LHSTy = lex->getType(); |
| 4304 | if (LHSTy->isPromotableIntegerType()) |
| 4305 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4306 | } |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 4307 | if (!isCompAssign) |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4308 | ImpCastExprToType(lex, LHSTy); |
| 4309 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4310 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4311 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4312 | // Sanity-check shift operands |
| 4313 | llvm::APSInt Right; |
| 4314 | // Check right/shifter operand |
| 4315 | if (rex->isIntegerConstantExpr(Right, Context)) { |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 4316 | if (Right.isNegative()) |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 4317 | Diag(Loc, diag::warn_shift_negative) << rex->getSourceRange(); |
| 4318 | else { |
| 4319 | llvm::APInt LeftBits(Right.getBitWidth(), |
| 4320 | Context.getTypeSize(lex->getType())); |
| 4321 | if (Right.uge(LeftBits)) |
| 4322 | Diag(Loc, diag::warn_shift_gt_typewidth) << rex->getSourceRange(); |
| 4323 | } |
| 4324 | } |
| 4325 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 4326 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 4327 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4330 | // C99 6.5.8, C++ [expr.rel] |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4331 | QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation Loc, |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4332 | unsigned OpaqueOpc, bool isRelational) { |
| 4333 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)OpaqueOpc; |
| 4334 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4335 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4336 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4337 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4338 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 4339 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 4340 | UsualArithmeticConversions(lex, rex); |
| 4341 | else { |
| 4342 | UsualUnaryConversions(lex); |
| 4343 | UsualUnaryConversions(rex); |
| 4344 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4345 | QualType lType = lex->getType(); |
| 4346 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4347 | |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4348 | if (!lType->isFloatingType() |
| 4349 | && !(lType->isBlockPointerType() && isRelational)) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4350 | // For non-floating point types, check for self-comparisons of the form |
| 4351 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4352 | // often indicate logic errors in the program. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4353 | // NOTE: Don't warn about comparisons of enum constants. These can arise |
Ted Kremenek | 9ecede7 | 2009-03-20 19:57:37 +0000 | [diff] [blame] | 4354 | // from macro expansions, and are usually quite deliberate. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4355 | Expr *LHSStripped = lex->IgnoreParens(); |
| 4356 | Expr *RHSStripped = rex->IgnoreParens(); |
| 4357 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) |
| 4358 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) |
Ted Kremenek | b82dcd8 | 2009-03-20 18:35:45 +0000 | [diff] [blame] | 4359 | if (DRL->getDecl() == DRR->getDecl() && |
| 4360 | !isa<EnumConstantDecl>(DRL->getDecl())) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4361 | Diag(Loc, diag::warn_selfcomparison); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4363 | if (isa<CastExpr>(LHSStripped)) |
| 4364 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 4365 | if (isa<CastExpr>(RHSStripped)) |
| 4366 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4367 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4368 | // Warn about comparisons against a string constant (unless the other |
| 4369 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4370 | Expr *literalString = 0; |
| 4371 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4372 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4373 | !RHSStripped->isNullPointerConstant(Context)) { |
| 4374 | literalString = lex; |
| 4375 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 4376 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 4377 | isa<ObjCEncodeExpr>(RHSStripped)) && |
| 4378 | !LHSStripped->isNullPointerConstant(Context)) { |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4379 | literalString = rex; |
| 4380 | literalStringStripped = RHSStripped; |
| 4381 | } |
| 4382 | |
| 4383 | if (literalString) { |
| 4384 | std::string resultComparison; |
| 4385 | switch (Opc) { |
| 4386 | case BinaryOperator::LT: resultComparison = ") < 0"; break; |
| 4387 | case BinaryOperator::GT: resultComparison = ") > 0"; break; |
| 4388 | case BinaryOperator::LE: resultComparison = ") <= 0"; break; |
| 4389 | case BinaryOperator::GE: resultComparison = ") >= 0"; break; |
| 4390 | case BinaryOperator::EQ: resultComparison = ") == 0"; break; |
| 4391 | case BinaryOperator::NE: resultComparison = ") != 0"; break; |
| 4392 | default: assert(false && "Invalid comparison operator"); |
| 4393 | } |
| 4394 | Diag(Loc, diag::warn_stringcompare) |
| 4395 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| 4396 | << literalString->getSourceRange() |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 4397 | << CodeModificationHint::CreateReplacement(SourceRange(Loc), ", ") |
| 4398 | << CodeModificationHint::CreateInsertion(lex->getLocStart(), |
| 4399 | "strcmp(") |
| 4400 | << CodeModificationHint::CreateInsertion( |
| 4401 | PP.getLocForEndOfToken(rex->getLocEnd()), |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 4402 | resultComparison); |
| 4403 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 4404 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4406 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4407 | QualType ResultTy = getLangOptions().CPlusPlus? Context.BoolTy :Context.IntTy; |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4408 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4409 | if (isRelational) { |
| 4410 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4411 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4412 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4413 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 4414 | if (lType->isFloatingType()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 4415 | assert(rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4416 | CheckFloatComparison(Loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 4417 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4418 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4419 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4420 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4421 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4422 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 4423 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 4424 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4425 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 4426 | // All of the following pointer related warnings are GCC extensions, except |
| 4427 | // when handling null pointer constants. One day, we can consider making them |
| 4428 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 4429 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4430 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4431 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 4432 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4433 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4434 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4435 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4436 | if (LCanPointeeTy == RCanPointeeTy) |
| 4437 | return ResultTy; |
| 4438 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4439 | // C++ [expr.rel]p2: |
| 4440 | // [...] Pointer conversions (4.10) and qualification |
| 4441 | // conversions (4.4) are performed on pointer operands (or on |
| 4442 | // a pointer operand and a null pointer constant) to bring |
| 4443 | // them to their composite pointer type. [...] |
| 4444 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4445 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4446 | // comparisons of pointers. |
Douglas Gregor | de866f3 | 2009-05-05 04:50:50 +0000 | [diff] [blame] | 4447 | QualType T = FindCompositePointerType(lex, rex); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 4448 | if (T.isNull()) { |
| 4449 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4450 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4451 | return QualType(); |
| 4452 | } |
| 4453 | |
| 4454 | ImpCastExprToType(lex, T); |
| 4455 | ImpCastExprToType(rex, T); |
| 4456 | return ResultTy; |
| 4457 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4458 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 4459 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 4460 | RCanPointeeTy.getUnqualifiedType())) { |
| 4461 | // Valid unless a relational comparison of function pointers |
| 4462 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 4463 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| 4464 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4465 | } |
| 4466 | } else if (!isRelational && |
| 4467 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 4468 | // Valid unless comparison between non-null pointer and function pointer |
| 4469 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 4470 | && !LHSIsNull && !RHSIsNull) { |
| 4471 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| 4472 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4473 | } |
| 4474 | } else { |
| 4475 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4476 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4477 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4478 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 4479 | if (LCanPointeeTy != RCanPointeeTy) |
| 4480 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4481 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4483 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4484 | if (getLangOptions().CPlusPlus) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4485 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4486 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | if (RHSIsNull && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4488 | (lType->isPointerType() || |
| 4489 | (!isRelational && lType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4490 | ImpCastExprToType(rex, lType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4491 | return ResultTy; |
| 4492 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4493 | if (LHSIsNull && |
| 4494 | (rType->isPointerType() || |
| 4495 | (!isRelational && rType->isMemberPointerType()))) { |
Anders Carlsson | 26ba850 | 2009-08-24 18:03:14 +0000 | [diff] [blame] | 4496 | ImpCastExprToType(lex, rType, CastExpr::CK_NullToMemberPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4497 | return ResultTy; |
| 4498 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4499 | |
| 4500 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4502 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 4503 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4504 | // In addition, pointers to members can be compared, or a pointer to |
| 4505 | // member and a null pointer constant. Pointer to member conversions |
| 4506 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 4507 | // them to a common type. If one operand is a null pointer constant, |
| 4508 | // the common type is the type of the other operand. Otherwise, the |
| 4509 | // common type is a pointer to member type similar (4.4) to the type |
| 4510 | // of one of the operands, with a cv-qualification signature (4.4) |
| 4511 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4512 | // types. |
| 4513 | QualType T = FindCompositePointerType(lex, rex); |
| 4514 | if (T.isNull()) { |
| 4515 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| 4516 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
| 4517 | return QualType(); |
| 4518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4519 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4520 | ImpCastExprToType(lex, T); |
| 4521 | ImpCastExprToType(rex, T); |
| 4522 | return ResultTy; |
| 4523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4524 | |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 4525 | // Comparison of nullptr_t with itself. |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 4526 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 4527 | return ResultTy; |
| 4528 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4529 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4530 | // Handle block pointer types. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4531 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4532 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 4533 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4534 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4535 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 4536 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4537 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4538 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4539 | } |
| 4540 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4541 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4542 | } |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4543 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4544 | if (!isRelational |
| 4545 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 4546 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4547 | if (!LHSIsNull && !RHSIsNull) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4548 | if (!((rType->isPointerType() && rType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4549 | ->getPointeeType()->isVoidType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4550 | || (lType->isPointerType() && lType->getAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4551 | ->getPointeeType()->isVoidType()))) |
| 4552 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| 4553 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4554 | } |
| 4555 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4556 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 4557 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 4558 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4559 | if ((lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType())) { |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4560 | if (lType->isPointerType() || rType->isPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4561 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 4562 | const PointerType *RPT = rType->getAs<PointerType>(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4563 | bool LPtrToVoid = LPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4564 | Context.getCanonicalType(LPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4565 | bool RPtrToVoid = RPT ? |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4566 | Context.getCanonicalType(RPT->getPointeeType())->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4567 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 4568 | if (!LPtrToVoid && !RPtrToVoid && |
| 4569 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 4570 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4571 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 4572 | } |
Daniel Dunbar | c6cb77f | 2008-10-23 23:30:52 +0000 | [diff] [blame] | 4573 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4574 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 4575 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4576 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4577 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4578 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| 4579 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4580 | ImpCastExprToType(rex, lType); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4581 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 4582 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 4583 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4584 | if (lType->isAnyPointerType() && rType->isIntegerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4585 | unsigned DiagID = 0; |
| 4586 | if (RHSIsNull) { |
| 4587 | if (isRelational) |
| 4588 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4589 | } else if (isRelational) |
| 4590 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4591 | else |
| 4592 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4594 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4595 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4596 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4597 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4598 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4599 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 4600 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4601 | if (lType->isIntegerType() && rType->isAnyPointerType()) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4602 | unsigned DiagID = 0; |
| 4603 | if (LHSIsNull) { |
| 4604 | if (isRelational) |
| 4605 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| 4606 | } else if (isRelational) |
| 4607 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| 4608 | else |
| 4609 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4610 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 4611 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4612 | Diag(Loc, DiagID) |
Chris Lattner | 149f138 | 2009-06-30 06:24:05 +0000 | [diff] [blame] | 4613 | << lType << rType << lex->getSourceRange() << rex->getSourceRange(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 4614 | } |
Chris Lattner | 1e0a390 | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 4615 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4616 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4617 | } |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4618 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4619 | if (!isRelational && RHSIsNull |
| 4620 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4621 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4622 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4623 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 4624 | if (!isRelational && LHSIsNull |
| 4625 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4626 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 4627 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 4628 | } |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4629 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4632 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4633 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4634 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 4635 | /// types. |
| 4636 | QualType Sema::CheckVectorCompareOperands(Expr *&lex, Expr *&rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4637 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4638 | bool isRelational) { |
| 4639 | // Check to make sure we're operating on vectors of the same type and width, |
| 4640 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4641 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4642 | if (vType.isNull()) |
| 4643 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4644 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4645 | QualType lType = lex->getType(); |
| 4646 | QualType rType = rex->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4647 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4648 | // For non-floating point types, check for self-comparisons of the form |
| 4649 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 4650 | // often indicate logic errors in the program. |
| 4651 | if (!lType->isFloatingType()) { |
| 4652 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 4653 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
| 4654 | if (DRL->getDecl() == DRR->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4655 | Diag(Loc, diag::warn_selfcomparison); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4656 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4657 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4658 | // Check for comparisons of floating point operands using != and ==. |
| 4659 | if (!isRelational && lType->isFloatingType()) { |
| 4660 | assert (rType->isFloatingType()); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4661 | CheckFloatComparison(Loc,lex,rex); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4662 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4663 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4664 | // Return the type for the comparison, which is the same as vector type for |
| 4665 | // integer vectors, or an integer type of identical size and number of |
| 4666 | // elements for floating point vectors. |
| 4667 | if (lType->isIntegerType()) |
| 4668 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4669 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4670 | const VectorType *VTy = lType->getAsVectorType(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4671 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4672 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4673 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 4674 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4675 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 4676 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4677 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 4678 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 4679 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 4680 | } |
| 4681 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4682 | inline QualType Sema::CheckBitwiseOperands( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | Expr *&lex, Expr *&rex, SourceLocation Loc, bool isCompAssign) { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 4684 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4685 | return CheckVectorOperands(Loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 4686 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4687 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4688 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4689 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 4690 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4691 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4695 | Expr *&lex, Expr *&rex, SourceLocation Loc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4696 | UsualUnaryConversions(lex); |
| 4697 | UsualUnaryConversions(rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4698 | |
Eli Friedman | 5773a6c | 2008-05-13 20:16:47 +0000 | [diff] [blame] | 4699 | if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4700 | return Context.IntTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4701 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
Fariborz Jahanian | d1fa644 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4708 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4709 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 4710 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 4711 | if (ObjCPropertyDecl *PDecl = PropExpr->getProperty()) { |
| 4712 | QualType BaseType = PropExpr->getBase()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4713 | if (const ObjCObjectPointerType *OPT = |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4714 | BaseType->getAsObjCInterfacePointerType()) |
| 4715 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 4716 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 4717 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4718 | } |
| 4719 | } |
| 4720 | return false; |
| 4721 | } |
| 4722 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4723 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 4724 | /// emit an error and return true. If so, return false. |
| 4725 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4726 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4728 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 4729 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 4730 | IsLV = Expr::MLV_ReadonlyProperty; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4731 | if (IsLV == Expr::MLV_Valid) |
| 4732 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4733 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4734 | unsigned Diag = 0; |
| 4735 | bool NeedType = false; |
| 4736 | switch (IsLV) { // C99 6.5.16p2 |
| 4737 | default: assert(0 && "Unknown result from isModifiableLvalue!"); |
| 4738 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4739 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4740 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 4741 | NeedType = true; |
| 4742 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4743 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4744 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 4745 | NeedType = true; |
| 4746 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 4747 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4748 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 4749 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4750 | case Expr::MLV_InvalidExpression: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4751 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 4752 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4753 | case Expr::MLV_IncompleteType: |
| 4754 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 4755 | return S.RequireCompleteType(Loc, E->getType(), |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4756 | PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| 4757 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4758 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4759 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 4760 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 4761 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4762 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 4763 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 4764 | case Expr::MLV_ReadonlyProperty: |
| 4765 | Diag = diag::error_readonly_property_assignment; |
| 4766 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 4767 | case Expr::MLV_NoSetterProperty: |
| 4768 | Diag = diag::error_nosetter_property_assignment; |
| 4769 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4770 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4771 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4772 | SourceRange Assign; |
| 4773 | if (Loc != OrigLoc) |
| 4774 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4775 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 4776 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4777 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4778 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4779 | return true; |
| 4780 | } |
| 4781 | |
| 4782 | |
| 4783 | |
| 4784 | // C99 6.5.16.1 |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4785 | QualType Sema::CheckAssignmentOperands(Expr *LHS, Expr *&RHS, |
| 4786 | SourceLocation Loc, |
| 4787 | QualType CompoundType) { |
| 4788 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 4789 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 4790 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4791 | |
| 4792 | QualType LHSType = LHS->getType(); |
| 4793 | QualType RHSType = CompoundType.isNull() ? RHS->getType() : CompoundType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4794 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4795 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4796 | if (CompoundType.isNull()) { |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4797 | // Simple assignment "x = y". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4798 | ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4799 | // Special case of NSObject attributes on c-style pointer types. |
| 4800 | if (ConvTy == IncompatiblePointer && |
| 4801 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4802 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4803 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 4804 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 4805 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4806 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4807 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 4808 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 4809 | // instead of "x += 4". |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4810 | Expr *RHSCheck = RHS; |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4811 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 4812 | RHSCheck = ICE->getSubExpr(); |
| 4813 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| 4814 | if ((UO->getOpcode() == UnaryOperator::Plus || |
| 4815 | UO->getOpcode() == UnaryOperator::Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4816 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4817 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4818 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 4819 | // And there is a space or other character before the subexpr of the |
| 4820 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 4821 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 4822 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 4823 | Diag(Loc, diag::warn_not_compound_assign) |
| 4824 | << (UO->getOpcode() == UnaryOperator::Plus ? "+" : "-") |
| 4825 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 4826 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4827 | } |
| 4828 | } else { |
| 4829 | // Compound assignment "x += y" |
Eli Friedman | 623712b | 2009-05-16 05:56:02 +0000 | [diff] [blame] | 4830 | ConvTy = CheckAssignmentConstraints(LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 4831 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4832 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4833 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| 4834 | RHS, "assigning")) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 4835 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4836 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4837 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 4838 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4839 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4840 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 4841 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 4842 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 4843 | // operand. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4844 | return LHSType.getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4847 | // C99 6.5.17 |
| 4848 | QualType Sema::CheckCommaOperands(Expr *LHS, Expr *&RHS, SourceLocation Loc) { |
Chris Lattner | 53fcaa9 | 2008-07-25 20:54:07 +0000 | [diff] [blame] | 4849 | // Comma performs lvalue conversion (C99 6.3.2.1), but not unary conversions. |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4850 | DefaultFunctionArrayConversion(RHS); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4851 | |
| 4852 | // FIXME: Check that RHS type is complete in C mode (it's legal for it to be |
| 4853 | // incomplete in C++). |
| 4854 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 4855 | return RHS->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4856 | } |
| 4857 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 4858 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 4859 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4860 | QualType Sema::CheckIncrementDecrementOperand(Expr *Op, SourceLocation OpLoc, |
| 4861 | bool isInc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4862 | if (Op->isTypeDependent()) |
| 4863 | return Context.DependentTy; |
| 4864 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4865 | QualType ResType = Op->getType(); |
| 4866 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4867 | |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 4868 | if (getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| 4869 | // Decrement of bool is not allowed. |
| 4870 | if (!isInc) { |
| 4871 | Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| 4872 | return QualType(); |
| 4873 | } |
| 4874 | // Increment of bool sets it to true, but is deprecated. |
| 4875 | Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| 4876 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4877 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4878 | } else if (ResType->isAnyPointerType()) { |
| 4879 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4881 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4882 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4883 | if (getLangOptions().CPlusPlus) { |
| 4884 | Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| 4885 | << Op->getSourceRange(); |
| 4886 | return QualType(); |
| 4887 | } |
| 4888 | |
| 4889 | // Pointer to void is a GNU extension in C. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4890 | Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4891 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | c983b86 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 4892 | if (getLangOptions().CPlusPlus) { |
| 4893 | Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| 4894 | << Op->getType() << Op->getSourceRange(); |
| 4895 | return QualType(); |
| 4896 | } |
| 4897 | |
| 4898 | Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4899 | << ResType << Op->getSourceRange(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4900 | } else if (RequireCompleteType(OpLoc, PointeeTy, |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4901 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | << Op->getSourceRange() |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 4903 | << ResType)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 4904 | return QualType(); |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 4905 | // Diagnose bad cases where we step over interface counts. |
| 4906 | else if (PointeeTy->isObjCInterfaceType() && LangOpts.ObjCNonFragileABI) { |
| 4907 | Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 4908 | << PointeeTy << Op->getSourceRange(); |
| 4909 | return QualType(); |
| 4910 | } |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4911 | } else if (ResType->isComplexType()) { |
| 4912 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| 4913 | Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4914 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4915 | } else { |
| 4916 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4917 | << ResType << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4918 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4919 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4920 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 4921 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4922 | if (CheckForModifiableLvalue(Op, OpLoc, *this)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4923 | return QualType(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 4924 | return ResType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4925 | } |
| 4926 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4927 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4928 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4929 | /// where the declaration is needed for type checking. We only need to |
| 4930 | /// handle cases when the expression references a function designator |
| 4931 | /// or is an lvalue. Here are some examples: |
| 4932 | /// - &(x) => x |
| 4933 | /// - &*****f => f for f a function designator. |
| 4934 | /// - &s.xx => s |
| 4935 | /// - &s.zz[1].yy -> s, if zz is an array |
| 4936 | /// - *(x + 1) -> x, if x is an array |
| 4937 | /// - &"123"[2] -> 0 |
| 4938 | /// - & __real__ x -> x |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 4939 | static NamedDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4940 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4941 | case Stmt::DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 4942 | case Stmt::QualifiedDeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4943 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4944 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4945 | // If this is an arrow operator, the address is an offset from |
| 4946 | // the base's value, so the object the base refers to is |
| 4947 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4948 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4949 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4950 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4951 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4952 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4953 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 4954 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4955 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 4956 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 4957 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 4958 | return getPrimaryDecl(ICE->getSubExpr()); |
| 4959 | } |
| 4960 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 4961 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4962 | case Stmt::UnaryOperatorClass: { |
| 4963 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4964 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4965 | switch(UO->getOpcode()) { |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 4966 | case UnaryOperator::Real: |
| 4967 | case UnaryOperator::Imag: |
| 4968 | case UnaryOperator::Extension: |
| 4969 | return getPrimaryDecl(UO->getSubExpr()); |
| 4970 | default: |
| 4971 | return 0; |
| 4972 | } |
| 4973 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4974 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4975 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 4976 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4977 | // If the result of an implicit cast is an l-value, we care about |
| 4978 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4979 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4980 | default: |
| 4981 | return 0; |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4986 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4987 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4988 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4989 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4990 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 4991 | /// we allow the '&' but retain the overloaded-function type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4992 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 4993 | // Make sure to ignore parentheses in subsequent checks |
| 4994 | op = op->IgnoreParens(); |
| 4995 | |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 4996 | if (op->isTypeDependent()) |
| 4997 | return Context.DependentTy; |
| 4998 | |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 4999 | if (getLangOptions().C99) { |
| 5000 | // Implement C99-only parts of addressof rules. |
| 5001 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 5002 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 5003 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 5004 | // (assuming the deref expression is valid). |
| 5005 | return uOp->getSubExpr()->getType(); |
| 5006 | } |
| 5007 | // Technically, there should be a check for array subscript |
| 5008 | // expressions here, but the result of one is always an lvalue anyway. |
| 5009 | } |
Douglas Gregor | 8e9bebd | 2008-10-21 16:13:35 +0000 | [diff] [blame] | 5010 | NamedDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 5011 | Expr::isLvalueResult lval = op->isLvalue(Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 5012 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5013 | if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| 5014 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5015 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5016 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 5017 | // FIXME: emit more specific diag... |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5018 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 5019 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5020 | return QualType(); |
| 5021 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 5022 | } else if (op->getBitField()) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5023 | // The operand cannot be a bit-field |
| 5024 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5025 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 5026 | return QualType(); |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5027 | } else if (isa<ExtVectorElementExpr>(op) || (isa<ArraySubscriptExpr>(op) && |
| 5028 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType())){ |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 5029 | // The operand cannot be an element of a vector |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5030 | Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 5031 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5032 | return QualType(); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 5033 | } else if (isa<ObjCPropertyRefExpr>(op)) { |
| 5034 | // cannot take address of a property expression. |
| 5035 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5036 | << "property expression" << op->getSourceRange(); |
| 5037 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 5038 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5039 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5040 | // with the register storage-class specifier. |
| 5041 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 5042 | if (vd->getStorageClass() == VarDecl::Register) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5043 | Diag(OpLoc, diag::err_typecheck_address_of) |
| 5044 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5045 | return QualType(); |
| 5046 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 5047 | } else if (isa<OverloadedFunctionDecl>(dcl) || |
| 5048 | isa<FunctionTemplateDecl>(dcl)) { |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 5049 | return Context.OverloadTy; |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5050 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 5051 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5052 | // Could be a pointer to member, though, if there is an explicit |
| 5053 | // scope qualifier for the class. |
| 5054 | if (isa<QualifiedDeclRefExpr>(op)) { |
| 5055 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5056 | if (Ctx && Ctx->isRecord()) { |
| 5057 | if (FD->getType()->isReferenceType()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5058 | Diag(OpLoc, |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5059 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| 5060 | << FD->getDeclName() << FD->getType(); |
| 5061 | return QualType(); |
| 5062 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5063 | |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5064 | return Context.getMemberPointerType(op->getType(), |
| 5065 | Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 5066 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 5067 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5068 | } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(dcl)) { |
Nuno Lopes | 6fea8d2 | 2008-12-16 22:58:26 +0000 | [diff] [blame] | 5069 | // Okay: we can take the address of a function. |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5070 | // As above. |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 5071 | if (isa<QualifiedDeclRefExpr>(op) && MD->isInstance()) |
| 5072 | return Context.getMemberPointerType(op->getType(), |
| 5073 | Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| 5074 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5075 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5076 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 5077 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 5078 | if (lval == Expr::LV_IncompleteVoidType) { |
| 5079 | // Taking the address of a void variable is technically illegal, but we |
| 5080 | // allow it in cases which are otherwise valid. |
| 5081 | // Example: "extern void x; void* y = &x;". |
| 5082 | Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| 5083 | } |
| 5084 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5085 | // If the operand has type "type", the result has type "pointer to type". |
| 5086 | return Context.getPointerType(op->getType()); |
| 5087 | } |
| 5088 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5089 | QualType Sema::CheckIndirectionOperand(Expr *Op, SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5090 | if (Op->isTypeDependent()) |
| 5091 | return Context.DependentTy; |
| 5092 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5093 | UsualUnaryConversions(Op); |
| 5094 | QualType Ty = Op->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5095 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5096 | // Note that per both C89 and C99, this is always legal, even if ptype is an |
| 5097 | // incomplete type or void. It would be possible to warn about dereferencing |
| 5098 | // a void pointer, but it's completely well-defined, and such a warning is |
| 5099 | // unlikely to catch any mistakes. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5100 | if (const PointerType *PT = Ty->getAs<PointerType>()) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 5101 | return PT->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5102 | |
Fariborz Jahanian | 16b1037 | 2009-09-03 00:43:07 +0000 | [diff] [blame] | 5103 | if (const ObjCObjectPointerType *OPT = Ty->getAsObjCObjectPointerType()) |
| 5104 | return OPT->getPointeeType(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5105 | |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5106 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 5107 | << Ty << Op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5108 | return QualType(); |
| 5109 | } |
| 5110 | |
| 5111 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 5112 | tok::TokenKind Kind) { |
| 5113 | BinaryOperator::Opcode Opc; |
| 5114 | switch (Kind) { |
| 5115 | default: assert(0 && "Unknown binop!"); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5116 | case tok::periodstar: Opc = BinaryOperator::PtrMemD; break; |
| 5117 | case tok::arrowstar: Opc = BinaryOperator::PtrMemI; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5118 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 5119 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 5120 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 5121 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 5122 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 5123 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 5124 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 5125 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 5126 | case tok::less: Opc = BinaryOperator::LT; break; |
| 5127 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 5128 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 5129 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 5130 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 5131 | case tok::amp: Opc = BinaryOperator::And; break; |
| 5132 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 5133 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 5134 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 5135 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 5136 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 5137 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 5138 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 5139 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 5140 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 5141 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 5142 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 5143 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 5144 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 5145 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 5146 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 5147 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 5148 | } |
| 5149 | return Opc; |
| 5150 | } |
| 5151 | |
| 5152 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 5153 | tok::TokenKind Kind) { |
| 5154 | UnaryOperator::Opcode Opc; |
| 5155 | switch (Kind) { |
| 5156 | default: assert(0 && "Unknown unary op!"); |
| 5157 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 5158 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 5159 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 5160 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 5161 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 5162 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 5163 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 5164 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5165 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 5166 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 5167 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 5168 | } |
| 5169 | return Opc; |
| 5170 | } |
| 5171 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5172 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 5173 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 5174 | /// built-in operations; ActOnBinOp handles overloaded operators. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5175 | Action::OwningExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| 5176 | unsigned Op, |
| 5177 | Expr *lhs, Expr *rhs) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5178 | QualType ResultTy; // Result type of the binary operator. |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5179 | BinaryOperator::Opcode Opc = (BinaryOperator::Opcode)Op; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5180 | // The following two variables are used for compound assignment operators |
| 5181 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 5182 | QualType CompResultTy; // Type of computation result |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5183 | |
| 5184 | switch (Opc) { |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5185 | case BinaryOperator::Assign: |
| 5186 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, QualType()); |
| 5187 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5188 | case BinaryOperator::PtrMemD: |
| 5189 | case BinaryOperator::PtrMemI: |
| 5190 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, OpLoc, |
| 5191 | Opc == BinaryOperator::PtrMemI); |
| 5192 | break; |
| 5193 | case BinaryOperator::Mul: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5194 | case BinaryOperator::Div: |
| 5195 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc); |
| 5196 | break; |
| 5197 | case BinaryOperator::Rem: |
| 5198 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 5199 | break; |
| 5200 | case BinaryOperator::Add: |
| 5201 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 5202 | break; |
| 5203 | case BinaryOperator::Sub: |
| 5204 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 5205 | break; |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5206 | case BinaryOperator::Shl: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5207 | case BinaryOperator::Shr: |
| 5208 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc); |
| 5209 | break; |
| 5210 | case BinaryOperator::LE: |
| 5211 | case BinaryOperator::LT: |
| 5212 | case BinaryOperator::GE: |
| 5213 | case BinaryOperator::GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5214 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5215 | break; |
| 5216 | case BinaryOperator::EQ: |
| 5217 | case BinaryOperator::NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 5218 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5219 | break; |
| 5220 | case BinaryOperator::And: |
| 5221 | case BinaryOperator::Xor: |
| 5222 | case BinaryOperator::Or: |
| 5223 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 5224 | break; |
| 5225 | case BinaryOperator::LAnd: |
| 5226 | case BinaryOperator::LOr: |
| 5227 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc); |
| 5228 | break; |
| 5229 | case BinaryOperator::MulAssign: |
| 5230 | case BinaryOperator::DivAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5231 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true); |
| 5232 | CompLHSTy = CompResultTy; |
| 5233 | if (!CompResultTy.isNull()) |
| 5234 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5235 | break; |
| 5236 | case BinaryOperator::RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5237 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 5238 | CompLHSTy = CompResultTy; |
| 5239 | if (!CompResultTy.isNull()) |
| 5240 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5241 | break; |
| 5242 | case BinaryOperator::AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5243 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5244 | if (!CompResultTy.isNull()) |
| 5245 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5246 | break; |
| 5247 | case BinaryOperator::SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5248 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| 5249 | if (!CompResultTy.isNull()) |
| 5250 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5251 | break; |
| 5252 | case BinaryOperator::ShlAssign: |
| 5253 | case BinaryOperator::ShrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5254 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, true); |
| 5255 | CompLHSTy = CompResultTy; |
| 5256 | if (!CompResultTy.isNull()) |
| 5257 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5258 | break; |
| 5259 | case BinaryOperator::AndAssign: |
| 5260 | case BinaryOperator::XorAssign: |
| 5261 | case BinaryOperator::OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5262 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 5263 | CompLHSTy = CompResultTy; |
| 5264 | if (!CompResultTy.isNull()) |
| 5265 | ResultTy = CheckAssignmentOperands(lhs, rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5266 | break; |
| 5267 | case BinaryOperator::Comma: |
| 5268 | ResultTy = CheckCommaOperands(lhs, rhs, OpLoc); |
| 5269 | break; |
| 5270 | } |
| 5271 | if (ResultTy.isNull()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5272 | return ExprError(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5273 | if (CompResultTy.isNull()) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5274 | return Owned(new (Context) BinaryOperator(lhs, rhs, Opc, ResultTy, OpLoc)); |
| 5275 | else |
| 5276 | return Owned(new (Context) CompoundAssignOperator(lhs, rhs, Opc, ResultTy, |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5277 | CompLHSTy, CompResultTy, |
| 5278 | OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5281 | // Binary Operators. 'Tok' is the token for the operator. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5282 | Action::OwningExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| 5283 | tok::TokenKind Kind, |
| 5284 | ExprArg LHS, ExprArg RHS) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5285 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5286 | Expr *lhs = LHS.takeAs<Expr>(), *rhs = RHS.takeAs<Expr>(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5287 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5288 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 5289 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5290 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5291 | if (getLangOptions().CPlusPlus && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5292 | (lhs->getType()->isOverloadableType() || |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5293 | rhs->getType()->isOverloadableType())) { |
| 5294 | // Find all of the overloaded operators visible from this |
| 5295 | // point. We perform both an operator-name lookup from the local |
| 5296 | // scope and an argument-dependent lookup based on the types of |
| 5297 | // the arguments. |
Douglas Gregor | 3fd95ce | 2009-03-13 00:33:25 +0000 | [diff] [blame] | 5298 | FunctionSet Functions; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5299 | OverloadedOperatorKind OverOp = BinaryOperator::getOverloadedOperator(Opc); |
| 5300 | if (OverOp != OO_None) { |
| 5301 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 5302 | Functions); |
| 5303 | Expr *Args[2] = { lhs, rhs }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | DeclarationName OpName |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5305 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5306 | ArgumentDependentLookup(OpName, Args, 2, Functions); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 5307 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5308 | |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 5309 | // Build the (potentially-overloaded, potentially-dependent) |
| 5310 | // binary operation. |
| 5311 | return CreateOverloadedBinOp(TokLoc, Opc, Functions, lhs, rhs); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5312 | } |
| 5313 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 5314 | // Build a built-in binary operation. |
| 5315 | return CreateBuiltinBinOp(TokLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5318 | Action::OwningExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5319 | unsigned OpcIn, |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5320 | ExprArg InputArg) { |
| 5321 | UnaryOperator::Opcode Opc = static_cast<UnaryOperator::Opcode>(OpcIn); |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5322 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 5323 | // FIXME: Input is modified below, but InputArg is not updated appropriately. |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5324 | Expr *Input = (Expr *)InputArg.get(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5325 | QualType resultType; |
| 5326 | switch (Opc) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5327 | case UnaryOperator::OffsetOf: |
| 5328 | assert(false && "Invalid unary operator"); |
| 5329 | break; |
| 5330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5331 | case UnaryOperator::PreInc: |
| 5332 | case UnaryOperator::PreDec: |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5333 | case UnaryOperator::PostInc: |
| 5334 | case UnaryOperator::PostDec: |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 5335 | resultType = CheckIncrementDecrementOperand(Input, OpLoc, |
Eli Friedman | de99a45 | 2009-07-22 22:25:00 +0000 | [diff] [blame] | 5336 | Opc == UnaryOperator::PreInc || |
| 5337 | Opc == UnaryOperator::PostInc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5338 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5339 | case UnaryOperator::AddrOf: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5340 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 5341 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5342 | case UnaryOperator::Deref: |
Steve Naroff | 1ca9b11 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 5343 | DefaultFunctionArrayConversion(Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5344 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 5345 | break; |
| 5346 | case UnaryOperator::Plus: |
| 5347 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5348 | UsualUnaryConversions(Input); |
| 5349 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5350 | if (resultType->isDependentType()) |
| 5351 | break; |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 5352 | if (resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 5353 | break; |
| 5354 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 5355 | resultType->isEnumeralType()) |
| 5356 | break; |
| 5357 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| 5358 | Opc == UnaryOperator::Plus && |
| 5359 | resultType->isPointerType()) |
| 5360 | break; |
| 5361 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5362 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5363 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5364 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5365 | UsualUnaryConversions(Input); |
| 5366 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5367 | if (resultType->isDependentType()) |
| 5368 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5369 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 5370 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 5371 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 5372 | Diag(OpLoc, diag::ext_integer_complement_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5373 | << resultType << Input->getSourceRange(); |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 5374 | else if (!resultType->isIntegerType()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5375 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5376 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5377 | break; |
| 5378 | case UnaryOperator::LNot: // logical negation |
| 5379 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5380 | DefaultFunctionArrayConversion(Input); |
| 5381 | resultType = Input->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5382 | if (resultType->isDependentType()) |
| 5383 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5384 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5385 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| 5386 | << resultType << Input->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5387 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5388 | // In C++, it's bool. C++ 5.3.1p8 |
| 5389 | resultType = getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5390 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5391 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5392 | case UnaryOperator::Imag: |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 5393 | resultType = CheckRealImagOperand(Input, OpLoc, Opc == UnaryOperator::Real); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 5394 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5395 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5396 | resultType = Input->getType(); |
| 5397 | break; |
| 5398 | } |
| 5399 | if (resultType.isNull()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5400 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5401 | |
| 5402 | InputArg.release(); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5403 | return Owned(new (Context) UnaryOperator(Input, Opc, resultType, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5406 | // Unary Operators. 'Tok' is the token for the operator. |
| 5407 | Action::OwningExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| 5408 | tok::TokenKind Op, ExprArg input) { |
| 5409 | Expr *Input = (Expr*)input.get(); |
| 5410 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 5411 | |
| 5412 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType()) { |
| 5413 | // Find all of the overloaded operators visible from this |
| 5414 | // point. We perform both an operator-name lookup from the local |
| 5415 | // scope and an argument-dependent lookup based on the types of |
| 5416 | // the arguments. |
| 5417 | FunctionSet Functions; |
| 5418 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| 5419 | if (OverOp != OO_None) { |
| 5420 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 5421 | Functions); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | DeclarationName OpName |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 5423 | = Context.DeclarationNames.getCXXOperatorName(OverOp); |
| 5424 | ArgumentDependentLookup(OpName, &Input, 1, Functions); |
| 5425 | } |
| 5426 | |
| 5427 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, move(input)); |
| 5428 | } |
| 5429 | |
| 5430 | return CreateBuiltinUnaryOp(OpLoc, Opc, move(input)); |
| 5431 | } |
| 5432 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 5433 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5434 | Sema::OwningExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
| 5435 | SourceLocation LabLoc, |
| 5436 | IdentifierInfo *LabelII) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5437 | // Look up the record for this label identifier. |
Chris Lattner | ea29a3a | 2009-04-18 20:01:55 +0000 | [diff] [blame] | 5438 | LabelStmt *&LabelDecl = getLabelMap()[LabelII]; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5439 | |
Daniel Dunbar | 0ffb125 | 2008-08-04 16:51:22 +0000 | [diff] [blame] | 5440 | // If we haven't seen this label yet, create a forward reference. It |
| 5441 | // will be validated and/or cleaned up in ActOnFinishFunctionBody. |
Steve Naroff | caaacec | 2009-03-13 15:38:40 +0000 | [diff] [blame] | 5442 | if (LabelDecl == 0) |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 5443 | LabelDecl = new (Context) LabelStmt(LabLoc, LabelII, 0); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5444 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5445 | // Create the AST node. The address of a label always has type 'void*'. |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5446 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 5447 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5450 | Sema::OwningExprResult |
| 5451 | Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtArg substmt, |
| 5452 | SourceLocation RPLoc) { // "({..})" |
| 5453 | Stmt *SubStmt = static_cast<Stmt*>(substmt.get()); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5454 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 5455 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 5456 | |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5457 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 5458 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5459 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 5460 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5461 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 5462 | // example, it is not possible to goto into a stmt expression apparently. |
| 5463 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5464 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5465 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 5466 | // as the type of the stmtexpr. |
| 5467 | QualType Ty = Context.VoidTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5468 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5469 | if (!Compound->body_empty()) { |
| 5470 | Stmt *LastStmt = Compound->body_back(); |
| 5471 | // If LastStmt is a label, skip down through into the body. |
| 5472 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) |
| 5473 | LastStmt = Label->getSubStmt(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5474 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5475 | if (Expr *LastExpr = dyn_cast<Expr>(LastStmt)) |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5476 | Ty = LastExpr->getType(); |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 5477 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5478 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5479 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 5480 | // expressions are not lvalues. |
| 5481 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5482 | substmt.release(); |
| 5483 | return Owned(new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc)); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 5484 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5485 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5486 | Sema::OwningExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| 5487 | SourceLocation BuiltinLoc, |
| 5488 | SourceLocation TypeLoc, |
| 5489 | TypeTy *argty, |
| 5490 | OffsetOfComponent *CompPtr, |
| 5491 | unsigned NumComponents, |
| 5492 | SourceLocation RPLoc) { |
| 5493 | // FIXME: This function leaks all expressions in the offset components on |
| 5494 | // error. |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5495 | // FIXME: Preserve type source info. |
| 5496 | QualType ArgTy = GetTypeFromParser(argty); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5497 | assert(!ArgTy.isNull() && "Missing type argument!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5498 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5499 | bool Dependent = ArgTy->isDependentType(); |
| 5500 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5501 | // We must have at least one component that refers to the type, and the first |
| 5502 | // one is known to be a field designator. Verify that the ArgTy represents |
| 5503 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5504 | if (!Dependent && !ArgTy->isRecordType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5505 | return ExprError(Diag(TypeLoc, diag::err_offsetof_record_type) << ArgTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5506 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5507 | // FIXME: Type must be complete per C99 7.17p3 because a declaring a variable |
| 5508 | // with an incomplete type would be illegal. |
Douglas Gregor | 4fdf1fa | 2009-03-11 16:48:53 +0000 | [diff] [blame] | 5509 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5510 | // Otherwise, create a null pointer as the base, and iteratively process |
| 5511 | // the offsetof designators. |
| 5512 | QualType ArgTyPtr = Context.getPointerType(ArgTy); |
| 5513 | Expr* Res = new (Context) ImplicitValueInitExpr(ArgTyPtr); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5514 | Res = new (Context) UnaryOperator(Res, UnaryOperator::Deref, |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5515 | ArgTy, SourceLocation()); |
Eli Friedman | 1d24259 | 2009-01-26 01:33:06 +0000 | [diff] [blame] | 5516 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5517 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 5518 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5519 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 5520 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 5521 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 5522 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 5523 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5524 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5525 | if (!Dependent) { |
Eli Friedman | c0d600c | 2009-05-03 21:22:18 +0000 | [diff] [blame] | 5526 | bool DidWarnAboutNonPOD = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5527 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5528 | // FIXME: Dependent case loses a lot of information here. And probably |
| 5529 | // leaks like a sieve. |
| 5530 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 5531 | const OffsetOfComponent &OC = CompPtr[i]; |
| 5532 | if (OC.isBrackets) { |
| 5533 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 5534 | const ArrayType *AT = Context.getAsArrayType(Res->getType()); |
| 5535 | if (!AT) { |
| 5536 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5537 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 5538 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 5542 | |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 5543 | // Promote the array so it looks more like a normal array subscript |
| 5544 | // expression. |
| 5545 | DefaultFunctionArrayConversion(Res); |
| 5546 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5547 | // C99 6.5.2.1p1 |
| 5548 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5549 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5550 | if (!Idx->isTypeDependent() && !Idx->getType()->isIntegerType()) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5551 | return ExprError(Diag(Idx->getLocStart(), |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 5552 | diag::err_typecheck_subscript_not_integer) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5553 | << Idx->getSourceRange()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5554 | |
| 5555 | Res = new (Context) ArraySubscriptExpr(Res, Idx, AT->getElementType(), |
| 5556 | OC.LocEnd); |
| 5557 | continue; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5558 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5559 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5560 | const RecordType *RC = Res->getType()->getAs<RecordType>(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5561 | if (!RC) { |
| 5562 | Res->Destroy(Context); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5563 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 5564 | << Res->getType()); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5565 | } |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 5566 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5567 | // Get the decl corresponding to this. |
| 5568 | RecordDecl *RD = RC->getDecl(); |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5569 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5570 | if (!CRD->isPOD() && !DidWarnAboutNonPOD) { |
Anders Carlsson | f9b8bc6 | 2009-05-02 17:45:47 +0000 | [diff] [blame] | 5571 | ExprError(Diag(BuiltinLoc, diag::warn_offsetof_non_pod_type) |
| 5572 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 5573 | << Res->getType()); |
Anders Carlsson | 5992e4a | 2009-05-02 18:36:10 +0000 | [diff] [blame] | 5574 | DidWarnAboutNonPOD = true; |
| 5575 | } |
Anders Carlsson | 6d7f149 | 2009-05-01 23:20:30 +0000 | [diff] [blame] | 5576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5577 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5578 | FieldDecl *MemberDecl |
| 5579 | = dyn_cast_or_null<FieldDecl>(LookupQualifiedName(RD, OC.U.IdentInfo, |
| 5580 | LookupMemberName) |
| 5581 | .getAsDecl()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5582 | // FIXME: Leaks Res |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5583 | if (!MemberDecl) |
Anders Carlsson | f4d84b6 | 2009-08-30 00:54:35 +0000 | [diff] [blame] | 5584 | return ExprError(Diag(BuiltinLoc, diag::err_typecheck_no_member_deprecated) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5585 | << OC.U.IdentInfo << SourceRange(OC.LocStart, OC.LocEnd)); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5586 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5587 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 5588 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5589 | if (cast<RecordDecl>(MemberDecl->getDeclContext())->isAnonymousStructOrUnion()) { |
Anders Carlsson | f1b1d59 | 2009-05-01 19:30:39 +0000 | [diff] [blame] | 5590 | Res = BuildAnonymousStructUnionMemberReference( |
| 5591 | SourceLocation(), MemberDecl, Res, SourceLocation()).takeAs<Expr>(); |
Eli Friedman | e935696 | 2009-04-26 20:50:44 +0000 | [diff] [blame] | 5592 | } else { |
| 5593 | // MemberDecl->getType() doesn't get the right qualifiers, but it |
| 5594 | // doesn't matter here. |
| 5595 | Res = new (Context) MemberExpr(Res, false, MemberDecl, OC.LocEnd, |
| 5596 | MemberDecl->getType().getNonReferenceType()); |
| 5597 | } |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5598 | } |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5599 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5600 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5601 | return Owned(new (Context) UnaryOperator(Res, UnaryOperator::OffsetOf, |
| 5602 | Context.getSizeType(), BuiltinLoc)); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
| 5605 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5606 | Sema::OwningExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
| 5607 | TypeTy *arg1,TypeTy *arg2, |
| 5608 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5609 | // FIXME: Preserve type source info. |
| 5610 | QualType argT1 = GetTypeFromParser(arg1); |
| 5611 | QualType argT2 = GetTypeFromParser(arg2); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5612 | |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5613 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5614 | |
Douglas Gregor | c12a9c5 | 2009-05-19 22:28:02 +0000 | [diff] [blame] | 5615 | if (getLangOptions().CPlusPlus) { |
| 5616 | Diag(BuiltinLoc, diag::err_types_compatible_p_in_cplusplus) |
| 5617 | << SourceRange(BuiltinLoc, RPLoc); |
| 5618 | return ExprError(); |
| 5619 | } |
| 5620 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5621 | return Owned(new (Context) TypesCompatibleExpr(Context.IntTy, BuiltinLoc, |
| 5622 | argT1, argT2, RPLoc)); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 5623 | } |
| 5624 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5625 | Sema::OwningExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| 5626 | ExprArg cond, |
| 5627 | ExprArg expr1, ExprArg expr2, |
| 5628 | SourceLocation RPLoc) { |
| 5629 | Expr *CondExpr = static_cast<Expr*>(cond.get()); |
| 5630 | Expr *LHSExpr = static_cast<Expr*>(expr1.get()); |
| 5631 | Expr *RHSExpr = static_cast<Expr*>(expr2.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5632 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5633 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 5634 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5635 | QualType resType; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 5636 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5637 | resType = Context.DependentTy; |
| 5638 | } else { |
| 5639 | // The conditional expression is required to be a constant expression. |
| 5640 | llvm::APSInt condEval(32); |
| 5641 | SourceLocation ExpLoc; |
| 5642 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5643 | return ExprError(Diag(ExpLoc, |
| 5644 | diag::err_typecheck_choose_expr_requires_constant) |
| 5645 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5646 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5647 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 5648 | resType = condEval.getZExtValue() ? LHSExpr->getType() : RHSExpr->getType(); |
| 5649 | } |
| 5650 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5651 | cond.release(); expr1.release(); expr2.release(); |
| 5652 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| 5653 | resType, RPLoc)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 5654 | } |
| 5655 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5656 | //===----------------------------------------------------------------------===// |
| 5657 | // Clang Extensions. |
| 5658 | //===----------------------------------------------------------------------===// |
| 5659 | |
| 5660 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5661 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5662 | // Analyze block parameters. |
| 5663 | BlockSemaInfo *BSI = new BlockSemaInfo(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5664 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5665 | // Add BSI to CurBlock. |
| 5666 | BSI->PrevBlockInfo = CurBlock; |
| 5667 | CurBlock = BSI; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5668 | |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5669 | BSI->ReturnType = QualType(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5670 | BSI->TheScope = BlockScope; |
Mike Stump | b83d287 | 2009-02-19 22:01:56 +0000 | [diff] [blame] | 5671 | BSI->hasBlockDeclRefExprs = false; |
Daniel Dunbar | 1d2154c | 2009-07-29 01:59:17 +0000 | [diff] [blame] | 5672 | BSI->hasPrototype = false; |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5673 | BSI->SavedFunctionNeedsScopeChecking = CurFunctionNeedsScopeChecking; |
| 5674 | CurFunctionNeedsScopeChecking = false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5675 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5676 | BSI->TheDecl = BlockDecl::Create(Context, CurContext, CaretLoc); |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 5677 | PushDeclContext(BlockScope, BSI->TheDecl); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5678 | } |
| 5679 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5680 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 5681 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5682 | |
| 5683 | if (ParamInfo.getNumTypeObjects() == 0 |
| 5684 | || ParamInfo.getTypeObject(0).Kind != DeclaratorChunk::Function) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5685 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5686 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5687 | |
Mike Stump | 4eeab84 | 2009-04-28 01:10:27 +0000 | [diff] [blame] | 5688 | if (T->isArrayType()) { |
| 5689 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5690 | diag::err_block_returns_array); |
| 5691 | return; |
| 5692 | } |
| 5693 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5694 | // The parameter list is optional, if there was none, assume (). |
| 5695 | if (!T->isFunctionType()) |
| 5696 | T = Context.getFunctionType(T, NULL, 0, 0, 0); |
| 5697 | |
| 5698 | CurBlock->hasPrototype = true; |
| 5699 | CurBlock->isVariadic = false; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5700 | // Check for a valid sentinel attribute on this block. |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5701 | if (CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5702 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5703 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5704 | // FIXME: remove the attribute. |
| 5705 | } |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5706 | QualType RetTy = T.getTypePtr()->getAsFunctionType()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5707 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5708 | // Do not allow returning a objc interface by-value. |
| 5709 | if (RetTy->isObjCInterfaceType()) { |
| 5710 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5711 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5712 | return; |
| 5713 | } |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 5714 | return; |
| 5715 | } |
| 5716 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5717 | // Analyze arguments to block. |
| 5718 | assert(ParamInfo.getTypeObject(0).Kind == DeclaratorChunk::Function && |
| 5719 | "Not a function declarator!"); |
| 5720 | DeclaratorChunk::FunctionTypeInfo &FTI = ParamInfo.getTypeObject(0).Fun; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5721 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5722 | CurBlock->hasPrototype = FTI.hasPrototype; |
| 5723 | CurBlock->isVariadic = true; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5724 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5725 | // Check for C99 6.7.5.3p10 - foo(void) is a non-varargs function that takes |
| 5726 | // no arguments, not a function that takes a single void argument. |
| 5727 | if (FTI.hasPrototype && |
| 5728 | FTI.NumArgs == 1 && !FTI.isVariadic && FTI.ArgInfo[0].Ident == 0 && |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5729 | (!FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType().getCVRQualifiers()&& |
| 5730 | FTI.ArgInfo[0].Param.getAs<ParmVarDecl>()->getType()->isVoidType())) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5731 | // empty arg list, don't push any params. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5732 | CurBlock->isVariadic = false; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5733 | } else if (FTI.hasPrototype) { |
| 5734 | for (unsigned i = 0, e = FTI.NumArgs; i != e; ++i) |
Chris Lattner | b28317a | 2009-03-28 19:18:32 +0000 | [diff] [blame] | 5735 | CurBlock->Params.push_back(FTI.ArgInfo[i].Param.getAs<ParmVarDecl>()); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5736 | CurBlock->isVariadic = FTI.isVariadic; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5737 | } |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5738 | CurBlock->TheDecl->setParams(Context, CurBlock->Params.data(), |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5739 | CurBlock->Params.size()); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 5740 | CurBlock->TheDecl->setIsVariadic(CurBlock->isVariadic); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5741 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5742 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| 5743 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) |
| 5744 | // If this has an identifier, add it to the scope stack. |
| 5745 | if ((*AI)->getIdentifier()) |
| 5746 | PushOnScopeChains(*AI, CurBlock->TheScope); |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5747 | |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5748 | // Check for a valid sentinel attribute on this block. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5749 | if (!CurBlock->isVariadic && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5750 | CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5751 | Diag(ParamInfo.getAttributes()->getLoc(), |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 5752 | diag::warn_attribute_sentinel_not_variadic) << 1; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 5753 | // FIXME: remove the attribute. |
| 5754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5755 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5756 | // Analyze the return type. |
| 5757 | QualType T = GetTypeForDeclarator(ParamInfo, CurScope); |
| 5758 | QualType RetTy = T->getAsFunctionType()->getResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5759 | |
Chris Lattner | 9097af1 | 2009-04-11 19:27:54 +0000 | [diff] [blame] | 5760 | // Do not allow returning a objc interface by-value. |
| 5761 | if (RetTy->isObjCInterfaceType()) { |
| 5762 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 5763 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 5764 | } else if (!RetTy->isDependentType()) |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5765 | CurBlock->ReturnType = RetTy; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 5769 | /// is invoked to pop the information about the block from the action impl. |
| 5770 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| 5771 | // Ensure that CurBlock is deleted. |
| 5772 | llvm::OwningPtr<BlockSemaInfo> CC(CurBlock); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5773 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5774 | CurFunctionNeedsScopeChecking = CurBlock->SavedFunctionNeedsScopeChecking; |
| 5775 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5776 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 5777 | PopDeclContext(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5778 | CurBlock = CurBlock->PrevBlockInfo; |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5779 | // FIXME: Delete the ParmVarDecl objects as well??? |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5780 | } |
| 5781 | |
| 5782 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 5783 | /// literal was successfully completed. ^(int x){...} |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5784 | Sema::OwningExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| 5785 | StmtArg body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 5786 | // If blocks are disabled, emit an error. |
| 5787 | if (!LangOpts.Blocks) |
| 5788 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5789 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5790 | // Ensure that CurBlock is deleted. |
| 5791 | llvm::OwningPtr<BlockSemaInfo> BSI(CurBlock); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5792 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 5793 | PopDeclContext(); |
| 5794 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5795 | // Pop off CurBlock, handle nested blocks. |
| 5796 | CurBlock = CurBlock->PrevBlockInfo; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5797 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5798 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 5799 | if (!BSI->ReturnType.isNull()) |
| 5800 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5801 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5802 | llvm::SmallVector<QualType, 8> ArgTypes; |
| 5803 | for (unsigned i = 0, e = BSI->Params.size(); i != e; ++i) |
| 5804 | ArgTypes.push_back(BSI->Params[i]->getType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5805 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5806 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5807 | QualType BlockTy; |
| 5808 | if (!BSI->hasPrototype) |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5809 | BlockTy = Context.getFunctionType(RetTy, 0, 0, false, 0, false, false, 0, 0, |
| 5810 | NoReturn); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5811 | else |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 5812 | BlockTy = Context.getFunctionType(RetTy, ArgTypes.data(), ArgTypes.size(), |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5813 | BSI->isVariadic, 0, false, false, 0, 0, |
| 5814 | NoReturn); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5815 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5816 | // FIXME: Check that return/parameter types are complete/non-abstract |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5817 | DiagnoseUnusedParameters(BSI->Params.begin(), BSI->Params.end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5818 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5819 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 5820 | // If needed, diagnose invalid gotos and switches in the block. |
| 5821 | if (CurFunctionNeedsScopeChecking) |
| 5822 | DiagnoseInvalidJumps(static_cast<CompoundStmt*>(body.get())); |
| 5823 | CurFunctionNeedsScopeChecking = BSI->SavedFunctionNeedsScopeChecking; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5824 | |
Anders Carlsson | e9146f2 | 2009-05-01 19:49:17 +0000 | [diff] [blame] | 5825 | BSI->TheDecl->setBody(body.takeAs<CompoundStmt>()); |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 5826 | CheckFallThroughForBlock(BlockTy, BSI->TheDecl->getBody()); |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5827 | return Owned(new (Context) BlockExpr(BSI->TheDecl, BlockTy, |
| 5828 | BSI->hasBlockDeclRefExprs)); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5831 | Sema::OwningExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 5832 | ExprArg expr, TypeTy *type, |
| 5833 | SourceLocation RPLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 5834 | QualType T = GetTypeFromParser(type); |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5835 | Expr *E = static_cast<Expr*>(expr.get()); |
| 5836 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5837 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5838 | InitBuiltinVaListType(); |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5839 | |
| 5840 | // Get the va_list type |
| 5841 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5842 | if (VaListType->isArrayType()) { |
| 5843 | // Deal with implicit array decay; for example, on x86-64, |
| 5844 | // va_list is an array, but it's supposed to decay to |
| 5845 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5846 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5847 | // Make sure the input expression also decays appropriately. |
| 5848 | UsualUnaryConversions(E); |
| 5849 | } else { |
| 5850 | // Otherwise, the va_list argument must be an l-value because |
| 5851 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5852 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5853 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 5854 | return ExprError(); |
| 5855 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 5856 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 5857 | if (!E->isTypeDependent() && |
| 5858 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5859 | return ExprError(Diag(E->getLocStart(), |
| 5860 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 5861 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 5862 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5863 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5864 | // FIXME: Check that type is complete/non-abstract |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5865 | // FIXME: Warn if a non-POD type is passed in. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5866 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5867 | expr.release(); |
| 5868 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, T.getNonReferenceType(), |
| 5869 | RPLoc)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 5870 | } |
| 5871 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5872 | Sema::OwningExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5873 | // The type of __null will be int or long, depending on the size of |
| 5874 | // pointers on the target. |
| 5875 | QualType Ty; |
| 5876 | if (Context.Target.getPointerWidth(0) == Context.Target.getIntWidth()) |
| 5877 | Ty = Context.IntTy; |
| 5878 | else |
| 5879 | Ty = Context.LongTy; |
| 5880 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 5881 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 5882 | } |
| 5883 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5884 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 5885 | SourceLocation Loc, |
| 5886 | QualType DstType, QualType SrcType, |
| 5887 | Expr *SrcExpr, const char *Flavor) { |
| 5888 | // Decode the result (notice that AST's are still created for extensions). |
| 5889 | bool isInvalid = false; |
| 5890 | unsigned DiagKind; |
| 5891 | switch (ConvTy) { |
| 5892 | default: assert(0 && "Unknown conversion type"); |
| 5893 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5894 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5895 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 5896 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5897 | case IntToPointer: |
| 5898 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 5899 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5900 | case IncompatiblePointer: |
| 5901 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 5902 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5903 | case IncompatiblePointerSign: |
| 5904 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 5905 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5906 | case FunctionVoidPointer: |
| 5907 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 5908 | break; |
| 5909 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 5910 | // If the qualifiers lost were because we were applying the |
| 5911 | // (deprecated) C++ conversion from a string literal to a char* |
| 5912 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 5913 | // Ideally, this check would be performed in |
| 5914 | // CheckPointerTypesForAssignment. However, that would require a |
| 5915 | // bit of refactoring (so that the second argument is an |
| 5916 | // expression, rather than a type), which should be done as part |
| 5917 | // of a larger effort to fix CheckPointerTypesForAssignment for |
| 5918 | // C++ semantics. |
| 5919 | if (getLangOptions().CPlusPlus && |
| 5920 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 5921 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5922 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 5923 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5924 | case IntToBlockPointer: |
| 5925 | DiagKind = diag::err_int_to_block_pointer; |
| 5926 | break; |
| 5927 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 5928 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5929 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5930 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5931 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 5932 | // it can give a more specific diagnostic. |
| 5933 | DiagKind = diag::warn_incompatible_qualified_id; |
| 5934 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5935 | case IncompatibleVectors: |
| 5936 | DiagKind = diag::warn_incompatible_vectors; |
| 5937 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5938 | case Incompatible: |
| 5939 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 5940 | isInvalid = true; |
| 5941 | break; |
| 5942 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5943 | |
Chris Lattner | d9d22dd | 2008-11-24 05:29:24 +0000 | [diff] [blame] | 5944 | Diag(Loc, DiagKind) << DstType << SrcType << Flavor |
| 5945 | << SrcExpr->getSourceRange(); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5946 | return isInvalid; |
| 5947 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5948 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 5949 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5950 | llvm::APSInt ICEResult; |
| 5951 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 5952 | if (Result) |
| 5953 | *Result = ICEResult; |
| 5954 | return false; |
| 5955 | } |
| 5956 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5957 | Expr::EvalResult EvalResult; |
| 5958 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5959 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5960 | EvalResult.HasSideEffects) { |
| 5961 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 5962 | |
| 5963 | if (EvalResult.Diag) { |
| 5964 | // We only show the note if it's not the usual "invalid subexpression" |
| 5965 | // or if it's actually in a subexpression. |
| 5966 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 5967 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 5968 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 5969 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5970 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5971 | return true; |
| 5972 | } |
| 5973 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5974 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 5975 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5976 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 5977 | if (EvalResult.Diag && |
| 5978 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice) != Diagnostic::Ignored) |
| 5979 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5980 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 5981 | if (Result) |
| 5982 | *Result = EvalResult.Val.getInt(); |
| 5983 | return false; |
| 5984 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5985 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5986 | Sema::ExpressionEvaluationContext |
| 5987 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5988 | // Introduce a new set of potentially referenced declarations to the stack. |
| 5989 | if (NewContext == PotentiallyPotentiallyEvaluated) |
| 5990 | PotentiallyReferencedDeclStack.push_back(PotentiallyReferencedDecls()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5991 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5992 | std::swap(ExprEvalContext, NewContext); |
| 5993 | return NewContext; |
| 5994 | } |
| 5995 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | void |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 5997 | Sema::PopExpressionEvaluationContext(ExpressionEvaluationContext OldContext, |
| 5998 | ExpressionEvaluationContext NewContext) { |
| 5999 | ExprEvalContext = NewContext; |
| 6000 | |
| 6001 | if (OldContext == PotentiallyPotentiallyEvaluated) { |
| 6002 | // Mark any remaining declarations in the current position of the stack |
| 6003 | // as "referenced". If they were not meant to be referenced, semantic |
| 6004 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| 6005 | PotentiallyReferencedDecls RemainingDecls; |
| 6006 | RemainingDecls.swap(PotentiallyReferencedDeclStack.back()); |
| 6007 | PotentiallyReferencedDeclStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6008 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6009 | for (PotentiallyReferencedDecls::iterator I = RemainingDecls.begin(), |
| 6010 | IEnd = RemainingDecls.end(); |
| 6011 | I != IEnd; ++I) |
| 6012 | MarkDeclarationReferenced(I->first, I->second); |
| 6013 | } |
| 6014 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6015 | |
| 6016 | /// \brief Note that the given declaration was referenced in the source code. |
| 6017 | /// |
| 6018 | /// This routine should be invoke whenever a given declaration is referenced |
| 6019 | /// in the source code, and where that reference occurred. If this declaration |
| 6020 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 6021 | /// C99 6.9p3), then the declaration will be marked as used. |
| 6022 | /// |
| 6023 | /// \param Loc the location where the declaration was referenced. |
| 6024 | /// |
| 6025 | /// \param D the declaration that has been referenced by the source code. |
| 6026 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 6027 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6028 | |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6029 | if (D->isUsed()) |
| 6030 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6031 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6032 | // Mark a parameter declaration "used", regardless of whether we're in a |
| 6033 | // template or not. |
| 6034 | if (isa<ParmVarDecl>(D)) |
| 6035 | D->setUsed(true); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6036 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6037 | // Do not mark anything as "used" within a dependent context; wait for |
| 6038 | // an instantiation. |
| 6039 | if (CurContext->isDependentContext()) |
| 6040 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6041 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6042 | switch (ExprEvalContext) { |
| 6043 | case Unevaluated: |
| 6044 | // We are in an expression that is not potentially evaluated; do nothing. |
| 6045 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6046 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6047 | case PotentiallyEvaluated: |
| 6048 | // We are in a potentially-evaluated expression, so this declaration is |
| 6049 | // "used"; handle this below. |
| 6050 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6051 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 6052 | case PotentiallyPotentiallyEvaluated: |
| 6053 | // We are in an expression that may be potentially evaluated; queue this |
| 6054 | // declaration reference until we know whether the expression is |
| 6055 | // potentially evaluated. |
| 6056 | PotentiallyReferencedDeclStack.back().push_back(std::make_pair(Loc, D)); |
| 6057 | return; |
| 6058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6059 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6060 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 6061 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6062 | unsigned TypeQuals; |
Fariborz Jahanian | 05a5c45 | 2009-06-22 20:37:23 +0000 | [diff] [blame] | 6063 | if (Constructor->isImplicit() && Constructor->isDefaultConstructor()) { |
| 6064 | if (!Constructor->isUsed()) |
| 6065 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6066 | } else if (Constructor->isImplicit() && |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6067 | Constructor->isCopyConstructor(Context, TypeQuals)) { |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 6068 | if (!Constructor->isUsed()) |
| 6069 | DefineImplicitCopyConstructor(Loc, Constructor, TypeQuals); |
| 6070 | } |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 6071 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| 6072 | if (Destructor->isImplicit() && !Destructor->isUsed()) |
| 6073 | DefineImplicitDestructor(Loc, Destructor); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6074 | |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 6075 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| 6076 | if (MethodDecl->isImplicit() && MethodDecl->isOverloadedOperator() && |
| 6077 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| 6078 | if (!MethodDecl->isUsed()) |
| 6079 | DefineImplicitOverloadedAssign(Loc, MethodDecl); |
| 6080 | } |
| 6081 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 6082 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6083 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6084 | // class templates. |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 6085 | if (!Function->getBody()) { |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 6086 | // FIXME: distinguish between implicit instantiations of function |
| 6087 | // templates and explicit specializations (the latter don't get |
| 6088 | // instantiated, naturally). |
| 6089 | if (Function->getInstantiatedFromMemberFunction() || |
| 6090 | Function->getPrimaryTemplate()) |
Douglas Gregor | b33fe2f | 2009-06-30 17:20:14 +0000 | [diff] [blame] | 6091 | PendingImplicitInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6093 | |
| 6094 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6095 | // FIXME: keep track of references to static functions |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6096 | Function->setUsed(true); |
| 6097 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 6098 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6099 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6100 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6101 | // Implicit instantiation of static data members of class templates. |
| 6102 | // FIXME: distinguish between implicit instantiations (which we need to |
| 6103 | // actually instantiate) and explicit specializations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6104 | if (Var->isStaticDataMember() && |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6105 | Var->getInstantiatedFromStaticDataMember()) |
| 6106 | PendingImplicitInstantiations.push_back(std::make_pair(Var, Loc)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6107 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6108 | // FIXME: keep track of references to static data? |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6109 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6110 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 6111 | return; |
| 6112 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 6113 | } |
| 6114 | |