Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 19 | #include "clang/Lex/LiteralSupport.h" |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 21 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 22 | #include "clang/Basic/LangOptions.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | using namespace clang; |
| 27 | |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 28 | /// ParseStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 29 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 30 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 31 | /// multiple tokens. However, the common case is that StringToks points to one |
| 32 | /// string. |
| 33 | /// |
| 34 | Action::ExprResult |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 35 | Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 36 | assert(NumStringToks && "Must have at least one string!"); |
| 37 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 38 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 39 | if (Literal.hadError) |
| 40 | return ExprResult(true); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 42 | SmallVector<SourceLocation, 4> StringTokLocs; |
| 43 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 44 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 45 | |
| 46 | // FIXME: handle wchar_t |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 47 | QualType t = Context.getPointerType(Context.CharTy); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 49 | // FIXME: use factory. |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 50 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 51 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 52 | Literal.AnyWide, t); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 55 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 56 | /// ParseIdentifierExpr - The parser read an identifier in expression context, |
| 57 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
| 58 | /// identifier is used in an function call context. |
| 59 | Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc, |
| 60 | IdentifierInfo &II, |
| 61 | bool HasTrailingLParen) { |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 62 | // Could be enum-constant or decl. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 63 | Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 64 | if (D == 0) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 65 | // Otherwise, this could be an implicitly declared function reference (legal |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 66 | // in C90, extension in C99). |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 67 | if (HasTrailingLParen && |
| 68 | // Not in C++. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 69 | !getLangOptions().CPlusPlus) |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 70 | D = ImplicitlyDefineFunction(Loc, II, S); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 71 | else { |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 72 | // If this name wasn't predeclared and if this is not a function call, |
| 73 | // diagnose the problem. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 74 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 75 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 78 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 79 | return new DeclRefExpr(VD, VD->getType()); |
| 80 | if (isa<TypedefDecl>(D)) |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 81 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
| 82 | |
| 83 | assert(0 && "Invalid decl"); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 86 | Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc, |
| 87 | tok::TokenKind Kind) { |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 88 | switch (Kind) { |
| 89 | default: |
| 90 | assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 91 | case tok::char_constant: // constant: character-constant |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 92 | // TODO: MOVE this to be some other callback. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 93 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 94 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 95 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 96 | return 0; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 100 | Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 101 | // fast path for a single digit (which is quite common). A single digit |
| 102 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 103 | if (Tok.getLength() == 1) { |
| 104 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
| 105 | return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy)); |
| 106 | } |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 107 | SmallString<512> IntegerBuffer; |
| 108 | IntegerBuffer.resize(Tok.getLength()); |
| 109 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 110 | |
| 111 | // Get the spelling of the token, which eliminates trigraphs, etc. Notes: |
| 112 | // - We know that ThisTokBuf points to a buffer that is big enough for the |
| 113 | // whole token and 'spelled' tokens can only shrink. |
| 114 | // - In practice, the local buffer is only used when the spelling doesn't |
| 115 | // match the original token (which is rare). The common case simply returns |
| 116 | // a pointer to a *constant* buffer (avoiding a copy). |
| 117 | |
| 118 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 119 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 120 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 121 | if (Literal.hadError) |
| 122 | return ExprResult(true); |
| 123 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 124 | if (Literal.isIntegerLiteral()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 125 | QualType t; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 126 | if (Literal.hasSuffix()) { |
| 127 | if (Literal.isLong) |
| 128 | t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 129 | else if (Literal.isLongLong) |
| 130 | t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 131 | else |
| 132 | t = Context.UnsignedIntTy; |
| 133 | } else { |
| 134 | t = Context.IntTy; // implicit type is "int" |
| 135 | } |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 136 | uintmax_t val; |
| 137 | if (Literal.GetIntegerValue(val)) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 138 | return new IntegerLiteral(val, t); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 139 | } |
| 140 | } else if (Literal.isFloatingLiteral()) { |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 141 | // FIXME: fill in the value and compute the real type... |
| 142 | return new FloatingLiteral(7.7, Context.FloatTy); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 143 | } |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 144 | return ExprResult(true); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R, |
| 148 | ExprTy *Val) { |
| 149 | return Val; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | // Unary Operators. 'Tok' is the token for the operator. |
| 154 | Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
| 155 | ExprTy *Input) { |
| 156 | UnaryOperator::Opcode Opc; |
| 157 | switch (Op) { |
| 158 | default: assert(0 && "Unknown unary op!"); |
| 159 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 160 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 161 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 162 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 163 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 164 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 165 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 166 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 167 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 168 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 169 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 170 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 171 | case tok::ampamp: Opc = UnaryOperator::AddrLabel; break; |
| 172 | case tok::kw___extension__: |
| 173 | return Input; |
| 174 | //Opc = UnaryOperator::Extension; |
| 175 | //break; |
| 176 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 177 | if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec) |
| 178 | return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 179 | else if (Opc == UnaryOperator::AddrOf) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 180 | return CheckAddressOfOperand((Expr *)Input, OpLoc); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 181 | else if (Opc == UnaryOperator::Deref) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 182 | return CheckIndirectionOperand((Expr *)Input, OpLoc); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 183 | else if (UnaryOperator::isArithmeticOp(Opc)) |
| 184 | return CheckArithmeticOperand((Expr *)Input, OpLoc, Opc); |
| 185 | |
| 186 | // will go away when all cases are handled... |
| 187 | return new UnaryOperator((Expr *)Input, Opc, QualType()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | Action::ExprResult Sema:: |
| 191 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 192 | SourceLocation LParenLoc, TypeTy *Ty, |
| 193 | SourceLocation RParenLoc) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 194 | // If error parsing type, ignore. |
| 195 | if (Ty == 0) return true; |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 196 | |
| 197 | // Verify that this is a valid expression. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 198 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 199 | |
| 200 | if (isa<FunctionType>(ArgTy) && isSizeof) { |
| 201 | // alignof(function) is allowed. |
| 202 | Diag(OpLoc, diag::ext_sizeof_function_type); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 203 | return new IntegerLiteral(1, Context.IntTy); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 204 | } else if (ArgTy->isVoidType()) { |
| 205 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 206 | } else if (ArgTy->isIncompleteType()) { |
| 207 | std::string TypeName; |
| 208 | ArgTy->getAsString(TypeName); |
| 209 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 210 | diag::err_alignof_incomplete_type, TypeName); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 211 | return new IntegerLiteral(0, Context.IntTy); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 212 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 213 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 214 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 219 | tok::TokenKind Kind, |
| 220 | ExprTy *Input) { |
| 221 | UnaryOperator::Opcode Opc; |
| 222 | switch (Kind) { |
| 223 | default: assert(0 && "Unknown unary op!"); |
| 224 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 225 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 226 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 227 | return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | Action::ExprResult Sema:: |
| 231 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 232 | ExprTy *Idx, SourceLocation RLoc) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 233 | QualType t1 = ((Expr *)Base)->getType(); |
| 234 | QualType t2 = ((Expr *)Idx)->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 235 | |
| 236 | assert(!t1.isNull() && "no type for array base expression"); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 237 | assert(!t2.isNull() && "no type for array index expression"); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 238 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 239 | QualType canonT1 = t1.getCanonicalType(); |
| 240 | QualType canonT2 = t2.getCanonicalType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 241 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 242 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| 243 | // 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] | 244 | // in the subscript position. As a result, we need to derive the array base |
| 245 | // and index from the expression types. |
| 246 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 247 | QualType baseType, indexType; |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 248 | if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) { |
| 249 | baseType = canonT1; |
| 250 | indexType = canonT2; |
| 251 | } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon |
| 252 | baseType = canonT2; |
| 253 | indexType = canonT1; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 254 | } else |
| 255 | return Diag(LLoc, diag::err_typecheck_subscript_value); |
| 256 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 257 | // C99 6.5.2.1p1 |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 258 | if (!indexType->isIntegerType()) |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 259 | return Diag(LLoc, diag::err_typecheck_subscript); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 260 | |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 261 | // FIXME: need to deal with const... |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 262 | QualType resultType; |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 263 | if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) { |
| 264 | resultType = ary->getElementType(); |
| 265 | } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) { |
| 266 | resultType = ary->getPointeeType(); |
| 267 | // in practice, the following check catches trying to index a pointer |
| 268 | // 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] | 269 | if (!resultType->isObjectType()) |
| 270 | return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 271 | } |
| 272 | return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType); |
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, |
| 313 | ExprTy **Args, unsigned NumArgs, |
| 314 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
| 315 | return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs); |
| 316 | } |
| 317 | |
| 318 | Action::ExprResult Sema:: |
| 319 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 320 | SourceLocation RParenLoc, ExprTy *Op) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 321 | // If error parsing type, ignore. |
| 322 | if (Ty == 0) return true; |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 323 | return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | |
| 327 | |
| 328 | // Binary Operators. 'Tok' is the token for the operator. |
| 329 | Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 330 | ExprTy *LHS, ExprTy *RHS) { |
| 331 | BinaryOperator::Opcode Opc; |
| 332 | switch (Kind) { |
| 333 | default: assert(0 && "Unknown binop!"); |
| 334 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 335 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 336 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 337 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 338 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 339 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 340 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 341 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 342 | case tok::less: Opc = BinaryOperator::LT; break; |
| 343 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 344 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 345 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 346 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 347 | case tok::amp: Opc = BinaryOperator::And; break; |
| 348 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 349 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 350 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 351 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 352 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 353 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 354 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 355 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 356 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 357 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 358 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 359 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 360 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 361 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 362 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 363 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 364 | } |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 365 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 366 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 367 | |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 368 | if (BinaryOperator::isMultiplicativeOp(Opc)) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 369 | return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 370 | else if (BinaryOperator::isAdditiveOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 371 | return CheckAdditiveOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 372 | else if (BinaryOperator::isShiftOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 373 | return CheckShiftOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 374 | else if (BinaryOperator::isRelationalOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 375 | return CheckRelationalOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 376 | else if (BinaryOperator::isEqualityOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 377 | return CheckEqualityOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 378 | else if (BinaryOperator::isBitwiseOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 379 | return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 380 | else if (BinaryOperator::isLogicalOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 381 | return CheckLogicalOperands(lhs, rhs, TokLoc, Opc); |
| 382 | |
| 383 | assert(0 && "ParseBinOp() not handling all binary ops properly"); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 387 | /// in the case of a the GNU conditional expr extension. |
| 388 | Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc, |
| 389 | SourceLocation ColonLoc, |
| 390 | ExprTy *Cond, ExprTy *LHS, |
| 391 | ExprTy *RHS) { |
| 392 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS); |
| 393 | } |
| 394 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 395 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 396 | /// operators (C99 6.3). The conversions of array and function types are |
| 397 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 398 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 399 | /// In these instances, this routine should *not* be called. |
| 400 | QualType Sema::UsualUnaryConversion(QualType t) { |
| 401 | assert(!t.isNull() && "UsualUnaryConversion - missing type"); |
Steve Naroff | 4b7ce03 | 2007-04-20 22:26:17 +0000 | [diff] [blame] | 402 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 403 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 404 | return Context.IntTy; |
| 405 | else if (t->isFunctionType()) // C99 6.3.2.1p4 |
| 406 | return Context.getPointerType(t); |
| 407 | else if (t->isArrayType()) // C99 6.3.2.1p3 |
| 408 | return Context.getPointerType(cast<ArrayType>(t)->getElementType()); |
| 409 | return t; |
| 410 | } |
| 411 | |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 412 | /// GetFloatingRank - Helper function for UsualArithmeticConversions(). |
| 413 | static inline int GetFloatingRank(QualType t) { |
| 414 | if (const BuiltinType *BT = dyn_cast<BuiltinType>(t.getCanonicalType())) { |
| 415 | switch (BT->getKind()) { |
| 416 | case BuiltinType::Float: |
| 417 | case BuiltinType::FloatComplex: |
| 418 | return 1; |
| 419 | case BuiltinType::Double: |
| 420 | case BuiltinType::DoubleComplex: |
| 421 | return 2; |
| 422 | case BuiltinType::LongDouble: |
| 423 | case BuiltinType::LongDoubleComplex: |
| 424 | return 3; |
| 425 | default: |
| 426 | assert(0 && "getFloatingPointRank(): not a floating type"); |
| 427 | } |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /// ConvertFloatingRankToComplexType - Another helper for converting floats. |
| 433 | static inline QualType ConvertFloatingRankToComplexType(int rank, |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 434 | ASTContext &C) { |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 435 | switch (rank) { |
| 436 | case 1: |
| 437 | return C.FloatComplexTy; |
| 438 | case 2: |
| 439 | return C.DoubleComplexTy; |
| 440 | case 3: |
| 441 | return C.LongDoubleComplexTy; |
| 442 | default: |
| 443 | assert(0 && "convertRankToComplex(): illegal value for rank"); |
| 444 | } |
| 445 | } |
| 446 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 447 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 448 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 449 | /// routine returns the first non-arithmetic type found. The client is |
| 450 | /// responsible for emitting appropriate error diagnostics. |
| 451 | QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) { |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 452 | QualType lhs = UsualUnaryConversion(t1); |
| 453 | QualType rhs = UsualUnaryConversion(t2); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 454 | |
| 455 | // if either operand is not of arithmetic type, no conversion is possible. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 456 | if (!lhs->isArithmeticType()) |
| 457 | return lhs; |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 458 | if (!rhs->isArithmeticType()) |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 459 | return rhs; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 460 | |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 461 | // if both arithmetic types are identical, no conversion is needed. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 462 | if (lhs == rhs) |
| 463 | return lhs; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 464 | |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 465 | // at this point, we have two different arithmetic types. |
| 466 | |
| 467 | // Handle complex types first (C99 6.3.1.8p1). |
| 468 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 469 | // if we have an integer operand, the result is the complex type. |
| 470 | if (rhs->isIntegerType()) |
| 471 | return lhs; |
| 472 | if (lhs->isIntegerType()) |
| 473 | return rhs; |
| 474 | |
| 475 | // the following code handles three different combinations: |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 476 | // complex/complex, complex/float, float/complex. When both operands |
| 477 | // are complex, the shorter operand is converted to the type of the longer, |
| 478 | // and that is the type of the result. This corresponds to what is done |
| 479 | // when combining two real floating-point operands. The fun begins when |
| 480 | // size promotion occur across type domains. GetFloatingRank & |
| 481 | // ConvertFloatingRankToComplexType handle this without enumerating all |
| 482 | // permutations. It also allows us to add new types without breakage. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 483 | int lhsRank = GetFloatingRank(lhs); |
| 484 | int rhsRank = GetFloatingRank(rhs); |
| 485 | |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 486 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 487 | // floating-point type, the less precise type is converted, within it's |
| 488 | // real or complex domain, to the precision of the other type. For example, |
| 489 | // when combining a "long double" with a "double _Complex", the |
| 490 | // "double _Complex" is promoted to "long double _Complex". |
| 491 | return ConvertFloatingRankToComplexType(std::max(lhsRank,rhsRank), Context); |
Steve Naroff | bf223ba | 2007-04-24 20:56:26 +0000 | [diff] [blame] | 492 | } |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 493 | // Now handle "real" floating types (i.e. float, double, long double). |
| 494 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 495 | // if we have an integer operand, the result is the real floating type. |
| 496 | if (rhs->isIntegerType()) |
| 497 | return lhs; |
| 498 | if (lhs->isIntegerType()) |
| 499 | return rhs; |
| 500 | |
| 501 | // we have two real floating types, float/complex combos were handled above. |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 502 | return GetFloatingRank(lhs) >= GetFloatingRank(rhs) ? lhs : rhs; |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 503 | } |
| 504 | // Lastly, handle two integers. |
| 505 | bool t1Unsigned = lhs->isUnsignedIntegerType(); |
| 506 | bool t2Unsigned = rhs->isUnsignedIntegerType(); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 507 | |
| 508 | if (t1Unsigned && t2Unsigned) |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 509 | return lhs; // FIXME: return the unsigned type with the greatest rank |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 510 | else if (!t1Unsigned && !t2Unsigned) |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 511 | return lhs; // FIXME: return the signed type with the greatest rank |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 512 | else |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 513 | return lhs; // FIXME: we have a mixture... |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 516 | Action::ExprResult Sema::CheckMultiplicativeOperands( |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 517 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 518 | { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 519 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 520 | |
| 521 | if ((BinaryOperator::Opcode)code == BinaryOperator::Rem) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 522 | if (!resType->isIntegerType()) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 523 | return Diag(loc, diag::err_typecheck_invalid_operands); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 524 | } else { // *, / |
| 525 | if (!resType->isArithmeticType()) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 526 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 527 | } |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 528 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 531 | Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6 |
| 532 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 533 | { |
| 534 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, Context.IntTy); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 537 | Action::ExprResult Sema::CheckShiftOperands( // C99 6.5.7 |
| 538 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 539 | { |
| 540 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
| 541 | |
| 542 | if (!resType->isIntegerType()) |
| 543 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 544 | |
| 545 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 548 | Action::ExprResult Sema::CheckRelationalOperands( // C99 6.5.8 |
| 549 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 550 | { |
| 551 | QualType lType = lex->getType(), rType = rex->getType(); |
| 552 | |
| 553 | if (lType->isRealType() && rType->isRealType()) |
| 554 | ; |
| 555 | else if (lType->isPointerType() && rType->isPointerType()) |
| 556 | ; |
| 557 | else { |
| 558 | // The following test is for GCC compatibility. |
| 559 | if (lType->isIntegerType() || rType->isIntegerType()) |
| 560 | return Diag(loc, diag::err_typecheck_comparison_of_pointer_integer); |
| 561 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 562 | } |
| 563 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, |
| 564 | Context.IntTy); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 567 | Action::ExprResult Sema::CheckEqualityOperands( // C99 6.5.9 |
| 568 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 569 | { |
| 570 | QualType lType = lex->getType(), rType = rex->getType(); |
| 571 | |
| 572 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 573 | ; |
| 574 | else if (lType->isPointerType() && rType->isPointerType()) |
| 575 | ; |
| 576 | else { |
| 577 | // The following test is for GCC compatibility. |
| 578 | if (lType->isIntegerType() || rType->isIntegerType()) |
| 579 | return Diag(loc, diag::err_typecheck_comparison_of_pointer_integer); |
| 580 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 581 | } |
| 582 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, |
| 583 | Context.IntTy); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 586 | Action::ExprResult Sema::CheckBitwiseOperands( |
| 587 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 588 | { |
| 589 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
| 590 | |
| 591 | if (!resType->isIntegerType()) |
| 592 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 593 | |
| 594 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 597 | Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
| 598 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 599 | { |
| 600 | return new BinaryOperator(lex, rex, (BinaryOperator::Opcode)code); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | Action::ExprResult |
| 604 | Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc, |
Steve Naroff | 7840336 | 2007-04-02 22:55:05 +0000 | [diff] [blame] | 605 | unsigned OpCode) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 606 | QualType qType = op->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 607 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 608 | assert(!qType.isNull() && "no type for increment/decrement expression"); |
| 609 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 610 | QualType canonType = qType.getCanonicalType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 611 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 612 | // C99 6.5.2.4p1 |
| 613 | if (const PointerType *pt = dyn_cast<PointerType>(canonType)) { |
| 614 | if (!pt->getPointeeType()->isObjectType()) // C99 6.5.2.4p2, 6.5.6p2 |
| 615 | return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, qType); |
| 616 | } else if (!canonType->isRealType()) { |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 617 | // FIXME: Allow Complex as a GCC extension. |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 618 | return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, qType); |
| 619 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 620 | // At this point, we know we have a real or pointer type. As a result, the |
| 621 | // following predicate is overkill (i.e. it will check for types we know we |
| 622 | // don't have in this context). Nevertheless, we model the C99 spec closely. |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 623 | if (!canonType.isModifiableLvalue()) |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 624 | return Diag(OpLoc, diag::err_typecheck_not_modifiable, qType); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 625 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 626 | return new UnaryOperator(op, (UnaryOperator::Opcode)OpCode, qType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 629 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 630 | /// This routine allows us to typecheck complex/recursive expressions |
| 631 | /// where the declaration is needed for type checking. Here are some |
| 632 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 633 | static Decl *getPrimaryDeclaration(Expr *e) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 634 | switch (e->getStmtClass()) { |
| 635 | case Stmt::DeclRefExprClass: |
| 636 | return cast<DeclRefExpr>(e)->getDecl(); |
| 637 | case Stmt::MemberExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 638 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 639 | case Stmt::ArraySubscriptExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 640 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 641 | case Stmt::CallExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 642 | return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 643 | case Stmt::UnaryOperatorClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 644 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 645 | case Stmt::ParenExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 646 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 647 | default: |
| 648 | return 0; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 653 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 654 | /// object cannot be declared with storage class register or be a bit field. |
| 655 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 656 | /// operator, and its result is never an lvalue. |
| 657 | Action::ExprResult |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 658 | Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 659 | Decl *dcl = getPrimaryDeclaration(op); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 660 | |
| 661 | if (!op->isLvalue()) { |
| 662 | if (dcl && isa<FunctionDecl>(dcl)) |
| 663 | ; // C99 6.5.3.2p1: Allow function designators. |
| 664 | else |
| 665 | return Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof); |
| 666 | } else if (dcl) { |
| 667 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 668 | // with the register storage-class specifier. |
| 669 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 670 | if (vd->getStorageClass() == VarDecl::Register) |
| 671 | return Diag(OpLoc, diag::err_typecheck_address_of_register); |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 672 | } else |
| 673 | assert(0 && "Unknown/unexpected decl type"); |
| 674 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 675 | // FIXME: add check for bitfields! |
| 676 | } |
| 677 | // If the operand has type "type", the result has type "pointer to type". |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 678 | return new UnaryOperator(op, UnaryOperator::AddrOf, |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 679 | Context.getPointerType(op->getType())); |
| 680 | } |
| 681 | |
| 682 | Action::ExprResult |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 683 | Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 684 | QualType qType = op->getType(); |
| 685 | |
| 686 | assert(!qType.isNull() && "no type for * expression"); |
| 687 | |
| 688 | QualType canonType = qType.getCanonicalType(); |
| 689 | |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame^] | 690 | return new UnaryOperator(op, UnaryOperator::Deref, QualType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 691 | } |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 692 | |
| 693 | /// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3). |
| 694 | Action::ExprResult |
| 695 | Sema::CheckArithmeticOperand(Expr *op, SourceLocation OpLoc, unsigned Opc) { |
| 696 | QualType resultType = UsualUnaryConversion(op->getType()); |
| 697 | |
| 698 | switch (Opc) { |
| 699 | case UnaryOperator::Plus: |
| 700 | case UnaryOperator::Minus: |
| 701 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 702 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 703 | break; |
| 704 | case UnaryOperator::Not: // bitwise complement |
| 705 | if (!resultType->isIntegerType()) // C99 6.5.3.3p1 |
| 706 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 707 | break; |
| 708 | case UnaryOperator::LNot: // logical negation |
| 709 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 710 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 711 | break; |
| 712 | } |
| 713 | return new UnaryOperator(op, (UnaryOperator::Opcode)Opc, resultType); |
| 714 | } |