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