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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 15 | #include "SemaUtil.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/Decl.h" |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Preprocessor.h" |
| 22 | #include "clang/Lex/LiteralSupport.h" |
| 23 | #include "clang/Basic/SourceManager.h" |
| 24 | #include "clang/Basic/Diagnostic.h" |
| 25 | #include "clang/Basic/LangOptions.h" |
| 26 | #include "clang/Basic/TargetInfo.h" |
| 27 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 31 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 33 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 34 | /// multiple tokens. However, the common case is that StringToks points to one |
| 35 | /// string. |
| 36 | /// |
| 37 | Action::ExprResult |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 38 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | assert(NumStringToks && "Must have at least one string!"); |
| 40 | |
| 41 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 42 | if (Literal.hadError) |
| 43 | return ExprResult(true); |
| 44 | |
| 45 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 46 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 47 | StringTokLocs.push_back(StringToks[i].getLocation()); |
| 48 | |
| 49 | // FIXME: handle wchar_t |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 50 | QualType t; |
| 51 | |
| 52 | if (Literal.Pascal) |
| 53 | t = Context.getPointerType(Context.UnsignedCharTy); |
| 54 | else |
| 55 | t = Context.getPointerType(Context.CharTy); |
| 56 | |
| 57 | if (Literal.Pascal && Literal.GetStringLength() > 256) |
| 58 | return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long, |
| 59 | SourceRange(StringToks[0].getLocation(), |
| 60 | StringToks[NumStringToks-1].getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | |
| 62 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| 63 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 64 | Literal.AnyWide, t, |
| 65 | StringToks[0].getLocation(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | StringToks[NumStringToks-1].getLocation()); |
| 67 | } |
| 68 | |
| 69 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 70 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
| 72 | /// identifier is used in an function call context. |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 73 | Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 74 | IdentifierInfo &II, |
| 75 | bool HasTrailingLParen) { |
| 76 | // Could be enum-constant or decl. |
Steve Naroff | 8c9f13e | 2007-09-16 16:16:00 +0000 | [diff] [blame] | 77 | ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | if (D == 0) { |
| 79 | // Otherwise, this could be an implicitly declared function reference (legal |
| 80 | // in C90, extension in C99). |
| 81 | if (HasTrailingLParen && |
| 82 | // Not in C++. |
| 83 | !getLangOptions().CPlusPlus) |
| 84 | D = ImplicitlyDefineFunction(Loc, II, S); |
| 85 | else { |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 86 | if (CurMethodDecl) { |
| 87 | ObjcInterfaceDecl *IFace = CurMethodDecl->getClassInterface(); |
| 88 | ObjcInterfaceDecl *clsDeclared; |
Steve Naroff | 7e3411b | 2007-11-15 02:58:25 +0000 | [diff] [blame] | 89 | if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) { |
| 90 | IdentifierInfo &II = Context.Idents.get("self"); |
| 91 | ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
| 92 | return new ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 93 | static_cast<Expr*>(SelfExpr.Val), true, true); |
| 94 | } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 95 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | // If this name wasn't predeclared and if this is not a function call, |
| 97 | // diagnose the problem. |
| 98 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
| 99 | } |
| 100 | } |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 101 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Steve Naroff | 53a3234 | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 102 | // Only create DeclRefExpr's for valid Decl's. |
Steve Naroff | 5912a35 | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 103 | if (VD->isInvalidDecl()) |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 104 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | return new DeclRefExpr(VD, VD->getType(), Loc); |
Steve Naroff | e1223f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 106 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | if (isa<TypedefDecl>(D)) |
| 108 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
Fariborz Jahanian | 5ef404f | 2007-12-05 18:16:33 +0000 | [diff] [blame] | 109 | if (isa<ObjcInterfaceDecl>(D)) |
| 110 | return Diag(Loc, diag::err_unexpected_interface, II.getName()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | |
| 112 | assert(0 && "Invalid decl"); |
Chris Lattner | eddbe03 | 2007-07-21 04:57:45 +0000 | [diff] [blame] | 113 | abort(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 116 | Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc, |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 117 | tok::TokenKind Kind) { |
| 118 | PreDefinedExpr::IdentType IT; |
| 119 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | switch (Kind) { |
| 121 | default: |
| 122 | assert(0 && "Unknown simple primary expr!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 124 | IT = PreDefinedExpr::Func; |
| 125 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 127 | IT = PreDefinedExpr::Function; |
| 128 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 130 | IT = PreDefinedExpr::PrettyFunction; |
| 131 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | } |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 133 | |
| 134 | // Pre-defined identifiers are always of type char *. |
| 135 | return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 138 | Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 139 | llvm::SmallString<16> CharBuffer; |
| 140 | CharBuffer.resize(Tok.getLength()); |
| 141 | const char *ThisTokBegin = &CharBuffer[0]; |
| 142 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 143 | |
| 144 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 145 | Tok.getLocation(), PP); |
| 146 | if (Literal.hadError()) |
| 147 | return ExprResult(true); |
| 148 | return new CharacterLiteral(Literal.getValue(), Context.IntTy, |
| 149 | Tok.getLocation()); |
| 150 | } |
| 151 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 152 | Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 153 | // fast path for a single digit (which is quite common). A single digit |
| 154 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 155 | if (Tok.getLength() == 1) { |
| 156 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
| 157 | |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 158 | unsigned IntSize = static_cast<unsigned>( |
| 159 | Context.getTypeSize(Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'), |
| 161 | Context.IntTy, |
| 162 | Tok.getLocation())); |
| 163 | } |
| 164 | llvm::SmallString<512> IntegerBuffer; |
| 165 | IntegerBuffer.resize(Tok.getLength()); |
| 166 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 167 | |
| 168 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 169 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 170 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 171 | Tok.getLocation(), PP); |
| 172 | if (Literal.hadError) |
| 173 | return ExprResult(true); |
| 174 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 175 | Expr *Res; |
| 176 | |
| 177 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 178 | QualType Ty; |
| 179 | const llvm::fltSemantics *Format; |
| 180 | uint64_t Size; unsigned Align; |
| 181 | |
| 182 | if (Literal.isFloat) { |
| 183 | Ty = Context.FloatTy; |
| 184 | Context.Target.getFloatInfo(Size, Align, Format, Tok.getLocation()); |
| 185 | } else if (Literal.isLong) { |
| 186 | Ty = Context.LongDoubleTy; |
| 187 | Context.Target.getLongDoubleInfo(Size, Align, Format, Tok.getLocation()); |
| 188 | } else { |
| 189 | Ty = Context.DoubleTy; |
| 190 | Context.Target.getDoubleInfo(Size, Align, Format, Tok.getLocation()); |
| 191 | } |
| 192 | |
Ted Kremenek | 720c4ec | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 193 | // isExact will be set by GetFloatValue(). |
| 194 | bool isExact = false; |
| 195 | |
| 196 | Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact, |
| 197 | Ty, Tok.getLocation()); |
| 198 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 199 | } else if (!Literal.isIntegerLiteral()) { |
| 200 | return ExprResult(true); |
| 201 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | QualType t; |
| 203 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 204 | // long long is a C99 feature. |
| 205 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 206 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 207 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 208 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | // Get the value in the widest-possible width. |
| 210 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0); |
| 211 | |
| 212 | if (Literal.GetIntegerValue(ResultVal)) { |
| 213 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 214 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 215 | t = Context.UnsignedLongLongTy; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 216 | assert(Context.getTypeSize(t, Tok.getLocation()) == |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | ResultVal.getBitWidth() && "long long is not intmax_t?"); |
| 218 | } else { |
| 219 | // If this value fits into a ULL, try to figure out what else it fits into |
| 220 | // according to the rules of C99 6.4.4.1p5. |
| 221 | |
| 222 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 223 | // be an unsigned int. |
| 224 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 225 | |
| 226 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 227 | if (!Literal.isLong && !Literal.isLongLong) { |
| 228 | // Are int/unsigned possibilities? |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 229 | unsigned IntSize = static_cast<unsigned>( |
| 230 | Context.getTypeSize(Context.IntTy,Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 231 | // Does it fit in a unsigned int? |
| 232 | if (ResultVal.isIntN(IntSize)) { |
| 233 | // Does it fit in a signed int? |
| 234 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| 235 | t = Context.IntTy; |
| 236 | else if (AllowUnsigned) |
| 237 | t = Context.UnsignedIntTy; |
| 238 | } |
| 239 | |
| 240 | if (!t.isNull()) |
| 241 | ResultVal.trunc(IntSize); |
| 242 | } |
| 243 | |
| 244 | // Are long/unsigned long possibilities? |
| 245 | if (t.isNull() && !Literal.isLongLong) { |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 246 | unsigned LongSize = static_cast<unsigned>( |
| 247 | Context.getTypeSize(Context.LongTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | |
| 249 | // Does it fit in a unsigned long? |
| 250 | if (ResultVal.isIntN(LongSize)) { |
| 251 | // Does it fit in a signed long? |
| 252 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| 253 | t = Context.LongTy; |
| 254 | else if (AllowUnsigned) |
| 255 | t = Context.UnsignedLongTy; |
| 256 | } |
| 257 | if (!t.isNull()) |
| 258 | ResultVal.trunc(LongSize); |
| 259 | } |
| 260 | |
| 261 | // Finally, check long long if needed. |
| 262 | if (t.isNull()) { |
Chris Lattner | 701e5eb | 2007-09-04 02:45:27 +0000 | [diff] [blame] | 263 | unsigned LongLongSize = static_cast<unsigned>( |
| 264 | Context.getTypeSize(Context.LongLongTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | |
| 266 | // Does it fit in a unsigned long long? |
| 267 | if (ResultVal.isIntN(LongLongSize)) { |
| 268 | // Does it fit in a signed long long? |
| 269 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
| 270 | t = Context.LongLongTy; |
| 271 | else if (AllowUnsigned) |
| 272 | t = Context.UnsignedLongLongTy; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // If we still couldn't decide a type, we probably have something that |
| 277 | // does not fit in a signed long long, but has no U suffix. |
| 278 | if (t.isNull()) { |
| 279 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| 280 | t = Context.UnsignedLongLongTy; |
| 281 | } |
| 282 | } |
| 283 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 284 | Res = new IntegerLiteral(ResultVal, t, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 286 | |
| 287 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 288 | if (Literal.isImaginary) |
| 289 | Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType())); |
| 290 | |
| 291 | return Res; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 294 | Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | ExprTy *Val) { |
| 296 | Expr *e = (Expr *)Val; |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 297 | assert((e != 0) && "ActOnParenExpr() missing expr"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | return new ParenExpr(L, R, e); |
| 299 | } |
| 300 | |
| 301 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 302 | /// See C99 6.3.2.1p[2-4] for more details. |
| 303 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 304 | SourceLocation OpLoc, bool isSizeof) { |
| 305 | // C99 6.5.3.4p1: |
| 306 | if (isa<FunctionType>(exprType) && isSizeof) |
| 307 | // alignof(function) is allowed. |
| 308 | Diag(OpLoc, diag::ext_sizeof_function_type); |
| 309 | else if (exprType->isVoidType()) |
| 310 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 311 | else if (exprType->isIncompleteType()) { |
| 312 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 313 | diag::err_alignof_incomplete_type, |
| 314 | exprType.getAsString()); |
| 315 | return QualType(); // error |
| 316 | } |
| 317 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 318 | return Context.getSizeType(); |
| 319 | } |
| 320 | |
| 321 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 322 | ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 323 | SourceLocation LPLoc, TypeTy *Ty, |
| 324 | SourceLocation RPLoc) { |
| 325 | // If error parsing type, ignore. |
| 326 | if (Ty == 0) return true; |
| 327 | |
| 328 | // Verify that this is a valid expression. |
| 329 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
| 330 | |
| 331 | QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof); |
| 332 | |
| 333 | if (resultType.isNull()) |
| 334 | return true; |
| 335 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
| 336 | } |
| 337 | |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 338 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) { |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 339 | DefaultFunctionArrayConversion(V); |
| 340 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 341 | // These operators return the element type of a complex type. |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 342 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 343 | return CT->getElementType(); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 344 | |
| 345 | // Otherwise they pass through real integer and floating point types here. |
| 346 | if (V->getType()->isArithmeticType()) |
| 347 | return V->getType(); |
| 348 | |
| 349 | // Reject anything else. |
| 350 | Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString()); |
| 351 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 355 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 356 | Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | tok::TokenKind Kind, |
| 358 | ExprTy *Input) { |
| 359 | UnaryOperator::Opcode Opc; |
| 360 | switch (Kind) { |
| 361 | default: assert(0 && "Unknown unary op!"); |
| 362 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 363 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 364 | } |
| 365 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
| 366 | if (result.isNull()) |
| 367 | return true; |
| 368 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
| 369 | } |
| 370 | |
| 371 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 372 | ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 373 | ExprTy *Idx, SourceLocation RLoc) { |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 374 | Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 375 | |
| 376 | // Perform default conversions. |
| 377 | DefaultFunctionArrayConversion(LHSExp); |
| 378 | DefaultFunctionArrayConversion(RHSExp); |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 380 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 382 | // 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] | 383 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 384 | // in the subscript position. As a result, we need to derive the array base |
| 385 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 386 | Expr *BaseExpr, *IndexExpr; |
| 387 | QualType ResultType; |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 388 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 389 | BaseExpr = LHSExp; |
| 390 | IndexExpr = RHSExp; |
| 391 | // FIXME: need to deal with const... |
| 392 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 393 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 394 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 395 | BaseExpr = RHSExp; |
| 396 | IndexExpr = LHSExp; |
| 397 | // FIXME: need to deal with const... |
| 398 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 399 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 400 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 401 | IndexExpr = RHSExp; |
Steve Naroff | 608e0ee | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 402 | |
| 403 | // Component access limited to variables (reject vec4.rg[1]). |
| 404 | if (!isa<DeclRefExpr>(BaseExpr)) |
| 405 | return Diag(LLoc, diag::err_ocuvector_component_access, |
| 406 | SourceRange(LLoc, RLoc)); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 407 | // FIXME: need to deal with const... |
| 408 | ResultType = VTy->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 409 | } else { |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 410 | return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value, |
| 411 | RHSExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 412 | } |
| 413 | // C99 6.5.2.1p1 |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 414 | if (!IndexExpr->getType()->isIntegerType()) |
| 415 | return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 416 | IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 418 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 419 | // the following check catches trying to index a pointer to a function (e.g. |
| 420 | // void (*)(int)). Functions are not objects in C99. |
| 421 | if (!ResultType->isObjectType()) |
| 422 | return Diag(BaseExpr->getLocStart(), |
| 423 | diag::err_typecheck_subscript_not_object, |
| 424 | BaseExpr->getType().getAsString(), BaseExpr->getSourceRange()); |
| 425 | |
| 426 | return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 429 | QualType Sema:: |
| 430 | CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc, |
| 431 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 432 | const OCUVectorType *vecType = baseType->getAsOCUVectorType(); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 433 | |
| 434 | // The vector accessor can't exceed the number of elements. |
| 435 | const char *compStr = CompName.getName(); |
| 436 | if (strlen(compStr) > vecType->getNumElements()) { |
| 437 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 438 | baseType.getAsString(), SourceRange(CompLoc)); |
| 439 | return QualType(); |
| 440 | } |
| 441 | // The component names must come from the same set. |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 442 | if (vecType->getPointAccessorIdx(*compStr) != -1) { |
| 443 | do |
| 444 | compStr++; |
| 445 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
| 446 | } else if (vecType->getColorAccessorIdx(*compStr) != -1) { |
| 447 | do |
| 448 | compStr++; |
| 449 | while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1); |
| 450 | } else if (vecType->getTextureAccessorIdx(*compStr) != -1) { |
| 451 | do |
| 452 | compStr++; |
| 453 | while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1); |
| 454 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 455 | |
| 456 | if (*compStr) { |
| 457 | // We didn't get to the end of the string. This means the component names |
| 458 | // didn't come from the same set *or* we encountered an illegal name. |
| 459 | Diag(OpLoc, diag::err_ocuvector_component_name_illegal, |
| 460 | std::string(compStr,compStr+1), SourceRange(CompLoc)); |
| 461 | return QualType(); |
| 462 | } |
| 463 | // Each component accessor can't exceed the vector type. |
| 464 | compStr = CompName.getName(); |
| 465 | while (*compStr) { |
| 466 | if (vecType->isAccessorWithinNumElements(*compStr)) |
| 467 | compStr++; |
| 468 | else |
| 469 | break; |
| 470 | } |
| 471 | if (*compStr) { |
| 472 | // We didn't get to the end of the string. This means a component accessor |
| 473 | // exceeds the number of elements in the vector. |
| 474 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 475 | baseType.getAsString(), SourceRange(CompLoc)); |
| 476 | return QualType(); |
| 477 | } |
| 478 | // The component accessor looks fine - now we need to compute the actual type. |
| 479 | // The vector type is implied by the component accessor. For example, |
| 480 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| 481 | unsigned CompSize = strlen(CompName.getName()); |
| 482 | if (CompSize == 1) |
| 483 | return vecType->getElementType(); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 484 | |
| 485 | QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize); |
| 486 | // Now look up the TypeDefDecl from the vector type. Without this, |
| 487 | // diagostics look bad. We want OCU vector types to appear built-in. |
| 488 | for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) { |
| 489 | if (OCUVectorDecls[i]->getUnderlyingType() == VT) |
| 490 | return Context.getTypedefType(OCUVectorDecls[i]); |
| 491 | } |
| 492 | 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] | 493 | } |
| 494 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 496 | ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 497 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 498 | IdentifierInfo &Member) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 499 | Expr *BaseExpr = static_cast<Expr *>(Base); |
| 500 | assert(BaseExpr && "no record expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 502 | QualType BaseType = BaseExpr->getType(); |
| 503 | assert(!BaseType.isNull() && "no type for member expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 505 | if (OpKind == tok::arrow) { |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 506 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 507 | BaseType = PT->getPointeeType(); |
| 508 | else |
| 509 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow, |
| 510 | SourceRange(MemberLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 512 | // The base type is either a record or an OCUVectorType. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 513 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 514 | RecordDecl *RDecl = RTy->getDecl(); |
| 515 | if (RTy->isIncompleteType()) |
| 516 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(), |
| 517 | BaseExpr->getSourceRange()); |
| 518 | // The record definition is complete, now make sure the member is valid. |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 519 | FieldDecl *MemberDecl = RDecl->getMember(&Member); |
| 520 | if (!MemberDecl) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 521 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(), |
| 522 | SourceRange(MemberLoc)); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 523 | return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc); |
| 524 | } else if (BaseType->isOCUVectorType() && OpKind == tok::period) { |
Steve Naroff | 608e0ee | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 525 | // Component access limited to variables (reject vec4.rg.g). |
| 526 | if (!isa<DeclRefExpr>(BaseExpr)) |
| 527 | return Diag(OpLoc, diag::err_ocuvector_component_access, |
| 528 | SourceRange(MemberLoc)); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 529 | QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 530 | if (ret.isNull()) |
| 531 | return true; |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 532 | return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc); |
Fariborz Jahanian | 232220c | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 533 | } else if (BaseType->isObjcInterfaceType()) { |
| 534 | ObjcInterfaceDecl *IFace; |
| 535 | if (isa<ObjcInterfaceType>(BaseType.getCanonicalType())) |
| 536 | IFace = dyn_cast<ObjcInterfaceType>(BaseType)->getDecl(); |
| 537 | else |
| 538 | IFace = dyn_cast<ObjcQualifiedInterfaceType>(BaseType) |
| 539 | ->getInterfaceType()->getDecl(); |
| 540 | ObjcInterfaceDecl *clsDeclared; |
| 541 | if (ObjcIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared)) |
| 542 | return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr, |
| 543 | OpKind==tok::arrow); |
| 544 | } |
| 545 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion, |
| 546 | SourceRange(MemberLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 549 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | /// This provides the location of the left/right parens and a list of comma |
| 551 | /// locations. |
| 552 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 553 | ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 554 | ExprTy **args, unsigned NumArgsInCall, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 555 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 556 | Expr *Fn = static_cast<Expr *>(fn); |
| 557 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 558 | assert(Fn && "no function call expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 559 | |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 560 | UsualUnaryConversions(Fn); |
| 561 | QualType funcType = Fn->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 562 | |
| 563 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 564 | // type pointer to function". |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 565 | const PointerType *PT = funcType->getAsPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | if (PT == 0) |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 567 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 568 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 569 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 570 | const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | if (funcT == 0) |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 572 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 573 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | |
| 575 | // If a prototype isn't declared, the parser implicitly defines a func decl |
| 576 | QualType resultType = funcT->getResultType(); |
| 577 | |
| 578 | if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) { |
| 579 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 580 | // assignment, to the types of the corresponding parameter, ... |
| 581 | |
| 582 | unsigned NumArgsInProto = proto->getNumArgs(); |
| 583 | unsigned NumArgsToCheck = NumArgsInCall; |
| 584 | |
| 585 | if (NumArgsInCall < NumArgsInProto) |
| 586 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 587 | Fn->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 588 | else if (NumArgsInCall > NumArgsInProto) { |
| 589 | if (!proto->isVariadic()) { |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 590 | Diag(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 591 | diag::err_typecheck_call_too_many_args, Fn->getSourceRange(), |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 592 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 593 | Args[NumArgsInCall-1]->getLocEnd())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | } |
| 595 | NumArgsToCheck = NumArgsInProto; |
| 596 | } |
| 597 | // Continue to check argument types (even if we have too few/many args). |
| 598 | for (unsigned i = 0; i < NumArgsToCheck; i++) { |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 599 | Expr *argExpr = Args[i]; |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 600 | assert(argExpr && "ActOnCallExpr(): missing argument expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 601 | |
| 602 | QualType lhsType = proto->getArgType(i); |
| 603 | QualType rhsType = argExpr->getType(); |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 604 | |
Steve Naroff | 82c7e6d | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 605 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 606 | if (const ArrayType *ary = lhsType->getAsArrayType()) |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 607 | lhsType = Context.getPointerType(ary->getElementType()); |
Steve Naroff | 82c7e6d | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 608 | else if (lhsType->isFunctionType()) |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 609 | lhsType = Context.getPointerType(lhsType); |
| 610 | |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 611 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 612 | argExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 613 | if (Args[i] != argExpr) // The expression was converted. |
| 614 | Args[i] = argExpr; // Make sure we store the converted expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 615 | SourceLocation l = argExpr->getLocStart(); |
| 616 | |
| 617 | // decode the result (notice that AST's are still created for extensions). |
| 618 | switch (result) { |
| 619 | case Compatible: |
| 620 | break; |
| 621 | case PointerFromInt: |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 622 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 623 | lhsType.getAsString(), rhsType.getAsString(), |
| 624 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | break; |
| 626 | case IntFromPointer: |
| 627 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 628 | lhsType.getAsString(), rhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 629 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 630 | break; |
| 631 | case IncompatiblePointer: |
| 632 | Diag(l, diag::ext_typecheck_passing_incompatible_pointer, |
| 633 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 634 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 635 | break; |
| 636 | case CompatiblePointerDiscardsQualifiers: |
| 637 | Diag(l, diag::ext_typecheck_passing_discards_qualifiers, |
| 638 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 639 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 640 | break; |
| 641 | case Incompatible: |
| 642 | return Diag(l, diag::err_typecheck_passing_incompatible, |
| 643 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 644 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 647 | if (NumArgsInCall > NumArgsInProto && proto->isVariadic()) { |
| 648 | // Promote the arguments (C99 6.5.2.2p7). |
| 649 | for (unsigned i = NumArgsInProto; i < NumArgsInCall; i++) { |
| 650 | Expr *argExpr = Args[i]; |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 651 | assert(argExpr && "ActOnCallExpr(): missing argument expression"); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 652 | |
| 653 | DefaultArgumentPromotion(argExpr); |
| 654 | if (Args[i] != argExpr) // The expression was converted. |
| 655 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 656 | } |
| 657 | } else if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) { |
| 658 | // Even if the types checked, bail if the number of arguments don't match. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 659 | return true; |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 660 | } |
| 661 | } else if (isa<FunctionTypeNoProto>(funcT)) { |
| 662 | // Promote the arguments (C99 6.5.2.2p6). |
| 663 | for (unsigned i = 0; i < NumArgsInCall; i++) { |
| 664 | Expr *argExpr = Args[i]; |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 665 | assert(argExpr && "ActOnCallExpr(): missing argument expression"); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 666 | |
| 667 | DefaultArgumentPromotion(argExpr); |
| 668 | if (Args[i] != argExpr) // The expression was converted. |
| 669 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 670 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 672 | // Do special checking on direct calls to functions. |
| 673 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 674 | if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr())) |
| 675 | if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl())) |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 676 | if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, |
| 677 | NumArgsInCall)) |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 678 | return true; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 680 | return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 684 | ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 685 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 686 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 687 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 688 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 689 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 690 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 691 | |
Anders Carlsson | b2f08e0 | 2007-12-06 20:10:20 +0000 | [diff] [blame] | 692 | // FIXME: This is just a temporary workaround to get |
| 693 | // test/Parser/compound_literal.c passing. (CheckInitializer does not support |
| 694 | // initializing a char array from a single string literal). |
| 695 | if (!literalType->isArrayType() || |
| 696 | !literalType->getAsArrayType()->getElementType()->isCharType()) { |
| 697 | // FIXME: add more semantic analysis (C99 6.5.2.5). |
| 698 | if (CheckInitializer(literalExpr, literalType, false)) |
| 699 | return 0; |
| 700 | } |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 701 | |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 702 | return new CompoundLiteralExpr(literalType, literalExpr); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 706 | ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 707 | SourceLocation RBraceLoc) { |
Steve Naroff | f009063 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 708 | Expr **InitList = reinterpret_cast<Expr**>(initlist); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 709 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 710 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | d35005e | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 711 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 712 | |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 713 | InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc); |
| 714 | e->setType(Context.VoidTy); // FIXME: just a place holder for now. |
| 715 | return e; |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 718 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) |
| 719 | { |
| 720 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 721 | |
| 722 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
| 723 | if (Context.getTypeSize(VectorTy, SourceLocation()) != |
| 724 | Context.getTypeSize(Ty, SourceLocation())) |
| 725 | return Diag(R.getBegin(), |
| 726 | Ty->isVectorType() ? |
| 727 | diag::err_invalid_conversion_between_vectors : |
| 728 | diag::err_invalid_conversion_between_vector_and_integer, |
| 729 | VectorTy.getAsString().c_str(), |
| 730 | Ty.getAsString().c_str(), R); |
| 731 | } else |
| 732 | return Diag(R.getBegin(), |
| 733 | diag::err_invalid_conversion_between_vector_and_scalar, |
| 734 | VectorTy.getAsString().c_str(), |
| 735 | Ty.getAsString().c_str(), R); |
| 736 | |
| 737 | return false; |
| 738 | } |
| 739 | |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 740 | Action::ExprResult Sema:: |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 741 | ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 742 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 743 | assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 744 | |
| 745 | Expr *castExpr = static_cast<Expr*>(Op); |
| 746 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 747 | |
Steve Naroff | 711602b | 2007-08-31 00:32:44 +0000 | [diff] [blame] | 748 | UsualUnaryConversions(castExpr); |
| 749 | |
Chris Lattner | 75af480 | 2007-07-18 16:00:06 +0000 | [diff] [blame] | 750 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 751 | // type needs to be scalar. |
Chris Lattner | 3da2db4 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 752 | if (!castType->isVoidType()) { // Cast to void allows any expr type. |
| 753 | if (!castType->isScalarType()) |
| 754 | return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, |
| 755 | castType.getAsString(), SourceRange(LParenLoc, RParenLoc)); |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 756 | if (!castExpr->getType()->isScalarType()) |
Chris Lattner | 3da2db4 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 757 | return Diag(castExpr->getLocStart(), |
| 758 | diag::err_typecheck_expect_scalar_operand, |
| 759 | castExpr->getType().getAsString(),castExpr->getSourceRange()); |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 760 | |
| 761 | if (castExpr->getType()->isVectorType()) { |
| 762 | if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), |
| 763 | castExpr->getType(), castType)) |
| 764 | return true; |
| 765 | } else if (castType->isVectorType()) { |
| 766 | if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), |
| 767 | castType, castExpr->getType())) |
| 768 | return true; |
Chris Lattner | 3da2db4 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 769 | } |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 770 | } |
| 771 | return new CastExpr(castType, castExpr, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Steve Naroff | d4dd30f | 2007-10-18 05:13:08 +0000 | [diff] [blame] | 774 | // promoteExprToType - a helper function to ensure we create exactly one |
| 775 | // ImplicitCastExpr. |
| 776 | static void promoteExprToType(Expr *&expr, QualType type) { |
| 777 | if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr)) |
| 778 | impCast->setType(type); |
| 779 | else |
| 780 | expr = new ImplicitCastExpr(type, expr); |
| 781 | return; |
| 782 | } |
| 783 | |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 784 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 785 | /// In that case, lex = cond. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 786 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 787 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 788 | UsualUnaryConversions(cond); |
| 789 | UsualUnaryConversions(lex); |
| 790 | UsualUnaryConversions(rex); |
| 791 | QualType condT = cond->getType(); |
| 792 | QualType lexT = lex->getType(); |
| 793 | QualType rexT = rex->getType(); |
| 794 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | // first, check the condition. |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 796 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 797 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 798 | condT.getAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 799 | return QualType(); |
| 800 | } |
| 801 | // now check the two expressions. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 802 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5 |
| 803 | UsualArithmeticConversions(lex, rex); |
| 804 | return lex->getType(); |
| 805 | } |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 806 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
| 807 | if (const RecordType *RHSRT = rexT->getAsRecordType()) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 808 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 809 | return lexT; |
| 810 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 811 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 812 | lexT.getAsString(), rexT.getAsString(), |
| 813 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 814 | return QualType(); |
| 815 | } |
| 816 | } |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 817 | // C99 6.5.15p3 |
Steve Naroff | d4dd30f | 2007-10-18 05:13:08 +0000 | [diff] [blame] | 818 | if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) { |
| 819 | promoteExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 820 | return lexT; |
Steve Naroff | d4dd30f | 2007-10-18 05:13:08 +0000 | [diff] [blame] | 821 | } |
| 822 | if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) { |
| 823 | promoteExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 824 | return rexT; |
Steve Naroff | d4dd30f | 2007-10-18 05:13:08 +0000 | [diff] [blame] | 825 | } |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 826 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 827 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 828 | // get the "pointed to" types |
| 829 | QualType lhptee = LHSPT->getPointeeType(); |
| 830 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 831 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 832 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 833 | if (lhptee->isVoidType() && |
| 834 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 835 | return lexT; |
| 836 | if (rhptee->isVoidType() && |
| 837 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 838 | return rexT; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 839 | |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 840 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 841 | rhptee.getUnqualifiedType())) { |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 842 | Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers, |
| 843 | lexT.getAsString(), rexT.getAsString(), |
| 844 | lex->getSourceRange(), rex->getSourceRange()); |
| 845 | return lexT; // FIXME: this is an _ext - is this return o.k? |
| 846 | } |
| 847 | // The pointer types are compatible. |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 848 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 849 | // differently qualified versions of compatible types, the result type is |
| 850 | // a pointer to an appropriately qualified version of the *composite* |
| 851 | // type. |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 852 | return lexT; // FIXME: Need to return the composite type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 855 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 856 | if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3 |
| 857 | return lexT; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | |
| 859 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 860 | lexT.getAsString(), rexT.getAsString(), |
| 861 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 862 | return QualType(); |
| 863 | } |
| 864 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 865 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 866 | /// in the case of a the GNU conditional expr extension. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 867 | Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | SourceLocation ColonLoc, |
| 869 | ExprTy *Cond, ExprTy *LHS, |
| 870 | ExprTy *RHS) { |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 871 | Expr *CondExpr = (Expr *) Cond; |
| 872 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 873 | |
| 874 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 875 | // was the condition. |
| 876 | bool isLHSNull = LHSExpr == 0; |
| 877 | if (isLHSNull) |
| 878 | LHSExpr = CondExpr; |
| 879 | |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 880 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 881 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 882 | if (result.isNull()) |
| 883 | return true; |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 884 | return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr, |
| 885 | RHSExpr, result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 888 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| 889 | /// do not have a prototype. Integer promotions are performed on each |
| 890 | /// argument, and arguments that have type float are promoted to double. |
| 891 | void Sema::DefaultArgumentPromotion(Expr *&expr) { |
| 892 | QualType t = expr->getType(); |
| 893 | assert(!t.isNull() && "DefaultArgumentPromotion - missing type"); |
| 894 | |
| 895 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 896 | promoteExprToType(expr, Context.IntTy); |
| 897 | if (t == Context.FloatTy) |
| 898 | promoteExprToType(expr, Context.DoubleTy); |
| 899 | } |
| 900 | |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 901 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 902 | void Sema::DefaultFunctionArrayConversion(Expr *&e) { |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 903 | QualType t = e->getType(); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 904 | assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type"); |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 905 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 906 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 907 | promoteExprToType(e, ref->getReferenceeType()); // C++ [expr] |
| 908 | t = e->getType(); |
| 909 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 910 | if (t->isFunctionType()) |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 911 | promoteExprToType(e, Context.getPointerType(t)); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 912 | else if (const ArrayType *ary = t->getAsArrayType()) |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 913 | promoteExprToType(e, Context.getPointerType(ary->getElementType())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 917 | /// operators (C99 6.3). The conversions of array and function types are |
| 918 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 919 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 920 | /// In these instances, this routine should *not* be called. |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 921 | void Sema::UsualUnaryConversions(Expr *&expr) { |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 922 | QualType t = expr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 923 | assert(!t.isNull() && "UsualUnaryConversions - missing type"); |
| 924 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 925 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 926 | promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr] |
| 927 | t = expr->getType(); |
| 928 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 929 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 930 | promoteExprToType(expr, Context.IntTy); |
| 931 | else |
| 932 | DefaultFunctionArrayConversion(expr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 935 | /// UsualArithmeticConversions - Performs various conversions that are common to |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 936 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 937 | /// routine returns the first non-arithmetic type found. The client is |
| 938 | /// responsible for emitting appropriate error diagnostics. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 939 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 940 | bool isCompAssign) { |
Steve Naroff | 8702a0f | 2007-08-25 19:54:59 +0000 | [diff] [blame] | 941 | if (!isCompAssign) { |
| 942 | UsualUnaryConversions(lhsExpr); |
| 943 | UsualUnaryConversions(rhsExpr); |
| 944 | } |
Steve Naroff | 3187e20 | 2007-10-18 18:55:53 +0000 | [diff] [blame] | 945 | // For conversion purposes, we ignore any qualifiers. |
| 946 | // For example, "const float" and "float" are equivalent. |
Steve Naroff | f68a63f | 2007-11-10 19:45:54 +0000 | [diff] [blame] | 947 | QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType(); |
| 948 | QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | |
| 950 | // If both types are identical, no conversion is needed. |
Steve Naroff | 3187e20 | 2007-10-18 18:55:53 +0000 | [diff] [blame] | 951 | if (lhs == rhs) |
| 952 | return lhs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 953 | |
| 954 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 955 | // The caller can deal with this (e.g. pointer + int). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 956 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 957 | return lhs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | |
| 959 | // At this point, we have two different arithmetic types. |
| 960 | |
| 961 | // Handle complex types first (C99 6.3.1.8p1). |
| 962 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 963 | // if we have an integer operand, the result is the complex type. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 964 | if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 965 | if (!isCompAssign) promoteExprToType(rhsExpr, lhs); |
| 966 | return lhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 967 | } |
| 968 | if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 969 | if (!isCompAssign) promoteExprToType(lhsExpr, rhs); |
| 970 | return rhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 971 | } |
Steve Naroff | f1448a0 | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 972 | // This handles complex/complex, complex/float, or float/complex. |
| 973 | // When both operands are complex, the shorter operand is converted to the |
| 974 | // type of the longer, and that is the type of the result. This corresponds |
| 975 | // to what is done when combining two real floating-point operands. |
| 976 | // The fun begins when size promotion occur across type domains. |
| 977 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 978 | // floating-point type, the less precise type is converted, within it's |
| 979 | // real or complex domain, to the precision of the other type. For example, |
| 980 | // when combining a "long double" with a "double _Complex", the |
| 981 | // "double _Complex" is promoted to "long double _Complex". |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 982 | int result = Context.compareFloatingType(lhs, rhs); |
| 983 | |
| 984 | if (result > 0) { // The left side is bigger, convert rhs. |
Steve Naroff | 55fe455 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 985 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
| 986 | if (!isCompAssign) |
| 987 | promoteExprToType(rhsExpr, rhs); |
| 988 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 989 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
| 990 | if (!isCompAssign) |
| 991 | promoteExprToType(lhsExpr, lhs); |
| 992 | } |
| 993 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 994 | // domains match. This is a requirement for our implementation, C99 |
| 995 | // does not require this promotion. |
| 996 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 997 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Steve Naroff | 2996036 | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 998 | if (!isCompAssign) |
| 999 | promoteExprToType(lhsExpr, rhs); |
| 1000 | return rhs; |
Steve Naroff | 55fe455 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1001 | } else { // handle "_Complex double, double". |
Steve Naroff | 2996036 | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1002 | if (!isCompAssign) |
| 1003 | promoteExprToType(rhsExpr, lhs); |
| 1004 | return lhs; |
Steve Naroff | 55fe455 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1005 | } |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1006 | } |
Steve Naroff | 2996036 | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1007 | return lhs; // The domain/size match exactly. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | // Now handle "real" floating types (i.e. float, double, long double). |
| 1010 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 1011 | // if we have an integer operand, the result is the real floating type. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1012 | if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1013 | if (!isCompAssign) promoteExprToType(rhsExpr, lhs); |
| 1014 | return lhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1015 | } |
| 1016 | if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1017 | if (!isCompAssign) promoteExprToType(lhsExpr, rhs); |
| 1018 | return rhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1019 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 1020 | // We have two real floating types, float/complex combos were handled above. |
| 1021 | // Convert the smaller operand to the bigger result. |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1022 | int result = Context.compareFloatingType(lhs, rhs); |
| 1023 | |
| 1024 | if (result > 0) { // convert the rhs |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1025 | if (!isCompAssign) promoteExprToType(rhsExpr, lhs); |
| 1026 | return lhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1027 | } |
Steve Naroff | fb0d496 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1028 | if (result < 0) { // convert the lhs |
| 1029 | if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 1030 | return rhs; |
| 1031 | } |
| 1032 | assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 1034 | // Finally, we have two differing integer types. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1035 | if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1036 | if (!isCompAssign) promoteExprToType(rhsExpr, lhs); |
| 1037 | return lhs; |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1038 | } |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1039 | if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 1040 | return rhs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 1044 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 1045 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 1046 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 1047 | // FIXME: add a couple examples in this comment. |
| 1048 | Sema::AssignmentCheckResult |
| 1049 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 1050 | QualType lhptee, rhptee; |
| 1051 | |
| 1052 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1053 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 1054 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | |
| 1056 | // make sure we operate on the canonical type |
| 1057 | lhptee = lhptee.getCanonicalType(); |
| 1058 | rhptee = rhptee.getCanonicalType(); |
| 1059 | |
| 1060 | AssignmentCheckResult r = Compatible; |
| 1061 | |
| 1062 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 1063 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 1064 | // qualifiers of the type *pointed to* by the right; |
| 1065 | if ((lhptee.getQualifiers() & rhptee.getQualifiers()) != |
| 1066 | rhptee.getQualifiers()) |
| 1067 | r = CompatiblePointerDiscardsQualifiers; |
| 1068 | |
| 1069 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 1070 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 1071 | // version of void... |
| 1072 | if (lhptee.getUnqualifiedType()->isVoidType() && |
| 1073 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 1074 | ; |
| 1075 | else if (rhptee.getUnqualifiedType()->isVoidType() && |
| 1076 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 1077 | ; |
| 1078 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 1079 | // unqualified versions of compatible types, ... |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1080 | else if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1081 | rhptee.getUnqualifiedType())) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1082 | r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
| 1083 | return r; |
| 1084 | } |
| 1085 | |
| 1086 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 1087 | /// has code to accommodate several GCC extensions when type checking |
| 1088 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 1089 | /// |
| 1090 | /// int a, *pint; |
| 1091 | /// short *pshort; |
| 1092 | /// struct foo *pfoo; |
| 1093 | /// |
| 1094 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 1095 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 1096 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 1097 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 1098 | /// |
| 1099 | /// As a result, the code for dealing with pointers is more complex than the |
| 1100 | /// C99 spec dictates. |
| 1101 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 1102 | /// |
| 1103 | Sema::AssignmentCheckResult |
| 1104 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Steve Naroff | 8eabdff | 2007-11-13 00:31:42 +0000 | [diff] [blame] | 1105 | if (lhsType.getCanonicalType().getUnqualifiedType() == |
| 1106 | rhsType.getCanonicalType().getUnqualifiedType()) |
Chris Lattner | 84d35ce | 2007-10-29 05:15:40 +0000 | [diff] [blame] | 1107 | return Compatible; // common case, fast path... |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 1108 | |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1109 | if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1110 | if (Context.referenceTypesAreCompatible(lhsType, rhsType)) |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1111 | return Compatible; |
| 1112 | } else if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1113 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 1114 | if (!getLangOptions().LaxVectorConversions) { |
| 1115 | if (lhsType.getCanonicalType() != rhsType.getCanonicalType()) |
| 1116 | return Incompatible; |
| 1117 | } else { |
| 1118 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
| 1119 | if ((lhsType->isIntegerType() && rhsType->isIntegerType()) || |
| 1120 | (lhsType->isRealFloatingType() && |
| 1121 | rhsType->isRealFloatingType())) { |
| 1122 | if (Context.getTypeSize(lhsType, SourceLocation()) == |
| 1123 | Context.getTypeSize(rhsType, SourceLocation())) |
| 1124 | return Compatible; |
| 1125 | } |
| 1126 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1127 | return Incompatible; |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1130 | return Compatible; |
| 1131 | } else if (lhsType->isPointerType()) { |
| 1132 | if (rhsType->isIntegerType()) |
| 1133 | return PointerFromInt; |
| 1134 | |
| 1135 | if (rhsType->isPointerType()) |
| 1136 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
| 1137 | } else if (rhsType->isPointerType()) { |
| 1138 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 1139 | if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy)) |
| 1140 | return IntFromPointer; |
| 1141 | |
| 1142 | if (lhsType->isPointerType()) |
| 1143 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
| 1144 | } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1145 | if (Context.tagTypesAreCompatible(lhsType, rhsType)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1146 | return Compatible; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1147 | } |
| 1148 | return Incompatible; |
| 1149 | } |
| 1150 | |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1151 | Sema::AssignmentCheckResult |
| 1152 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1153 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 1154 | // a null pointer constant. |
| 1155 | if (lhsType->isPointerType() && rExpr->isNullPointerConstant(Context)) { |
| 1156 | promoteExprToType(rExpr, lhsType); |
| 1157 | return Compatible; |
| 1158 | } |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1159 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1160 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1161 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1162 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1163 | // |
| 1164 | // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references |
| 1165 | // are better understood. |
| 1166 | if (!lhsType->isReferenceType()) |
| 1167 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1168 | |
| 1169 | Sema::AssignmentCheckResult result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1170 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1171 | result = CheckAssignmentConstraints(lhsType, rExpr->getType()); |
| 1172 | |
| 1173 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 1174 | // type of the assignment expression. |
| 1175 | if (rExpr->getType() != lhsType) |
| 1176 | promoteExprToType(rExpr, lhsType); |
| 1177 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | Sema::AssignmentCheckResult |
| 1181 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 1182 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 1183 | } |
| 1184 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1185 | inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1186 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 1187 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1188 | lex->getSourceRange(), rex->getSourceRange()); |
| 1189 | } |
| 1190 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1191 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 1192 | Expr *&rex) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1193 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1194 | |
| 1195 | // make sure the vector types are identical. |
| 1196 | if (lhsType == rhsType) |
| 1197 | return lhsType; |
| 1198 | // You cannot convert between vector values of different size. |
| 1199 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 1200 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1201 | lex->getSourceRange(), rex->getSourceRange()); |
| 1202 | return QualType(); |
| 1203 | } |
| 1204 | |
| 1205 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1206 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1207 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1208 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1209 | |
| 1210 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1211 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1212 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1213 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1214 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1215 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1216 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1217 | InvalidOperands(loc, lex, rex); |
| 1218 | return QualType(); |
| 1219 | } |
| 1220 | |
| 1221 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1222 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1223 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1224 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1225 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1226 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1227 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1228 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1229 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1230 | InvalidOperands(loc, lex, rex); |
| 1231 | return QualType(); |
| 1232 | } |
| 1233 | |
| 1234 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1235 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1236 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1237 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1238 | return CheckVectorOperands(loc, lex, rex); |
| 1239 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1240 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1243 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1244 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1245 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1246 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
| 1247 | return lex->getType(); |
| 1248 | if (lex->getType()->isIntegerType() && rex->getType()->isPointerType()) |
| 1249 | return rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | InvalidOperands(loc, lex, rex); |
| 1251 | return QualType(); |
| 1252 | } |
| 1253 | |
| 1254 | inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1255 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1257 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1258 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1259 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1260 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | |
| 1262 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1263 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1264 | return compType; |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1265 | |
| 1266 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1267 | return compType; |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1268 | if (lex->getType()->isPointerType() && rex->getType()->isPointerType()) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1269 | return Context.getPointerDiffType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1270 | InvalidOperands(loc, lex, rex); |
| 1271 | return QualType(); |
| 1272 | } |
| 1273 | |
| 1274 | inline QualType Sema::CheckShiftOperands( // C99 6.5.7 |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1275 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1276 | { |
| 1277 | // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong |
| 1278 | // for int << longlong -> the result type should be int, not long long. |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1279 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1280 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1281 | // handle the common case first (both operands are arithmetic). |
| 1282 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1283 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | InvalidOperands(loc, lex, rex); |
| 1285 | return QualType(); |
| 1286 | } |
| 1287 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1288 | inline QualType Sema::CheckCompareOperands( // C99 6.5.8 |
| 1289 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1290 | { |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1291 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1292 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1293 | UsualArithmeticConversions(lex, rex); |
| 1294 | else { |
| 1295 | UsualUnaryConversions(lex); |
| 1296 | UsualUnaryConversions(rex); |
| 1297 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1298 | QualType lType = lex->getType(); |
| 1299 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1300 | |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1301 | // For non-floating point types, check for self-comparisons of the form |
| 1302 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 1303 | // often indicate logic errors in the program. |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 1304 | if (!lType->isFloatingType()) { |
| 1305 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex))) |
| 1306 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex))) |
| 1307 | if (DRL->getDecl() == DRR->getDecl()) |
| 1308 | Diag(loc, diag::warn_selfcomparison); |
| 1309 | } |
| 1310 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1311 | if (isRelational) { |
| 1312 | if (lType->isRealType() && rType->isRealType()) |
| 1313 | return Context.IntTy; |
| 1314 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1315 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1316 | if (lType->isFloatingType()) { |
| 1317 | assert (rType->isFloatingType()); |
Ted Kremenek | 588e5eb | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1318 | CheckFloatComparison(loc,lex,rex); |
Ted Kremenek | 6a26155 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1321 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 1322 | return Context.IntTy; |
| 1323 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1324 | |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1325 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 1326 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 1327 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1328 | // All of the following pointer related warnings are GCC extensions, except |
| 1329 | // when handling null pointer constants. One day, we can consider making them |
| 1330 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 1331 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Steve Naroff | 66296cb | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 1332 | |
| 1333 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
| 1334 | !lType->getAsPointerType()->getPointeeType()->isVoidType() && |
| 1335 | !rType->getAsPointerType()->getPointeeType()->isVoidType() && |
Steve Naroff | ec0550f | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1336 | !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(), |
| 1337 | rType.getUnqualifiedType())) { |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1338 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 1339 | lType.getAsString(), rType.getAsString(), |
| 1340 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | } |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1342 | promoteExprToType(rex, lType); // promote the pointer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1343 | return Context.IntTy; |
| 1344 | } |
| 1345 | if (lType->isPointerType() && rType->isIntegerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1346 | if (!RHSIsNull) |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1347 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1348 | lType.getAsString(), rType.getAsString(), |
| 1349 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1350 | promoteExprToType(rex, lType); // promote the integer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1351 | return Context.IntTy; |
| 1352 | } |
| 1353 | if (lType->isIntegerType() && rType->isPointerType()) { |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1354 | if (!LHSIsNull) |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1355 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1356 | lType.getAsString(), rType.getAsString(), |
| 1357 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | d28f815 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1358 | promoteExprToType(lex, rType); // promote the integer to pointer |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1359 | return Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | } |
| 1361 | InvalidOperands(loc, lex, rex); |
| 1362 | return QualType(); |
| 1363 | } |
| 1364 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1365 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1366 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1368 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1370 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1371 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1372 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1373 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1374 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1375 | InvalidOperands(loc, lex, rex); |
| 1376 | return QualType(); |
| 1377 | } |
| 1378 | |
| 1379 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1380 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1382 | UsualUnaryConversions(lex); |
| 1383 | UsualUnaryConversions(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1384 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1385 | if (lex->getType()->isScalarType() || rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | return Context.IntTy; |
| 1387 | InvalidOperands(loc, lex, rex); |
| 1388 | return QualType(); |
| 1389 | } |
| 1390 | |
| 1391 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1392 | Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1393 | { |
| 1394 | QualType lhsType = lex->getType(); |
| 1395 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
| 1396 | bool hadError = false; |
| 1397 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(); |
| 1398 | |
| 1399 | switch (mlval) { // C99 6.5.16p2 |
| 1400 | case Expr::MLV_Valid: |
| 1401 | break; |
| 1402 | case Expr::MLV_ConstQualified: |
| 1403 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 1404 | hadError = true; |
| 1405 | break; |
| 1406 | case Expr::MLV_ArrayType: |
| 1407 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 1408 | lhsType.getAsString(), lex->getSourceRange()); |
| 1409 | return QualType(); |
| 1410 | case Expr::MLV_NotObjectType: |
| 1411 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 1412 | lhsType.getAsString(), lex->getSourceRange()); |
| 1413 | return QualType(); |
| 1414 | case Expr::MLV_InvalidExpression: |
| 1415 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 1416 | lex->getSourceRange()); |
| 1417 | return QualType(); |
| 1418 | case Expr::MLV_IncompleteType: |
| 1419 | case Expr::MLV_IncompleteVoidType: |
| 1420 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 1421 | lhsType.getAsString(), lex->getSourceRange()); |
| 1422 | return QualType(); |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1423 | case Expr::MLV_DuplicateVectorComponents: |
| 1424 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 1425 | lex->getSourceRange()); |
| 1426 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1427 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1428 | AssignmentCheckResult result; |
| 1429 | |
| 1430 | if (compoundType.isNull()) |
| 1431 | result = CheckSingleAssignmentConstraints(lhsType, rex); |
| 1432 | else |
| 1433 | result = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1435 | // decode the result (notice that extensions still return a type). |
| 1436 | switch (result) { |
| 1437 | case Compatible: |
| 1438 | break; |
| 1439 | case Incompatible: |
| 1440 | Diag(loc, diag::err_typecheck_assign_incompatible, |
| 1441 | lhsType.getAsString(), rhsType.getAsString(), |
| 1442 | lex->getSourceRange(), rex->getSourceRange()); |
| 1443 | hadError = true; |
| 1444 | break; |
| 1445 | case PointerFromInt: |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1446 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1447 | lhsType.getAsString(), rhsType.getAsString(), |
| 1448 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | case IntFromPointer: |
| 1451 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1452 | lhsType.getAsString(), rhsType.getAsString(), |
| 1453 | lex->getSourceRange(), rex->getSourceRange()); |
| 1454 | break; |
| 1455 | case IncompatiblePointer: |
| 1456 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer, |
| 1457 | lhsType.getAsString(), rhsType.getAsString(), |
| 1458 | lex->getSourceRange(), rex->getSourceRange()); |
| 1459 | break; |
| 1460 | case CompatiblePointerDiscardsQualifiers: |
| 1461 | Diag(loc, diag::ext_typecheck_assign_discards_qualifiers, |
| 1462 | lhsType.getAsString(), rhsType.getAsString(), |
| 1463 | lex->getSourceRange(), rex->getSourceRange()); |
| 1464 | break; |
| 1465 | } |
| 1466 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 1467 | // left operand unless the left operand has qualified type, in which case |
| 1468 | // it is the unqualified version of the type of the left operand. |
| 1469 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 1470 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1471 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 1472 | // oprdu. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1473 | return hadError ? QualType() : lhsType.getUnqualifiedType(); |
| 1474 | } |
| 1475 | |
| 1476 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1477 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1478 | UsualUnaryConversions(rex); |
| 1479 | return rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1482 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 1483 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1484 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1485 | QualType resType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1486 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
| 1487 | |
Steve Naroff | 084f9ed | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1488 | // C99 6.5.2.4p1: We allow complex as a GCC extension. |
Steve Naroff | d848a38 | 2007-11-11 14:15:57 +0000 | [diff] [blame] | 1489 | if (const PointerType *pt = resType->getAsPointerType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1490 | if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2 |
| 1491 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1492 | resType.getAsString(), op->getSourceRange()); |
| 1493 | return QualType(); |
| 1494 | } |
Steve Naroff | 084f9ed | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1495 | } else if (!resType->isRealType()) { |
| 1496 | if (resType->isComplexType()) |
| 1497 | // C99 does not support ++/-- on complex types. |
| 1498 | Diag(OpLoc, diag::ext_integer_increment_complex, |
| 1499 | resType.getAsString(), op->getSourceRange()); |
| 1500 | else { |
| 1501 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 1502 | resType.getAsString(), op->getSourceRange()); |
| 1503 | return QualType(); |
| 1504 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1505 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 1506 | // At this point, we know we have a real, complex or pointer type. |
| 1507 | // Now make sure the operand is a modifiable lvalue. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1508 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(); |
| 1509 | if (mlval != Expr::MLV_Valid) { |
| 1510 | // FIXME: emit a more precise diagnostic... |
| 1511 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
| 1512 | op->getSourceRange()); |
| 1513 | return QualType(); |
| 1514 | } |
| 1515 | return resType; |
| 1516 | } |
| 1517 | |
| 1518 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
| 1519 | /// This routine allows us to typecheck complex/recursive expressions |
| 1520 | /// where the declaration is needed for type checking. Here are some |
| 1521 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
| 1522 | static Decl *getPrimaryDeclaration(Expr *e) { |
| 1523 | switch (e->getStmtClass()) { |
| 1524 | case Stmt::DeclRefExprClass: |
| 1525 | return cast<DeclRefExpr>(e)->getDecl(); |
| 1526 | case Stmt::MemberExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1527 | // Fields cannot be declared with a 'register' storage class. |
| 1528 | // &X->f is always ok, even if X is declared register. |
| 1529 | if (cast<MemberExpr>(e)->isArrow()) |
| 1530 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
| 1532 | case Stmt::ArraySubscriptExprClass: |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1533 | // &X[4] and &4[X] is invalid if X is invalid. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1534 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1535 | case Stmt::UnaryOperatorClass: |
| 1536 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
| 1537 | case Stmt::ParenExprClass: |
| 1538 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1539 | case Stmt::ImplicitCastExprClass: |
| 1540 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
| 1541 | return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | default: |
| 1543 | return 0; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 1548 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 1549 | /// object cannot be declared with storage class register or be a bit field. |
| 1550 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 1551 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| 1552 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
| 1553 | Decl *dcl = getPrimaryDeclaration(op); |
| 1554 | Expr::isLvalueResult lval = op->isLvalue(); |
| 1555 | |
| 1556 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1557 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 1558 | // FIXME: emit more specific diag... |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 1560 | op->getSourceRange()); |
| 1561 | return QualType(); |
| 1562 | } |
| 1563 | } else if (dcl) { |
| 1564 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 1565 | // with the register storage-class specifier. |
| 1566 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 1567 | if (vd->getStorageClass() == VarDecl::Register) { |
| 1568 | Diag(OpLoc, diag::err_typecheck_address_of_register, |
| 1569 | op->getSourceRange()); |
| 1570 | return QualType(); |
| 1571 | } |
| 1572 | } else |
| 1573 | assert(0 && "Unknown/unexpected decl type"); |
| 1574 | |
| 1575 | // FIXME: add check for bitfields! |
| 1576 | } |
| 1577 | // If the operand has type "type", the result has type "pointer to type". |
| 1578 | return Context.getPointerType(op->getType()); |
| 1579 | } |
| 1580 | |
| 1581 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1582 | UsualUnaryConversions(op); |
| 1583 | QualType qType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1585 | if (const PointerType *PT = qType->getAsPointerType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1586 | QualType ptype = PT->getPointeeType(); |
| 1587 | // C99 6.5.3.2p4. "if it points to an object,...". |
| 1588 | if (ptype->isIncompleteType()) { // An incomplete type is not an object |
| 1589 | // GCC compat: special case 'void *' (treat as warning). |
| 1590 | if (ptype->isVoidType()) { |
| 1591 | Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void, |
| 1592 | qType.getAsString(), op->getSourceRange()); |
| 1593 | } else { |
| 1594 | Diag(OpLoc, diag::err_typecheck_deref_incomplete_type, |
| 1595 | ptype.getAsString(), op->getSourceRange()); |
| 1596 | return QualType(); |
| 1597 | } |
| 1598 | } |
| 1599 | return ptype; |
| 1600 | } |
| 1601 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
| 1602 | qType.getAsString(), op->getSourceRange()); |
| 1603 | return QualType(); |
| 1604 | } |
| 1605 | |
| 1606 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 1607 | tok::TokenKind Kind) { |
| 1608 | BinaryOperator::Opcode Opc; |
| 1609 | switch (Kind) { |
| 1610 | default: assert(0 && "Unknown binop!"); |
| 1611 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 1612 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 1613 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 1614 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 1615 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 1616 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 1617 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 1618 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 1619 | case tok::less: Opc = BinaryOperator::LT; break; |
| 1620 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 1621 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 1622 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 1623 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 1624 | case tok::amp: Opc = BinaryOperator::And; break; |
| 1625 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 1626 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 1627 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 1628 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 1629 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 1630 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 1631 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 1632 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 1633 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 1634 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 1635 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 1636 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 1637 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 1638 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 1639 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 1640 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 1641 | } |
| 1642 | return Opc; |
| 1643 | } |
| 1644 | |
| 1645 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 1646 | tok::TokenKind Kind) { |
| 1647 | UnaryOperator::Opcode Opc; |
| 1648 | switch (Kind) { |
| 1649 | default: assert(0 && "Unknown unary op!"); |
| 1650 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 1651 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 1652 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 1653 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 1654 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 1655 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 1656 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 1657 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 1658 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 1659 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 1660 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 1661 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 1662 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 1663 | } |
| 1664 | return Opc; |
| 1665 | } |
| 1666 | |
| 1667 | // Binary Operators. 'Tok' is the token for the operator. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1668 | Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | ExprTy *LHS, ExprTy *RHS) { |
| 1670 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 1671 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 1672 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1673 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 1674 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1675 | |
| 1676 | QualType ResultTy; // Result type of the binary operator. |
| 1677 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 1678 | |
| 1679 | switch (Opc) { |
| 1680 | default: |
| 1681 | assert(0 && "Unknown binary expr!"); |
| 1682 | case BinaryOperator::Assign: |
| 1683 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
| 1684 | break; |
| 1685 | case BinaryOperator::Mul: |
| 1686 | case BinaryOperator::Div: |
| 1687 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1688 | break; |
| 1689 | case BinaryOperator::Rem: |
| 1690 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1691 | break; |
| 1692 | case BinaryOperator::Add: |
| 1693 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1694 | break; |
| 1695 | case BinaryOperator::Sub: |
| 1696 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1697 | break; |
| 1698 | case BinaryOperator::Shl: |
| 1699 | case BinaryOperator::Shr: |
| 1700 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1701 | break; |
| 1702 | case BinaryOperator::LE: |
| 1703 | case BinaryOperator::LT: |
| 1704 | case BinaryOperator::GE: |
| 1705 | case BinaryOperator::GT: |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1706 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1707 | break; |
| 1708 | case BinaryOperator::EQ: |
| 1709 | case BinaryOperator::NE: |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1710 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1711 | break; |
| 1712 | case BinaryOperator::And: |
| 1713 | case BinaryOperator::Xor: |
| 1714 | case BinaryOperator::Or: |
| 1715 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1716 | break; |
| 1717 | case BinaryOperator::LAnd: |
| 1718 | case BinaryOperator::LOr: |
| 1719 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
| 1720 | break; |
| 1721 | case BinaryOperator::MulAssign: |
| 1722 | case BinaryOperator::DivAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1723 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | if (!CompTy.isNull()) |
| 1725 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1726 | break; |
| 1727 | case BinaryOperator::RemAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1728 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1729 | if (!CompTy.isNull()) |
| 1730 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1731 | break; |
| 1732 | case BinaryOperator::AddAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1733 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1734 | if (!CompTy.isNull()) |
| 1735 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1736 | break; |
| 1737 | case BinaryOperator::SubAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1738 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | if (!CompTy.isNull()) |
| 1740 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1741 | break; |
| 1742 | case BinaryOperator::ShlAssign: |
| 1743 | case BinaryOperator::ShrAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1744 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1745 | if (!CompTy.isNull()) |
| 1746 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1747 | break; |
| 1748 | case BinaryOperator::AndAssign: |
| 1749 | case BinaryOperator::XorAssign: |
| 1750 | case BinaryOperator::OrAssign: |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1751 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1752 | if (!CompTy.isNull()) |
| 1753 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1754 | break; |
| 1755 | case BinaryOperator::Comma: |
| 1756 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
| 1757 | break; |
| 1758 | } |
| 1759 | if (ResultTy.isNull()) |
| 1760 | return true; |
| 1761 | if (CompTy.isNull()) |
Chris Lattner | 17d1b2a | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 1762 | return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1763 | else |
Chris Lattner | 17d1b2a | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 1764 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | // Unary Operators. 'Tok' is the token for the operator. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1768 | Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1769 | ExprTy *input) { |
| 1770 | Expr *Input = (Expr*)input; |
| 1771 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 1772 | QualType resultType; |
| 1773 | switch (Opc) { |
| 1774 | default: |
| 1775 | assert(0 && "Unimplemented unary expr!"); |
| 1776 | case UnaryOperator::PreInc: |
| 1777 | case UnaryOperator::PreDec: |
| 1778 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
| 1779 | break; |
| 1780 | case UnaryOperator::AddrOf: |
| 1781 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 1782 | break; |
| 1783 | case UnaryOperator::Deref: |
| 1784 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 1785 | break; |
| 1786 | case UnaryOperator::Plus: |
| 1787 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1788 | UsualUnaryConversions(Input); |
| 1789 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1790 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 1791 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1792 | resultType.getAsString()); |
| 1793 | break; |
| 1794 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1795 | UsualUnaryConversions(Input); |
| 1796 | resultType = Input->getType(); |
Steve Naroff | 084f9ed | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1797 | // C99 6.5.3.3p1. We allow complex as a GCC extension. |
| 1798 | if (!resultType->isIntegerType()) { |
| 1799 | if (resultType->isComplexType()) |
| 1800 | // C99 does not support '~' for complex conjugation. |
| 1801 | Diag(OpLoc, diag::ext_integer_complement_complex, |
| 1802 | resultType.getAsString()); |
| 1803 | else |
| 1804 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1805 | resultType.getAsString()); |
| 1806 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1807 | break; |
| 1808 | case UnaryOperator::LNot: // logical negation |
| 1809 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1810 | DefaultFunctionArrayConversion(Input); |
| 1811 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1812 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 1813 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1814 | resultType.getAsString()); |
| 1815 | // LNot always has type int. C99 6.5.3.3p5. |
| 1816 | resultType = Context.IntTy; |
| 1817 | break; |
| 1818 | case UnaryOperator::SizeOf: |
| 1819 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true); |
| 1820 | break; |
| 1821 | case UnaryOperator::AlignOf: |
| 1822 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false); |
| 1823 | break; |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1824 | case UnaryOperator::Real: |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1825 | case UnaryOperator::Imag: |
Chris Lattner | 5d79425 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 1826 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1827 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1828 | case UnaryOperator::Extension: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1829 | resultType = Input->getType(); |
| 1830 | break; |
| 1831 | } |
| 1832 | if (resultType.isNull()) |
| 1833 | return true; |
| 1834 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
| 1835 | } |
| 1836 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1837 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 1838 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1839 | SourceLocation LabLoc, |
| 1840 | IdentifierInfo *LabelII) { |
| 1841 | // Look up the record for this label identifier. |
| 1842 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 1843 | |
| 1844 | // If we haven't seen this label yet, create a forward reference. |
| 1845 | if (LabelDecl == 0) |
| 1846 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 1847 | |
| 1848 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 1849 | return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 1850 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1853 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1854 | SourceLocation RPLoc) { // "({..})" |
| 1855 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 1856 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 1857 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 1858 | |
| 1859 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 1860 | // example, it is not possible to goto into a stmt expression apparently. |
| 1861 | // More semantic analysis is needed. |
| 1862 | |
| 1863 | // FIXME: the last statement in the compount stmt has its value used. We |
| 1864 | // should not warn about it being unused. |
| 1865 | |
| 1866 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 1867 | // as the type of the stmtexpr. |
| 1868 | QualType Ty = Context.VoidTy; |
| 1869 | |
| 1870 | if (!Compound->body_empty()) |
| 1871 | if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back())) |
| 1872 | Ty = LastExpr->getType(); |
| 1873 | |
| 1874 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 1875 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1876 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1877 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc, |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1878 | SourceLocation TypeLoc, |
| 1879 | TypeTy *argty, |
| 1880 | OffsetOfComponent *CompPtr, |
| 1881 | unsigned NumComponents, |
| 1882 | SourceLocation RPLoc) { |
| 1883 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 1884 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 1885 | |
| 1886 | // We must have at least one component that refers to the type, and the first |
| 1887 | // one is known to be a field designator. Verify that the ArgTy represents |
| 1888 | // a struct/union/class. |
| 1889 | if (!ArgTy->isRecordType()) |
| 1890 | return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString()); |
| 1891 | |
| 1892 | // Otherwise, create a compound literal expression as the base, and |
| 1893 | // iteratively process the offsetof designators. |
| 1894 | Expr *Res = new CompoundLiteralExpr(ArgTy, 0); |
| 1895 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 1896 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 1897 | // GCC extension, diagnose them. |
| 1898 | if (NumComponents != 1) |
| 1899 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator, |
| 1900 | SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd)); |
| 1901 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1902 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 1903 | const OffsetOfComponent &OC = CompPtr[i]; |
| 1904 | if (OC.isBrackets) { |
| 1905 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 1906 | const ArrayType *AT = Res->getType()->getAsArrayType(); |
| 1907 | if (!AT) { |
| 1908 | delete Res; |
| 1909 | return Diag(OC.LocEnd, diag::err_offsetof_array_type, |
| 1910 | Res->getType().getAsString()); |
| 1911 | } |
| 1912 | |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 1913 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 1914 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1915 | // C99 6.5.2.1p1 |
| 1916 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 1917 | if (!Idx->getType()->isIntegerType()) |
| 1918 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript, |
| 1919 | Idx->getSourceRange()); |
| 1920 | |
| 1921 | Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd); |
| 1922 | continue; |
| 1923 | } |
| 1924 | |
| 1925 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 1926 | if (!RC) { |
| 1927 | delete Res; |
| 1928 | return Diag(OC.LocEnd, diag::err_offsetof_record_type, |
| 1929 | Res->getType().getAsString()); |
| 1930 | } |
| 1931 | |
| 1932 | // Get the decl corresponding to this. |
| 1933 | RecordDecl *RD = RC->getDecl(); |
| 1934 | FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo); |
| 1935 | if (!MemberDecl) |
| 1936 | return Diag(BuiltinLoc, diag::err_typecheck_no_member, |
| 1937 | OC.U.IdentInfo->getName(), |
| 1938 | SourceRange(OC.LocStart, OC.LocEnd)); |
Chris Lattner | 704fe35 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 1939 | |
| 1940 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 1941 | // FIXME: Verify that MemberDecl isn't a bitfield. |
| 1942 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1943 | Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd); |
| 1944 | } |
| 1945 | |
| 1946 | return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(), |
| 1947 | BuiltinLoc); |
| 1948 | } |
| 1949 | |
| 1950 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1951 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1952 | TypeTy *arg1, TypeTy *arg2, |
| 1953 | SourceLocation RPLoc) { |
| 1954 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 1955 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 1956 | |
| 1957 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 1958 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1959 | return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 1962 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1963 | ExprTy *expr1, ExprTy *expr2, |
| 1964 | SourceLocation RPLoc) { |
| 1965 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 1966 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 1967 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 1968 | |
| 1969 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 1970 | |
| 1971 | // The conditional expression is required to be a constant expression. |
| 1972 | llvm::APSInt condEval(32); |
| 1973 | SourceLocation ExpLoc; |
| 1974 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 1975 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant, |
| 1976 | CondExpr->getSourceRange()); |
| 1977 | |
| 1978 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 1979 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 1980 | RHSExpr->getType(); |
| 1981 | return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc); |
| 1982 | } |
| 1983 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1984 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 1985 | ExprTy *expr, TypeTy *type, |
| 1986 | SourceLocation RPLoc) |
| 1987 | { |
| 1988 | Expr *E = static_cast<Expr*>(expr); |
| 1989 | QualType T = QualType::getFromOpaquePtr(type); |
| 1990 | |
| 1991 | InitBuiltinVaListType(); |
| 1992 | |
| 1993 | Sema::AssignmentCheckResult result; |
| 1994 | |
| 1995 | result = CheckAssignmentConstraints(Context.getBuiltinVaListType(), |
| 1996 | E->getType()); |
| 1997 | if (result != Compatible) |
| 1998 | return Diag(E->getLocStart(), |
| 1999 | diag::err_first_argument_to_va_arg_not_of_type_va_list, |
| 2000 | E->getType().getAsString(), |
| 2001 | E->getSourceRange()); |
| 2002 | |
| 2003 | // FIXME: Warn if a non-POD type is passed in. |
| 2004 | |
| 2005 | return new VAArgExpr(BuiltinLoc, E, T, RPLoc); |
| 2006 | } |
| 2007 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2008 | // TODO: Move this to SemaObjC.cpp |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2009 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation AtLoc, |
| 2010 | ExprTy *string) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2011 | StringLiteral* S = static_cast<StringLiteral *>(string); |
| 2012 | |
| 2013 | if (CheckBuiltinCFStringArgument(S)) |
| 2014 | return true; |
| 2015 | |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2016 | if (Context.getObjcConstantStringInterface().isNull()) { |
| 2017 | // Initialize the constant string interface lazily. This assumes |
| 2018 | // the NSConstantString interface is seen in this translation unit. |
| 2019 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
| 2020 | ScopedDecl *IFace = LookupScopedDecl(NSIdent, Decl::IDNS_Ordinary, |
| 2021 | SourceLocation(), TUScope); |
Steve Naroff | a1fe117 | 2007-10-16 00:00:18 +0000 | [diff] [blame] | 2022 | ObjcInterfaceDecl *strIFace = dyn_cast_or_null<ObjcInterfaceDecl>(IFace); |
Steve Naroff | 806a4eb | 2007-10-18 23:53:51 +0000 | [diff] [blame] | 2023 | if (!strIFace) |
| 2024 | return Diag(S->getLocStart(), diag::err_undef_interface, |
| 2025 | NSIdent->getName()); |
Steve Naroff | a1fe117 | 2007-10-16 00:00:18 +0000 | [diff] [blame] | 2026 | Context.setObjcConstantStringInterface(strIFace); |
Steve Naroff | 2198891 | 2007-10-15 23:35:17 +0000 | [diff] [blame] | 2027 | } |
| 2028 | QualType t = Context.getObjcConstantStringInterface(); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2029 | t = Context.getPointerType(t); |
Steve Naroff | beaf299 | 2007-11-03 11:27:19 +0000 | [diff] [blame] | 2030 | return new ObjCStringLiteral(S, t, AtLoc); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 2031 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2032 | |
| 2033 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
Chris Lattner | 674af95 | 2007-10-16 22:51:17 +0000 | [diff] [blame] | 2034 | SourceLocation EncodeLoc, |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 2035 | SourceLocation LParenLoc, |
| 2036 | TypeTy *Ty, |
| 2037 | SourceLocation RParenLoc) { |
| 2038 | QualType EncodedType = QualType::getFromOpaquePtr(Ty); |
| 2039 | |
| 2040 | QualType t = Context.getPointerType(Context.CharTy); |
| 2041 | return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); |
| 2042 | } |
Steve Naroff | 708391a | 2007-09-17 21:01:15 +0000 | [diff] [blame] | 2043 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2044 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 2045 | SourceLocation AtLoc, |
Fariborz Jahanian | 2a35fa9 | 2007-10-16 23:21:02 +0000 | [diff] [blame] | 2046 | SourceLocation SelLoc, |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2047 | SourceLocation LParenLoc, |
| 2048 | SourceLocation RParenLoc) { |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 2049 | QualType t = Context.getObjcSelType(); |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2050 | return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); |
| 2051 | } |
| 2052 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2053 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 2054 | SourceLocation AtLoc, |
| 2055 | SourceLocation ProtoLoc, |
| 2056 | SourceLocation LParenLoc, |
| 2057 | SourceLocation RParenLoc) { |
| 2058 | ObjcProtocolDecl* PDecl = ObjcProtocols[ProtocolId]; |
| 2059 | if (!PDecl) { |
| 2060 | Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName()); |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 2064 | QualType t = Context.getObjcProtoType(); |
Fariborz Jahanian | 3e27aa1 | 2007-10-18 22:59:23 +0000 | [diff] [blame] | 2065 | if (t.isNull()) |
| 2066 | return true; |
Fariborz Jahanian | 66c5dfc | 2007-12-07 00:18:54 +0000 | [diff] [blame] | 2067 | t = Context.getPointerType(t); |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2068 | return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); |
| 2069 | } |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2070 | |
| 2071 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
| 2072 | ObjcMethodDecl *Method) { |
| 2073 | bool anyIncompatibleArgs = false; |
| 2074 | |
| 2075 | for (unsigned i = 0; i < NumArgs; i++) { |
| 2076 | Expr *argExpr = Args[i]; |
| 2077 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 2078 | |
| 2079 | QualType lhsType = Method->getParamDecl(i)->getType(); |
| 2080 | QualType rhsType = argExpr->getType(); |
| 2081 | |
| 2082 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
| 2083 | if (const ArrayType *ary = lhsType->getAsArrayType()) |
| 2084 | lhsType = Context.getPointerType(ary->getElementType()); |
| 2085 | else if (lhsType->isFunctionType()) |
| 2086 | lhsType = Context.getPointerType(lhsType); |
| 2087 | |
| 2088 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 2089 | argExpr); |
| 2090 | if (Args[i] != argExpr) // The expression was converted. |
| 2091 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 2092 | SourceLocation l = argExpr->getLocStart(); |
| 2093 | |
| 2094 | // decode the result (notice that AST's are still created for extensions). |
| 2095 | switch (result) { |
| 2096 | case Compatible: |
| 2097 | break; |
| 2098 | case PointerFromInt: |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 2099 | Diag(l, diag::ext_typecheck_sending_pointer_int, |
| 2100 | lhsType.getAsString(), rhsType.getAsString(), |
| 2101 | argExpr->getSourceRange()); |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | case IntFromPointer: |
| 2104 | Diag(l, diag::ext_typecheck_sending_pointer_int, |
| 2105 | lhsType.getAsString(), rhsType.getAsString(), |
| 2106 | argExpr->getSourceRange()); |
| 2107 | break; |
| 2108 | case IncompatiblePointer: |
| 2109 | Diag(l, diag::ext_typecheck_sending_incompatible_pointer, |
| 2110 | rhsType.getAsString(), lhsType.getAsString(), |
| 2111 | argExpr->getSourceRange()); |
| 2112 | break; |
| 2113 | case CompatiblePointerDiscardsQualifiers: |
| 2114 | Diag(l, diag::ext_typecheck_passing_discards_qualifiers, |
| 2115 | rhsType.getAsString(), lhsType.getAsString(), |
| 2116 | argExpr->getSourceRange()); |
| 2117 | break; |
| 2118 | case Incompatible: |
| 2119 | Diag(l, diag::err_typecheck_sending_incompatible, |
| 2120 | rhsType.getAsString(), lhsType.getAsString(), |
| 2121 | argExpr->getSourceRange()); |
| 2122 | anyIncompatibleArgs = true; |
| 2123 | } |
| 2124 | } |
| 2125 | return anyIncompatibleArgs; |
| 2126 | } |
| 2127 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2128 | // ActOnClassMessage - used for both unary and keyword messages. |
| 2129 | // ArgExprs is optional - if it is present, the number of expressions |
| 2130 | // is obtained from Sel.getNumArgs(). |
| 2131 | Sema::ExprResult Sema::ActOnClassMessage( |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2132 | Scope *S, |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 2133 | IdentifierInfo *receiverName, Selector Sel, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2134 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
Steve Naroff | 708391a | 2007-09-17 21:01:15 +0000 | [diff] [blame] | 2135 | { |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 2136 | assert(receiverName && "missing receiver class name"); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2137 | |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2138 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2139 | ObjcInterfaceDecl* ClassDecl = 0; |
| 2140 | if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) { |
| 2141 | ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass(); |
Fariborz Jahanian | cffff84 | 2007-11-12 20:20:37 +0000 | [diff] [blame] | 2142 | if (ClassDecl && CurMethodDecl->isInstance()) { |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame^] | 2143 | // Synthesize a cast to the super class. This hack allows us to loosely |
| 2144 | // represent super without creating a special expression node. |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2145 | IdentifierInfo &II = Context.Idents.get("self"); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame^] | 2146 | ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false); |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2147 | QualType superTy = Context.getObjcInterfaceType(ClassDecl); |
| 2148 | superTy = Context.getPointerType(superTy); |
| 2149 | ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(), |
| 2150 | SourceLocation(), ReceiverExpr.Val); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame^] | 2151 | // We are really in an instance method, redirect. |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2152 | return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2153 | Args, NumArgs); |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2154 | } |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame^] | 2155 | // We are sending a message to 'super' within a class method. Do nothing, |
| 2156 | // the receiver will pass through as 'super' (how convenient:-). |
| 2157 | } else |
Fariborz Jahanian | 0523aaf | 2007-11-12 20:13:27 +0000 | [diff] [blame] | 2158 | ClassDecl = getObjCInterfaceDecl(receiverName); |
Steve Naroff | 9bcb5fc | 2007-12-07 03:50:46 +0000 | [diff] [blame^] | 2159 | |
| 2160 | // FIXME: can ClassDecl ever be null? |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 2161 | ObjcMethodDecl *Method = ClassDecl->lookupClassMethod(Sel); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2162 | QualType returnType; |
Steve Naroff | 945c0a8 | 2007-11-05 15:27:52 +0000 | [diff] [blame] | 2163 | |
| 2164 | // Before we give up, check if the selector is an instance method. |
| 2165 | if (!Method) |
| 2166 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2167 | if (!Method) { |
| 2168 | Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(), |
| 2169 | SourceRange(lbrac, rbrac)); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 2170 | returnType = Context.getObjcIdType(); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2171 | } else { |
Steve Naroff | 3bea81b | 2007-10-16 21:36:54 +0000 | [diff] [blame] | 2172 | returnType = Method->getResultType(); |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2173 | if (Sel.getNumArgs()) { |
| 2174 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 2175 | return true; |
| 2176 | } |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2177 | } |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 2178 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2179 | lbrac, rbrac, ArgExprs, NumArgs); |
Steve Naroff | 708391a | 2007-09-17 21:01:15 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2182 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 2183 | // ArgExprs is optional - if it is present, the number of expressions |
| 2184 | // is obtained from Sel.getNumArgs(). |
| 2185 | Sema::ExprResult Sema::ActOnInstanceMessage( |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 2186 | ExprTy *receiver, Selector Sel, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2187 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 2188 | { |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2189 | assert(receiver && "missing receiver expression"); |
| 2190 | |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2191 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2192 | Expr *RExpr = static_cast<Expr *>(receiver); |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 2193 | QualType receiverType = RExpr->getType(); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2194 | QualType returnType; |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 2195 | ObjcMethodDecl *Method; |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2196 | |
Steve Naroff | 7c24915 | 2007-11-11 17:52:25 +0000 | [diff] [blame] | 2197 | if (receiverType == Context.getObjcIdType() || |
| 2198 | receiverType == Context.getObjcClassType()) { |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 2199 | Method = InstanceMethodPool[Sel].Method; |
Steve Naroff | 817da7c | 2007-11-13 04:10:18 +0000 | [diff] [blame] | 2200 | // If we didn't find an public method, look for a private one. |
| 2201 | if (!Method && CurMethodDecl) { |
| 2202 | NamedDecl *impCxt = CurMethodDecl->getMethodContext(); |
| 2203 | if (ObjcImplementationDecl *IMD = |
| 2204 | dyn_cast<ObjcImplementationDecl>(impCxt)) { |
| 2205 | if (receiverType == Context.getObjcIdType()) |
| 2206 | Method = IMD->lookupInstanceMethod(Sel); |
| 2207 | else |
| 2208 | Method = IMD->lookupClassMethod(Sel); |
| 2209 | } else if (ObjcCategoryImplDecl *CID = |
| 2210 | dyn_cast<ObjcCategoryImplDecl>(impCxt)) { |
| 2211 | if (receiverType == Context.getObjcIdType()) |
| 2212 | Method = CID->lookupInstanceMethod(Sel); |
| 2213 | else |
| 2214 | Method = CID->lookupClassMethod(Sel); |
| 2215 | } |
| 2216 | } |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2217 | if (!Method) { |
| 2218 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 2219 | SourceRange(lbrac, rbrac)); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 2220 | returnType = Context.getObjcIdType(); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2221 | } else { |
Steve Naroff | 3bea81b | 2007-10-16 21:36:54 +0000 | [diff] [blame] | 2222 | returnType = Method->getResultType(); |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2223 | if (Sel.getNumArgs()) |
| 2224 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 2225 | return true; |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2226 | } |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2227 | } else { |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 2228 | // FIXME (snaroff): checking in this code from Patrick. Needs to be |
| 2229 | // revisited. how do we get the ClassDecl from the receiver expression? |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2230 | while (receiverType->isPointerType()) { |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 2231 | PointerType *pointerType = |
| 2232 | static_cast<PointerType*>(receiverType.getTypePtr()); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2233 | receiverType = pointerType->getPointeeType(); |
| 2234 | } |
Chris Lattner | 22b73ba | 2007-10-10 23:42:28 +0000 | [diff] [blame] | 2235 | assert(ObjcInterfaceType::classof(receiverType.getTypePtr()) && |
| 2236 | "bad receiver type"); |
Steve Naroff | 3b95017 | 2007-10-10 21:53:07 +0000 | [diff] [blame] | 2237 | ObjcInterfaceDecl* ClassDecl = static_cast<ObjcInterfaceType*>( |
| 2238 | receiverType.getTypePtr())->getDecl(); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2239 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 2240 | // than the following method (which can do *many* linear searches). The |
| 2241 | // idea is to add class info to InstanceMethodPool... |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 2242 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2243 | if (!Method) { |
Steve Naroff | c43d868 | 2007-11-11 00:10:47 +0000 | [diff] [blame] | 2244 | // If we have an implementation in scope, check "private" methods. |
| 2245 | if (ObjcImplementationDecl *ImpDecl = |
| 2246 | ObjcImplementations[ClassDecl->getIdentifier()]) |
| 2247 | Method = ImpDecl->lookupInstanceMethod(Sel); |
| 2248 | } |
| 2249 | if (!Method) { |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2250 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 2251 | SourceRange(lbrac, rbrac)); |
Steve Naroff | 8ee529b | 2007-10-31 18:42:27 +0000 | [diff] [blame] | 2252 | returnType = Context.getObjcIdType(); |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2253 | } else { |
Steve Naroff | 3bea81b | 2007-10-16 21:36:54 +0000 | [diff] [blame] | 2254 | returnType = Method->getResultType(); |
Steve Naroff | 81bfde9 | 2007-10-16 23:12:48 +0000 | [diff] [blame] | 2255 | if (Sel.getNumArgs()) |
| 2256 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 2257 | return true; |
Steve Naroff | 983df5b | 2007-10-16 20:39:36 +0000 | [diff] [blame] | 2258 | } |
Steve Naroff | 6a8a9a4 | 2007-10-02 20:01:56 +0000 | [diff] [blame] | 2259 | } |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 2260 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 2261 | ArgExprs, NumArgs); |
Steve Naroff | 708391a | 2007-09-17 21:01:15 +0000 | [diff] [blame] | 2262 | } |