Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/LiteralSupport.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 27 | /// ParseStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 28 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 29 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 30 | /// multiple tokens. However, the common case is that StringToks points to one |
| 31 | /// string. |
| 32 | /// |
| 33 | Action::ExprResult |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 34 | Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 35 | assert(NumStringToks && "Must have at least one string!"); |
| 36 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 37 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 38 | if (Literal.hadError) |
| 39 | return ExprResult(true); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 41 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 42 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 43 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 44 | |
| 45 | // FIXME: handle wchar_t |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 46 | QualType t = Context.getPointerType(Context.CharTy); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 48 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 49 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 50 | Literal.AnyWide, t, StringToks[0].getLocation(), |
| 51 | StringToks[NumStringToks-1].getLocation()); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 54 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 55 | /// ParseIdentifierExpr - The parser read an identifier in expression context, |
| 56 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
| 57 | /// identifier is used in an function call context. |
| 58 | Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc, |
| 59 | IdentifierInfo &II, |
| 60 | bool HasTrailingLParen) { |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 61 | // Could be enum-constant or decl. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 62 | Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 63 | if (D == 0) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 64 | // Otherwise, this could be an implicitly declared function reference (legal |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 65 | // in C90, extension in C99). |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 66 | if (HasTrailingLParen && |
| 67 | // Not in C++. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 68 | !getLangOptions().CPlusPlus) |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 69 | D = ImplicitlyDefineFunction(Loc, II, S); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 70 | else { |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 71 | // If this name wasn't predeclared and if this is not a function call, |
| 72 | // diagnose the problem. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 73 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 74 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 77 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 78 | return new DeclRefExpr(VD, VD->getType(), Loc); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 79 | if (isa<TypedefDecl>(D)) |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 80 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
| 81 | |
| 82 | assert(0 && "Invalid decl"); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 85 | Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc, |
| 86 | tok::TokenKind Kind) { |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 87 | switch (Kind) { |
| 88 | default: |
| 89 | assert(0 && "Unknown simple primary expr!"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 90 | // TODO: MOVE this to be some other callback. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 91 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 92 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 93 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 94 | return 0; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 98 | Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 99 | llvm::SmallString<16> CharBuffer; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 100 | CharBuffer.resize(Tok.getLength()); |
| 101 | const char *ThisTokBegin = &CharBuffer[0]; |
| 102 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 103 | |
| 104 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 105 | Tok.getLocation(), PP); |
| 106 | if (Literal.hadError()) |
| 107 | return ExprResult(true); |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 108 | return new CharacterLiteral(Literal.getValue(), Context.IntTy, |
| 109 | Tok.getLocation()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 112 | Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 113 | // fast path for a single digit (which is quite common). A single digit |
| 114 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 115 | if (Tok.getLength() == 1) { |
| 116 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 117 | |
| 118 | unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation()); |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 119 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'), |
| 120 | Context.IntTy, |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 121 | Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 123 | llvm::SmallString<512> IntegerBuffer; |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 124 | IntegerBuffer.resize(Tok.getLength()); |
| 125 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 126 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 127 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 128 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 129 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 130 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 131 | if (Literal.hadError) |
| 132 | return ExprResult(true); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 133 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 134 | if (Literal.isIntegerLiteral()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 135 | QualType t; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 136 | |
| 137 | // Get the value in the widest-possible width. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 138 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 139 | |
| 140 | if (Literal.GetIntegerValue(ResultVal)) { |
| 141 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 142 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 143 | t = Context.UnsignedLongLongTy; |
| 144 | assert(Context.getIntegerBitwidth(t, Tok.getLocation()) == |
| 145 | ResultVal.getBitWidth() && "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 146 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 147 | // If this value fits into a ULL, try to figure out what else it fits into |
| 148 | // according to the rules of C99 6.4.4.1p5. |
| 149 | |
| 150 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 151 | // be an unsigned int. |
| 152 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 153 | |
| 154 | // Check from smallest to largest, picking the smallest type we can. |
| 155 | if (!Literal.isLong) { // Are int/unsigned possibilities? |
| 156 | unsigned IntSize = Context.Target.getIntWidth(Tok.getLocation()); |
| 157 | // Does it fit in a unsigned int? |
| 158 | if (ResultVal.isIntN(IntSize)) { |
| 159 | // Does it fit in a signed int? |
| 160 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| 161 | t = Context.IntTy; |
| 162 | else if (AllowUnsigned) |
| 163 | t = Context.UnsignedIntTy; |
| 164 | } |
| 165 | |
| 166 | if (!t.isNull()) |
| 167 | ResultVal.trunc(IntSize); |
| 168 | } |
| 169 | |
| 170 | // Are long/unsigned long possibilities? |
| 171 | if (t.isNull() && !Literal.isLongLong) { |
| 172 | unsigned LongSize = Context.Target.getLongWidth(Tok.getLocation()); |
| 173 | |
| 174 | // Does it fit in a unsigned long? |
| 175 | if (ResultVal.isIntN(LongSize)) { |
| 176 | // Does it fit in a signed long? |
| 177 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| 178 | t = Context.LongTy; |
| 179 | else if (AllowUnsigned) |
| 180 | t = Context.UnsignedLongTy; |
| 181 | } |
| 182 | if (!t.isNull()) |
| 183 | ResultVal.trunc(LongSize); |
| 184 | } |
| 185 | |
| 186 | // Finally, check long long if needed. |
| 187 | if (t.isNull()) { |
| 188 | unsigned LongLongSize = |
| 189 | Context.Target.getLongLongWidth(Tok.getLocation()); |
| 190 | |
| 191 | // Does it fit in a unsigned long long? |
| 192 | if (ResultVal.isIntN(LongLongSize)) { |
| 193 | // Does it fit in a signed long long? |
| 194 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
| 195 | t = Context.LongLongTy; |
| 196 | else if (AllowUnsigned) |
| 197 | t = Context.UnsignedLongLongTy; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // If we still couldn't decide a type, we probably have something that |
| 202 | // does not fit in a signed long long, but has no U suffix. |
| 203 | if (t.isNull()) { |
| 204 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| 205 | t = Context.UnsignedLongLongTy; |
| 206 | } |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 208 | |
| 209 | return new IntegerLiteral(ResultVal, t, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 210 | } else if (Literal.isFloatingLiteral()) { |
Steve Naroff | 97b9e91 | 2007-07-09 23:53:58 +0000 | [diff] [blame] | 211 | // FIXME: handle float values > 32 (including compute the real type...). |
| 212 | return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy, |
| 213 | Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 214 | } |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 215 | return ExprResult(true); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R, |
| 219 | ExprTy *Val) { |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 220 | Expr *e = (Expr *)Val; |
| 221 | assert((e != 0) && "ParseParenExpr() missing expr"); |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 222 | return new ParenExpr(L, R, e); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 225 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 226 | /// See C99 6.3.2.1p[2-4] for more details. |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 227 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 228 | SourceLocation OpLoc, bool isSizeof) { |
| 229 | // C99 6.5.3.4p1: |
| 230 | if (isa<FunctionType>(exprType) && isSizeof) |
| 231 | // alignof(function) is allowed. |
| 232 | Diag(OpLoc, diag::ext_sizeof_function_type); |
| 233 | else if (exprType->isVoidType()) |
| 234 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 235 | else if (exprType->isIncompleteType()) { |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 236 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
Chris Lattner | 3dc3d77 | 2007-05-16 18:07:12 +0000 | [diff] [blame] | 237 | diag::err_alignof_incomplete_type, |
| 238 | exprType.getAsString()); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 239 | return QualType(); // error |
| 240 | } |
| 241 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 242 | return Context.getSizeType(); |
| 243 | } |
| 244 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 245 | Action::ExprResult Sema:: |
| 246 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 247 | SourceLocation LPLoc, TypeTy *Ty, |
| 248 | SourceLocation RPLoc) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 249 | // If error parsing type, ignore. |
| 250 | if (Ty == 0) return true; |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 251 | |
| 252 | // Verify that this is a valid expression. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 253 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 254 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 255 | QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof); |
| 256 | |
| 257 | if (resultType.isNull()) |
| 258 | return true; |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 259 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 264 | tok::TokenKind Kind, |
| 265 | ExprTy *Input) { |
| 266 | UnaryOperator::Opcode Opc; |
| 267 | switch (Kind) { |
| 268 | default: assert(0 && "Unknown unary op!"); |
| 269 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 270 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 271 | } |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 272 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 273 | if (result.isNull()) |
| 274 | return true; |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 275 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | Action::ExprResult Sema:: |
| 279 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 280 | ExprTy *Idx, SourceLocation RLoc) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 281 | QualType t1 = ((Expr *)Base)->getType(); |
| 282 | QualType t2 = ((Expr *)Idx)->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 283 | |
| 284 | assert(!t1.isNull() && "no type for array base expression"); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 285 | assert(!t2.isNull() && "no type for array index expression"); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 286 | |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 287 | QualType canonT1 = DefaultFunctionArrayConversion(t1).getCanonicalType(); |
| 288 | QualType canonT2 = DefaultFunctionArrayConversion(t2).getCanonicalType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 289 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 290 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| 291 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 292 | // in the subscript position. As a result, we need to derive the array base |
| 293 | // and index from the expression types. |
| 294 | |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 295 | Expr *baseExpr, *indexExpr; |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 296 | QualType baseType, indexType; |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 297 | if (isa<PointerType>(canonT1) || isa<VectorType>(canonT1)) { |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 298 | baseType = canonT1; |
| 299 | indexType = canonT2; |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 300 | baseExpr = static_cast<Expr *>(Base); |
| 301 | indexExpr = static_cast<Expr *>(Idx); |
| 302 | } else if (isa<PointerType>(canonT2)) { // uncommon |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 303 | baseType = canonT2; |
| 304 | indexType = canonT1; |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 305 | baseExpr = static_cast<Expr *>(Idx); |
| 306 | indexExpr = static_cast<Expr *>(Base); |
| 307 | } else { |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 308 | return Diag(static_cast<Expr *>(Base)->getLocStart(), |
| 309 | diag::err_typecheck_subscript_value, |
| 310 | static_cast<Expr *>(Base)->getSourceRange()); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 311 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 312 | // C99 6.5.2.1p1 |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 313 | if (!indexType->isIntegerType()) { |
| 314 | return Diag(indexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 315 | indexExpr->getSourceRange()); |
| 316 | } |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 317 | QualType resultType; |
| 318 | if (PointerType *ary = dyn_cast<PointerType>(baseType)) { |
| 319 | // FIXME: need to deal with const... |
| 320 | resultType = ary->getPointeeType(); |
| 321 | // in practice, the following check catches trying to index a pointer |
| 322 | // to a function (e.g. void (*)(int)). Functions are not objects in c99. |
| 323 | if (!resultType->isObjectType()) { |
| 324 | return Diag(baseExpr->getLocStart(), |
| 325 | diag::err_typecheck_subscript_not_object, |
| 326 | baseType.getAsString(), baseExpr->getSourceRange()); |
| 327 | } |
| 328 | } else if (VectorType *vec = dyn_cast<VectorType>(baseType)) |
| 329 | resultType = vec->getElementType(); |
| 330 | |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 331 | return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | Action::ExprResult Sema:: |
| 335 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 336 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 337 | IdentifierInfo &Member) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 338 | QualType qualifiedType = ((Expr *)Base)->getType(); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 339 | |
| 340 | assert(!qualifiedType.isNull() && "no type for member expression"); |
| 341 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 342 | QualType canonType = qualifiedType.getCanonicalType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 343 | |
| 344 | if (OpKind == tok::arrow) { |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 345 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) { |
| 346 | qualifiedType = PT->getPointeeType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 347 | canonType = qualifiedType.getCanonicalType(); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 348 | } else |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 349 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow); |
| 350 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 351 | if (!isa<RecordType>(canonType)) |
| 352 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion); |
| 353 | |
| 354 | // get the struct/union definition from the type. |
| 355 | RecordDecl *RD = cast<RecordType>(canonType)->getDecl(); |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 356 | |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 357 | if (canonType->isIncompleteType()) |
| 358 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName()); |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 359 | |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 360 | FieldDecl *MemberDecl = RD->getMember(&Member); |
| 361 | if (!MemberDecl) |
| 362 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName()); |
| 363 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 364 | return new MemberExpr((Expr*)Base, OpKind == tok::arrow, |
| 365 | MemberDecl, MemberLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 369 | /// This provides the location of the left/right parens and a list of comma |
| 370 | /// locations. |
| 371 | Action::ExprResult Sema:: |
| 372 | ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 373 | ExprTy **Args, unsigned NumArgsInCall, |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 374 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 375 | Expr *funcExpr = (Expr *)Fn; |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 376 | assert(funcExpr && "no function call expression"); |
| 377 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 378 | QualType qType = UsualUnaryConversions(funcExpr); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 379 | assert(!qType.isNull() && "no type for function call expression"); |
| 380 | |
Chris Lattner | 3d01e4e | 2007-06-06 05:14:05 +0000 | [diff] [blame] | 381 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 382 | // type pointer to function". |
| 383 | const PointerType *PT = dyn_cast<PointerType>(qType); |
| 384 | if (PT == 0) PT = dyn_cast<PointerType>(qType.getCanonicalType()); |
| 385 | |
| 386 | if (PT == 0) |
| 387 | return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function, |
| 388 | SourceRange(funcExpr->getLocStart(), RParenLoc)); |
Chris Lattner | 3343f81 | 2007-06-06 05:05:41 +0000 | [diff] [blame] | 389 | |
| 390 | const FunctionType *funcT = dyn_cast<FunctionType>(PT->getPointeeType()); |
Chris Lattner | 3d01e4e | 2007-06-06 05:14:05 +0000 | [diff] [blame] | 391 | if (funcT == 0) |
| 392 | funcT = dyn_cast<FunctionType>(PT->getPointeeType().getCanonicalType()); |
| 393 | |
| 394 | if (funcT == 0) |
| 395 | return Diag(funcExpr->getLocStart(), diag::err_typecheck_call_not_function, |
| 396 | SourceRange(funcExpr->getLocStart(), RParenLoc)); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 397 | |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 398 | // If a prototype isn't declared, the parser implicitly defines a func decl |
| 399 | QualType resultType = funcT->getResultType(); |
| 400 | |
| 401 | if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) { |
| 402 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 403 | // assignment, to the types of the corresponding parameter, ... |
| 404 | |
| 405 | unsigned NumArgsInProto = proto->getNumArgs(); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 406 | unsigned NumArgsToCheck = NumArgsInCall; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 407 | |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 408 | if (NumArgsInCall < NumArgsInProto) |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 409 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 410 | funcExpr->getSourceRange()); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 411 | else if (NumArgsInCall > NumArgsInProto) { |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 412 | if (!proto->isVariadic()) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 413 | Diag(((Expr **)Args)[NumArgsInProto+1]->getLocStart(), |
| 414 | diag::err_typecheck_call_too_many_args, funcExpr->getSourceRange(), |
| 415 | ((Expr **)Args)[NumArgsInProto+1]->getSourceRange()); |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 416 | } |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 417 | NumArgsToCheck = NumArgsInProto; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 418 | } |
| 419 | // Continue to check argument types (even if we have too few/many args). |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 420 | for (unsigned i = 0; i < NumArgsToCheck; i++) { |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 421 | Expr *argExpr = ((Expr **)Args)[i]; |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 422 | assert(argExpr && "ParseCallExpr(): missing argument expression"); |
| 423 | |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 424 | QualType lhsType = proto->getArgType(i); |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 425 | QualType rhsType = argExpr->getType(); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 426 | |
| 427 | if (lhsType == rhsType) // common case, fast path... |
| 428 | continue; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 429 | |
| 430 | AssignmentCheckResult result = CheckAssignmentConstraints(lhsType, |
| 431 | rhsType); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 432 | SourceLocation l = argExpr->getLocStart(); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 433 | |
| 434 | // decode the result (notice that AST's are still created for extensions). |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 435 | switch (result) { |
| 436 | case Compatible: |
| 437 | break; |
| 438 | case PointerFromInt: |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 439 | // check for null pointer constant (C99 6.3.2.3p3) |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 440 | if (!argExpr->isNullPointerConstant()) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 441 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 442 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 443 | funcExpr->getSourceRange(), argExpr->getSourceRange()); |
| 444 | } |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 445 | break; |
| 446 | case IntFromPointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 447 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 448 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 449 | funcExpr->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 450 | break; |
| 451 | case IncompatiblePointer: |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 452 | Diag(l, diag::ext_typecheck_passing_incompatible_pointer, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 453 | rhsType.getAsString(), lhsType.getAsString(), |
| 454 | funcExpr->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 455 | break; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 456 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 457 | Diag(l, diag::ext_typecheck_passing_discards_qualifiers, |
| 458 | rhsType.getAsString(), lhsType.getAsString(), |
| 459 | funcExpr->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 460 | break; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 461 | case Incompatible: |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 462 | return Diag(l, diag::err_typecheck_passing_incompatible, |
| 463 | rhsType.getAsString(), lhsType.getAsString(), |
| 464 | funcExpr->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 467 | // Even if the types checked, bail if we had the wrong number of arguments. |
| 468 | if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic()) |
| 469 | return true; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 470 | } |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 471 | return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType, |
| 472 | RParenLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | Action::ExprResult Sema:: |
| 476 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 477 | SourceLocation RParenLoc, ExprTy *Op) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 478 | // If error parsing type, ignore. |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 479 | assert((Ty != 0) && "ParseCastExpr(): missing type"); |
Chris Lattner | 1d411a8 | 2007-06-05 20:52:21 +0000 | [diff] [blame] | 480 | // FIXME: Sema for cast is completely missing. |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 481 | return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 484 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 485 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | b66fb74 | 2007-07-13 17:39:21 +0000 | [diff] [blame^] | 486 | QualType condT = UsualUnaryConversions(cond); |
| 487 | QualType lexT = UsualUnaryConversions(lex); |
| 488 | QualType rexT = UsualUnaryConversions(rex); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 489 | |
| 490 | // first, check the condition. |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 491 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 492 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 493 | condT.getAsString()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 494 | return QualType(); |
| 495 | } |
| 496 | // now check the two expressions. |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 497 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) // C99 6.5.15p3,5 |
| 498 | return UsualArithmeticConversions(lex, rex); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 499 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 500 | if ((lexT->isStructureType() && rexT->isStructureType()) || // C99 6.5.15p3 |
| 501 | (lexT->isUnionType() && rexT->isUnionType())) { |
| 502 | TagType *lTag = cast<TagType>(lexT.getCanonicalType()); |
| 503 | TagType *rTag = cast<TagType>(rexT.getCanonicalType()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 504 | |
| 505 | if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier()) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 506 | return lexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 507 | else { |
| 508 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 509 | lexT.getAsString(), rexT.getAsString(), |
| 510 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 511 | return QualType(); |
| 512 | } |
| 513 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 514 | if (lexT->isPointerType() && rex->isNullPointerConstant()) // C99 6.5.15p3 |
| 515 | return lexT; |
| 516 | if (rexT->isPointerType() && lex->isNullPointerConstant()) |
| 517 | return rexT; |
Steve Naroff | 30d1fbc | 2007-05-20 19:46:53 +0000 | [diff] [blame] | 518 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 519 | if (lexT->isPointerType() && rexT->isPointerType()) { // C99 6.5.15p3,6 |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 520 | QualType lhptee, rhptee; |
| 521 | |
| 522 | // get the "pointed to" type |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 523 | lhptee = cast<PointerType>(lexT.getCanonicalType())->getPointeeType(); |
| 524 | rhptee = cast<PointerType>(rexT.getCanonicalType())->getPointeeType(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 525 | |
| 526 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 527 | if (lhptee.getUnqualifiedType()->isVoidType() && |
| 528 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 529 | return lexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 530 | if (rhptee.getUnqualifiedType()->isVoidType() && |
| 531 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 532 | return rexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 533 | |
| 534 | // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types |
| 535 | // *or* to differently qualified versions of compatible types, the result |
| 536 | // type is a pointer to an appropriately qualified version of the |
| 537 | // *composite* type. |
| 538 | if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 539 | rhptee.getUnqualifiedType())) { |
| 540 | Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 541 | lexT.getAsString(), rexT.getAsString(), |
| 542 | lex->getSourceRange(), rex->getSourceRange()); |
| 543 | return lexT; // FIXME: this is an _ext - is this return o.k? |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 546 | if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3 |
| 547 | return lexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 548 | |
| 549 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 550 | lexT.getAsString(), rexT.getAsString(), |
| 551 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 552 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 555 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 556 | /// in the case of a the GNU conditional expr extension. |
| 557 | Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc, |
| 558 | SourceLocation ColonLoc, |
| 559 | ExprTy *Cond, ExprTy *LHS, |
| 560 | ExprTy *RHS) { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 561 | QualType result = CheckConditionalOperands((Expr *&)Cond, (Expr *&)LHS, |
| 562 | (Expr *&)RHS, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 563 | if (result.isNull()) |
| 564 | return true; |
| 565 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Chris Lattner | e399420 | 2007-07-11 21:50:45 +0000 | [diff] [blame] | 568 | QualType Sema::DefaultFunctionArrayConversion(QualType t) { |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 569 | if (t->isFunctionType()) // C99 6.3.2.1p4 |
Chris Lattner | ea3c20b | 2007-06-02 22:47:04 +0000 | [diff] [blame] | 570 | return Context.getPointerType(t); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 571 | if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType())) |
| 572 | return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3 |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 573 | return t; |
| 574 | } |
| 575 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 576 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 577 | /// operators (C99 6.3). The conversions of array and function types are |
| 578 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 579 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 580 | /// In these instances, this routine should *not* be called. |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 581 | QualType Sema::UsualUnaryConversions(Expr *&expr) { |
| 582 | QualType t = expr->getType(); |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 583 | assert(!t.isNull() && "UsualUnaryConversions - missing type"); |
| 584 | |
| 585 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 586 | return Context.IntTy; |
| 587 | return DefaultFunctionArrayConversion(t); |
| 588 | } |
| 589 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 590 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 591 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 592 | /// routine returns the first non-arithmetic type found. The client is |
| 593 | /// responsible for emitting appropriate error diagnostics. |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 594 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) { |
| 595 | QualType lhs = UsualUnaryConversions(lhsExpr); |
| 596 | QualType rhs = UsualUnaryConversions(rhsExpr); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 598 | // If both types are identical, no conversion is needed. |
| 599 | if (lhs == rhs) |
| 600 | return lhs; |
| 601 | |
| 602 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 603 | // The caller can deal with this (e.g. pointer + int). |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 604 | if (!lhs->isArithmeticType()) |
| 605 | return lhs; |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 606 | if (!rhs->isArithmeticType()) |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 607 | return rhs; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 608 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 609 | // At this point, we have two different arithmetic types. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 610 | |
| 611 | // Handle complex types first (C99 6.3.1.8p1). |
| 612 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 613 | // if we have an integer operand, the result is the complex type. |
| 614 | if (rhs->isIntegerType()) |
| 615 | return lhs; |
| 616 | if (lhs->isIntegerType()) |
| 617 | return rhs; |
| 618 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 619 | return Context.maxComplexType(lhs, rhs); |
Steve Naroff | bf223ba | 2007-04-24 20:56:26 +0000 | [diff] [blame] | 620 | } |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 621 | |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 622 | // Now handle "real" floating types (i.e. float, double, long double). |
| 623 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 624 | // if we have an integer operand, the result is the real floating type. |
| 625 | if (rhs->isIntegerType()) |
| 626 | return lhs; |
| 627 | if (lhs->isIntegerType()) |
| 628 | return rhs; |
| 629 | |
| 630 | // we have two real floating types, float/complex combos were handled above. |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 631 | return Context.maxFloatingType(lhs, rhs); |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 632 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 633 | return Context.maxIntegerType(lhs, rhs); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 636 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 637 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 638 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 639 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 640 | // FIXME: add a couple examples in this comment. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 641 | Sema::AssignmentCheckResult |
| 642 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 643 | QualType lhptee, rhptee; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 644 | |
| 645 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 646 | lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType(); |
| 647 | rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType(); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 648 | |
| 649 | // make sure we operate on the canonical type |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 650 | lhptee = lhptee.getCanonicalType(); |
| 651 | rhptee = rhptee.getCanonicalType(); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 652 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 653 | AssignmentCheckResult r = Compatible; |
| 654 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 655 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 656 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 657 | // qualifiers of the type *pointed to* by the right; |
| 658 | if ((lhptee.getQualifiers() & rhptee.getQualifiers()) != |
| 659 | rhptee.getQualifiers()) |
| 660 | r = CompatiblePointerDiscardsQualifiers; |
| 661 | |
| 662 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 663 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 664 | // version of void... |
| 665 | if (lhptee.getUnqualifiedType()->isVoidType() && |
| 666 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 667 | ; |
| 668 | else if (rhptee.getUnqualifiedType()->isVoidType() && |
| 669 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 670 | ; |
| 671 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 672 | // unqualified versions of compatible types, ... |
| 673 | else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 674 | rhptee.getUnqualifiedType())) |
| 675 | r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 676 | return r; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 679 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 680 | /// has code to accommodate several GCC extensions when type checking |
| 681 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 682 | /// |
| 683 | /// int a, *pint; |
| 684 | /// short *pshort; |
| 685 | /// struct foo *pfoo; |
| 686 | /// |
| 687 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 688 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 689 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 690 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 691 | /// |
| 692 | /// As a result, the code for dealing with pointers is more complex than the |
| 693 | /// C99 spec dictates. |
| 694 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 695 | /// |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 696 | Sema::AssignmentCheckResult |
| 697 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 698 | // This check seems unnatural, however it is necessary to insure the proper |
| 699 | // conversion of functions/arrays. If the conversion were done for all |
| 700 | // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary |
| 701 | // expressions that surpress this implicit conversion (&, sizeof). |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 702 | rhsType = DefaultFunctionArrayConversion(rhsType); |
Steve Naroff | b891de3 | 2007-05-02 23:51:10 +0000 | [diff] [blame] | 703 | |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 704 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) { |
Steve Naroff | e728ba3 | 2007-07-10 22:20:04 +0000 | [diff] [blame] | 705 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
| 706 | if (lhsType.getCanonicalType() != rhsType.getCanonicalType()) |
| 707 | return Incompatible; |
| 708 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 709 | return Compatible; |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 710 | } else if (lhsType->isPointerType()) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 711 | if (rhsType->isIntegerType()) |
| 712 | return PointerFromInt; |
| 713 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 714 | if (rhsType->isPointerType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 715 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 716 | } else if (rhsType->isPointerType()) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 717 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 718 | if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy)) |
| 719 | return IntFromPointer; |
| 720 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 721 | if (lhsType->isPointerType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 722 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 723 | } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
| 724 | if (Type::tagTypesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 725 | return Compatible; |
Bill Wendling | db4f06e | 2007-06-02 23:29:59 +0000 | [diff] [blame] | 726 | } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
| 727 | if (Type::referenceTypesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 728 | return Compatible; |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 729 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 730 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 733 | inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 734 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 735 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 736 | lex->getSourceRange(), rex->getSourceRange()); |
| 737 | } |
| 738 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 739 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 740 | Expr *&rex) { |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 741 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 742 | |
| 743 | // make sure the vector types are identical. |
| 744 | if (lhsType == rhsType) |
| 745 | return lhsType; |
| 746 | // You cannot convert between vector values of different size. |
| 747 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 748 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 749 | lex->getSourceRange(), rex->getSourceRange()); |
| 750 | return QualType(); |
| 751 | } |
| 752 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 753 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 754 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 755 | { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 756 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 757 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 758 | |
| 759 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 760 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 761 | if (resType->isArithmeticType()) |
| 762 | return resType; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 763 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 764 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 767 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 768 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 769 | { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 770 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 771 | |
| 772 | if (resType->isIntegerType()) |
| 773 | return resType; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 774 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 775 | return QualType(); |
| 776 | } |
| 777 | |
| 778 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 779 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 780 | { |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 781 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 782 | |
| 783 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 784 | return CheckVectorOperands(loc, lex, rex); |
| 785 | |
| 786 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 4ae0ac6 | 2007-07-06 23:09:18 +0000 | [diff] [blame] | 787 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 788 | // handle the common case first (both operands are arithmetic). |
| 789 | if (resType->isArithmeticType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 790 | return resType; |
| 791 | |
| 792 | if ((lhsType->isPointerType() && rhsType->isIntegerType()) || |
| 793 | (lhsType->isIntegerType() && rhsType->isPointerType())) |
| 794 | return resType; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 795 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 796 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 799 | inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 800 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 801 | { |
| 802 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 803 | |
| 804 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
| 805 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 806 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 807 | |
| 808 | // handle the common case first (both operands are arithmetic). |
| 809 | if (resType->isArithmeticType()) |
| 810 | return resType; |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 811 | if (lhsType->isPointerType() && rhsType->isIntegerType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 812 | return resType; |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 813 | if (lhsType->isPointerType() && rhsType->isPointerType()) |
| 814 | return Context.getPointerDiffType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 815 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 816 | return QualType(); |
| 817 | } |
| 818 | |
| 819 | inline QualType Sema::CheckShiftOperands( // C99 6.5.7 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 820 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 821 | { |
Chris Lattner | 1d411a8 | 2007-06-05 20:52:21 +0000 | [diff] [blame] | 822 | // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong |
| 823 | // for int << longlong -> the result type should be int, not long long. |
Steve Naroff | d6fbee8 | 2007-06-13 15:42:33 +0000 | [diff] [blame] | 824 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 825 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 826 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 827 | if (resType->isIntegerType()) |
| 828 | return resType; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 829 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 830 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 833 | inline QualType Sema::CheckRelationalOperands( // C99 6.5.8 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 834 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 835 | { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 836 | QualType lType = UsualUnaryConversions(lex); |
| 837 | QualType rType = UsualUnaryConversions(rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 838 | |
| 839 | if (lType->isRealType() && rType->isRealType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 840 | return Context.IntTy; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 841 | |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 842 | if (lType->isPointerType()) { |
| 843 | if (rType->isPointerType()) |
| 844 | return Context.IntTy; |
| 845 | if (rType->isIntegerType()) { |
| 846 | if (!rex->isNullPointerConstant()) |
| 847 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 848 | lex->getSourceRange(), rex->getSourceRange()); |
| 849 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 850 | } |
| 851 | } else if (rType->isPointerType()) { |
| 852 | if (lType->isIntegerType()) { |
| 853 | if (!lex->isNullPointerConstant()) |
| 854 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 855 | lex->getSourceRange(), rex->getSourceRange()); |
| 856 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 857 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 858 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 859 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 860 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 863 | inline QualType Sema::CheckEqualityOperands( // C99 6.5.9 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 864 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 865 | { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 866 | QualType lType = UsualUnaryConversions(lex); |
| 867 | QualType rType = UsualUnaryConversions(rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 868 | |
| 869 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 870 | return Context.IntTy; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 871 | |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 872 | if (lType->isPointerType()) { |
| 873 | if (rType->isPointerType()) |
| 874 | return Context.IntTy; |
| 875 | if (rType->isIntegerType()) { |
| 876 | if (!rex->isNullPointerConstant()) |
| 877 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 878 | lex->getSourceRange(), rex->getSourceRange()); |
| 879 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 880 | } |
| 881 | } else if (rType->isPointerType()) { |
| 882 | if (lType->isIntegerType()) { |
| 883 | if (!lex->isNullPointerConstant()) |
| 884 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 885 | lex->getSourceRange(), rex->getSourceRange()); |
| 886 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 887 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 888 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 889 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 890 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 893 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 894 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 895 | { |
Steve Naroff | d6fbee8 | 2007-06-13 15:42:33 +0000 | [diff] [blame] | 896 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 897 | |
| 898 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
| 899 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 900 | QualType resType = UsualArithmeticConversions(lex, rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 901 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 902 | if (resType->isIntegerType()) |
| 903 | return resType; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 904 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 905 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 908 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 909 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 910 | { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 911 | QualType lhsType = UsualUnaryConversions(lex); |
| 912 | QualType rhsType = UsualUnaryConversions(rex); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 913 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 914 | if (lhsType->isScalarType() || rhsType->isScalarType()) |
| 915 | return Context.IntTy; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 916 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 917 | return QualType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 920 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
| 921 | Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType) |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 922 | { |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 923 | QualType lhsType = lex->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 924 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 925 | bool hadError = false; |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 926 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(); |
| 927 | |
| 928 | switch (mlval) { // C99 6.5.16p2 |
| 929 | case Expr::MLV_Valid: |
| 930 | break; |
| 931 | case Expr::MLV_ConstQualified: |
| 932 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 933 | hadError = true; |
| 934 | break; |
| 935 | case Expr::MLV_ArrayType: |
| 936 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 937 | lhsType.getAsString(), lex->getSourceRange()); |
| 938 | return QualType(); |
| 939 | case Expr::MLV_NotObjectType: |
| 940 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 941 | lhsType.getAsString(), lex->getSourceRange()); |
| 942 | return QualType(); |
| 943 | case Expr::MLV_InvalidExpression: |
| 944 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 945 | lex->getSourceRange()); |
| 946 | return QualType(); |
| 947 | case Expr::MLV_IncompleteType: |
| 948 | case Expr::MLV_IncompleteVoidType: |
| 949 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 950 | lhsType.getAsString(), lex->getSourceRange()); |
| 951 | return QualType(); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 952 | } |
| 953 | if (lhsType == rhsType) // common case, fast path... |
| 954 | return lhsType; |
| 955 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 956 | AssignmentCheckResult result = CheckAssignmentConstraints(lhsType, rhsType); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 957 | |
| 958 | // decode the result (notice that extensions still return a type). |
| 959 | switch (result) { |
| 960 | case Compatible: |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 961 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 962 | case Incompatible: |
Steve Naroff | e845e27 | 2007-05-18 01:06:45 +0000 | [diff] [blame] | 963 | Diag(loc, diag::err_typecheck_assign_incompatible, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 964 | lhsType.getAsString(), rhsType.getAsString(), |
| 965 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 966 | hadError = true; |
| 967 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 968 | case PointerFromInt: |
| 969 | // check for null pointer constant (C99 6.3.2.3p3) |
Steve Naroff | 9992bba | 2007-05-30 16:27:15 +0000 | [diff] [blame] | 970 | if (compoundType.isNull() && !rex->isNullPointerConstant()) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 971 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 972 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 973 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 9992bba | 2007-05-30 16:27:15 +0000 | [diff] [blame] | 974 | } |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 975 | break; |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 976 | case IntFromPointer: |
| 977 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 978 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 979 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 980 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 981 | case IncompatiblePointer: |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 982 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer, |
| 983 | lhsType.getAsString(), rhsType.getAsString(), |
| 984 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 985 | break; |
| 986 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 987 | Diag(loc, diag::ext_typecheck_assign_discards_qualifiers, |
| 988 | lhsType.getAsString(), rhsType.getAsString(), |
| 989 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 990 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 991 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 992 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 993 | // left operand unless the left operand has qualified type, in which case |
| 994 | // it is the unqualified version of the type of the left operand. |
| 995 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 996 | // is converted to the type of the assignment expression (above). |
| 997 | // C++ 5.17p1: the type of the assignment expression is that of its left oprdu. |
| 998 | return hadError ? QualType() : lhsType.getUnqualifiedType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1001 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1002 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
| 1003 | return UsualUnaryConversions(rex); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1006 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 1007 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1008 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1009 | QualType resType = op->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1010 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 1011 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 1012 | // C99 6.5.2.4p1 |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1013 | if (const PointerType *pt = dyn_cast<PointerType>(resType)) { |
| 1014 | if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1015 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1016 | resType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1017 | return QualType(); |
| 1018 | } |
| 1019 | } else if (!resType->isRealType()) { |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 1020 | // FIXME: Allow Complex as a GCC extension. |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1021 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 1022 | resType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1023 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 1024 | } |
Steve Naroff | 094046f | 2007-05-13 03:21:25 +0000 | [diff] [blame] | 1025 | // At this point, we know we have a real or pointer type. Now make sure |
| 1026 | // the operand is a modifiable lvalue. |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1027 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(); |
| 1028 | if (mlval != Expr::MLV_Valid) { |
| 1029 | // FIXME: emit a more precise diagnostic... |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1030 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 1031 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1032 | return QualType(); |
| 1033 | } |
| 1034 | return resType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1037 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1038 | /// This routine allows us to typecheck complex/recursive expressions |
| 1039 | /// where the declaration is needed for type checking. Here are some |
| 1040 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1041 | static Decl *getPrimaryDeclaration(Expr *e) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1042 | switch (e->getStmtClass()) { |
| 1043 | case Stmt::DeclRefExprClass: |
| 1044 | return cast<DeclRefExpr>(e)->getDecl(); |
| 1045 | case Stmt::MemberExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1046 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1047 | case Stmt::ArraySubscriptExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1048 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1049 | case Stmt::CallExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1050 | return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1051 | case Stmt::UnaryOperatorClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1052 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1053 | case Stmt::ParenExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1054 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1055 | default: |
| 1056 | return 0; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 1061 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 1062 | /// object cannot be declared with storage class register or be a bit field. |
| 1063 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 1064 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1065 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1066 | Decl *dcl = getPrimaryDeclaration(op); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1067 | Expr::isLvalueResult lval = op->isLvalue(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1068 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1069 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 1070 | if (dcl && isa<FunctionDecl>(dcl)) // allow function designators |
| 1071 | ; |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1072 | else { // FIXME: emit more specific diag... |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1073 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 1074 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1075 | return QualType(); |
| 1076 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1077 | } else if (dcl) { |
| 1078 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 1079 | // with the register storage-class specifier. |
| 1080 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1081 | if (vd->getStorageClass() == VarDecl::Register) { |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1082 | Diag(OpLoc, diag::err_typecheck_address_of_register, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 1083 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1084 | return QualType(); |
| 1085 | } |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 1086 | } else |
| 1087 | assert(0 && "Unknown/unexpected decl type"); |
| 1088 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1089 | // FIXME: add check for bitfields! |
| 1090 | } |
| 1091 | // If the operand has type "type", the result has type "pointer to type". |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1092 | return Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1095 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1096 | QualType qType = UsualUnaryConversions(op); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1097 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1098 | assert(!qType.isNull() && "no type for * expression"); |
| 1099 | |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1100 | if (PointerType *PT = dyn_cast<PointerType>(qType.getCanonicalType())) { |
| 1101 | QualType ptype = PT->getPointeeType(); |
| 1102 | // C99 6.5.3.2p4. "if it points to an object,...". |
| 1103 | if (ptype->isIncompleteType()) { // An incomplete type is not an object |
| 1104 | // GCC compat: special case 'void *' (treat as warning). |
| 1105 | if (ptype->isVoidType()) { |
| 1106 | Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1107 | qType.getAsString(), op->getSourceRange()); |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1108 | } else { |
| 1109 | Diag(OpLoc, diag::err_typecheck_deref_incomplete_type, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1110 | ptype.getAsString(), op->getSourceRange()); |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1111 | return QualType(); |
| 1112 | } |
| 1113 | } |
| 1114 | return ptype; |
| 1115 | } |
| 1116 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1117 | qType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1118 | return QualType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1119 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1120 | |
| 1121 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 1122 | tok::TokenKind Kind) { |
| 1123 | BinaryOperator::Opcode Opc; |
| 1124 | switch (Kind) { |
| 1125 | default: assert(0 && "Unknown binop!"); |
| 1126 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 1127 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 1128 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 1129 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 1130 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 1131 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 1132 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 1133 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 1134 | case tok::less: Opc = BinaryOperator::LT; break; |
| 1135 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 1136 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 1137 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 1138 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 1139 | case tok::amp: Opc = BinaryOperator::And; break; |
| 1140 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 1141 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 1142 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 1143 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 1144 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 1145 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 1146 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 1147 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 1148 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 1149 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 1150 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 1151 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 1152 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 1153 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 1154 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 1155 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 1156 | } |
| 1157 | return Opc; |
| 1158 | } |
| 1159 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1160 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 1161 | tok::TokenKind Kind) { |
| 1162 | UnaryOperator::Opcode Opc; |
| 1163 | switch (Kind) { |
| 1164 | default: assert(0 && "Unknown unary op!"); |
| 1165 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 1166 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 1167 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 1168 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 1169 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 1170 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 1171 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 1172 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 1173 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 1174 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 1175 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 1176 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
Chris Lattner | d0f7651 | 2007-06-08 22:16:53 +0000 | [diff] [blame] | 1177 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1178 | } |
| 1179 | return Opc; |
| 1180 | } |
| 1181 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1182 | // Binary Operators. 'Tok' is the token for the operator. |
| 1183 | Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 1184 | ExprTy *LHS, ExprTy *RHS) { |
| 1185 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 1186 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 1187 | |
| 1188 | assert((lhs != 0) && "ParseBinOp(): missing left expression"); |
| 1189 | assert((rhs != 0) && "ParseBinOp(): missing right expression"); |
| 1190 | |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1191 | QualType ResultTy; // Result type of the binary operator. |
| 1192 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1193 | |
| 1194 | switch (Opc) { |
| 1195 | default: |
| 1196 | assert(0 && "Unknown binary expr!"); |
| 1197 | case BinaryOperator::Assign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1198 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | case BinaryOperator::Mul: |
| 1201 | case BinaryOperator::Div: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1202 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1203 | break; |
| 1204 | case BinaryOperator::Rem: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1205 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1206 | break; |
| 1207 | case BinaryOperator::Add: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1208 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1209 | break; |
| 1210 | case BinaryOperator::Sub: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1211 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | case BinaryOperator::Shl: |
| 1214 | case BinaryOperator::Shr: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1215 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1216 | break; |
| 1217 | case BinaryOperator::LE: |
| 1218 | case BinaryOperator::LT: |
| 1219 | case BinaryOperator::GE: |
| 1220 | case BinaryOperator::GT: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1221 | ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1222 | break; |
| 1223 | case BinaryOperator::EQ: |
| 1224 | case BinaryOperator::NE: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1225 | ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | case BinaryOperator::And: |
| 1228 | case BinaryOperator::Xor: |
| 1229 | case BinaryOperator::Or: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1230 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1231 | break; |
| 1232 | case BinaryOperator::LAnd: |
| 1233 | case BinaryOperator::LOr: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1234 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1235 | break; |
| 1236 | case BinaryOperator::MulAssign: |
| 1237 | case BinaryOperator::DivAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1238 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1239 | if (!CompTy.isNull()) |
| 1240 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1241 | break; |
| 1242 | case BinaryOperator::RemAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1243 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1244 | if (!CompTy.isNull()) |
| 1245 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | case BinaryOperator::AddAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1248 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1249 | if (!CompTy.isNull()) |
| 1250 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1251 | break; |
| 1252 | case BinaryOperator::SubAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1253 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1254 | if (!CompTy.isNull()) |
| 1255 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1256 | break; |
| 1257 | case BinaryOperator::ShlAssign: |
| 1258 | case BinaryOperator::ShrAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1259 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1260 | if (!CompTy.isNull()) |
| 1261 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1262 | break; |
| 1263 | case BinaryOperator::AndAssign: |
| 1264 | case BinaryOperator::XorAssign: |
| 1265 | case BinaryOperator::OrAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1266 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1267 | if (!CompTy.isNull()) |
| 1268 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1269 | break; |
| 1270 | case BinaryOperator::Comma: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1271 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1272 | break; |
| 1273 | } |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1274 | if (ResultTy.isNull()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1275 | return true; |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1276 | if (CompTy.isNull()) |
| 1277 | return new BinaryOperator(lhs, rhs, Opc, ResultTy); |
| 1278 | else |
Chris Lattner | 9369a56 | 2007-06-29 16:31:29 +0000 | [diff] [blame] | 1279 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1282 | // Unary Operators. 'Tok' is the token for the operator. |
| 1283 | Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1284 | ExprTy *input) { |
| 1285 | Expr *Input = (Expr*)input; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1286 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 1287 | QualType resultType; |
| 1288 | switch (Opc) { |
| 1289 | default: |
| 1290 | assert(0 && "Unimplemented unary expr!"); |
| 1291 | case UnaryOperator::PreInc: |
| 1292 | case UnaryOperator::PreDec: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1293 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1294 | break; |
| 1295 | case UnaryOperator::AddrOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1296 | resultType = CheckAddressOfOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1297 | break; |
| 1298 | case UnaryOperator::Deref: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1299 | resultType = CheckIndirectionOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1300 | break; |
| 1301 | case UnaryOperator::Plus: |
| 1302 | case UnaryOperator::Minus: |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1303 | resultType = UsualUnaryConversions(Input); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1304 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 1305 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1306 | resultType.getAsString()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1307 | break; |
| 1308 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1309 | resultType = UsualUnaryConversions(Input); |
Steve Naroff | 1c8b7d3 | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 1310 | if (!resultType->isIntegerType()) // C99 6.5.3.3p1 |
| 1311 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1312 | resultType.getAsString()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1313 | break; |
| 1314 | case UnaryOperator::LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 1315 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1316 | resultType = DefaultFunctionArrayConversion(Input->getType()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1317 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 1318 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1319 | resultType.getAsString()); |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 1320 | // LNot always has type int. C99 6.5.3.3p5. |
| 1321 | resultType = Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | case UnaryOperator::SizeOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1324 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1325 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1326 | case UnaryOperator::AlignOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1327 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false); |
| 1328 | break; |
| 1329 | case UnaryOperator::Extension: |
| 1330 | // FIXME: does __extension__ cause any promotions? I would think not. |
| 1331 | resultType = Input->getType(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1332 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1333 | } |
| 1334 | if (resultType.isNull()) |
| 1335 | return true; |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1336 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1337 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 1338 | |
| 1339 | /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 1340 | Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc, |
| 1341 | SourceLocation LabLoc, |
| 1342 | IdentifierInfo *LabelII) { |
| 1343 | // Look up the record for this label identifier. |
| 1344 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 1345 | |
| 1346 | // If we haven't seen this label yet, create a forward reference. |
| 1347 | if (LabelDecl == 0) |
| 1348 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 1349 | |
| 1350 | // Create the AST node. The address of a label always has type 'void*'. |
| 1351 | return new AddrLabel(OpLoc, LabLoc, LabelDecl, |
| 1352 | Context.getPointerType(Context.VoidTy)); |
| 1353 | } |
| 1354 | |