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