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 |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 34 | Sema::ParseStringLiteral(const Token *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 | bb0ab46 | 2007-07-21 04:57:45 +0000 | [diff] [blame] | 83 | abort(); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 85 | |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 86 | Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc, |
| 87 | tok::TokenKind Kind) { |
| 88 | PreDefinedExpr::IdentType IT; |
| 89 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 90 | switch (Kind) { |
| 91 | default: |
| 92 | assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 93 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 94 | IT = PreDefinedExpr::Func; |
| 95 | break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 96 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 97 | IT = PreDefinedExpr::Function; |
| 98 | break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 99 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 100 | IT = PreDefinedExpr::PrettyFunction; |
| 101 | break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 102 | } |
Anders Carlsson | 625bfc8 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 103 | |
| 104 | // Pre-defined identifiers are always of type char *. |
| 105 | return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 108 | Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 109 | llvm::SmallString<16> CharBuffer; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 110 | CharBuffer.resize(Tok.getLength()); |
| 111 | const char *ThisTokBegin = &CharBuffer[0]; |
| 112 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 113 | |
| 114 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 115 | Tok.getLocation(), PP); |
| 116 | if (Literal.hadError()) |
| 117 | return ExprResult(true); |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 118 | return new CharacterLiteral(Literal.getValue(), Context.IntTy, |
| 119 | Tok.getLocation()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 122 | Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 123 | // fast path for a single digit (which is quite common). A single digit |
| 124 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 125 | if (Tok.getLength() == 1) { |
| 126 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 128 | unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation()); |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 129 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'), |
| 130 | Context.IntTy, |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 131 | Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 132 | } |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 133 | llvm::SmallString<512> IntegerBuffer; |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 134 | IntegerBuffer.resize(Tok.getLength()); |
| 135 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 136 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 137 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 138 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 139 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 140 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 141 | if (Literal.hadError) |
| 142 | return ExprResult(true); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 143 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 144 | if (Literal.isIntegerLiteral()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 145 | QualType t; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 146 | |
| 147 | // Get the value in the widest-possible width. |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 148 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 149 | |
| 150 | if (Literal.GetIntegerValue(ResultVal)) { |
| 151 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 152 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 153 | t = Context.UnsignedLongLongTy; |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 154 | assert(Context.getTypeSize(t, Tok.getLocation()) == |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 155 | ResultVal.getBitWidth() && "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 156 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 157 | // If this value fits into a ULL, try to figure out what else it fits into |
| 158 | // according to the rules of C99 6.4.4.1p5. |
| 159 | |
| 160 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 161 | // be an unsigned int. |
| 162 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 163 | |
| 164 | // Check from smallest to largest, picking the smallest type we can. |
| 165 | if (!Literal.isLong) { // Are int/unsigned possibilities? |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 166 | unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation()); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 167 | // Does it fit in a unsigned int? |
| 168 | if (ResultVal.isIntN(IntSize)) { |
| 169 | // Does it fit in a signed int? |
| 170 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| 171 | t = Context.IntTy; |
| 172 | else if (AllowUnsigned) |
| 173 | t = Context.UnsignedIntTy; |
| 174 | } |
| 175 | |
| 176 | if (!t.isNull()) |
| 177 | ResultVal.trunc(IntSize); |
| 178 | } |
| 179 | |
| 180 | // Are long/unsigned long possibilities? |
| 181 | if (t.isNull() && !Literal.isLongLong) { |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 182 | unsigned LongSize = Context.getTypeSize(Context.LongTy, |
| 183 | Tok.getLocation()); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 184 | |
| 185 | // Does it fit in a unsigned long? |
| 186 | if (ResultVal.isIntN(LongSize)) { |
| 187 | // Does it fit in a signed long? |
| 188 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| 189 | t = Context.LongTy; |
| 190 | else if (AllowUnsigned) |
| 191 | t = Context.UnsignedLongTy; |
| 192 | } |
| 193 | if (!t.isNull()) |
| 194 | ResultVal.trunc(LongSize); |
| 195 | } |
| 196 | |
| 197 | // Finally, check long long if needed. |
| 198 | if (t.isNull()) { |
| 199 | unsigned LongLongSize = |
Chris Lattner | 4481b42 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 200 | Context.getTypeSize(Context.LongLongTy, Tok.getLocation()); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 201 | |
| 202 | // Does it fit in a unsigned long long? |
| 203 | if (ResultVal.isIntN(LongLongSize)) { |
| 204 | // Does it fit in a signed long long? |
| 205 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
| 206 | t = Context.LongLongTy; |
| 207 | else if (AllowUnsigned) |
| 208 | t = Context.UnsignedLongLongTy; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // If we still couldn't decide a type, we probably have something that |
| 213 | // does not fit in a signed long long, but has no U suffix. |
| 214 | if (t.isNull()) { |
| 215 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| 216 | t = Context.UnsignedLongLongTy; |
| 217 | } |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 219 | |
| 220 | return new IntegerLiteral(ResultVal, t, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 221 | } else if (Literal.isFloatingLiteral()) { |
Steve Naroff | 97b9e91 | 2007-07-09 23:53:58 +0000 | [diff] [blame] | 222 | // FIXME: handle float values > 32 (including compute the real type...). |
| 223 | return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy, |
| 224 | Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 225 | } |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 226 | return ExprResult(true); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R, |
| 230 | ExprTy *Val) { |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 231 | Expr *e = (Expr *)Val; |
| 232 | assert((e != 0) && "ParseParenExpr() missing expr"); |
Steve Naroff | 53f07dc | 2007-05-17 21:49:33 +0000 | [diff] [blame] | 233 | return new ParenExpr(L, R, e); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 236 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 237 | /// See C99 6.3.2.1p[2-4] for more details. |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 238 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 239 | SourceLocation OpLoc, bool isSizeof) { |
| 240 | // C99 6.5.3.4p1: |
| 241 | if (isa<FunctionType>(exprType) && isSizeof) |
| 242 | // alignof(function) is allowed. |
| 243 | Diag(OpLoc, diag::ext_sizeof_function_type); |
| 244 | else if (exprType->isVoidType()) |
| 245 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 246 | else if (exprType->isIncompleteType()) { |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 247 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
Chris Lattner | 3dc3d77 | 2007-05-16 18:07:12 +0000 | [diff] [blame] | 248 | diag::err_alignof_incomplete_type, |
| 249 | exprType.getAsString()); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 250 | return QualType(); // error |
| 251 | } |
| 252 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 253 | return Context.getSizeType(); |
| 254 | } |
| 255 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 256 | Action::ExprResult Sema:: |
| 257 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 258 | SourceLocation LPLoc, TypeTy *Ty, |
| 259 | SourceLocation RPLoc) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 260 | // If error parsing type, ignore. |
| 261 | if (Ty == 0) return true; |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 262 | |
| 263 | // Verify that this is a valid expression. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 264 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 265 | |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 266 | QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof); |
| 267 | |
| 268 | if (resultType.isNull()) |
| 269 | return true; |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 270 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 275 | tok::TokenKind Kind, |
| 276 | ExprTy *Input) { |
| 277 | UnaryOperator::Opcode Opc; |
| 278 | switch (Kind) { |
| 279 | default: assert(0 && "Unknown unary op!"); |
| 280 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 281 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 282 | } |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 283 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 284 | if (result.isNull()) |
| 285 | return true; |
Steve Naroff | 509fe02 | 2007-05-17 01:16:00 +0000 | [diff] [blame] | 286 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | Action::ExprResult Sema:: |
| 290 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 291 | ExprTy *Idx, SourceLocation RLoc) { |
Chris Lattner | 5981db4 | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 292 | Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx); |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 293 | |
| 294 | // Perform default conversions. |
| 295 | DefaultFunctionArrayConversion(LHSExp); |
| 296 | DefaultFunctionArrayConversion(RHSExp); |
Chris Lattner | 5981db4 | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 298 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 299 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 300 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| 301 | // 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] | 302 | // in the subscript position. As a result, we need to derive the array base |
| 303 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 304 | Expr *BaseExpr, *IndexExpr; |
| 305 | QualType ResultType; |
Chris Lattner | c996b17 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 306 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 307 | BaseExpr = LHSExp; |
| 308 | IndexExpr = RHSExp; |
| 309 | // FIXME: need to deal with const... |
| 310 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c996b17 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 311 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 312 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 313 | BaseExpr = RHSExp; |
| 314 | IndexExpr = LHSExp; |
| 315 | // FIXME: need to deal with const... |
| 316 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 317 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 318 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 319 | IndexExpr = RHSExp; |
| 320 | // FIXME: need to deal with const... |
| 321 | ResultType = VTy->getElementType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 322 | } else { |
Chris Lattner | 5981db4 | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 323 | return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value, |
| 324 | RHSExp->getSourceRange()); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 325 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 326 | // C99 6.5.2.1p1 |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 327 | if (!IndexExpr->getType()->isIntegerType()) |
| 328 | return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 329 | IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 331 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 332 | // the following check catches trying to index a pointer to a function (e.g. |
| 333 | // void (*)(int)). Functions are not objects in C99. |
| 334 | if (!ResultType->isObjectType()) |
| 335 | return Diag(BaseExpr->getLocStart(), |
| 336 | diag::err_typecheck_subscript_not_object, |
| 337 | BaseExpr->getType().getAsString(), BaseExpr->getSourceRange()); |
| 338 | |
| 339 | return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 342 | QualType Sema:: |
| 343 | CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc, |
| 344 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 345 | const OCUVectorType *vecType = baseType->getAsOCUVectorType(); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 346 | |
| 347 | // The vector accessor can't exceed the number of elements. |
| 348 | const char *compStr = CompName.getName(); |
| 349 | if (strlen(compStr) > vecType->getNumElements()) { |
| 350 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 351 | baseType.getAsString(), SourceRange(CompLoc)); |
| 352 | return QualType(); |
| 353 | } |
| 354 | // The component names must come from the same set. |
| 355 | if (vecType->isPointAccessor(*compStr)) |
| 356 | do { compStr++; } while (*compStr && vecType->isPointAccessor(*compStr)); |
| 357 | else if (vecType->isColorAccessor(*compStr)) |
| 358 | do { compStr++; } while (*compStr && vecType->isColorAccessor(*compStr)); |
| 359 | else if (vecType->isTextureAccessor(*compStr)) |
| 360 | do { compStr++; } while (*compStr && vecType->isTextureAccessor(*compStr)); |
| 361 | |
| 362 | if (*compStr) { |
| 363 | // We didn't get to the end of the string. This means the component names |
| 364 | // didn't come from the same set *or* we encountered an illegal name. |
| 365 | Diag(OpLoc, diag::err_ocuvector_component_name_illegal, |
| 366 | std::string(compStr,compStr+1), SourceRange(CompLoc)); |
| 367 | return QualType(); |
| 368 | } |
| 369 | // Each component accessor can't exceed the vector type. |
| 370 | compStr = CompName.getName(); |
| 371 | while (*compStr) { |
| 372 | if (vecType->isAccessorWithinNumElements(*compStr)) |
| 373 | compStr++; |
| 374 | else |
| 375 | break; |
| 376 | } |
| 377 | if (*compStr) { |
| 378 | // We didn't get to the end of the string. This means a component accessor |
| 379 | // exceeds the number of elements in the vector. |
| 380 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 381 | baseType.getAsString(), SourceRange(CompLoc)); |
| 382 | return QualType(); |
| 383 | } |
| 384 | // The component accessor looks fine - now we need to compute the actual type. |
| 385 | // The vector type is implied by the component accessor. For example, |
| 386 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| 387 | unsigned CompSize = strlen(CompName.getName()); |
| 388 | if (CompSize == 1) |
| 389 | return vecType->getElementType(); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 390 | |
| 391 | QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize); |
| 392 | // Now look up the TypeDefDecl from the vector type. Without this, |
| 393 | // diagostics look bad. We want OCU vector types to appear built-in. |
| 394 | for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) { |
| 395 | if (OCUVectorDecls[i]->getUnderlyingType() == VT) |
| 396 | return Context.getTypedefType(OCUVectorDecls[i]); |
| 397 | } |
| 398 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 401 | Action::ExprResult Sema:: |
| 402 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 403 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 404 | IdentifierInfo &Member) { |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 405 | Expr *BaseExpr = static_cast<Expr *>(Base); |
| 406 | assert(BaseExpr && "no record expression"); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 407 | |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 408 | QualType BaseType = BaseExpr->getType(); |
| 409 | assert(!BaseType.isNull() && "no type for member expression"); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 410 | |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 411 | if (OpKind == tok::arrow) { |
Chris Lattner | c996b17 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 412 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 413 | BaseType = PT->getPointeeType(); |
| 414 | else |
| 415 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow, |
| 416 | SourceRange(MemberLoc)); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 417 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 418 | // The base type is either a record or an OCUVectorType. |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 419 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 420 | RecordDecl *RDecl = RTy->getDecl(); |
| 421 | if (RTy->isIncompleteType()) |
| 422 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(), |
| 423 | BaseExpr->getSourceRange()); |
| 424 | // The record definition is complete, now make sure the member is valid. |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 425 | FieldDecl *MemberDecl = RDecl->getMember(&Member); |
| 426 | if (!MemberDecl) |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 427 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(), |
| 428 | SourceRange(MemberLoc)); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 429 | return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc); |
| 430 | } else if (BaseType->isOCUVectorType() && OpKind == tok::period) { |
| 431 | QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 432 | if (ret.isNull()) |
| 433 | return true; |
Steve Naroff | f7a5da1 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 434 | return new OCUVectorComponent(ret, BaseExpr, Member, MemberLoc); |
Steve Naroff | 185616f | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 435 | } else |
| 436 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion, |
| 437 | SourceRange(MemberLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 441 | /// This provides the location of the left/right parens and a list of comma |
| 442 | /// locations. |
| 443 | Action::ExprResult Sema:: |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 444 | ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
| 445 | ExprTy **args, unsigned NumArgsInCall, |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 446 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 447 | Expr *Fn = static_cast<Expr *>(fn); |
| 448 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 449 | assert(Fn && "no function call expression"); |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 450 | |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 451 | UsualUnaryConversions(Fn); |
| 452 | QualType funcType = Fn->getType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 3d01e4e | 2007-06-06 05:14:05 +0000 | [diff] [blame] | 454 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 455 | // type pointer to function". |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 456 | const PointerType *PT = funcType->getAsPointerType(); |
Chris Lattner | 3d01e4e | 2007-06-06 05:14:05 +0000 | [diff] [blame] | 457 | if (PT == 0) |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 458 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 459 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Chris Lattner | 3343f81 | 2007-06-06 05:05:41 +0000 | [diff] [blame] | 460 | |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 461 | const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType(); |
Chris Lattner | 3d01e4e | 2007-06-06 05:14:05 +0000 | [diff] [blame] | 462 | if (funcT == 0) |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 463 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 464 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 465 | |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 466 | // If a prototype isn't declared, the parser implicitly defines a func decl |
| 467 | QualType resultType = funcT->getResultType(); |
| 468 | |
| 469 | if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) { |
| 470 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 471 | // assignment, to the types of the corresponding parameter, ... |
| 472 | |
| 473 | unsigned NumArgsInProto = proto->getNumArgs(); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 474 | unsigned NumArgsToCheck = NumArgsInCall; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 475 | |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 476 | if (NumArgsInCall < NumArgsInProto) |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 477 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 478 | Fn->getSourceRange()); |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 479 | else if (NumArgsInCall > NumArgsInProto) { |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 480 | if (!proto->isVariadic()) { |
Chris Lattner | a6f5ab5 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 481 | Diag(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 482 | diag::err_typecheck_call_too_many_args, Fn->getSourceRange(), |
Chris Lattner | a6f5ab5 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 483 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 484 | Args[NumArgsInCall-1]->getLocEnd())); |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 485 | } |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 486 | NumArgsToCheck = NumArgsInProto; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 487 | } |
| 488 | // 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] | 489 | for (unsigned i = 0; i < NumArgsToCheck; i++) { |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 490 | Expr *argExpr = Args[i]; |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 491 | assert(argExpr && "ParseCallExpr(): missing argument expression"); |
| 492 | |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 493 | QualType lhsType = proto->getArgType(i); |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 494 | QualType rhsType = argExpr->getType(); |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 495 | |
Steve Naroff | b8af1c2 | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 496 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 497 | if (const ArrayType *ary = lhsType->getAsArrayType()) |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 498 | lhsType = Context.getPointerType(ary->getElementType()); |
Steve Naroff | b8af1c2 | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 499 | else if (lhsType->isFunctionType()) |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 500 | lhsType = Context.getPointerType(lhsType); |
| 501 | |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 502 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 503 | argExpr); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 504 | SourceLocation l = argExpr->getLocStart(); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 505 | |
| 506 | // decode the result (notice that AST's are still created for extensions). |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 507 | switch (result) { |
| 508 | case Compatible: |
| 509 | break; |
| 510 | case PointerFromInt: |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 511 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 512 | if (!argExpr->isNullPointerConstant(Context)) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 513 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 514 | lhsType.getAsString(), rhsType.getAsString(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 515 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 516 | } |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 517 | break; |
| 518 | case IntFromPointer: |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 519 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 520 | lhsType.getAsString(), rhsType.getAsString(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 521 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 522 | break; |
| 523 | case IncompatiblePointer: |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 524 | Diag(l, diag::ext_typecheck_passing_incompatible_pointer, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 525 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 526 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 527 | break; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 528 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 529 | Diag(l, diag::ext_typecheck_passing_discards_qualifiers, |
| 530 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 531 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 532 | break; |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 533 | case Incompatible: |
Steve Naroff | 8563f65 | 2007-05-28 19:25:56 +0000 | [diff] [blame] | 534 | return Diag(l, diag::err_typecheck_passing_incompatible, |
| 535 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 536 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 539 | // Even if the types checked, bail if we had the wrong number of arguments. |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 540 | if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) |
Steve Naroff | b8c289d | 2007-05-08 22:18:00 +0000 | [diff] [blame] | 541 | return true; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | 38dbdb2 | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 543 | return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | Action::ExprResult Sema:: |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 547 | ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 548 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 549 | assert((Ty != 0) && "ParseCompoundLiteral(): missing type"); |
| 550 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 551 | // FIXME: put back this assert when initializers are worked out. |
| 552 | //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression"); |
| 553 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 554 | |
| 555 | // FIXME: add semantic analysis (C99 6.5.2.5). |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 556 | return new CompoundLiteralExpr(literalType, literalExpr); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | Action::ExprResult Sema:: |
| 560 | ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, |
| 561 | SourceLocation RParenLoc) { |
| 562 | // FIXME: add semantic analysis (C99 6.7.8). This involves |
| 563 | // knowledge of the object being intialized. As a result, the code for |
| 564 | // doing the semantic analysis will likely be located elsewhere (i.e. in |
| 565 | // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral). |
| 566 | return false; // FIXME instantiate an InitListExpr. |
| 567 | } |
| 568 | |
| 569 | Action::ExprResult Sema:: |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 570 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 571 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 572 | assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr"); |
| 573 | |
| 574 | Expr *castExpr = static_cast<Expr*>(Op); |
| 575 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 576 | |
Chris Lattner | bd27073 | 2007-07-18 16:00:06 +0000 | [diff] [blame] | 577 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 578 | // type needs to be scalar. |
| 579 | if (!castType->isScalarType() && !castType->isVoidType()) { |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 580 | return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, |
| 581 | castType.getAsString(), SourceRange(LParenLoc, RParenLoc)); |
| 582 | } |
| 583 | if (!castExpr->getType()->isScalarType()) { |
| 584 | return Diag(castExpr->getLocStart(), |
| 585 | diag::err_typecheck_expect_scalar_operand, |
| 586 | castExpr->getType().getAsString(), castExpr->getSourceRange()); |
| 587 | } |
| 588 | return new CastExpr(castType, castExpr, LParenLoc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 591 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 592 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 593 | UsualUnaryConversions(cond); |
| 594 | UsualUnaryConversions(lex); |
| 595 | UsualUnaryConversions(rex); |
| 596 | QualType condT = cond->getType(); |
| 597 | QualType lexT = lex->getType(); |
| 598 | QualType rexT = rex->getType(); |
| 599 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 600 | // first, check the condition. |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 601 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 602 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 603 | condT.getAsString()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 604 | return QualType(); |
| 605 | } |
| 606 | // now check the two expressions. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 607 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5 |
| 608 | UsualArithmeticConversions(lex, rex); |
| 609 | return lex->getType(); |
| 610 | } |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 611 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
| 612 | if (const RecordType *RHSRT = rexT->getAsRecordType()) { |
| 613 | |
| 614 | if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier()) |
| 615 | return lexT; |
| 616 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 617 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 618 | lexT.getAsString(), rexT.getAsString(), |
| 619 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 620 | return QualType(); |
| 621 | } |
| 622 | } |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 623 | // C99 6.5.15p3 |
| 624 | if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 625 | return lexT; |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 626 | if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 627 | return rexT; |
Steve Naroff | 30d1fbc | 2007-05-20 19:46:53 +0000 | [diff] [blame] | 628 | |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 629 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 630 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 631 | // get the "pointed to" types |
| 632 | QualType lhptee = LHSPT->getPointeeType(); |
| 633 | QualType rhptee = RHSPT->getPointeeType(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 634 | |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 635 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 636 | if (lhptee->isVoidType() && |
| 637 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 638 | return lexT; |
| 639 | if (rhptee->isVoidType() && |
| 640 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 641 | return rexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 642 | |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 643 | if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 644 | rhptee.getUnqualifiedType())) { |
| 645 | Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers, |
| 646 | lexT.getAsString(), rexT.getAsString(), |
| 647 | lex->getSourceRange(), rex->getSourceRange()); |
| 648 | return lexT; // FIXME: this is an _ext - is this return o.k? |
| 649 | } |
| 650 | // The pointer types are compatible. |
| 651 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 652 | // differently qualified versions of compatible types, the result type is a |
| 653 | // pointer to an appropriately qualified version of the *composite* type. |
| 654 | return lexT; // FIXME: Need to return the composite type. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 657 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 658 | if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3 |
| 659 | return lexT; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 660 | |
| 661 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 662 | lexT.getAsString(), rexT.getAsString(), |
| 663 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 664 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 667 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 668 | /// in the case of a the GNU conditional expr extension. |
| 669 | Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc, |
| 670 | SourceLocation ColonLoc, |
| 671 | ExprTy *Cond, ExprTy *LHS, |
| 672 | ExprTy *RHS) { |
Chris Lattner | daaa9f2 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 673 | Expr *CondExpr = (Expr *) Cond; |
| 674 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
| 675 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 676 | RHSExpr, QuestionLoc); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 677 | if (result.isNull()) |
| 678 | return true; |
Chris Lattner | daaa9f2 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 679 | return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 682 | // promoteExprToType - a helper function to ensure we create exactly one |
| 683 | // ImplicitCastExpr. As a convenience (to the caller), we return the type. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 684 | static void promoteExprToType(Expr *&expr, QualType type) { |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 685 | if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr)) |
| 686 | impCast->setType(type); |
| 687 | else |
| 688 | expr = new ImplicitCastExpr(type, expr); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 689 | return; |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 693 | void Sema::DefaultFunctionArrayConversion(Expr *&e) { |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 694 | QualType t = e->getType(); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 695 | assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type"); |
Bill Wendling | dfc8107 | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 696 | |
Chris Lattner | cd1d086 | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 697 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | 354fb26 | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 698 | promoteExprToType(e, ref->getReferenceeType()); // C++ [expr] |
| 699 | t = e->getType(); |
| 700 | } |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 701 | if (t->isFunctionType()) |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 702 | promoteExprToType(e, Context.getPointerType(t)); |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 703 | else if (const ArrayType *ary = t->getAsArrayType()) |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 704 | promoteExprToType(e, Context.getPointerType(ary->getElementType())); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 707 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 708 | /// operators (C99 6.3). The conversions of array and function types are |
| 709 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 710 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 711 | /// In these instances, this routine should *not* be called. |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 712 | void Sema::UsualUnaryConversions(Expr *&expr) { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 713 | QualType t = expr->getType(); |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 714 | assert(!t.isNull() && "UsualUnaryConversions - missing type"); |
| 715 | |
Chris Lattner | cd1d086 | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 716 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | 354fb26 | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 717 | promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr] |
| 718 | t = expr->getType(); |
| 719 | } |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 720 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 721 | promoteExprToType(expr, Context.IntTy); |
| 722 | else |
| 723 | DefaultFunctionArrayConversion(expr); |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 726 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 727 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 728 | /// routine returns the first non-arithmetic type found. The client is |
| 729 | /// responsible for emitting appropriate error diagnostics. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 730 | void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 731 | UsualUnaryConversions(lhsExpr); |
| 732 | UsualUnaryConversions(rhsExpr); |
| 733 | |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 734 | QualType lhs = lhsExpr->getType(); |
| 735 | QualType rhs = rhsExpr->getType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 736 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 737 | // If both types are identical, no conversion is needed. |
| 738 | if (lhs == rhs) |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 739 | return; |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 740 | |
| 741 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 742 | // The caller can deal with this (e.g. pointer + int). |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 743 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 744 | return; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 0c46c5d | 2007-06-02 23:53:17 +0000 | [diff] [blame] | 746 | // At this point, we have two different arithmetic types. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 747 | |
| 748 | // Handle complex types first (C99 6.3.1.8p1). |
| 749 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 750 | // if we have an integer operand, the result is the complex type. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 751 | if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type. |
| 752 | promoteExprToType(rhsExpr, lhs); |
| 753 | return; |
| 754 | } |
| 755 | if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type. |
| 756 | promoteExprToType(lhsExpr, rhs); |
| 757 | return; |
| 758 | } |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 759 | // Two complex types. Convert the smaller operand to the bigger result. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 760 | if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs |
| 761 | promoteExprToType(rhsExpr, lhs); |
| 762 | return; |
| 763 | } |
| 764 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 765 | return; |
Steve Naroff | bf223ba | 2007-04-24 20:56:26 +0000 | [diff] [blame] | 766 | } |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 767 | // Now handle "real" floating types (i.e. float, double, long double). |
| 768 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 769 | // if we have an integer operand, the result is the real floating type. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 770 | if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type. |
| 771 | promoteExprToType(rhsExpr, lhs); |
| 772 | return; |
| 773 | } |
| 774 | if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type. |
| 775 | promoteExprToType(lhsExpr, rhs); |
| 776 | return; |
| 777 | } |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 778 | // We have two real floating types, float/complex combos were handled above. |
| 779 | // Convert the smaller operand to the bigger result. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 780 | if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs |
| 781 | promoteExprToType(rhsExpr, lhs); |
| 782 | return; |
| 783 | } |
| 784 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 785 | return; |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 786 | } |
Steve Naroff | 81569d2 | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 787 | // Finally, we have two differing integer types. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 788 | if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs |
| 789 | promoteExprToType(rhsExpr, lhs); |
| 790 | return; |
| 791 | } |
| 792 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 793 | return; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 796 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 797 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 798 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 799 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 800 | // FIXME: add a couple examples in this comment. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 801 | Sema::AssignmentCheckResult |
| 802 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 803 | QualType lhptee, rhptee; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 804 | |
| 805 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | e5a6cbd | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 806 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 807 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 808 | |
| 809 | // make sure we operate on the canonical type |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 810 | lhptee = lhptee.getCanonicalType(); |
| 811 | rhptee = rhptee.getCanonicalType(); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 812 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 813 | AssignmentCheckResult r = Compatible; |
| 814 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 815 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 816 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 817 | // qualifiers of the type *pointed to* by the right; |
| 818 | if ((lhptee.getQualifiers() & rhptee.getQualifiers()) != |
| 819 | rhptee.getQualifiers()) |
| 820 | r = CompatiblePointerDiscardsQualifiers; |
| 821 | |
| 822 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 823 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 824 | // version of void... |
| 825 | if (lhptee.getUnqualifiedType()->isVoidType() && |
| 826 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 827 | ; |
| 828 | else if (rhptee.getUnqualifiedType()->isVoidType() && |
| 829 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 830 | ; |
| 831 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 832 | // unqualified versions of compatible types, ... |
| 833 | else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 834 | rhptee.getUnqualifiedType())) |
| 835 | r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 836 | return r; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 839 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 840 | /// has code to accommodate several GCC extensions when type checking |
| 841 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 842 | /// |
| 843 | /// int a, *pint; |
| 844 | /// short *pshort; |
| 845 | /// struct foo *pfoo; |
| 846 | /// |
| 847 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 848 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 849 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 850 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 851 | /// |
| 852 | /// As a result, the code for dealing with pointers is more complex than the |
| 853 | /// C99 spec dictates. |
| 854 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 855 | /// |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 856 | Sema::AssignmentCheckResult |
| 857 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Steve Naroff | 44fd8ff | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 858 | if (lhsType == rhsType) // common case, fast path... |
| 859 | return Compatible; |
| 860 | |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 861 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) { |
Steve Naroff | e728ba3 | 2007-07-10 22:20:04 +0000 | [diff] [blame] | 862 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
| 863 | if (lhsType.getCanonicalType() != rhsType.getCanonicalType()) |
| 864 | return Incompatible; |
| 865 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 866 | return Compatible; |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 867 | } else if (lhsType->isPointerType()) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 868 | if (rhsType->isIntegerType()) |
| 869 | return PointerFromInt; |
| 870 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 871 | if (rhsType->isPointerType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 872 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 873 | } else if (rhsType->isPointerType()) { |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 874 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 875 | if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy)) |
| 876 | return IntFromPointer; |
| 877 | |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 878 | if (lhsType->isPointerType()) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 879 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 880 | } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
| 881 | if (Type::tagTypesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 882 | return Compatible; |
Bill Wendling | db4f06e | 2007-06-02 23:29:59 +0000 | [diff] [blame] | 883 | } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
| 884 | if (Type::referenceTypesAreCompatible(lhsType, rhsType)) |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 885 | return Compatible; |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 886 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 887 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 890 | Sema::AssignmentCheckResult |
| 891 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
| 892 | // This check seems unnatural, however it is necessary to insure the proper |
| 893 | // conversion of functions/arrays. If the conversion were done for all |
| 894 | // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary |
| 895 | // expressions that surpress this implicit conversion (&, sizeof). |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 896 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 897 | |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 898 | return CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | Sema::AssignmentCheckResult |
| 902 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 903 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 904 | } |
| 905 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 906 | inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 907 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 908 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 909 | lex->getSourceRange(), rex->getSourceRange()); |
| 910 | } |
| 911 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 912 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 913 | Expr *&rex) { |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 914 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 915 | |
| 916 | // make sure the vector types are identical. |
| 917 | if (lhsType == rhsType) |
| 918 | return lhsType; |
| 919 | // You cannot convert between vector values of different size. |
| 920 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 921 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 922 | lex->getSourceRange(), rex->getSourceRange()); |
| 923 | return QualType(); |
| 924 | } |
| 925 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 926 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 927 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 928 | { |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 929 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 930 | |
| 931 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 932 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 933 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 934 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 935 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 936 | // handle the common case first (both operands are arithmetic). |
| 937 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 938 | return lex->getType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 939 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 940 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 943 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 944 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 945 | { |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 946 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 947 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 948 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 949 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 950 | // handle the common case first (both operands are arithmetic). |
| 951 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 952 | return lex->getType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 953 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 954 | return QualType(); |
| 955 | } |
| 956 | |
| 957 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 958 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 959 | { |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 960 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 961 | return CheckVectorOperands(loc, lex, rex); |
| 962 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 963 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 964 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 965 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 966 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 967 | return lex->getType(); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 968 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 969 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
| 970 | return lex->getType(); |
| 971 | if (lex->getType()->isIntegerType() && rex->getType()->isPointerType()) |
| 972 | return rex->getType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 973 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 974 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 977 | inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 978 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 979 | { |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 980 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 981 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 982 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 983 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 984 | |
| 985 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 986 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 987 | return lex->getType(); |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 988 | |
| 989 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 990 | return lex->getType(); |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 991 | if (lex->getType()->isPointerType() && rex->getType()->isPointerType()) |
Chris Lattner | d2b88ab | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 992 | return Context.getPointerDiffType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 993 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 994 | return QualType(); |
| 995 | } |
| 996 | |
| 997 | inline QualType Sema::CheckShiftOperands( // C99 6.5.7 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 998 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 999 | { |
Chris Lattner | 1d411a8 | 2007-06-05 20:52:21 +0000 | [diff] [blame] | 1000 | // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong |
| 1001 | // for int << longlong -> the result type should be int, not long long. |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1002 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1003 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1004 | // handle the common case first (both operands are arithmetic). |
| 1005 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 1006 | return lex->getType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 1007 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1008 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1011 | inline QualType Sema::CheckRelationalOperands( // C99 6.5.8 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1012 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1013 | { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1014 | UsualUnaryConversions(lex); |
| 1015 | UsualUnaryConversions(rex); |
| 1016 | QualType lType = lex->getType(); |
| 1017 | QualType rType = rex->getType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1018 | |
| 1019 | if (lType->isRealType() && rType->isRealType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1020 | return Context.IntTy; |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 1021 | |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1022 | if (lType->isPointerType()) { |
| 1023 | if (rType->isPointerType()) |
| 1024 | return Context.IntTy; |
| 1025 | if (rType->isIntegerType()) { |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1026 | if (!rex->isNullPointerConstant(Context)) |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1027 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1028 | lex->getSourceRange(), rex->getSourceRange()); |
| 1029 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 1030 | } |
| 1031 | } else if (rType->isPointerType()) { |
| 1032 | if (lType->isIntegerType()) { |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1033 | if (!lex->isNullPointerConstant(Context)) |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1034 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1035 | lex->getSourceRange(), rex->getSourceRange()); |
| 1036 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 1037 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1038 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 1039 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1040 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1043 | inline QualType Sema::CheckEqualityOperands( // C99 6.5.9 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1044 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1045 | { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1046 | UsualUnaryConversions(lex); |
| 1047 | UsualUnaryConversions(rex); |
| 1048 | QualType lType = lex->getType(); |
| 1049 | QualType rType = rex->getType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1050 | |
| 1051 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1052 | return Context.IntTy; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1053 | |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1054 | if (lType->isPointerType()) { |
| 1055 | if (rType->isPointerType()) |
| 1056 | return Context.IntTy; |
| 1057 | if (rType->isIntegerType()) { |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1058 | if (!rex->isNullPointerConstant(Context)) |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1059 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1060 | lex->getSourceRange(), rex->getSourceRange()); |
| 1061 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 1062 | } |
| 1063 | } else if (rType->isPointerType()) { |
| 1064 | if (lType->isIntegerType()) { |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1065 | if (!lex->isNullPointerConstant(Context)) |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 1066 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1067 | lex->getSourceRange(), rex->getSourceRange()); |
| 1068 | return Context.IntTy; // the previous diagnostic is a GCC extension. |
| 1069 | } |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1070 | } |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 1071 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1072 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1075 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1076 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1077 | { |
Steve Naroff | 94a5aca | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1078 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 1079 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1080 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1081 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1082 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1083 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 1084 | return lex->getType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 1085 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1086 | return QualType(); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1089 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1090 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1091 | { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1092 | UsualUnaryConversions(lex); |
| 1093 | UsualUnaryConversions(rex); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 1094 | |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1095 | if (lex->getType()->isScalarType() || rex->getType()->isScalarType()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1096 | return Context.IntTy; |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 1097 | InvalidOperands(loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1098 | return QualType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1101 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
| 1102 | Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType) |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1103 | { |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 1104 | QualType lhsType = lex->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1105 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1106 | bool hadError = false; |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1107 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(); |
| 1108 | |
| 1109 | switch (mlval) { // C99 6.5.16p2 |
| 1110 | case Expr::MLV_Valid: |
| 1111 | break; |
| 1112 | case Expr::MLV_ConstQualified: |
| 1113 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 1114 | hadError = true; |
| 1115 | break; |
| 1116 | case Expr::MLV_ArrayType: |
| 1117 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 1118 | lhsType.getAsString(), lex->getSourceRange()); |
| 1119 | return QualType(); |
| 1120 | case Expr::MLV_NotObjectType: |
| 1121 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 1122 | lhsType.getAsString(), lex->getSourceRange()); |
| 1123 | return QualType(); |
| 1124 | case Expr::MLV_InvalidExpression: |
| 1125 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 1126 | lex->getSourceRange()); |
| 1127 | return QualType(); |
| 1128 | case Expr::MLV_IncompleteType: |
| 1129 | case Expr::MLV_IncompleteVoidType: |
| 1130 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 1131 | lhsType.getAsString(), lex->getSourceRange()); |
| 1132 | return QualType(); |
Steve Naroff | 0d595ca | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1133 | case Expr::MLV_DuplicateVectorComponents: |
| 1134 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 1135 | lex->getSourceRange()); |
| 1136 | return QualType(); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1137 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1138 | AssignmentCheckResult result; |
| 1139 | |
| 1140 | if (compoundType.isNull()) |
| 1141 | result = CheckSingleAssignmentConstraints(lhsType, rex); |
| 1142 | else |
| 1143 | result = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1144 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1145 | // decode the result (notice that extensions still return a type). |
| 1146 | switch (result) { |
| 1147 | case Compatible: |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1148 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1149 | case Incompatible: |
Steve Naroff | e845e27 | 2007-05-18 01:06:45 +0000 | [diff] [blame] | 1150 | Diag(loc, diag::err_typecheck_assign_incompatible, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 1151 | lhsType.getAsString(), rhsType.getAsString(), |
| 1152 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1153 | hadError = true; |
| 1154 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1155 | case PointerFromInt: |
| 1156 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 0e9d622 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1157 | if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) { |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 1158 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1159 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1160 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 9992bba | 2007-05-30 16:27:15 +0000 | [diff] [blame] | 1161 | } |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1162 | break; |
Steve Naroff | 56faab2 | 2007-05-30 04:20:12 +0000 | [diff] [blame] | 1163 | case IntFromPointer: |
| 1164 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1165 | lhsType.getAsString(), rhsType.getAsString(), |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1166 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1167 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1168 | case IncompatiblePointer: |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1169 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer, |
| 1170 | lhsType.getAsString(), rhsType.getAsString(), |
| 1171 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1172 | break; |
| 1173 | case CompatiblePointerDiscardsQualifiers: |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1174 | Diag(loc, diag::ext_typecheck_assign_discards_qualifiers, |
| 1175 | lhsType.getAsString(), rhsType.getAsString(), |
| 1176 | lex->getSourceRange(), rex->getSourceRange()); |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 1177 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1178 | } |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 1179 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 1180 | // left operand unless the left operand has qualified type, in which case |
| 1181 | // it is the unqualified version of the type of the left operand. |
| 1182 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 1183 | // is converted to the type of the assignment expression (above). |
| 1184 | // C++ 5.17p1: the type of the assignment expression is that of its left oprdu. |
| 1185 | return hadError ? QualType() : lhsType.getUnqualifiedType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1188 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1189 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1190 | UsualUnaryConversions(rex); |
| 1191 | return rex->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1194 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 1195 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1196 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1197 | QualType resType = op->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1198 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 1199 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 1200 | // C99 6.5.2.4p1 |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1201 | if (const PointerType *pt = dyn_cast<PointerType>(resType)) { |
| 1202 | if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1203 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1204 | resType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1205 | return QualType(); |
| 1206 | } |
| 1207 | } else if (!resType->isRealType()) { |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 1208 | // FIXME: Allow Complex as a GCC extension. |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1209 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 1210 | resType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1211 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 1212 | } |
Steve Naroff | 094046f | 2007-05-13 03:21:25 +0000 | [diff] [blame] | 1213 | // At this point, we know we have a real or pointer type. Now make sure |
| 1214 | // the operand is a modifiable lvalue. |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1215 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(); |
| 1216 | if (mlval != Expr::MLV_Valid) { |
| 1217 | // FIXME: emit a more precise diagnostic... |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1218 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 1219 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1220 | return QualType(); |
| 1221 | } |
| 1222 | return resType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1225 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1226 | /// This routine allows us to typecheck complex/recursive expressions |
| 1227 | /// where the declaration is needed for type checking. Here are some |
| 1228 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1229 | static Decl *getPrimaryDeclaration(Expr *e) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1230 | switch (e->getStmtClass()) { |
| 1231 | case Stmt::DeclRefExprClass: |
| 1232 | return cast<DeclRefExpr>(e)->getDecl(); |
| 1233 | case Stmt::MemberExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1234 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1235 | case Stmt::ArraySubscriptExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1236 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1237 | case Stmt::CallExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1238 | return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1239 | case Stmt::UnaryOperatorClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1240 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1241 | case Stmt::ParenExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1242 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1243 | default: |
| 1244 | return 0; |
| 1245 | } |
| 1246 | } |
| 1247 | |
| 1248 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 1249 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 1250 | /// object cannot be declared with storage class register or be a bit field. |
| 1251 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 1252 | /// 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] | 1253 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1254 | Decl *dcl = getPrimaryDeclaration(op); |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1255 | Expr::isLvalueResult lval = op->isLvalue(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1256 | |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1257 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Steve Naroff | 5dd642e | 2007-05-14 18:14:51 +0000 | [diff] [blame] | 1258 | if (dcl && isa<FunctionDecl>(dcl)) // allow function designators |
| 1259 | ; |
Steve Naroff | 9358c71 | 2007-05-27 23:58:33 +0000 | [diff] [blame] | 1260 | else { // FIXME: emit more specific diag... |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1261 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 1262 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1263 | return QualType(); |
| 1264 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1265 | } else if (dcl) { |
| 1266 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 1267 | // with the register storage-class specifier. |
| 1268 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1269 | if (vd->getStorageClass() == VarDecl::Register) { |
Steve Naroff | 71ce2e0 | 2007-05-18 22:53:50 +0000 | [diff] [blame] | 1270 | Diag(OpLoc, diag::err_typecheck_address_of_register, |
Chris Lattner | 84e160a | 2007-05-19 07:03:17 +0000 | [diff] [blame] | 1271 | op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1272 | return QualType(); |
| 1273 | } |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 1274 | } else |
| 1275 | assert(0 && "Unknown/unexpected decl type"); |
| 1276 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1277 | // FIXME: add check for bitfields! |
| 1278 | } |
| 1279 | // 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] | 1280 | return Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1283 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1284 | UsualUnaryConversions(op); |
| 1285 | QualType qType = op->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1286 | |
Chris Lattner | c996b17 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1287 | if (const PointerType *PT = qType->getAsPointerType()) { |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1288 | QualType ptype = PT->getPointeeType(); |
| 1289 | // C99 6.5.3.2p4. "if it points to an object,...". |
| 1290 | if (ptype->isIncompleteType()) { // An incomplete type is not an object |
| 1291 | // GCC compat: special case 'void *' (treat as warning). |
| 1292 | if (ptype->isVoidType()) { |
| 1293 | Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1294 | qType.getAsString(), op->getSourceRange()); |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1295 | } else { |
| 1296 | Diag(OpLoc, diag::err_typecheck_deref_incomplete_type, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1297 | ptype.getAsString(), op->getSourceRange()); |
Steve Naroff | 758ada1 | 2007-05-28 16:15:57 +0000 | [diff] [blame] | 1298 | return QualType(); |
| 1299 | } |
| 1300 | } |
| 1301 | return ptype; |
| 1302 | } |
| 1303 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
Steve Naroff | eb9da94 | 2007-05-30 00:06:37 +0000 | [diff] [blame] | 1304 | qType.getAsString(), op->getSourceRange()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1305 | return QualType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 1306 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1307 | |
| 1308 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 1309 | tok::TokenKind Kind) { |
| 1310 | BinaryOperator::Opcode Opc; |
| 1311 | switch (Kind) { |
| 1312 | default: assert(0 && "Unknown binop!"); |
| 1313 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 1314 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 1315 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 1316 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 1317 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 1318 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 1319 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 1320 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 1321 | case tok::less: Opc = BinaryOperator::LT; break; |
| 1322 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 1323 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 1324 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 1325 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 1326 | case tok::amp: Opc = BinaryOperator::And; break; |
| 1327 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 1328 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 1329 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 1330 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 1331 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 1332 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 1333 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 1334 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 1335 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 1336 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 1337 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 1338 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 1339 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 1340 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 1341 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 1342 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 1343 | } |
| 1344 | return Opc; |
| 1345 | } |
| 1346 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1347 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 1348 | tok::TokenKind Kind) { |
| 1349 | UnaryOperator::Opcode Opc; |
| 1350 | switch (Kind) { |
| 1351 | default: assert(0 && "Unknown unary op!"); |
| 1352 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 1353 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 1354 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 1355 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 1356 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 1357 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 1358 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 1359 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 1360 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 1361 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 1362 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 1363 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
Chris Lattner | d0f7651 | 2007-06-08 22:16:53 +0000 | [diff] [blame] | 1364 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1365 | } |
| 1366 | return Opc; |
| 1367 | } |
| 1368 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1369 | // Binary Operators. 'Tok' is the token for the operator. |
| 1370 | Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 1371 | ExprTy *LHS, ExprTy *RHS) { |
| 1372 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 1373 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 1374 | |
| 1375 | assert((lhs != 0) && "ParseBinOp(): missing left expression"); |
| 1376 | assert((rhs != 0) && "ParseBinOp(): missing right expression"); |
| 1377 | |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1378 | QualType ResultTy; // Result type of the binary operator. |
| 1379 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1380 | |
| 1381 | switch (Opc) { |
| 1382 | default: |
| 1383 | assert(0 && "Unknown binary expr!"); |
| 1384 | case BinaryOperator::Assign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1385 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | case BinaryOperator::Mul: |
| 1388 | case BinaryOperator::Div: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1389 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | case BinaryOperator::Rem: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1392 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | case BinaryOperator::Add: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1395 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | case BinaryOperator::Sub: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1398 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | case BinaryOperator::Shl: |
| 1401 | case BinaryOperator::Shr: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1402 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | case BinaryOperator::LE: |
| 1405 | case BinaryOperator::LT: |
| 1406 | case BinaryOperator::GE: |
| 1407 | case BinaryOperator::GT: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1408 | ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1409 | break; |
| 1410 | case BinaryOperator::EQ: |
| 1411 | case BinaryOperator::NE: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1412 | ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1413 | break; |
| 1414 | case BinaryOperator::And: |
| 1415 | case BinaryOperator::Xor: |
| 1416 | case BinaryOperator::Or: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1417 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1418 | break; |
| 1419 | case BinaryOperator::LAnd: |
| 1420 | case BinaryOperator::LOr: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1421 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | case BinaryOperator::MulAssign: |
| 1424 | case BinaryOperator::DivAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1425 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1426 | if (!CompTy.isNull()) |
| 1427 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | case BinaryOperator::RemAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1430 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1431 | if (!CompTy.isNull()) |
| 1432 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1433 | break; |
| 1434 | case BinaryOperator::AddAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1435 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1436 | if (!CompTy.isNull()) |
| 1437 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1438 | break; |
| 1439 | case BinaryOperator::SubAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1440 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1441 | if (!CompTy.isNull()) |
| 1442 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1443 | break; |
| 1444 | case BinaryOperator::ShlAssign: |
| 1445 | case BinaryOperator::ShrAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1446 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1447 | if (!CompTy.isNull()) |
| 1448 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | case BinaryOperator::AndAssign: |
| 1451 | case BinaryOperator::XorAssign: |
| 1452 | case BinaryOperator::OrAssign: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1453 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1454 | if (!CompTy.isNull()) |
| 1455 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1456 | break; |
| 1457 | case BinaryOperator::Comma: |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1458 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1459 | break; |
| 1460 | } |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1461 | if (ResultTy.isNull()) |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1462 | return true; |
Chris Lattner | 256c21b | 2007-06-28 03:53:10 +0000 | [diff] [blame] | 1463 | if (CompTy.isNull()) |
| 1464 | return new BinaryOperator(lhs, rhs, Opc, ResultTy); |
| 1465 | else |
Chris Lattner | 9369a56 | 2007-06-29 16:31:29 +0000 | [diff] [blame] | 1466 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1469 | // Unary Operators. 'Tok' is the token for the operator. |
| 1470 | Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1471 | ExprTy *input) { |
| 1472 | Expr *Input = (Expr*)input; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1473 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 1474 | QualType resultType; |
| 1475 | switch (Opc) { |
| 1476 | default: |
| 1477 | assert(0 && "Unimplemented unary expr!"); |
| 1478 | case UnaryOperator::PreInc: |
| 1479 | case UnaryOperator::PreDec: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1480 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1481 | break; |
| 1482 | case UnaryOperator::AddrOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1483 | resultType = CheckAddressOfOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1484 | break; |
| 1485 | case UnaryOperator::Deref: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1486 | resultType = CheckIndirectionOperand(Input, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1487 | break; |
| 1488 | case UnaryOperator::Plus: |
| 1489 | case UnaryOperator::Minus: |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1490 | UsualUnaryConversions(Input); |
| 1491 | resultType = Input->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1492 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 1493 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1494 | resultType.getAsString()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1497 | UsualUnaryConversions(Input); |
| 1498 | resultType = Input->getType(); |
Steve Naroff | 1c8b7d3 | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 1499 | if (!resultType->isIntegerType()) // C99 6.5.3.3p1 |
| 1500 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1501 | resultType.getAsString()); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | case UnaryOperator::LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 1504 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1505 | DefaultFunctionArrayConversion(Input); |
| 1506 | resultType = Input->getType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1507 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
Chris Lattner | c04bd6a | 2007-05-16 18:09:54 +0000 | [diff] [blame] | 1508 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1509 | resultType.getAsString()); |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 1510 | // LNot always has type int. C99 6.5.3.3p5. |
| 1511 | resultType = Context.IntTy; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1512 | break; |
| 1513 | case UnaryOperator::SizeOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1514 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1515 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1516 | case UnaryOperator::AlignOf: |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1517 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false); |
| 1518 | break; |
| 1519 | case UnaryOperator::Extension: |
| 1520 | // FIXME: does __extension__ cause any promotions? I would think not. |
| 1521 | resultType = Input->getType(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 1522 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1523 | } |
| 1524 | if (resultType.isNull()) |
| 1525 | return true; |
Chris Lattner | 8655428 | 2007-06-08 22:32:33 +0000 | [diff] [blame] | 1526 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 1527 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 1528 | |
| 1529 | /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 1530 | Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc, |
| 1531 | SourceLocation LabLoc, |
| 1532 | IdentifierInfo *LabelII) { |
| 1533 | // Look up the record for this label identifier. |
| 1534 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 1535 | |
| 1536 | // If we haven't seen this label yet, create a forward reference. |
| 1537 | if (LabelDecl == 0) |
| 1538 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 1539 | |
| 1540 | // Create the AST node. The address of a label always has type 'void*'. |
| 1541 | return new AddrLabel(OpLoc, LabLoc, LabelDecl, |
| 1542 | Context.getPointerType(Context.VoidTy)); |
| 1543 | } |
| 1544 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1545 | Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
| 1546 | SourceLocation RPLoc) { // "({..})" |
| 1547 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 1548 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 1549 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 1550 | |
| 1551 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 1552 | // example, it is not possible to goto into a stmt expression apparently. |
| 1553 | // More semantic analysis is needed. |
| 1554 | |
| 1555 | // FIXME: the last statement in the compount stmt has its value used. We |
| 1556 | // should not warn about it being unused. |
| 1557 | |
| 1558 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 1559 | // as the type of the stmtexpr. |
| 1560 | QualType Ty = Context.VoidTy; |
| 1561 | |
| 1562 | if (!Compound->body_empty()) |
| 1563 | if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back())) |
| 1564 | Ty = LastExpr->getType(); |
| 1565 | |
| 1566 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 1567 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame^] | 1568 | |
| 1569 | Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation LPLoc, |
| 1570 | TypeTy *arg1, TypeTy *arg2, |
| 1571 | SourceLocation RPLoc) { |
| 1572 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 1573 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 1574 | |
| 1575 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 1576 | |
| 1577 | return new TypesCompatibleExpr(Context.IntTy, LPLoc, argT1, argT2, RPLoc); |
| 1578 | } |
| 1579 | |