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