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