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