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