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