Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/Expr.h" |
| 18 | #include "clang/Lex/Preprocessor.h" |
| 19 | #include "clang/Lex/LiteralSupport.h" |
| 20 | #include "clang/Basic/SourceManager.h" |
| 21 | #include "clang/Basic/Diagnostic.h" |
| 22 | #include "clang/Basic/LangOptions.h" |
| 23 | #include "clang/Basic/TargetInfo.h" |
| 24 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
| 28 | /// ParseStringLiteral - The specified tokens were lexed as pasted string |
| 29 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 30 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 31 | /// multiple tokens. However, the common case is that StringToks points to one |
| 32 | /// string. |
| 33 | /// |
| 34 | Action::ExprResult |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 35 | Sema::ParseStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | assert(NumStringToks && "Must have at least one string!"); |
| 37 | |
| 38 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 39 | if (Literal.hadError) |
| 40 | return ExprResult(true); |
| 41 | |
| 42 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 43 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 44 | StringTokLocs.push_back(StringToks[i].getLocation()); |
| 45 | |
| 46 | // FIXME: handle wchar_t |
| 47 | QualType t = Context.getPointerType(Context.CharTy); |
| 48 | |
| 49 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| 50 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
| 51 | Literal.AnyWide, t, StringToks[0].getLocation(), |
| 52 | StringToks[NumStringToks-1].getLocation()); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /// ParseIdentifierExpr - The parser read an identifier in expression context, |
| 57 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
| 58 | /// identifier is used in an function call context. |
| 59 | Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc, |
| 60 | IdentifierInfo &II, |
| 61 | bool HasTrailingLParen) { |
| 62 | // Could be enum-constant or decl. |
| 63 | Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
| 64 | if (D == 0) { |
| 65 | // Otherwise, this could be an implicitly declared function reference (legal |
| 66 | // in C90, extension in C99). |
| 67 | if (HasTrailingLParen && |
| 68 | // Not in C++. |
| 69 | !getLangOptions().CPlusPlus) |
| 70 | D = ImplicitlyDefineFunction(Loc, II, S); |
| 71 | else { |
| 72 | // If this name wasn't predeclared and if this is not a function call, |
| 73 | // diagnose the problem. |
| 74 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
| 75 | } |
| 76 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 78 | return new DeclRefExpr(VD, VD->getType(), Loc); |
| 79 | if (isa<TypedefDecl>(D)) |
| 80 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
| 81 | |
| 82 | assert(0 && "Invalid decl"); |
Chris Lattner | eddbe03 | 2007-07-21 04:57:45 +0000 | [diff] [blame] | 83 | abort(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 86 | Sema::ExprResult Sema::ParsePreDefinedExpr(SourceLocation Loc, |
| 87 | tok::TokenKind Kind) { |
| 88 | PreDefinedExpr::IdentType IT; |
| 89 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | switch (Kind) { |
| 91 | default: |
| 92 | assert(0 && "Unknown simple primary expr!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 94 | IT = PreDefinedExpr::Func; |
| 95 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 97 | IT = PreDefinedExpr::Function; |
| 98 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 100 | IT = PreDefinedExpr::PrettyFunction; |
| 101 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | } |
Anders Carlsson | 2274266 | 2007-07-21 05:21:51 +0000 | [diff] [blame] | 103 | |
| 104 | // Pre-defined identifiers are always of type char *. |
| 105 | return new PreDefinedExpr(Loc, Context.getPointerType(Context.CharTy), IT); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 108 | Sema::ExprResult Sema::ParseCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | llvm::SmallString<16> CharBuffer; |
| 110 | CharBuffer.resize(Tok.getLength()); |
| 111 | const char *ThisTokBegin = &CharBuffer[0]; |
| 112 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 113 | |
| 114 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 115 | Tok.getLocation(), PP); |
| 116 | if (Literal.hadError()) |
| 117 | return ExprResult(true); |
| 118 | return new CharacterLiteral(Literal.getValue(), Context.IntTy, |
| 119 | Tok.getLocation()); |
| 120 | } |
| 121 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 122 | Action::ExprResult Sema::ParseNumericConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | // fast path for a single digit (which is quite common). A single digit |
| 124 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 125 | if (Tok.getLength() == 1) { |
| 126 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
| 127 | |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 128 | unsigned IntSize = Context.getTypeSize(Context.IntTy, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'), |
| 130 | Context.IntTy, |
| 131 | Tok.getLocation())); |
| 132 | } |
| 133 | llvm::SmallString<512> IntegerBuffer; |
| 134 | IntegerBuffer.resize(Tok.getLength()); |
| 135 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 136 | |
| 137 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 138 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 139 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 140 | Tok.getLocation(), PP); |
| 141 | if (Literal.hadError) |
| 142 | return ExprResult(true); |
| 143 | |
| 144 | if (Literal.isIntegerLiteral()) { |
| 145 | QualType t; |
| 146 | |
| 147 | // Get the value in the widest-possible width. |
| 148 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(Tok.getLocation()), 0); |
| 149 | |
| 150 | if (Literal.GetIntegerValue(ResultVal)) { |
| 151 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 152 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 153 | t = Context.UnsignedLongLongTy; |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 154 | assert(Context.getTypeSize(t, Tok.getLocation()) == |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | ResultVal.getBitWidth() && "long long is not intmax_t?"); |
| 156 | } else { |
| 157 | // If this value fits into a ULL, try to figure out what else it fits into |
| 158 | // according to the rules of C99 6.4.4.1p5. |
| 159 | |
| 160 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 161 | // be an unsigned int. |
| 162 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 163 | |
| 164 | // Check from smallest to largest, picking the smallest type we can. |
| 165 | if (!Literal.isLong) { // Are int/unsigned possibilities? |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 166 | unsigned IntSize = Context.getTypeSize(Context.IntTy,Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | // Does it fit in a unsigned int? |
| 168 | if (ResultVal.isIntN(IntSize)) { |
| 169 | // Does it fit in a signed int? |
| 170 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| 171 | t = Context.IntTy; |
| 172 | else if (AllowUnsigned) |
| 173 | t = Context.UnsignedIntTy; |
| 174 | } |
| 175 | |
| 176 | if (!t.isNull()) |
| 177 | ResultVal.trunc(IntSize); |
| 178 | } |
| 179 | |
| 180 | // Are long/unsigned long possibilities? |
| 181 | if (t.isNull() && !Literal.isLongLong) { |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 182 | unsigned LongSize = Context.getTypeSize(Context.LongTy, |
| 183 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | |
| 185 | // Does it fit in a unsigned long? |
| 186 | if (ResultVal.isIntN(LongSize)) { |
| 187 | // Does it fit in a signed long? |
| 188 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| 189 | t = Context.LongTy; |
| 190 | else if (AllowUnsigned) |
| 191 | t = Context.UnsignedLongTy; |
| 192 | } |
| 193 | if (!t.isNull()) |
| 194 | ResultVal.trunc(LongSize); |
| 195 | } |
| 196 | |
| 197 | // Finally, check long long if needed. |
| 198 | if (t.isNull()) { |
| 199 | unsigned LongLongSize = |
Chris Lattner | d2d2a11 | 2007-07-14 01:29:45 +0000 | [diff] [blame] | 200 | Context.getTypeSize(Context.LongLongTy, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | |
| 202 | // Does it fit in a unsigned long long? |
| 203 | if (ResultVal.isIntN(LongLongSize)) { |
| 204 | // Does it fit in a signed long long? |
| 205 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
| 206 | t = Context.LongLongTy; |
| 207 | else if (AllowUnsigned) |
| 208 | t = Context.UnsignedLongLongTy; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // If we still couldn't decide a type, we probably have something that |
| 213 | // does not fit in a signed long long, but has no U suffix. |
| 214 | if (t.isNull()) { |
| 215 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| 216 | t = Context.UnsignedLongLongTy; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return new IntegerLiteral(ResultVal, t, Tok.getLocation()); |
| 221 | } else if (Literal.isFloatingLiteral()) { |
| 222 | // FIXME: handle float values > 32 (including compute the real type...). |
| 223 | return new FloatingLiteral(Literal.GetFloatValue(), Context.FloatTy, |
| 224 | Tok.getLocation()); |
| 225 | } |
| 226 | return ExprResult(true); |
| 227 | } |
| 228 | |
| 229 | Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R, |
| 230 | ExprTy *Val) { |
| 231 | Expr *e = (Expr *)Val; |
| 232 | assert((e != 0) && "ParseParenExpr() missing expr"); |
| 233 | return new ParenExpr(L, R, e); |
| 234 | } |
| 235 | |
| 236 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 237 | /// See C99 6.3.2.1p[2-4] for more details. |
| 238 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 239 | SourceLocation OpLoc, bool isSizeof) { |
| 240 | // C99 6.5.3.4p1: |
| 241 | if (isa<FunctionType>(exprType) && isSizeof) |
| 242 | // alignof(function) is allowed. |
| 243 | Diag(OpLoc, diag::ext_sizeof_function_type); |
| 244 | else if (exprType->isVoidType()) |
| 245 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 246 | else if (exprType->isIncompleteType()) { |
| 247 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 248 | diag::err_alignof_incomplete_type, |
| 249 | exprType.getAsString()); |
| 250 | return QualType(); // error |
| 251 | } |
| 252 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 253 | return Context.getSizeType(); |
| 254 | } |
| 255 | |
| 256 | Action::ExprResult Sema:: |
| 257 | ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
| 258 | SourceLocation LPLoc, TypeTy *Ty, |
| 259 | SourceLocation RPLoc) { |
| 260 | // If error parsing type, ignore. |
| 261 | if (Ty == 0) return true; |
| 262 | |
| 263 | // Verify that this is a valid expression. |
| 264 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
| 265 | |
| 266 | QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof); |
| 267 | |
| 268 | if (resultType.isNull()) |
| 269 | return true; |
| 270 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc, |
| 275 | tok::TokenKind Kind, |
| 276 | ExprTy *Input) { |
| 277 | UnaryOperator::Opcode Opc; |
| 278 | switch (Kind) { |
| 279 | default: assert(0 && "Unknown unary op!"); |
| 280 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 281 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 282 | } |
| 283 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
| 284 | if (result.isNull()) |
| 285 | return true; |
| 286 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
| 287 | } |
| 288 | |
| 289 | Action::ExprResult Sema:: |
| 290 | ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
| 291 | ExprTy *Idx, SourceLocation RLoc) { |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 292 | Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 293 | |
| 294 | // Perform default conversions. |
| 295 | DefaultFunctionArrayConversion(LHSExp); |
| 296 | DefaultFunctionArrayConversion(RHSExp); |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 297 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 298 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 300 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| 301 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
| 302 | // in the subscript position. As a result, we need to derive the array base |
| 303 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 304 | Expr *BaseExpr, *IndexExpr; |
| 305 | QualType ResultType; |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 306 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 307 | BaseExpr = LHSExp; |
| 308 | IndexExpr = RHSExp; |
| 309 | // FIXME: need to deal with const... |
| 310 | ResultType = PTy->getPointeeType(); |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 311 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 312 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 313 | BaseExpr = RHSExp; |
| 314 | IndexExpr = LHSExp; |
| 315 | // FIXME: need to deal with const... |
| 316 | ResultType = PTy->getPointeeType(); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 317 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 318 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 319 | IndexExpr = RHSExp; |
Steve Naroff | 608e0ee | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 320 | |
| 321 | // Component access limited to variables (reject vec4.rg[1]). |
| 322 | if (!isa<DeclRefExpr>(BaseExpr)) |
| 323 | return Diag(LLoc, diag::err_ocuvector_component_access, |
| 324 | SourceRange(LLoc, RLoc)); |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 325 | // FIXME: need to deal with const... |
| 326 | ResultType = VTy->getElementType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | } else { |
Chris Lattner | 727a80d | 2007-07-15 23:59:53 +0000 | [diff] [blame] | 328 | return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value, |
| 329 | RHSExp->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 330 | } |
| 331 | // C99 6.5.2.1p1 |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 332 | if (!IndexExpr->getType()->isIntegerType()) |
| 333 | return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 334 | IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 336 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 337 | // the following check catches trying to index a pointer to a function (e.g. |
| 338 | // void (*)(int)). Functions are not objects in C99. |
| 339 | if (!ResultType->isObjectType()) |
| 340 | return Diag(BaseExpr->getLocStart(), |
| 341 | diag::err_typecheck_subscript_not_object, |
| 342 | BaseExpr->getType().getAsString(), BaseExpr->getSourceRange()); |
| 343 | |
| 344 | return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 347 | QualType Sema:: |
| 348 | CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc, |
| 349 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 350 | const OCUVectorType *vecType = baseType->getAsOCUVectorType(); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 351 | |
| 352 | // The vector accessor can't exceed the number of elements. |
| 353 | const char *compStr = CompName.getName(); |
| 354 | if (strlen(compStr) > vecType->getNumElements()) { |
| 355 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 356 | baseType.getAsString(), SourceRange(CompLoc)); |
| 357 | return QualType(); |
| 358 | } |
| 359 | // The component names must come from the same set. |
Chris Lattner | 88dca04 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 360 | if (vecType->getPointAccessorIdx(*compStr) != -1) { |
| 361 | do |
| 362 | compStr++; |
| 363 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
| 364 | } else if (vecType->getColorAccessorIdx(*compStr) != -1) { |
| 365 | do |
| 366 | compStr++; |
| 367 | while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1); |
| 368 | } else if (vecType->getTextureAccessorIdx(*compStr) != -1) { |
| 369 | do |
| 370 | compStr++; |
| 371 | while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1); |
| 372 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 373 | |
| 374 | if (*compStr) { |
| 375 | // We didn't get to the end of the string. This means the component names |
| 376 | // didn't come from the same set *or* we encountered an illegal name. |
| 377 | Diag(OpLoc, diag::err_ocuvector_component_name_illegal, |
| 378 | std::string(compStr,compStr+1), SourceRange(CompLoc)); |
| 379 | return QualType(); |
| 380 | } |
| 381 | // Each component accessor can't exceed the vector type. |
| 382 | compStr = CompName.getName(); |
| 383 | while (*compStr) { |
| 384 | if (vecType->isAccessorWithinNumElements(*compStr)) |
| 385 | compStr++; |
| 386 | else |
| 387 | break; |
| 388 | } |
| 389 | if (*compStr) { |
| 390 | // We didn't get to the end of the string. This means a component accessor |
| 391 | // exceeds the number of elements in the vector. |
| 392 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 393 | baseType.getAsString(), SourceRange(CompLoc)); |
| 394 | return QualType(); |
| 395 | } |
| 396 | // The component accessor looks fine - now we need to compute the actual type. |
| 397 | // The vector type is implied by the component accessor. For example, |
| 398 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| 399 | unsigned CompSize = strlen(CompName.getName()); |
| 400 | if (CompSize == 1) |
| 401 | return vecType->getElementType(); |
Steve Naroff | bea0b34 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 402 | |
| 403 | QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize); |
| 404 | // Now look up the TypeDefDecl from the vector type. Without this, |
| 405 | // diagostics look bad. We want OCU vector types to appear built-in. |
| 406 | for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) { |
| 407 | if (OCUVectorDecls[i]->getUnderlyingType() == VT) |
| 408 | return Context.getTypedefType(OCUVectorDecls[i]); |
| 409 | } |
| 410 | return VT; // should never get here (a typedef type should always be found). |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 413 | Action::ExprResult Sema:: |
| 414 | ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
| 415 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 416 | IdentifierInfo &Member) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 417 | Expr *BaseExpr = static_cast<Expr *>(Base); |
| 418 | assert(BaseExpr && "no record expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 420 | QualType BaseType = BaseExpr->getType(); |
| 421 | assert(!BaseType.isNull() && "no type for member expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 423 | if (OpKind == tok::arrow) { |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 424 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 425 | BaseType = PT->getPointeeType(); |
| 426 | else |
| 427 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow, |
| 428 | SourceRange(MemberLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | } |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 430 | // The base type is either a record or an OCUVectorType. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 431 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 432 | RecordDecl *RDecl = RTy->getDecl(); |
| 433 | if (RTy->isIncompleteType()) |
| 434 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(), |
| 435 | BaseExpr->getSourceRange()); |
| 436 | // The record definition is complete, now make sure the member is valid. |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 437 | FieldDecl *MemberDecl = RDecl->getMember(&Member); |
| 438 | if (!MemberDecl) |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 439 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(), |
| 440 | SourceRange(MemberLoc)); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 441 | return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc); |
| 442 | } else if (BaseType->isOCUVectorType() && OpKind == tok::period) { |
Steve Naroff | 608e0ee | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 443 | // Component access limited to variables (reject vec4.rg.g). |
| 444 | if (!isa<DeclRefExpr>(BaseExpr)) |
| 445 | return Diag(OpLoc, diag::err_ocuvector_component_access, |
| 446 | SourceRange(MemberLoc)); |
Steve Naroff | e1b31fe | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 447 | QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 448 | if (ret.isNull()) |
| 449 | return true; |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 450 | return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc); |
Steve Naroff | dfa6aae | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 451 | } else |
| 452 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion, |
| 453 | SourceRange(MemberLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /// ParseCallExpr - Handle a call to Fn with the specified array of arguments. |
| 457 | /// This provides the location of the left/right parens and a list of comma |
| 458 | /// locations. |
| 459 | Action::ExprResult Sema:: |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 460 | ParseCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
| 461 | ExprTy **args, unsigned NumArgsInCall, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 462 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 463 | Expr *Fn = static_cast<Expr *>(fn); |
| 464 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 465 | assert(Fn && "no function call expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 467 | UsualUnaryConversions(Fn); |
| 468 | QualType funcType = Fn->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | |
| 470 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 471 | // type pointer to function". |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 472 | const PointerType *PT = funcType->getAsPointerType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | if (PT == 0) |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 474 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 475 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 476 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 477 | const FunctionType *funcT = PT->getPointeeType()->getAsFunctionType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | if (funcT == 0) |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 479 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 480 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | |
| 482 | // If a prototype isn't declared, the parser implicitly defines a func decl |
| 483 | QualType resultType = funcT->getResultType(); |
| 484 | |
| 485 | if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) { |
| 486 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 487 | // assignment, to the types of the corresponding parameter, ... |
| 488 | |
| 489 | unsigned NumArgsInProto = proto->getNumArgs(); |
| 490 | unsigned NumArgsToCheck = NumArgsInCall; |
| 491 | |
| 492 | if (NumArgsInCall < NumArgsInProto) |
| 493 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 494 | Fn->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | else if (NumArgsInCall > NumArgsInProto) { |
| 496 | if (!proto->isVariadic()) { |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 497 | Diag(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 498 | diag::err_typecheck_call_too_many_args, Fn->getSourceRange(), |
Chris Lattner | d472b31 | 2007-07-21 03:09:58 +0000 | [diff] [blame] | 499 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 500 | Args[NumArgsInCall-1]->getLocEnd())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | } |
| 502 | NumArgsToCheck = NumArgsInProto; |
| 503 | } |
| 504 | // Continue to check argument types (even if we have too few/many args). |
| 505 | for (unsigned i = 0; i < NumArgsToCheck; i++) { |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 506 | Expr *argExpr = Args[i]; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 507 | assert(argExpr && "ParseCallExpr(): missing argument expression"); |
| 508 | |
| 509 | QualType lhsType = proto->getArgType(i); |
| 510 | QualType rhsType = argExpr->getType(); |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 511 | |
Steve Naroff | 82c7e6d | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 512 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 513 | if (const ArrayType *ary = lhsType->getAsArrayType()) |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 514 | lhsType = Context.getPointerType(ary->getElementType()); |
Steve Naroff | 82c7e6d | 2007-07-25 20:45:33 +0000 | [diff] [blame] | 515 | else if (lhsType->isFunctionType()) |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 516 | lhsType = Context.getPointerType(lhsType); |
| 517 | |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 518 | AssignmentCheckResult result = CheckSingleAssignmentConstraints(lhsType, |
| 519 | argExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | SourceLocation l = argExpr->getLocStart(); |
| 521 | |
| 522 | // decode the result (notice that AST's are still created for extensions). |
| 523 | switch (result) { |
| 524 | case Compatible: |
| 525 | break; |
| 526 | case PointerFromInt: |
| 527 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 528 | if (!argExpr->isNullPointerConstant(Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 529 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 530 | lhsType.getAsString(), rhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 531 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | } |
| 533 | break; |
| 534 | case IntFromPointer: |
| 535 | Diag(l, diag::ext_typecheck_passing_pointer_int, |
| 536 | lhsType.getAsString(), rhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 537 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | break; |
| 539 | case IncompatiblePointer: |
| 540 | Diag(l, diag::ext_typecheck_passing_incompatible_pointer, |
| 541 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 542 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 543 | break; |
| 544 | case CompatiblePointerDiscardsQualifiers: |
| 545 | Diag(l, diag::ext_typecheck_passing_discards_qualifiers, |
| 546 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 547 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | break; |
| 549 | case Incompatible: |
| 550 | return Diag(l, diag::err_typecheck_passing_incompatible, |
| 551 | rhsType.getAsString(), lhsType.getAsString(), |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 552 | Fn->getSourceRange(), argExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | // Even if the types checked, bail if we had the wrong number of arguments. |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 556 | if (NumArgsInCall != NumArgsInProto && !proto->isVariadic()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 559 | |
| 560 | // Do special checking on direct calls to functions. |
| 561 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 562 | if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr())) |
| 563 | if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl())) |
Anders Carlsson | 9cdc4d3 | 2007-08-17 15:44:17 +0000 | [diff] [blame] | 564 | if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args, NumArgsInCall)) |
Anders Carlsson | 71993dd | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 565 | return true; |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 566 | |
Chris Lattner | 74c469f | 2007-07-21 03:03:59 +0000 | [diff] [blame] | 567 | return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | Action::ExprResult Sema:: |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 571 | ParseCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 572 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 573 | assert((Ty != 0) && "ParseCompoundLiteral(): missing type"); |
| 574 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 575 | // FIXME: put back this assert when initializers are worked out. |
| 576 | //assert((InitExpr != 0) && "ParseCompoundLiteral(): missing expression"); |
| 577 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 578 | |
| 579 | // FIXME: add semantic analysis (C99 6.5.2.5). |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 580 | return new CompoundLiteralExpr(literalType, literalExpr); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | Action::ExprResult Sema:: |
| 584 | ParseInitList(SourceLocation LParenLoc, ExprTy **InitList, unsigned NumInit, |
| 585 | SourceLocation RParenLoc) { |
| 586 | // FIXME: add semantic analysis (C99 6.7.8). This involves |
| 587 | // knowledge of the object being intialized. As a result, the code for |
| 588 | // doing the semantic analysis will likely be located elsewhere (i.e. in |
| 589 | // consumers of InitListExpr (e.g. ParseDeclarator, ParseCompoundLiteral). |
| 590 | return false; // FIXME instantiate an InitListExpr. |
| 591 | } |
| 592 | |
| 593 | Action::ExprResult Sema:: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 594 | ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
| 595 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 596 | assert((Ty != 0) && (Op != 0) && "ParseCastExpr(): missing type or expr"); |
| 597 | |
| 598 | Expr *castExpr = static_cast<Expr*>(Op); |
| 599 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 600 | |
Chris Lattner | 75af480 | 2007-07-18 16:00:06 +0000 | [diff] [blame] | 601 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 602 | // type needs to be scalar. |
| 603 | if (!castType->isScalarType() && !castType->isVoidType()) { |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 604 | return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, |
| 605 | castType.getAsString(), SourceRange(LParenLoc, RParenLoc)); |
| 606 | } |
| 607 | if (!castExpr->getType()->isScalarType()) { |
| 608 | return Diag(castExpr->getLocStart(), |
| 609 | diag::err_typecheck_expect_scalar_operand, |
| 610 | castExpr->getType().getAsString(), castExpr->getSourceRange()); |
| 611 | } |
| 612 | return new CastExpr(castType, castExpr, LParenLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 616 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 617 | UsualUnaryConversions(cond); |
| 618 | UsualUnaryConversions(lex); |
| 619 | UsualUnaryConversions(rex); |
| 620 | QualType condT = cond->getType(); |
| 621 | QualType lexT = lex->getType(); |
| 622 | QualType rexT = rex->getType(); |
| 623 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 624 | // first, check the condition. |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 625 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 626 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 627 | condT.getAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 628 | return QualType(); |
| 629 | } |
| 630 | // now check the two expressions. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 631 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { // C99 6.5.15p3,5 |
| 632 | UsualArithmeticConversions(lex, rex); |
| 633 | return lex->getType(); |
| 634 | } |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 635 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
| 636 | if (const RecordType *RHSRT = rexT->getAsRecordType()) { |
| 637 | |
| 638 | if (LHSRT->getDecl()->getIdentifier() ==RHSRT->getDecl()->getIdentifier()) |
| 639 | return lexT; |
| 640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 642 | lexT.getAsString(), rexT.getAsString(), |
| 643 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | return QualType(); |
| 645 | } |
| 646 | } |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 647 | // C99 6.5.15p3 |
| 648 | if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 649 | return lexT; |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 650 | if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 651 | return rexT; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 652 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 653 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 654 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 655 | // get the "pointed to" types |
| 656 | QualType lhptee = LHSPT->getPointeeType(); |
| 657 | QualType rhptee = RHSPT->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 658 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 659 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 660 | if (lhptee->isVoidType() && |
| 661 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 662 | return lexT; |
| 663 | if (rhptee->isVoidType() && |
| 664 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 665 | return rexT; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 667 | if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 668 | rhptee.getUnqualifiedType())) { |
| 669 | Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers, |
| 670 | lexT.getAsString(), rexT.getAsString(), |
| 671 | lex->getSourceRange(), rex->getSourceRange()); |
| 672 | return lexT; // FIXME: this is an _ext - is this return o.k? |
| 673 | } |
| 674 | // The pointer types are compatible. |
| 675 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 676 | // differently qualified versions of compatible types, the result type is a |
| 677 | // pointer to an appropriately qualified version of the *composite* type. |
| 678 | return lexT; // FIXME: Need to return the composite type. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 681 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 682 | if (lexT->isVoidType() && rexT->isVoidType()) // C99 6.5.15p3 |
| 683 | return lexT; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | |
| 685 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 686 | lexT.getAsString(), rexT.getAsString(), |
| 687 | lex->getSourceRange(), rex->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 688 | return QualType(); |
| 689 | } |
| 690 | |
| 691 | /// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| 692 | /// in the case of a the GNU conditional expr extension. |
| 693 | Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc, |
| 694 | SourceLocation ColonLoc, |
| 695 | ExprTy *Cond, ExprTy *LHS, |
| 696 | ExprTy *RHS) { |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 697 | Expr *CondExpr = (Expr *) Cond; |
| 698 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
| 699 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 700 | RHSExpr, QuestionLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 701 | if (result.isNull()) |
| 702 | return true; |
Chris Lattner | 2682490 | 2007-07-16 21:39:03 +0000 | [diff] [blame] | 703 | return new ConditionalOperator(CondExpr, LHSExpr, RHSExpr, result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 706 | // promoteExprToType - a helper function to ensure we create exactly one |
| 707 | // ImplicitCastExpr. As a convenience (to the caller), we return the type. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 708 | static void promoteExprToType(Expr *&expr, QualType type) { |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 709 | if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr)) |
| 710 | impCast->setType(type); |
| 711 | else |
| 712 | expr = new ImplicitCastExpr(type, expr); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 713 | return; |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 717 | void Sema::DefaultFunctionArrayConversion(Expr *&e) { |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 718 | QualType t = e->getType(); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 719 | assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type"); |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 720 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 721 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 722 | promoteExprToType(e, ref->getReferenceeType()); // C++ [expr] |
| 723 | t = e->getType(); |
| 724 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 725 | if (t->isFunctionType()) |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 726 | promoteExprToType(e, Context.getPointerType(t)); |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 727 | else if (const ArrayType *ary = t->getAsArrayType()) |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 728 | promoteExprToType(e, Context.getPointerType(ary->getElementType())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /// UsualUnaryConversion - Performs various conversions that are common to most |
| 732 | /// operators (C99 6.3). The conversions of array and function types are |
| 733 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 734 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 735 | /// In these instances, this routine should *not* be called. |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 736 | void Sema::UsualUnaryConversions(Expr *&expr) { |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 737 | QualType t = expr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 738 | assert(!t.isNull() && "UsualUnaryConversions - missing type"); |
| 739 | |
Chris Lattner | a1d9fde | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 740 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Bill Wendling | ea5e79f | 2007-07-17 04:16:47 +0000 | [diff] [blame] | 741 | promoteExprToType(expr, ref->getReferenceeType()); // C++ [expr] |
| 742 | t = expr->getType(); |
| 743 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 744 | if (t->isPromotableIntegerType()) // C99 6.3.1.1p2 |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 745 | promoteExprToType(expr, Context.IntTy); |
| 746 | else |
| 747 | DefaultFunctionArrayConversion(expr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 751 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 752 | /// routine returns the first non-arithmetic type found. The client is |
| 753 | /// responsible for emitting appropriate error diagnostics. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 754 | void Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 755 | UsualUnaryConversions(lhsExpr); |
| 756 | UsualUnaryConversions(rhsExpr); |
| 757 | |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 758 | QualType lhs = lhsExpr->getType(); |
| 759 | QualType rhs = rhsExpr->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 760 | |
| 761 | // If both types are identical, no conversion is needed. |
| 762 | if (lhs == rhs) |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 763 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | |
| 765 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 766 | // The caller can deal with this (e.g. pointer + int). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 767 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 768 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 769 | |
| 770 | // At this point, we have two different arithmetic types. |
| 771 | |
| 772 | // Handle complex types first (C99 6.3.1.8p1). |
| 773 | if (lhs->isComplexType() || rhs->isComplexType()) { |
| 774 | // if we have an integer operand, the result is the complex type. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 775 | if (rhs->isIntegerType()) { // convert the rhs to the lhs complex type. |
| 776 | promoteExprToType(rhsExpr, lhs); |
| 777 | return; |
| 778 | } |
| 779 | if (lhs->isIntegerType()) { // convert the lhs to the rhs complex type. |
| 780 | promoteExprToType(lhsExpr, rhs); |
| 781 | return; |
| 782 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 783 | // Two complex types. Convert the smaller operand to the bigger result. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 784 | if (Context.maxComplexType(lhs, rhs) == lhs) { // convert the rhs |
| 785 | promoteExprToType(rhsExpr, lhs); |
| 786 | return; |
| 787 | } |
| 788 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 789 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | // Now handle "real" floating types (i.e. float, double, long double). |
| 792 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 793 | // if we have an integer operand, the result is the real floating type. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 794 | if (rhs->isIntegerType()) { // convert rhs to the lhs floating point type. |
| 795 | promoteExprToType(rhsExpr, lhs); |
| 796 | return; |
| 797 | } |
| 798 | if (lhs->isIntegerType()) { // convert lhs to the rhs floating point type. |
| 799 | promoteExprToType(lhsExpr, rhs); |
| 800 | return; |
| 801 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 802 | // We have two real floating types, float/complex combos were handled above. |
| 803 | // Convert the smaller operand to the bigger result. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 804 | if (Context.maxFloatingType(lhs, rhs) == lhs) { // convert the rhs |
| 805 | promoteExprToType(rhsExpr, lhs); |
| 806 | return; |
| 807 | } |
| 808 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 809 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 810 | } |
Steve Naroff | fa2eaab | 2007-07-15 02:02:06 +0000 | [diff] [blame] | 811 | // Finally, we have two differing integer types. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 812 | if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs |
| 813 | promoteExprToType(rhsExpr, lhs); |
| 814 | return; |
| 815 | } |
| 816 | promoteExprToType(lhsExpr, rhs); // convert the lhs |
| 817 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 821 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 822 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 823 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 824 | // FIXME: add a couple examples in this comment. |
| 825 | Sema::AssignmentCheckResult |
| 826 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 827 | QualType lhptee, rhptee; |
| 828 | |
| 829 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 2dcb6bb | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 830 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 831 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 832 | |
| 833 | // make sure we operate on the canonical type |
| 834 | lhptee = lhptee.getCanonicalType(); |
| 835 | rhptee = rhptee.getCanonicalType(); |
| 836 | |
| 837 | AssignmentCheckResult r = Compatible; |
| 838 | |
| 839 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 840 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 841 | // qualifiers of the type *pointed to* by the right; |
| 842 | if ((lhptee.getQualifiers() & rhptee.getQualifiers()) != |
| 843 | rhptee.getQualifiers()) |
| 844 | r = CompatiblePointerDiscardsQualifiers; |
| 845 | |
| 846 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 847 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 848 | // version of void... |
| 849 | if (lhptee.getUnqualifiedType()->isVoidType() && |
| 850 | (rhptee->isObjectType() || rhptee->isIncompleteType())) |
| 851 | ; |
| 852 | else if (rhptee.getUnqualifiedType()->isVoidType() && |
| 853 | (lhptee->isObjectType() || lhptee->isIncompleteType())) |
| 854 | ; |
| 855 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 856 | // unqualified versions of compatible types, ... |
| 857 | else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(), |
| 858 | rhptee.getUnqualifiedType())) |
| 859 | r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
| 860 | return r; |
| 861 | } |
| 862 | |
| 863 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 864 | /// has code to accommodate several GCC extensions when type checking |
| 865 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 866 | /// |
| 867 | /// int a, *pint; |
| 868 | /// short *pshort; |
| 869 | /// struct foo *pfoo; |
| 870 | /// |
| 871 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 872 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 873 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 874 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 875 | /// |
| 876 | /// As a result, the code for dealing with pointers is more complex than the |
| 877 | /// C99 spec dictates. |
| 878 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 879 | /// |
| 880 | Sema::AssignmentCheckResult |
| 881 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Steve Naroff | 700204c | 2007-07-24 21:46:40 +0000 | [diff] [blame] | 882 | if (lhsType == rhsType) // common case, fast path... |
| 883 | return Compatible; |
| 884 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 885 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) { |
| 886 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
| 887 | if (lhsType.getCanonicalType() != rhsType.getCanonicalType()) |
| 888 | return Incompatible; |
| 889 | } |
| 890 | return Compatible; |
| 891 | } else if (lhsType->isPointerType()) { |
| 892 | if (rhsType->isIntegerType()) |
| 893 | return PointerFromInt; |
| 894 | |
| 895 | if (rhsType->isPointerType()) |
| 896 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
| 897 | } else if (rhsType->isPointerType()) { |
| 898 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 899 | if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy)) |
| 900 | return IntFromPointer; |
| 901 | |
| 902 | if (lhsType->isPointerType()) |
| 903 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
| 904 | } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
| 905 | if (Type::tagTypesAreCompatible(lhsType, rhsType)) |
| 906 | return Compatible; |
| 907 | } else if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
| 908 | if (Type::referenceTypesAreCompatible(lhsType, rhsType)) |
| 909 | return Compatible; |
| 910 | } |
| 911 | return Incompatible; |
| 912 | } |
| 913 | |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 914 | Sema::AssignmentCheckResult |
| 915 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
| 916 | // This check seems unnatural, however it is necessary to insure the proper |
| 917 | // conversion of functions/arrays. If the conversion were done for all |
| 918 | // DeclExpr's (created by ParseIdentifierExpr), it would mess up the unary |
| 919 | // expressions that surpress this implicit conversion (&, sizeof). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 920 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 921 | |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 922 | return CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | Sema::AssignmentCheckResult |
| 926 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 927 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 928 | } |
| 929 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 930 | inline void Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 932 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 933 | lex->getSourceRange(), rex->getSourceRange()); |
| 934 | } |
| 935 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 936 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 937 | Expr *&rex) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 938 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 939 | |
| 940 | // make sure the vector types are identical. |
| 941 | if (lhsType == rhsType) |
| 942 | return lhsType; |
| 943 | // You cannot convert between vector values of different size. |
| 944 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 945 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 946 | lex->getSourceRange(), rex->getSourceRange()); |
| 947 | return QualType(); |
| 948 | } |
| 949 | |
| 950 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 951 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 952 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 953 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 954 | |
| 955 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 957 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 958 | UsualArithmeticConversions(lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 960 | // handle the common case first (both operands are arithmetic). |
| 961 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 962 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 963 | InvalidOperands(loc, lex, rex); |
| 964 | return QualType(); |
| 965 | } |
| 966 | |
| 967 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 968 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 969 | { |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 970 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 971 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 972 | UsualArithmeticConversions(lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 973 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 974 | // handle the common case first (both operands are arithmetic). |
| 975 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 976 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | InvalidOperands(loc, lex, rex); |
| 978 | return QualType(); |
| 979 | } |
| 980 | |
| 981 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 982 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 983 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 984 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 985 | return CheckVectorOperands(loc, lex, rex); |
| 986 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 987 | UsualArithmeticConversions(lex, rex); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 988 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 990 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 991 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 993 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
| 994 | return lex->getType(); |
| 995 | if (lex->getType()->isIntegerType() && rex->getType()->isPointerType()) |
| 996 | return rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | InvalidOperands(loc, lex, rex); |
| 998 | return QualType(); |
| 999 | } |
| 1000 | |
| 1001 | inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1002 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1004 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1006 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1007 | UsualArithmeticConversions(lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | |
| 1009 | // handle the common case first (both operands are arithmetic). |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1010 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1011 | return lex->getType(); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1012 | |
| 1013 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1014 | return lex->getType(); |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1015 | if (lex->getType()->isPointerType() && rex->getType()->isPointerType()) |
Chris Lattner | 8b9023b | 2007-07-13 03:05:23 +0000 | [diff] [blame] | 1016 | return Context.getPointerDiffType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1017 | InvalidOperands(loc, lex, rex); |
| 1018 | return QualType(); |
| 1019 | } |
| 1020 | |
| 1021 | inline QualType Sema::CheckShiftOperands( // C99 6.5.7 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1022 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1023 | { |
| 1024 | // FIXME: Shifts don't perform usual arithmetic conversions. This is wrong |
| 1025 | // for int << longlong -> the result type should be int, not long long. |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1026 | UsualArithmeticConversions(lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1027 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1028 | // handle the common case first (both operands are arithmetic). |
| 1029 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 1030 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | InvalidOperands(loc, lex, rex); |
| 1032 | return QualType(); |
| 1033 | } |
| 1034 | |
| 1035 | inline QualType Sema::CheckRelationalOperands( // C99 6.5.8 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1036 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1037 | { |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1038 | // C99 6.5.8p3 |
| 1039 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1040 | UsualArithmeticConversions(lex, rex); |
| 1041 | else { |
| 1042 | UsualUnaryConversions(lex); |
| 1043 | UsualUnaryConversions(rex); |
| 1044 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1045 | QualType lType = lex->getType(); |
| 1046 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (lType->isRealType() && rType->isRealType()) |
| 1049 | return Context.IntTy; |
| 1050 | |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1051 | // All of the following pointer related warnings are GCC extensions. One |
| 1052 | // day, we can consider making them errors (when -pedantic-errors is enabled). |
| 1053 | if (lType->isPointerType() && rType->isPointerType()) { |
| 1054 | if (!Type::pointerTypesAreCompatible(lType, rType)) { |
| 1055 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 1056 | lType.getAsString(), rType.getAsString(), |
| 1057 | lex->getSourceRange(), rex->getSourceRange()); |
| 1058 | promoteExprToType(rex, lType); // promote the pointer to pointer |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1059 | } |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1060 | return Context.IntTy; |
| 1061 | } |
| 1062 | if (lType->isPointerType() && rType->isIntegerType()) { |
| 1063 | if (!rex->isNullPointerConstant(Context)) { |
| 1064 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1065 | lType.getAsString(), rType.getAsString(), |
| 1066 | lex->getSourceRange(), rex->getSourceRange()); |
| 1067 | promoteExprToType(rex, lType); // promote the integer to pointer |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1068 | } |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1069 | return Context.IntTy; |
| 1070 | } |
| 1071 | if (lType->isIntegerType() && rType->isPointerType()) { |
| 1072 | if (!lex->isNullPointerConstant(Context)) { |
| 1073 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1074 | lType.getAsString(), rType.getAsString(), |
| 1075 | lex->getSourceRange(), rex->getSourceRange()); |
| 1076 | promoteExprToType(lex, rType); // promote the integer to pointer |
| 1077 | } |
| 1078 | return Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1079 | } |
| 1080 | InvalidOperands(loc, lex, rex); |
| 1081 | return QualType(); |
| 1082 | } |
| 1083 | |
| 1084 | inline QualType Sema::CheckEqualityOperands( // C99 6.5.9 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1085 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1086 | { |
Steve Naroff | 30bf771 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1087 | // C99 6.5.9p4 |
| 1088 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1089 | UsualArithmeticConversions(lex, rex); |
| 1090 | else { |
| 1091 | UsualUnaryConversions(lex); |
| 1092 | UsualUnaryConversions(rex); |
| 1093 | } |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1094 | QualType lType = lex->getType(); |
| 1095 | QualType rType = rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 1098 | return Context.IntTy; |
| 1099 | |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1100 | // All of the following pointer related warnings are GCC extensions. One |
| 1101 | // day, we can consider making them errors (when -pedantic-errors is enabled). |
| 1102 | if (lType->isPointerType() && rType->isPointerType()) { |
| 1103 | if (!Type::pointerTypesAreCompatible(lType, rType)) { |
| 1104 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 1105 | lType.getAsString(), rType.getAsString(), |
| 1106 | lex->getSourceRange(), rex->getSourceRange()); |
| 1107 | promoteExprToType(rex, lType); // promote the pointer to pointer |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1108 | } |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1109 | return Context.IntTy; |
| 1110 | } |
| 1111 | if (lType->isPointerType() && rType->isIntegerType()) { |
| 1112 | if (!rex->isNullPointerConstant(Context)) { |
| 1113 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1114 | lType.getAsString(), rType.getAsString(), |
| 1115 | lex->getSourceRange(), rex->getSourceRange()); |
| 1116 | promoteExprToType(rex, lType); // promote the integer to pointer |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1117 | } |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1118 | return Context.IntTy; |
| 1119 | } |
| 1120 | if (lType->isIntegerType() && rType->isPointerType()) { |
| 1121 | if (!lex->isNullPointerConstant(Context)) { |
| 1122 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1123 | lType.getAsString(), rType.getAsString(), |
| 1124 | lex->getSourceRange(), rex->getSourceRange()); |
| 1125 | promoteExprToType(lex, rType); // promote the integer to pointer |
| 1126 | } |
| 1127 | return Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | } |
| 1129 | InvalidOperands(loc, lex, rex); |
| 1130 | return QualType(); |
| 1131 | } |
| 1132 | |
| 1133 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1134 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | { |
Steve Naroff | 3e5e556 | 2007-07-16 22:23:01 +0000 | [diff] [blame] | 1136 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1137 | return CheckVectorOperands(loc, lex, rex); |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1138 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1139 | UsualArithmeticConversions(lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1140 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1141 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
| 1142 | return lex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1143 | InvalidOperands(loc, lex, rex); |
| 1144 | return QualType(); |
| 1145 | } |
| 1146 | |
| 1147 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1148 | Expr *&lex, Expr *&rex, SourceLocation loc) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1149 | { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1150 | UsualUnaryConversions(lex); |
| 1151 | UsualUnaryConversions(rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1152 | |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 1153 | if (lex->getType()->isScalarType() || rex->getType()->isScalarType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1154 | return Context.IntTy; |
| 1155 | InvalidOperands(loc, lex, rex); |
| 1156 | return QualType(); |
| 1157 | } |
| 1158 | |
| 1159 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
| 1160 | Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType) |
| 1161 | { |
| 1162 | QualType lhsType = lex->getType(); |
| 1163 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
| 1164 | bool hadError = false; |
| 1165 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(); |
| 1166 | |
| 1167 | switch (mlval) { // C99 6.5.16p2 |
| 1168 | case Expr::MLV_Valid: |
| 1169 | break; |
| 1170 | case Expr::MLV_ConstQualified: |
| 1171 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 1172 | hadError = true; |
| 1173 | break; |
| 1174 | case Expr::MLV_ArrayType: |
| 1175 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 1176 | lhsType.getAsString(), lex->getSourceRange()); |
| 1177 | return QualType(); |
| 1178 | case Expr::MLV_NotObjectType: |
| 1179 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 1180 | lhsType.getAsString(), lex->getSourceRange()); |
| 1181 | return QualType(); |
| 1182 | case Expr::MLV_InvalidExpression: |
| 1183 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 1184 | lex->getSourceRange()); |
| 1185 | return QualType(); |
| 1186 | case Expr::MLV_IncompleteType: |
| 1187 | case Expr::MLV_IncompleteVoidType: |
| 1188 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 1189 | lhsType.getAsString(), lex->getSourceRange()); |
| 1190 | return QualType(); |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1191 | case Expr::MLV_DuplicateVectorComponents: |
| 1192 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 1193 | lex->getSourceRange()); |
| 1194 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 1196 | AssignmentCheckResult result; |
| 1197 | |
| 1198 | if (compoundType.isNull()) |
| 1199 | result = CheckSingleAssignmentConstraints(lhsType, rex); |
| 1200 | else |
| 1201 | result = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1203 | // decode the result (notice that extensions still return a type). |
| 1204 | switch (result) { |
| 1205 | case Compatible: |
| 1206 | break; |
| 1207 | case Incompatible: |
| 1208 | Diag(loc, diag::err_typecheck_assign_incompatible, |
| 1209 | lhsType.getAsString(), rhsType.getAsString(), |
| 1210 | lex->getSourceRange(), rex->getSourceRange()); |
| 1211 | hadError = true; |
| 1212 | break; |
| 1213 | case PointerFromInt: |
| 1214 | // check for null pointer constant (C99 6.3.2.3p3) |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1215 | if (compoundType.isNull() && !rex->isNullPointerConstant(Context)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1216 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1217 | lhsType.getAsString(), rhsType.getAsString(), |
| 1218 | lex->getSourceRange(), rex->getSourceRange()); |
| 1219 | } |
| 1220 | break; |
| 1221 | case IntFromPointer: |
| 1222 | Diag(loc, diag::ext_typecheck_assign_pointer_int, |
| 1223 | lhsType.getAsString(), rhsType.getAsString(), |
| 1224 | lex->getSourceRange(), rex->getSourceRange()); |
| 1225 | break; |
| 1226 | case IncompatiblePointer: |
| 1227 | Diag(loc, diag::ext_typecheck_assign_incompatible_pointer, |
| 1228 | lhsType.getAsString(), rhsType.getAsString(), |
| 1229 | lex->getSourceRange(), rex->getSourceRange()); |
| 1230 | break; |
| 1231 | case CompatiblePointerDiscardsQualifiers: |
| 1232 | Diag(loc, diag::ext_typecheck_assign_discards_qualifiers, |
| 1233 | lhsType.getAsString(), rhsType.getAsString(), |
| 1234 | lex->getSourceRange(), rex->getSourceRange()); |
| 1235 | break; |
| 1236 | } |
| 1237 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 1238 | // left operand unless the left operand has qualified type, in which case |
| 1239 | // it is the unqualified version of the type of the left operand. |
| 1240 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 1241 | // is converted to the type of the assignment expression (above). |
| 1242 | // C++ 5.17p1: the type of the assignment expression is that of its left oprdu. |
| 1243 | return hadError ? QualType() : lhsType.getUnqualifiedType(); |
| 1244 | } |
| 1245 | |
| 1246 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1247 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1248 | UsualUnaryConversions(rex); |
| 1249 | return rex->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1252 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 1253 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 1255 | QualType resType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1256 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
| 1257 | |
| 1258 | // C99 6.5.2.4p1 |
| 1259 | if (const PointerType *pt = dyn_cast<PointerType>(resType)) { |
| 1260 | if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2 |
| 1261 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1262 | resType.getAsString(), op->getSourceRange()); |
| 1263 | return QualType(); |
| 1264 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame^] | 1265 | } else if (!resType->isRealType() && !resType->isComplexType()) { |
| 1266 | // Allowing Complex is a GCC extension. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1267 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 1268 | resType.getAsString(), op->getSourceRange()); |
| 1269 | return QualType(); |
| 1270 | } |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame^] | 1271 | // At this point, we know we have a real, complex or pointer type. |
| 1272 | // Now make sure the operand is a modifiable lvalue. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(); |
| 1274 | if (mlval != Expr::MLV_Valid) { |
| 1275 | // FIXME: emit a more precise diagnostic... |
| 1276 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
| 1277 | op->getSourceRange()); |
| 1278 | return QualType(); |
| 1279 | } |
| 1280 | return resType; |
| 1281 | } |
| 1282 | |
| 1283 | /// getPrimaryDeclaration - Helper function for CheckAddressOfOperand(). |
| 1284 | /// This routine allows us to typecheck complex/recursive expressions |
| 1285 | /// where the declaration is needed for type checking. Here are some |
| 1286 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
| 1287 | static Decl *getPrimaryDeclaration(Expr *e) { |
| 1288 | switch (e->getStmtClass()) { |
| 1289 | case Stmt::DeclRefExprClass: |
| 1290 | return cast<DeclRefExpr>(e)->getDecl(); |
| 1291 | case Stmt::MemberExprClass: |
| 1292 | return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase()); |
| 1293 | case Stmt::ArraySubscriptExprClass: |
| 1294 | return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase()); |
| 1295 | case Stmt::CallExprClass: |
| 1296 | return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee()); |
| 1297 | case Stmt::UnaryOperatorClass: |
| 1298 | return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr()); |
| 1299 | case Stmt::ParenExprClass: |
| 1300 | return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr()); |
| 1301 | default: |
| 1302 | return 0; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 1307 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 1308 | /// object cannot be declared with storage class register or be a bit field. |
| 1309 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 1310 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| 1311 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
| 1312 | Decl *dcl = getPrimaryDeclaration(op); |
| 1313 | Expr::isLvalueResult lval = op->isLvalue(); |
| 1314 | |
| 1315 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
| 1316 | if (dcl && isa<FunctionDecl>(dcl)) // allow function designators |
| 1317 | ; |
| 1318 | else { // FIXME: emit more specific diag... |
| 1319 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 1320 | op->getSourceRange()); |
| 1321 | return QualType(); |
| 1322 | } |
| 1323 | } else if (dcl) { |
| 1324 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 1325 | // with the register storage-class specifier. |
| 1326 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 1327 | if (vd->getStorageClass() == VarDecl::Register) { |
| 1328 | Diag(OpLoc, diag::err_typecheck_address_of_register, |
| 1329 | op->getSourceRange()); |
| 1330 | return QualType(); |
| 1331 | } |
| 1332 | } else |
| 1333 | assert(0 && "Unknown/unexpected decl type"); |
| 1334 | |
| 1335 | // FIXME: add check for bitfields! |
| 1336 | } |
| 1337 | // If the operand has type "type", the result has type "pointer to type". |
| 1338 | return Context.getPointerType(op->getType()); |
| 1339 | } |
| 1340 | |
| 1341 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1342 | UsualUnaryConversions(op); |
| 1343 | QualType qType = op->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1344 | |
Chris Lattner | befee48 | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1345 | if (const PointerType *PT = qType->getAsPointerType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | QualType ptype = PT->getPointeeType(); |
| 1347 | // C99 6.5.3.2p4. "if it points to an object,...". |
| 1348 | if (ptype->isIncompleteType()) { // An incomplete type is not an object |
| 1349 | // GCC compat: special case 'void *' (treat as warning). |
| 1350 | if (ptype->isVoidType()) { |
| 1351 | Diag(OpLoc, diag::ext_typecheck_deref_ptr_to_void, |
| 1352 | qType.getAsString(), op->getSourceRange()); |
| 1353 | } else { |
| 1354 | Diag(OpLoc, diag::err_typecheck_deref_incomplete_type, |
| 1355 | ptype.getAsString(), op->getSourceRange()); |
| 1356 | return QualType(); |
| 1357 | } |
| 1358 | } |
| 1359 | return ptype; |
| 1360 | } |
| 1361 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
| 1362 | qType.getAsString(), op->getSourceRange()); |
| 1363 | return QualType(); |
| 1364 | } |
| 1365 | |
| 1366 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 1367 | tok::TokenKind Kind) { |
| 1368 | BinaryOperator::Opcode Opc; |
| 1369 | switch (Kind) { |
| 1370 | default: assert(0 && "Unknown binop!"); |
| 1371 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 1372 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 1373 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 1374 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 1375 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 1376 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 1377 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 1378 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 1379 | case tok::less: Opc = BinaryOperator::LT; break; |
| 1380 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 1381 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 1382 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 1383 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 1384 | case tok::amp: Opc = BinaryOperator::And; break; |
| 1385 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 1386 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 1387 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 1388 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 1389 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 1390 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 1391 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 1392 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 1393 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 1394 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 1395 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 1396 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 1397 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 1398 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 1399 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 1400 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 1401 | } |
| 1402 | return Opc; |
| 1403 | } |
| 1404 | |
| 1405 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 1406 | tok::TokenKind Kind) { |
| 1407 | UnaryOperator::Opcode Opc; |
| 1408 | switch (Kind) { |
| 1409 | default: assert(0 && "Unknown unary op!"); |
| 1410 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 1411 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 1412 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 1413 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 1414 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 1415 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 1416 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 1417 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 1418 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 1419 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 1420 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 1421 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 1422 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 1423 | } |
| 1424 | return Opc; |
| 1425 | } |
| 1426 | |
| 1427 | // Binary Operators. 'Tok' is the token for the operator. |
| 1428 | Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
| 1429 | ExprTy *LHS, ExprTy *RHS) { |
| 1430 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 1431 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 1432 | |
| 1433 | assert((lhs != 0) && "ParseBinOp(): missing left expression"); |
| 1434 | assert((rhs != 0) && "ParseBinOp(): missing right expression"); |
| 1435 | |
| 1436 | QualType ResultTy; // Result type of the binary operator. |
| 1437 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 1438 | |
| 1439 | switch (Opc) { |
| 1440 | default: |
| 1441 | assert(0 && "Unknown binary expr!"); |
| 1442 | case BinaryOperator::Assign: |
| 1443 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
| 1444 | break; |
| 1445 | case BinaryOperator::Mul: |
| 1446 | case BinaryOperator::Div: |
| 1447 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1448 | break; |
| 1449 | case BinaryOperator::Rem: |
| 1450 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1451 | break; |
| 1452 | case BinaryOperator::Add: |
| 1453 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1454 | break; |
| 1455 | case BinaryOperator::Sub: |
| 1456 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1457 | break; |
| 1458 | case BinaryOperator::Shl: |
| 1459 | case BinaryOperator::Shr: |
| 1460 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1461 | break; |
| 1462 | case BinaryOperator::LE: |
| 1463 | case BinaryOperator::LT: |
| 1464 | case BinaryOperator::GE: |
| 1465 | case BinaryOperator::GT: |
| 1466 | ResultTy = CheckRelationalOperands(lhs, rhs, TokLoc); |
| 1467 | break; |
| 1468 | case BinaryOperator::EQ: |
| 1469 | case BinaryOperator::NE: |
| 1470 | ResultTy = CheckEqualityOperands(lhs, rhs, TokLoc); |
| 1471 | break; |
| 1472 | case BinaryOperator::And: |
| 1473 | case BinaryOperator::Xor: |
| 1474 | case BinaryOperator::Or: |
| 1475 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1476 | break; |
| 1477 | case BinaryOperator::LAnd: |
| 1478 | case BinaryOperator::LOr: |
| 1479 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
| 1480 | break; |
| 1481 | case BinaryOperator::MulAssign: |
| 1482 | case BinaryOperator::DivAssign: |
| 1483 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1484 | if (!CompTy.isNull()) |
| 1485 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1486 | break; |
| 1487 | case BinaryOperator::RemAssign: |
| 1488 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1489 | if (!CompTy.isNull()) |
| 1490 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1491 | break; |
| 1492 | case BinaryOperator::AddAssign: |
| 1493 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1494 | if (!CompTy.isNull()) |
| 1495 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1496 | break; |
| 1497 | case BinaryOperator::SubAssign: |
| 1498 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1499 | if (!CompTy.isNull()) |
| 1500 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1501 | break; |
| 1502 | case BinaryOperator::ShlAssign: |
| 1503 | case BinaryOperator::ShrAssign: |
| 1504 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1505 | if (!CompTy.isNull()) |
| 1506 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1507 | break; |
| 1508 | case BinaryOperator::AndAssign: |
| 1509 | case BinaryOperator::XorAssign: |
| 1510 | case BinaryOperator::OrAssign: |
| 1511 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1512 | if (!CompTy.isNull()) |
| 1513 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1514 | break; |
| 1515 | case BinaryOperator::Comma: |
| 1516 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
| 1517 | break; |
| 1518 | } |
| 1519 | if (ResultTy.isNull()) |
| 1520 | return true; |
| 1521 | if (CompTy.isNull()) |
| 1522 | return new BinaryOperator(lhs, rhs, Opc, ResultTy); |
| 1523 | else |
| 1524 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy); |
| 1525 | } |
| 1526 | |
| 1527 | // Unary Operators. 'Tok' is the token for the operator. |
| 1528 | Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
| 1529 | ExprTy *input) { |
| 1530 | Expr *Input = (Expr*)input; |
| 1531 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 1532 | QualType resultType; |
| 1533 | switch (Opc) { |
| 1534 | default: |
| 1535 | assert(0 && "Unimplemented unary expr!"); |
| 1536 | case UnaryOperator::PreInc: |
| 1537 | case UnaryOperator::PreDec: |
| 1538 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
| 1539 | break; |
| 1540 | case UnaryOperator::AddrOf: |
| 1541 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 1542 | break; |
| 1543 | case UnaryOperator::Deref: |
| 1544 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 1545 | break; |
| 1546 | case UnaryOperator::Plus: |
| 1547 | case UnaryOperator::Minus: |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1548 | UsualUnaryConversions(Input); |
| 1549 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1550 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 1551 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1552 | resultType.getAsString()); |
| 1553 | break; |
| 1554 | case UnaryOperator::Not: // bitwise complement |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1555 | UsualUnaryConversions(Input); |
| 1556 | resultType = Input->getType(); |
Steve Naroff | c63b96a | 2007-07-12 21:46:55 +0000 | [diff] [blame] | 1557 | if (!resultType->isIntegerType()) // C99 6.5.3.3p1 |
| 1558 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1559 | resultType.getAsString()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1560 | break; |
| 1561 | case UnaryOperator::LNot: // logical negation |
| 1562 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 1563 | DefaultFunctionArrayConversion(Input); |
| 1564 | resultType = Input->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 1566 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1567 | resultType.getAsString()); |
| 1568 | // LNot always has type int. C99 6.5.3.3p5. |
| 1569 | resultType = Context.IntTy; |
| 1570 | break; |
| 1571 | case UnaryOperator::SizeOf: |
| 1572 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true); |
| 1573 | break; |
| 1574 | case UnaryOperator::AlignOf: |
| 1575 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false); |
| 1576 | break; |
| 1577 | case UnaryOperator::Extension: |
| 1578 | // FIXME: does __extension__ cause any promotions? I would think not. |
| 1579 | resultType = Input->getType(); |
| 1580 | break; |
| 1581 | } |
| 1582 | if (resultType.isNull()) |
| 1583 | return true; |
| 1584 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
| 1585 | } |
| 1586 | |
| 1587 | /// ParseAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 1588 | Sema::ExprResult Sema::ParseAddrLabel(SourceLocation OpLoc, |
| 1589 | SourceLocation LabLoc, |
| 1590 | IdentifierInfo *LabelII) { |
| 1591 | // Look up the record for this label identifier. |
| 1592 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 1593 | |
| 1594 | // If we haven't seen this label yet, create a forward reference. |
| 1595 | if (LabelDecl == 0) |
| 1596 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 1597 | |
| 1598 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | 6481a57 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 1599 | return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 1600 | Context.getPointerType(Context.VoidTy)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 1603 | Sema::ExprResult Sema::ParseStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
| 1604 | SourceLocation RPLoc) { // "({..})" |
| 1605 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 1606 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 1607 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 1608 | |
| 1609 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 1610 | // example, it is not possible to goto into a stmt expression apparently. |
| 1611 | // More semantic analysis is needed. |
| 1612 | |
| 1613 | // FIXME: the last statement in the compount stmt has its value used. We |
| 1614 | // should not warn about it being unused. |
| 1615 | |
| 1616 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 1617 | // as the type of the stmtexpr. |
| 1618 | QualType Ty = Context.VoidTy; |
| 1619 | |
| 1620 | if (!Compound->body_empty()) |
| 1621 | if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back())) |
| 1622 | Ty = LastExpr->getType(); |
| 1623 | |
| 1624 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 1625 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1626 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1627 | Sema::ExprResult Sema::ParseTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1628 | TypeTy *arg1, TypeTy *arg2, |
| 1629 | SourceLocation RPLoc) { |
| 1630 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 1631 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 1632 | |
| 1633 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 1634 | |
Steve Naroff | 363bcff | 2007-08-01 23:45:51 +0000 | [diff] [blame] | 1635 | return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2, RPLoc); |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 1638 | Sema::ExprResult Sema::ParseChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
| 1639 | ExprTy *expr1, ExprTy *expr2, |
| 1640 | SourceLocation RPLoc) { |
| 1641 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 1642 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 1643 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 1644 | |
| 1645 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 1646 | |
| 1647 | // The conditional expression is required to be a constant expression. |
| 1648 | llvm::APSInt condEval(32); |
| 1649 | SourceLocation ExpLoc; |
| 1650 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 1651 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant, |
| 1652 | CondExpr->getSourceRange()); |
| 1653 | |
| 1654 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 1655 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 1656 | RHSExpr->getType(); |
| 1657 | return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc); |
| 1658 | } |
| 1659 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1660 | // TODO: Move this to SemaObjC.cpp |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1661 | Sema::ExprResult Sema::ParseObjCStringLiteral(ExprTy *string) { |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1662 | StringLiteral* S = static_cast<StringLiteral *>(string); |
| 1663 | |
| 1664 | if (CheckBuiltinCFStringArgument(S)) |
| 1665 | return true; |
| 1666 | |
| 1667 | QualType t = Context.getCFConstantStringType(); |
| 1668 | t = t.getQualifiedType(QualType::Const); |
| 1669 | t = Context.getPointerType(t); |
| 1670 | |
| 1671 | return new ObjCStringLiteral(S, t); |
| 1672 | } |
Anders Carlsson | f9bcf01 | 2007-08-22 15:14:15 +0000 | [diff] [blame] | 1673 | |
| 1674 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 1675 | SourceLocation LParenLoc, |
| 1676 | TypeTy *Ty, |
| 1677 | SourceLocation RParenLoc) { |
| 1678 | QualType EncodedType = QualType::getFromOpaquePtr(Ty); |
| 1679 | |
| 1680 | QualType t = Context.getPointerType(Context.CharTy); |
| 1681 | return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); |
| 1682 | } |