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 | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 28 | // Sema.h avoids including Expr.h. As a result, all the Check* functions take |
| 29 | // an unsigned which is really an enum. These typedefs provide a short hand |
| 30 | // notiation for casting (to keep the lines within 80 columns:-) |
| 31 | typedef BinaryOperator::Opcode BOP; |
| 32 | typedef UnaryOperator::Opcode UOP; |
| 33 | |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 34 | /// ParseStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 35 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 36 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 37 | /// multiple tokens. However, the common case is that StringToks points to one |
| 38 | /// string. |
| 39 | /// |
| 40 | Action::ExprResult |
Steve Naroff | df7855b | 2007-02-21 23:46:25 +0000 | [diff] [blame] | 41 | Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 42 | assert(NumStringToks && "Must have at least one string!"); |
| 43 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 44 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 45 | if (Literal.hadError) |
| 46 | return ExprResult(true); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 48 | SmallVector<SourceLocation, 4> StringTokLocs; |
| 49 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 50 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 51 | |
| 52 | // FIXME: handle wchar_t |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 53 | QualType t = Context.getPointerType(Context.CharTy); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 55 | // FIXME: use factory. |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 56 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 57 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 58 | Literal.AnyWide, t); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 61 | |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 62 | /// ParseIdentifierExpr - The parser read an identifier in expression context, |
| 63 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
| 64 | /// identifier is used in an function call context. |
| 65 | Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc, |
| 66 | IdentifierInfo &II, |
| 67 | bool HasTrailingLParen) { |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 68 | // Could be enum-constant or decl. |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 69 | Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 70 | if (D == 0) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 71 | // Otherwise, this could be an implicitly declared function reference (legal |
Chris Lattner | 9561a0b | 2007-01-28 08:20:04 +0000 | [diff] [blame] | 72 | // in C90, extension in C99). |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 73 | if (HasTrailingLParen && |
| 74 | // Not in C++. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 75 | !getLangOptions().CPlusPlus) |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 76 | D = ImplicitlyDefineFunction(Loc, II, S); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 77 | else { |
Chris Lattner | ac18be9 | 2006-11-20 06:49:47 +0000 | [diff] [blame] | 78 | // If this name wasn't predeclared and if this is not a function call, |
| 79 | // diagnose the problem. |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 80 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 81 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 84 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 85 | return new DeclRefExpr(VD, VD->getType()); |
| 86 | if (isa<TypedefDecl>(D)) |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 87 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
| 88 | |
| 89 | assert(0 && "Invalid decl"); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 92 | Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc, |
| 93 | tok::TokenKind Kind) { |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 94 | switch (Kind) { |
| 95 | default: |
| 96 | assert(0 && "Unknown simple primary expr!"); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 97 | // TODO: MOVE this to be some other callback. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 98 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 99 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 100 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 101 | return 0; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 105 | Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) { |
| 106 | SmallString<16> CharBuffer; |
| 107 | CharBuffer.resize(Tok.getLength()); |
| 108 | const char *ThisTokBegin = &CharBuffer[0]; |
| 109 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 110 | |
| 111 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 112 | Tok.getLocation(), PP); |
| 113 | if (Literal.hadError()) |
| 114 | return ExprResult(true); |
| 115 | return new CharacterLiteral(Literal.getValue(), Context.IntTy); |
| 116 | } |
| 117 | |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 118 | Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 119 | // fast path for a single digit (which is quite common). A single digit |
| 120 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 121 | if (Tok.getLength() == 1) { |
| 122 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
| 123 | return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy)); |
| 124 | } |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 125 | SmallString<512> IntegerBuffer; |
| 126 | IntegerBuffer.resize(Tok.getLength()); |
| 127 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 128 | |
| 129 | // Get the spelling of the token, which eliminates trigraphs, etc. Notes: |
| 130 | // - We know that ThisTokBuf points to a buffer that is big enough for the |
| 131 | // whole token and 'spelled' tokens can only shrink. |
| 132 | // - In practice, the local buffer is only used when the spelling doesn't |
| 133 | // match the original token (which is rare). The common case simply returns |
| 134 | // a pointer to a *constant* buffer (avoiding a copy). |
| 135 | |
| 136 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 137 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 138 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 139 | if (Literal.hadError) |
| 140 | return ExprResult(true); |
| 141 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 142 | if (Literal.isIntegerLiteral()) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 143 | QualType t; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 144 | if (Literal.hasSuffix()) { |
| 145 | if (Literal.isLong) |
| 146 | t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy; |
| 147 | else if (Literal.isLongLong) |
| 148 | t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy; |
| 149 | else |
| 150 | t = Context.UnsignedIntTy; |
| 151 | } else { |
| 152 | t = Context.IntTy; // implicit type is "int" |
| 153 | } |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 154 | uintmax_t val; |
| 155 | if (Literal.GetIntegerValue(val)) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 156 | return new IntegerLiteral(val, t); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 157 | } |
| 158 | } else if (Literal.isFloatingLiteral()) { |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 159 | // FIXME: fill in the value and compute the real type... |
| 160 | return new FloatingLiteral(7.7, Context.FloatTy); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 161 | } |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 162 | return ExprResult(true); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R, |
| 166 | ExprTy *Val) { |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 167 | Expr *e = (Expr *)Val; |
| 168 | assert((e != 0) && "ParseParenExpr() missing expr"); |
| 169 | return e; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | // Unary Operators. 'Tok' is the token for the operator. |
| 174 | Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
| 175 | ExprTy *Input) { |
| 176 | UnaryOperator::Opcode Opc; |
| 177 | switch (Op) { |
| 178 | default: assert(0 && "Unknown unary op!"); |
| 179 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 180 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 181 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 182 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 183 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 184 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 185 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 186 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 187 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 188 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 189 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 190 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 191 | case tok::ampamp: Opc = UnaryOperator::AddrLabel; break; |
| 192 | case tok::kw___extension__: |
| 193 | return Input; |
| 194 | //Opc = UnaryOperator::Extension; |
| 195 | //break; |
| 196 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 197 | if (Opc == UnaryOperator::PreInc || Opc == UnaryOperator::PreDec) |
| 198 | return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 199 | else if (Opc == UnaryOperator::AddrOf) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 200 | return CheckAddressOfOperand((Expr *)Input, OpLoc); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 201 | else if (Opc == UnaryOperator::Deref) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 202 | return CheckIndirectionOperand((Expr *)Input, OpLoc); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 203 | else if (UnaryOperator::isArithmeticOp(Opc)) |
| 204 | return CheckArithmeticOperand((Expr *)Input, OpLoc, Opc); |
| 205 | |
| 206 | // will go away when all cases are handled... |
| 207 | return new UnaryOperator((Expr *)Input, Opc, QualType()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | Action::ExprResult Sema:: |
| 211 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 212 | SourceLocation LParenLoc, TypeTy *Ty, |
| 213 | SourceLocation RParenLoc) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 214 | // If error parsing type, ignore. |
| 215 | if (Ty == 0) return true; |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 216 | |
| 217 | // Verify that this is a valid expression. |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 218 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 219 | |
| 220 | if (isa<FunctionType>(ArgTy) && isSizeof) { |
| 221 | // alignof(function) is allowed. |
| 222 | Diag(OpLoc, diag::ext_sizeof_function_type); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 223 | return new IntegerLiteral(1, Context.IntTy); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 224 | } else if (ArgTy->isVoidType()) { |
| 225 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 226 | } else if (ArgTy->isIncompleteType()) { |
| 227 | std::string TypeName; |
| 228 | ArgTy->getAsString(TypeName); |
| 229 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 230 | diag::err_alignof_incomplete_type, TypeName); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 231 | return new IntegerLiteral(0, Context.IntTy); |
Chris Lattner | 6531c10 | 2007-01-23 22:29:49 +0000 | [diff] [blame] | 232 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 233 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 234 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, Context.getSizeType()); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
| 238 | Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 239 | tok::TokenKind Kind, |
| 240 | ExprTy *Input) { |
| 241 | UnaryOperator::Opcode Opc; |
| 242 | switch (Kind) { |
| 243 | default: assert(0 && "Unknown unary op!"); |
| 244 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 245 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 246 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 247 | return CheckIncrementDecrementOperand((Expr *)Input, OpLoc, Opc); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | Action::ExprResult Sema:: |
| 251 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 252 | ExprTy *Idx, SourceLocation RLoc) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 253 | QualType t1 = ((Expr *)Base)->getType(); |
| 254 | QualType t2 = ((Expr *)Idx)->getType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 255 | |
| 256 | assert(!t1.isNull() && "no type for array base expression"); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 257 | assert(!t2.isNull() && "no type for array index expression"); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 258 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 259 | QualType canonT1 = t1.getCanonicalType(); |
| 260 | QualType canonT2 = t2.getCanonicalType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 261 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 262 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| 263 | // 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] | 264 | // in the subscript position. As a result, we need to derive the array base |
| 265 | // and index from the expression types. |
| 266 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 267 | QualType baseType, indexType; |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 268 | if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) { |
| 269 | baseType = canonT1; |
| 270 | indexType = canonT2; |
| 271 | } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon |
| 272 | baseType = canonT2; |
| 273 | indexType = canonT1; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 274 | } else |
| 275 | return Diag(LLoc, diag::err_typecheck_subscript_value); |
| 276 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 277 | // C99 6.5.2.1p1 |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 278 | if (!indexType->isIntegerType()) |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 279 | return Diag(LLoc, diag::err_typecheck_subscript); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 280 | |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 281 | // FIXME: need to deal with const... |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 282 | QualType resultType; |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 283 | if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) { |
| 284 | resultType = ary->getElementType(); |
| 285 | } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) { |
| 286 | resultType = ary->getPointeeType(); |
| 287 | // in practice, the following check catches trying to index a pointer |
| 288 | // 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] | 289 | if (!resultType->isObjectType()) |
| 290 | return Diag(LLoc, diag::err_typecheck_subscript_not_object, baseType); |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 291 | } |
| 292 | return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | Action::ExprResult Sema:: |
| 296 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 297 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 298 | IdentifierInfo &Member) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 299 | QualType qualifiedType = ((Expr *)Base)->getType(); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 300 | |
| 301 | assert(!qualifiedType.isNull() && "no type for member expression"); |
| 302 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 303 | QualType canonType = qualifiedType.getCanonicalType(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 304 | |
| 305 | if (OpKind == tok::arrow) { |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 306 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) { |
| 307 | qualifiedType = PT->getPointeeType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 308 | canonType = qualifiedType.getCanonicalType(); |
Steve Naroff | ca8f712 | 2007-04-01 01:41:35 +0000 | [diff] [blame] | 309 | } else |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 310 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow); |
| 311 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 312 | if (!isa<RecordType>(canonType)) |
| 313 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion); |
| 314 | |
| 315 | // get the struct/union definition from the type. |
| 316 | RecordDecl *RD = cast<RecordType>(canonType)->getDecl(); |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 317 | |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 318 | if (canonType->isIncompleteType()) |
| 319 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName()); |
Steve Naroff | cc32142 | 2007-03-26 23:09:51 +0000 | [diff] [blame] | 320 | |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 321 | FieldDecl *MemberDecl = RD->getMember(&Member); |
| 322 | if (!MemberDecl) |
| 323 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName()); |
| 324 | |
| 325 | return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 329 | /// This provides the location of the left/right parens and a list of comma |
| 330 | /// locations. |
| 331 | Action::ExprResult Sema:: |
| 332 | ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc, |
| 333 | ExprTy **Args, unsigned NumArgs, |
| 334 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 335 | QualType qType = ((Expr *)Fn)->getType(); |
| 336 | |
| 337 | assert(!qType.isNull() && "no type for function call expression"); |
| 338 | |
| 339 | QualType canonType = qType.getCanonicalType(); |
| 340 | QualType resultType; |
| 341 | |
| 342 | if (const FunctionType *funcT = dyn_cast<FunctionType>(canonType)) { |
| 343 | resultType = funcT->getResultType(); |
| 344 | } |
| 345 | return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgs, resultType); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | Action::ExprResult Sema:: |
| 349 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 350 | SourceLocation RParenLoc, ExprTy *Op) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 351 | // If error parsing type, ignore. |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 352 | assert((Ty != 0) && "ParseCastExpr(): missing type"); |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 353 | return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | |
| 358 | // Binary Operators. 'Tok' is the token for the operator. |
| 359 | Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 360 | ExprTy *LHS, ExprTy *RHS) { |
| 361 | BinaryOperator::Opcode Opc; |
| 362 | switch (Kind) { |
| 363 | default: assert(0 && "Unknown binop!"); |
| 364 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 365 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 366 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 367 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 368 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 369 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 370 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 371 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 372 | case tok::less: Opc = BinaryOperator::LT; break; |
| 373 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 374 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 375 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 376 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 377 | case tok::amp: Opc = BinaryOperator::And; break; |
| 378 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 379 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 380 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 381 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 382 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 383 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 384 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 385 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 386 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 387 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 388 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 389 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 390 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 391 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 392 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 393 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 394 | } |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 395 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 396 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 397 | |
| 398 | assert((lhs != 0) && "ParseBinOp(): missing left expression"); |
| 399 | assert((rhs != 0) && "ParseBinOp(): missing right expression"); |
| 400 | |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 401 | if (BinaryOperator::isMultiplicativeOp(Opc)) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 402 | return CheckMultiplicativeOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 403 | else if (BinaryOperator::isAdditiveOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 404 | return CheckAdditiveOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 405 | else if (BinaryOperator::isShiftOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 406 | return CheckShiftOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 407 | else if (BinaryOperator::isRelationalOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 408 | return CheckRelationalOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 409 | else if (BinaryOperator::isEqualityOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 410 | return CheckEqualityOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 411 | else if (BinaryOperator::isBitwiseOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 412 | return CheckBitwiseOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 413 | else if (BinaryOperator::isLogicalOp(Opc)) |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 414 | return CheckLogicalOperands(lhs, rhs, TokLoc, Opc); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 415 | else if (BinaryOperator::isAssignmentOp(Opc)) |
| 416 | return CheckAssignmentOperands(lhs, rhs, TokLoc, Opc); |
| 417 | else if (Opc == BinaryOperator::Comma) |
| 418 | return CheckCommaOperands(lhs, rhs, TokLoc); |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 419 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 420 | assert(0 && "ParseBinOp(): illegal binary op"); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 424 | /// in the case of a the GNU conditional expr extension. |
| 425 | Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc, |
| 426 | SourceLocation ColonLoc, |
| 427 | ExprTy *Cond, ExprTy *LHS, |
| 428 | ExprTy *RHS) { |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 429 | QualType lhs = ((Expr *)LHS)->getType(); |
| 430 | QualType rhs = ((Expr *)RHS)->getType(); |
| 431 | |
| 432 | assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type"); |
| 433 | assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type"); |
| 434 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 435 | QualType canonType = rhs.getCanonicalType(); // FIXME |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 436 | return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, canonType); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 439 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 440 | /// operators (C99 6.3). The conversions of array and function types are |
| 441 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 442 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 443 | /// In these instances, this routine should *not* be called. |
| 444 | QualType Sema::UsualUnaryConversion(QualType t) { |
| 445 | assert(!t.isNull() && "UsualUnaryConversion - missing type"); |
Steve Naroff | 4b7ce03 | 2007-04-20 22:26:17 +0000 | [diff] [blame] | 446 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 447 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
| 448 | return Context.IntTy; |
| 449 | else if (t->isFunctionType()) // C99 6.3.2.1p4 |
| 450 | return Context.getPointerType(t); |
| 451 | else if (t->isArrayType()) // C99 6.3.2.1p3 |
| 452 | return Context.getPointerType(cast<ArrayType>(t)->getElementType()); |
| 453 | return t; |
| 454 | } |
| 455 | |
| 456 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 457 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 458 | /// routine returns the first non-arithmetic type found. The client is |
| 459 | /// responsible for emitting appropriate error diagnostics. |
| 460 | QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) { |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 461 | QualType lhs = UsualUnaryConversion(t1); |
| 462 | QualType rhs = UsualUnaryConversion(t2); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 463 | |
| 464 | // if either operand is not of arithmetic type, no conversion is possible. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 465 | if (!lhs->isArithmeticType()) |
| 466 | return lhs; |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 467 | if (!rhs->isArithmeticType()) |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 468 | return rhs; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 469 | |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 470 | // if both arithmetic types are identical, no conversion is needed. |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 471 | if (lhs == rhs) |
| 472 | return lhs; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 473 | |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 474 | // at this point, we have two different arithmetic types. |
| 475 | |
| 476 | // Handle complex types first (C99 6.3.1.8p1). |
| 477 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 478 | // if we have an integer operand, the result is the complex type. |
| 479 | if (rhs->isIntegerType()) |
| 480 | return lhs; |
| 481 | if (lhs->isIntegerType()) |
| 482 | return rhs; |
| 483 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 484 | return Context.maxComplexType(lhs, rhs); |
Steve Naroff | bf223ba | 2007-04-24 20:56:26 +0000 | [diff] [blame] | 485 | } |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 486 | // Now handle "real" floating types (i.e. float, double, long double). |
| 487 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 488 | // if we have an integer operand, the result is the real floating type. |
| 489 | if (rhs->isIntegerType()) |
| 490 | return lhs; |
| 491 | if (lhs->isIntegerType()) |
| 492 | return rhs; |
| 493 | |
| 494 | // we have two real floating types, float/complex combos were handled above. |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 495 | return Context.maxFloatingType(lhs, rhs); |
Steve Naroff | b01bbe3 | 2007-04-25 01:22:31 +0000 | [diff] [blame] | 496 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 497 | return Context.maxIntegerType(lhs, rhs); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 500 | QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType, |
| 501 | Expr *rex, SourceLocation loc) { |
| 502 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
| 503 | return lhsType; |
| 504 | else if (lhsType->isPointerType()) { |
| 505 | if (rhsType->isIntegerType()) { |
| 506 | // check for null pointer constant (C99 6.3.2.3p3) |
| 507 | const IntegerLiteral *constant = dyn_cast<IntegerLiteral>(rex); |
| 508 | if (!constant || constant->getValue() != 0) |
| 509 | Diag(loc, diag::ext_typecheck_assign_pointer_from_int); |
| 510 | return lhsType; |
| 511 | } |
| 512 | // FIXME: make sure the qualifier are matching |
| 513 | if (rhsType->isPointerType()) { |
| 514 | if (!Type::pointerTypesAreCompatible(lhsType, rhsType)) |
| 515 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer); |
| 516 | return lhsType; |
| 517 | } |
| 518 | } else if (rhsType->isPointerType()) { |
| 519 | if (lhsType->isIntegerType()) { |
| 520 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 521 | if (lhsType != Context.BoolTy) |
| 522 | Diag(loc, diag::ext_typecheck_assign_int_from_pointer); |
| 523 | return lhsType; |
| 524 | } |
| 525 | // - both operands are pointers to qualified or unqualified versions of |
| 526 | // compatible types, and the type pointed to by the left has *all* the |
| 527 | // qualifiers of the type pointed to by the right; |
| 528 | if (lhsType->isPointerType()) { |
| 529 | if (!Type::pointerTypesAreCompatible(lhsType, rhsType)) |
| 530 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer); |
| 531 | return lhsType; |
| 532 | } |
| 533 | } else if (lhsType->isArrayType() && rhsType->isArrayType()) { |
| 534 | /// int aryInt[3], aryInt2[3]; |
| 535 | /// aryInt = aryInt2; // gcc considers this an error (FIXME?) |
| 536 | if (Type::arrayTypesAreCompatible(lhsType, rhsType)) |
| 537 | return lhsType; |
| 538 | } else if (lhsType->isStructureType() && rhsType->isStructureType()) { |
| 539 | if (Type::structureTypesAreCompatible(lhsType, rhsType)) |
| 540 | return lhsType; |
| 541 | } else if (lhsType->isUnionType() && rhsType->isUnionType()) { |
| 542 | if (Type::unionTypesAreCompatible(lhsType, rhsType)) |
| 543 | return lhsType; |
| 544 | } else if (lhsType->isFunctionType() && rhsType->isFunctionType()) { |
| 545 | if (Type::functionTypesAreCompatible(lhsType, rhsType)) |
| 546 | return lhsType; |
| 547 | } |
| 548 | return QualType(); // incompatible |
| 549 | } |
| 550 | |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 551 | Action::ExprResult Sema::CheckMultiplicativeOperands( |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 552 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 553 | { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 554 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 555 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 556 | if ((BOP)code == BinaryOperator::Rem) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 557 | if (!resType->isIntegerType()) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 558 | return Diag(loc, diag::err_typecheck_invalid_operands); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 559 | } else { // *, / |
| 560 | if (!resType->isArithmeticType()) |
Steve Naroff | 5c10d4b | 2007-04-20 23:42:24 +0000 | [diff] [blame] | 561 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 562 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 563 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 566 | Action::ExprResult Sema::CheckAdditiveOperands( // C99 6.5.6 |
| 567 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 568 | { |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 569 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 570 | QualType resType = UsualArithmeticConversions(lhsType, rhsType); |
| 571 | |
| 572 | // handle the common case first (both operands are arithmetic). |
| 573 | if (resType->isArithmeticType()) |
| 574 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
| 575 | else { |
| 576 | if ((BOP)code == BinaryOperator::Add) { |
| 577 | if ((lhsType->isPointerType() && rhsType->isIntegerType()) || |
| 578 | (lhsType->isIntegerType() && rhsType->isPointerType())) |
| 579 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
| 580 | } else { // - |
| 581 | if ((lhsType->isPointerType() && rhsType->isIntegerType()) || |
| 582 | (lhsType->isPointerType() && rhsType->isPointerType())) |
| 583 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
| 584 | } |
| 585 | } |
| 586 | return Diag(loc, diag::err_typecheck_invalid_operands); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 589 | Action::ExprResult Sema::CheckShiftOperands( // C99 6.5.7 |
| 590 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 591 | { |
| 592 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
| 593 | |
| 594 | if (!resType->isIntegerType()) |
| 595 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 596 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 597 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 600 | Action::ExprResult Sema::CheckRelationalOperands( // C99 6.5.8 |
| 601 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 602 | { |
| 603 | QualType lType = lex->getType(), rType = rex->getType(); |
| 604 | |
| 605 | if (lType->isRealType() && rType->isRealType()) |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 606 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
| 607 | |
| 608 | if (lType->isPointerType() && rType->isPointerType()) |
| 609 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
| 610 | |
| 611 | if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension. |
| 612 | return Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer); |
| 613 | return Diag(loc, diag::err_typecheck_invalid_operands); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 616 | Action::ExprResult Sema::CheckEqualityOperands( // C99 6.5.9 |
| 617 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 618 | { |
| 619 | QualType lType = lex->getType(), rType = rex->getType(); |
| 620 | |
| 621 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 622 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
| 623 | |
| 624 | if (lType->isPointerType() && rType->isPointerType()) |
| 625 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
| 626 | |
| 627 | if (lType->isIntegerType() || rType->isIntegerType()) // GCC extension. |
| 628 | return Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer); |
| 629 | return Diag(loc, diag::err_typecheck_invalid_operands); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 632 | Action::ExprResult Sema::CheckBitwiseOperands( |
| 633 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 634 | { |
| 635 | QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType()); |
| 636 | |
| 637 | if (!resType->isIntegerType()) |
| 638 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 639 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 640 | return new BinaryOperator(lex, rex, (BOP)code, resType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 643 | Action::ExprResult Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
| 644 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 645 | { |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 646 | QualType lhsType = UsualUnaryConversion(lex->getType()); |
| 647 | QualType rhsType = UsualUnaryConversion(rex->getType()); |
| 648 | |
| 649 | if (!lhsType->isScalarType() || !rhsType->isScalarType()) |
| 650 | return Diag(loc, diag::err_typecheck_invalid_operands); |
| 651 | |
| 652 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Steve Naroff | dd92b93 | 2007-05-02 02:47:33 +0000 | [diff] [blame] | 655 | /// CheckAssignmentOperands (C99 6.5.16) - This routine currently |
| 656 | /// has code to accommodate several GCC extensions when type checking |
| 657 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 658 | /// |
| 659 | /// int a, *pint; |
| 660 | /// short *pshort; |
| 661 | /// struct foo *pfoo; |
| 662 | /// |
| 663 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 664 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 665 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 666 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 667 | /// |
| 668 | /// As a result, the code for dealing with pointers is more complex than the |
| 669 | /// C99 spec dictates. |
| 670 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 671 | /// |
| 672 | Action::ExprResult Sema::CheckAssignmentOperands( |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 673 | Expr *lex, Expr *rex, SourceLocation loc, unsigned code) |
| 674 | { |
Steve Naroff | 0af9120 | 2007-04-27 21:51:21 +0000 | [diff] [blame] | 675 | QualType lhsType = lex->getType(); |
| 676 | QualType rhsType = rex->getType(); |
| 677 | |
Steve Naroff | dd92b93 | 2007-05-02 02:47:33 +0000 | [diff] [blame] | 678 | if ((BOP)code == BinaryOperator::Assign) { // C99 6.5.16.1 |
| 679 | if (lhsType.isConstQualified()) |
| 680 | return Diag(loc, diag::err_typecheck_assign_const); |
| 681 | |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 682 | if (lhsType == rhsType) // common case, fast path... |
Steve Naroff | dd92b93 | 2007-05-02 02:47:33 +0000 | [diff] [blame] | 683 | return new BinaryOperator(lex, rex, (BOP)code, lhsType); |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 684 | |
| 685 | QualType result = UsualAssignmentConversions(lhsType, rhsType, rex, loc); |
| 686 | if (result.isNull()) |
| 687 | return Diag(loc, diag::err_typecheck_assign_incompatible); |
| 688 | else |
| 689 | return new BinaryOperator(lex, rex, (BOP)code, result); |
Steve Naroff | dd92b93 | 2007-05-02 02:47:33 +0000 | [diff] [blame] | 690 | } |
Steve Naroff | dd92b93 | 2007-05-02 02:47:33 +0000 | [diff] [blame] | 691 | // FIXME: type check compound assignments... |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 692 | return new BinaryOperator(lex, rex, (BOP)code, Context.IntTy); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | Action::ExprResult Sema::CheckCommaOperands( // C99 6.5.17 |
| 696 | Expr *lex, Expr *rex, SourceLocation loc) |
| 697 | { |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 698 | QualType rhsType = UsualUnaryConversion(rex->getType()); |
| 699 | return new BinaryOperator(lex, rex, BinaryOperator::Comma, rhsType); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | Action::ExprResult |
| 703 | Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc, |
Steve Naroff | 7840336 | 2007-04-02 22:55:05 +0000 | [diff] [blame] | 704 | unsigned OpCode) { |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 705 | QualType qType = op->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 706 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 707 | assert(!qType.isNull() && "no type for increment/decrement expression"); |
| 708 | |
Steve Naroff | e5aa9be | 2007-04-05 22:36:20 +0000 | [diff] [blame] | 709 | QualType canonType = qType.getCanonicalType(); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 710 | |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 711 | // C99 6.5.2.4p1 |
| 712 | if (const PointerType *pt = dyn_cast<PointerType>(canonType)) { |
| 713 | if (!pt->getPointeeType()->isObjectType()) // C99 6.5.2.4p2, 6.5.6p2 |
| 714 | return Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, qType); |
| 715 | } else if (!canonType->isRealType()) { |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 716 | // FIXME: Allow Complex as a GCC extension. |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 717 | return Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, qType); |
| 718 | } |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 719 | // At this point, we know we have a real or pointer type. As a result, the |
| 720 | // following predicate is overkill (i.e. it will check for types we know we |
| 721 | // 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] | 722 | if (!canonType.isModifiableLvalue()) |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 723 | return Diag(OpLoc, diag::err_typecheck_not_modifiable, qType); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 724 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 725 | return new UnaryOperator(op, (UOP)OpCode, qType); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 728 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 729 | /// This routine allows us to typecheck complex/recursive expressions |
| 730 | /// where the declaration is needed for type checking. Here are some |
| 731 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 732 | static Decl *getPrimaryDeclaration(Expr *e) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 733 | switch (e->getStmtClass()) { |
| 734 | case Stmt::DeclRefExprClass: |
| 735 | return cast<DeclRefExpr>(e)->getDecl(); |
| 736 | case Stmt::MemberExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 737 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 738 | case Stmt::ArraySubscriptExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 739 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 740 | case Stmt::CallExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 741 | return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 742 | case Stmt::UnaryOperatorClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 743 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 744 | case Stmt::ParenExprClass: |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 745 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 746 | default: |
| 747 | return 0; |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 752 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 753 | /// object cannot be declared with storage class register or be a bit field. |
| 754 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 755 | /// operator, and its result is never an lvalue. |
| 756 | Action::ExprResult |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 757 | Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 758 | Decl *dcl = getPrimaryDeclaration(op); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 759 | |
| 760 | if (!op->isLvalue()) { |
| 761 | if (dcl && isa<FunctionDecl>(dcl)) |
| 762 | ; // C99 6.5.3.2p1: Allow function designators. |
| 763 | else |
| 764 | return Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof); |
| 765 | } else if (dcl) { |
| 766 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 767 | // with the register storage-class specifier. |
| 768 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 769 | if (vd->getStorageClass() == VarDecl::Register) |
| 770 | return Diag(OpLoc, diag::err_typecheck_address_of_register); |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 771 | } else |
| 772 | assert(0 && "Unknown/unexpected decl type"); |
| 773 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 774 | // FIXME: add check for bitfields! |
| 775 | } |
| 776 | // 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] | 777 | return new UnaryOperator(op, UnaryOperator::AddrOf, |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 778 | Context.getPointerType(op->getType())); |
| 779 | } |
| 780 | |
| 781 | Action::ExprResult |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 782 | Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 783 | QualType qType = op->getType(); |
| 784 | |
| 785 | assert(!qType.isNull() && "no type for * expression"); |
| 786 | |
| 787 | QualType canonType = qType.getCanonicalType(); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 788 | |
| 789 | // FIXME: add type checking and fix result type |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 790 | |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 791 | return new UnaryOperator(op, UnaryOperator::Deref, Context.IntTy); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 792 | } |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 793 | |
| 794 | /// CheckArithmeticOperand - Check the arithmetic unary operators (C99 6.5.3.3). |
| 795 | Action::ExprResult |
| 796 | Sema::CheckArithmeticOperand(Expr *op, SourceLocation OpLoc, unsigned Opc) { |
| 797 | QualType resultType = UsualUnaryConversion(op->getType()); |
| 798 | |
| 799 | switch (Opc) { |
| 800 | case UnaryOperator::Plus: |
| 801 | case UnaryOperator::Minus: |
| 802 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 803 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 804 | break; |
| 805 | case UnaryOperator::Not: // bitwise complement |
| 806 | if (!resultType->isIntegerType()) // C99 6.5.3.3p1 |
| 807 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 808 | break; |
| 809 | case UnaryOperator::LNot: // logical negation |
| 810 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 811 | return Diag(OpLoc, diag::err_typecheck_unary_expr, resultType); |
| 812 | break; |
| 813 | } |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 814 | return new UnaryOperator(op, (UOP)Opc, resultType); |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 815 | } |