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