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