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