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