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