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 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
Ted Kremenek | 30c6675 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 15 | #include "SemaUtil.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 17 | #include "clang/AST/Expr.h" |
Steve Naroff | c39ca26 | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 18 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | #include "clang/Lex/LiteralSupport.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 28 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 29 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 30 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 31 | /// multiple tokens. However, the common case is that StringToks points to one |
| 32 | /// string. |
| 33 | /// |
| 34 | Action::ExprResult |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 35 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 36 | assert(NumStringToks && "Must have at least one string!"); |
| 37 | |
| 38 | StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target); |
| 39 | if (Literal.hadError) |
| 40 | return ExprResult(true); |
| 41 | |
| 42 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| 43 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 44 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 45 | |
| 46 | // Verify that pascal strings aren't too large. |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 47 | if (Literal.Pascal && Literal.GetStringLength() > 256) |
| 48 | return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long, |
| 49 | SourceRange(StringToks[0].getLocation(), |
| 50 | StringToks[NumStringToks-1].getLocation())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 52 | QualType StrTy = Context.CharTy; |
| 53 | // FIXME: handle wchar_t |
| 54 | if (Literal.Pascal) StrTy = Context.UnsignedCharTy; |
| 55 | |
| 56 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 57 | // the nul terminator character as well as the string length for pascal |
| 58 | // strings. |
| 59 | StrTy = Context.getConstantArrayType(StrTy, |
| 60 | llvm::APInt(32, Literal.GetStringLength()+1), |
| 61 | ArrayType::Normal, 0); |
| 62 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 63 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| 64 | return new StringLiteral(Literal.GetString(), Literal.GetStringLength(), |
Chris Lattner | a6dcce3 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 65 | Literal.AnyWide, StrTy, |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 66 | StringToks[0].getLocation(), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 67 | StringToks[NumStringToks-1].getLocation()); |
| 68 | } |
| 69 | |
| 70 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 71 | /// ActOnIdentifierExpr - The parser read an identifier in expression context, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | /// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this |
Steve Naroff | e50e14c | 2008-03-19 23:46:26 +0000 | [diff] [blame] | 73 | /// identifier is used in a function call context. |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 74 | Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 75 | IdentifierInfo &II, |
| 76 | bool HasTrailingLParen) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 77 | // Could be enum-constant, value decl, instance variable, etc. |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 78 | Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, Loc, S); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 79 | |
| 80 | // If this reference is in an Objective-C method, then ivar lookup happens as |
| 81 | // well. |
| 82 | if (CurMethodDecl) { |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 83 | ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D); |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 84 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 85 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 86 | // found a decl, but that decl is outside the current method (i.e. a global |
| 87 | // variable). In these two cases, we do a lookup for an ivar with this |
| 88 | // name, if the lookup suceeds, we replace it our current decl. |
Steve Naroff | e57c21a | 2008-04-01 23:04:06 +0000 | [diff] [blame] | 89 | if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) { |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 90 | ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface(), *DeclClass; |
| 91 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) { |
| 92 | // FIXME: This should use a new expr for a direct reference, don't turn |
| 93 | // this into Self->ivar, just return a BareIVarExpr or something. |
| 94 | IdentifierInfo &II = Context.Idents.get("self"); |
| 95 | ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false); |
| 96 | return new ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| 97 | static_cast<Expr*>(SelfExpr.Val), true, true); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | if (D == 0) { |
| 103 | // Otherwise, this could be an implicitly declared function reference (legal |
| 104 | // in C90, extension in C99). |
| 105 | if (HasTrailingLParen && |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 106 | !getLangOptions().CPlusPlus) // Not in C++. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 107 | D = ImplicitlyDefineFunction(Loc, II, S); |
| 108 | else { |
| 109 | // If this name wasn't predeclared and if this is not a function call, |
| 110 | // diagnose the problem. |
| 111 | return Diag(Loc, diag::err_undeclared_var_use, II.getName()); |
| 112 | } |
| 113 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 114 | |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 115 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
Chris Lattner | ee4c3bf | 2008-02-29 16:48:43 +0000 | [diff] [blame] | 116 | // check if referencing an identifier with __attribute__((deprecated)). |
| 117 | if (VD->getAttr<DeprecatedAttr>()) |
| 118 | Diag(Loc, diag::warn_deprecated, VD->getName()); |
| 119 | |
Steve Naroff | cae537d | 2007-08-28 18:45:29 +0000 | [diff] [blame] | 120 | // Only create DeclRefExpr's for valid Decl's. |
Steve Naroff | d1ad6ae | 2007-08-28 20:14:24 +0000 | [diff] [blame] | 121 | if (VD->isInvalidDecl()) |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 122 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 123 | return new DeclRefExpr(VD, VD->getType(), Loc); |
Steve Naroff | 91b03f7 | 2007-08-28 03:03:08 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | c72d22d | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 126 | if (isa<TypedefDecl>(D)) |
| 127 | return Diag(Loc, diag::err_unexpected_typedef, II.getName()); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 128 | if (isa<ObjCInterfaceDecl>(D)) |
Fariborz Jahanian | 3102df9 | 2007-12-05 18:16:33 +0000 | [diff] [blame] | 129 | return Diag(Loc, diag::err_unexpected_interface, II.getName()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 130 | |
| 131 | assert(0 && "Invalid decl"); |
| 132 | abort(); |
| 133 | } |
| 134 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 135 | Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 136 | tok::TokenKind Kind) { |
| 137 | PreDefinedExpr::IdentType IT; |
| 138 | |
| 139 | switch (Kind) { |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 140 | default: assert(0 && "Unknown simple primary expr!"); |
| 141 | case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2] |
| 142 | case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break; |
| 143 | case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 145 | |
| 146 | // Verify that this is in a function context. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 147 | if (CurFunctionDecl == 0 && CurMethodDecl == 0) |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 148 | return Diag(Loc, diag::err_predef_outside_function); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 150 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 151 | // string. |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 152 | unsigned Length; |
| 153 | if (CurFunctionDecl) |
| 154 | Length = CurFunctionDecl->getIdentifier()->getLength(); |
| 155 | else |
Fariborz Jahanian | dcecd5c | 2008-01-17 17:37:26 +0000 | [diff] [blame] | 156 | Length = CurMethodDecl->getSynthesizedMethodSize(); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 157 | |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 158 | llvm::APInt LengthI(32, Length + 1); |
Chris Lattner | e12ca5d | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 159 | QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const); |
Chris Lattner | fc9511c | 2008-01-12 19:32:28 +0000 | [diff] [blame] | 160 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
Chris Lattner | 7e63751 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 161 | return new PreDefinedExpr(Loc, ResTy, IT); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 164 | Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 165 | llvm::SmallString<16> CharBuffer; |
| 166 | CharBuffer.resize(Tok.getLength()); |
| 167 | const char *ThisTokBegin = &CharBuffer[0]; |
| 168 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 169 | |
| 170 | CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 171 | Tok.getLocation(), PP); |
| 172 | if (Literal.hadError()) |
| 173 | return ExprResult(true); |
Chris Lattner | 6b22fb7 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 174 | |
| 175 | QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy; |
| 176 | |
| 177 | return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 180 | Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 181 | // fast path for a single digit (which is quite common). A single digit |
| 182 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 183 | if (Tok.getLength() == 1) { |
| 184 | const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation()); |
| 185 | |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 186 | unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 187 | return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'), |
| 188 | Context.IntTy, |
| 189 | Tok.getLocation())); |
| 190 | } |
| 191 | llvm::SmallString<512> IntegerBuffer; |
| 192 | IntegerBuffer.resize(Tok.getLength()); |
| 193 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| 194 | |
| 195 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 196 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin); |
| 197 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| 198 | Tok.getLocation(), PP); |
| 199 | if (Literal.hadError) |
| 200 | return ExprResult(true); |
| 201 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 202 | Expr *Res; |
| 203 | |
| 204 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 205 | QualType Ty; |
| 206 | const llvm::fltSemantics *Format; |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 207 | |
| 208 | if (Literal.isFloat) { |
| 209 | Ty = Context.FloatTy; |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 210 | Format = Context.Target.getFloatFormat(); |
| 211 | } else if (!Literal.isLong) { |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 212 | Ty = Context.DoubleTy; |
Chris Lattner | fc18dcc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 213 | Format = Context.Target.getDoubleFormat(); |
| 214 | } else { |
| 215 | Ty = Context.LongDoubleTy; |
| 216 | Format = Context.Target.getLongDoubleFormat(); |
Chris Lattner | 858eece | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Ted Kremenek | ddedbe2 | 2007-11-29 00:56:49 +0000 | [diff] [blame] | 219 | // isExact will be set by GetFloatValue(). |
| 220 | bool isExact = false; |
| 221 | |
| 222 | Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact, |
| 223 | Ty, Tok.getLocation()); |
| 224 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 225 | } else if (!Literal.isIntegerLiteral()) { |
| 226 | return ExprResult(true); |
| 227 | } else { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 228 | QualType t; |
| 229 | |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 230 | // long long is a C99 feature. |
| 231 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 9bd4708 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 232 | Literal.isLongLong) |
Neil Booth | 7421e9c | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 233 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 234 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 235 | // Get the value in the widest-possible width. |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 236 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 237 | |
| 238 | if (Literal.GetIntegerValue(ResultVal)) { |
| 239 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 240 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 241 | t = Context.UnsignedLongLongTy; |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 242 | assert(Context.getTypeSize(t) == ResultVal.getBitWidth() && |
| 243 | "long long is not intmax_t?"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 244 | } else { |
| 245 | // If this value fits into a ULL, try to figure out what else it fits into |
| 246 | // according to the rules of C99 6.4.4.1p5. |
| 247 | |
| 248 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 249 | // be an unsigned int. |
| 250 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 251 | |
| 252 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 98540b6 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 253 | if (!Literal.isLong && !Literal.isLongLong) { |
| 254 | // Are int/unsigned possibilities? |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 255 | unsigned IntSize = |
| 256 | static_cast<unsigned>(Context.getTypeSize(Context.IntTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 257 | // Does it fit in a unsigned int? |
| 258 | if (ResultVal.isIntN(IntSize)) { |
| 259 | // Does it fit in a signed int? |
| 260 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| 261 | t = Context.IntTy; |
| 262 | else if (AllowUnsigned) |
| 263 | t = Context.UnsignedIntTy; |
| 264 | } |
| 265 | |
| 266 | if (!t.isNull()) |
| 267 | ResultVal.trunc(IntSize); |
| 268 | } |
| 269 | |
| 270 | // Are long/unsigned long possibilities? |
| 271 | if (t.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 272 | unsigned LongSize = |
| 273 | static_cast<unsigned>(Context.getTypeSize(Context.LongTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 274 | |
| 275 | // Does it fit in a unsigned long? |
| 276 | if (ResultVal.isIntN(LongSize)) { |
| 277 | // Does it fit in a signed long? |
| 278 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| 279 | t = Context.LongTy; |
| 280 | else if (AllowUnsigned) |
| 281 | t = Context.UnsignedLongTy; |
| 282 | } |
| 283 | if (!t.isNull()) |
| 284 | ResultVal.trunc(LongSize); |
| 285 | } |
| 286 | |
| 287 | // Finally, check long long if needed. |
| 288 | if (t.isNull()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 289 | unsigned LongLongSize = |
| 290 | static_cast<unsigned>(Context.getTypeSize(Context.LongLongTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 291 | |
| 292 | // Does it fit in a unsigned long long? |
| 293 | if (ResultVal.isIntN(LongLongSize)) { |
| 294 | // Does it fit in a signed long long? |
| 295 | if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0) |
| 296 | t = Context.LongLongTy; |
| 297 | else if (AllowUnsigned) |
| 298 | t = Context.UnsignedLongLongTy; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // If we still couldn't decide a type, we probably have something that |
| 303 | // does not fit in a signed long long, but has no U suffix. |
| 304 | if (t.isNull()) { |
| 305 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| 306 | t = Context.UnsignedLongLongTy; |
| 307 | } |
| 308 | } |
| 309 | |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 310 | Res = new IntegerLiteral(ResultVal, t, Tok.getLocation()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 1de66eb | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 312 | |
| 313 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 314 | if (Literal.isImaginary) |
| 315 | Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType())); |
| 316 | |
| 317 | return Res; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 320 | Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 321 | ExprTy *Val) { |
| 322 | Expr *e = (Expr *)Val; |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 323 | assert((e != 0) && "ActOnParenExpr() missing expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 324 | return new ParenExpr(L, R, e); |
| 325 | } |
| 326 | |
| 327 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| 328 | /// See C99 6.3.2.1p[2-4] for more details. |
| 329 | QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType, |
| 330 | SourceLocation OpLoc, bool isSizeof) { |
| 331 | // C99 6.5.3.4p1: |
| 332 | if (isa<FunctionType>(exprType) && isSizeof) |
| 333 | // alignof(function) is allowed. |
| 334 | Diag(OpLoc, diag::ext_sizeof_function_type); |
| 335 | else if (exprType->isVoidType()) |
| 336 | Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof"); |
| 337 | else if (exprType->isIncompleteType()) { |
| 338 | Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type : |
| 339 | diag::err_alignof_incomplete_type, |
| 340 | exprType.getAsString()); |
| 341 | return QualType(); // error |
| 342 | } |
| 343 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| 344 | return Context.getSizeType(); |
| 345 | } |
| 346 | |
| 347 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 348 | ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 349 | SourceLocation LPLoc, TypeTy *Ty, |
| 350 | SourceLocation RPLoc) { |
| 351 | // If error parsing type, ignore. |
| 352 | if (Ty == 0) return true; |
| 353 | |
| 354 | // Verify that this is a valid expression. |
| 355 | QualType ArgTy = QualType::getFromOpaquePtr(Ty); |
| 356 | |
| 357 | QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof); |
| 358 | |
| 359 | if (resultType.isNull()) |
| 360 | return true; |
| 361 | return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc); |
| 362 | } |
| 363 | |
Chris Lattner | 5110ad5 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 364 | QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) { |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 365 | DefaultFunctionArrayConversion(V); |
| 366 | |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 367 | // These operators return the element type of a complex type. |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 368 | if (const ComplexType *CT = V->getType()->getAsComplexType()) |
| 369 | return CT->getElementType(); |
Chris Lattner | a16e42d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 370 | |
| 371 | // Otherwise they pass through real integer and floating point types here. |
| 372 | if (V->getType()->isArithmeticType()) |
| 373 | return V->getType(); |
| 374 | |
| 375 | // Reject anything else. |
| 376 | Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString()); |
| 377 | return QualType(); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 381 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 382 | Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 383 | tok::TokenKind Kind, |
| 384 | ExprTy *Input) { |
| 385 | UnaryOperator::Opcode Opc; |
| 386 | switch (Kind) { |
| 387 | default: assert(0 && "Unknown unary op!"); |
| 388 | case tok::plusplus: Opc = UnaryOperator::PostInc; break; |
| 389 | case tok::minusminus: Opc = UnaryOperator::PostDec; break; |
| 390 | } |
| 391 | QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc); |
| 392 | if (result.isNull()) |
| 393 | return true; |
| 394 | return new UnaryOperator((Expr *)Input, Opc, result, OpLoc); |
| 395 | } |
| 396 | |
| 397 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 398 | ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 399 | ExprTy *Idx, SourceLocation RLoc) { |
| 400 | Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx); |
| 401 | |
| 402 | // Perform default conversions. |
| 403 | DefaultFunctionArrayConversion(LHSExp); |
| 404 | DefaultFunctionArrayConversion(RHSExp); |
| 405 | |
| 406 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| 407 | |
| 408 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 409 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 410 | // in the subscript position. As a result, we need to derive the array base |
| 411 | // and index from the expression types. |
| 412 | Expr *BaseExpr, *IndexExpr; |
| 413 | QualType ResultType; |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 414 | if (const PointerType *PTy = LHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 415 | BaseExpr = LHSExp; |
| 416 | IndexExpr = RHSExp; |
| 417 | // FIXME: need to deal with const... |
| 418 | ResultType = PTy->getPointeeType(); |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 419 | } else if (const PointerType *PTy = RHSTy->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 420 | // Handle the uncommon case of "123[Ptr]". |
| 421 | BaseExpr = RHSExp; |
| 422 | IndexExpr = LHSExp; |
| 423 | // FIXME: need to deal with const... |
| 424 | ResultType = PTy->getPointeeType(); |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 425 | } else if (const VectorType *VTy = LHSTy->getAsVectorType()) { |
| 426 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 427 | IndexExpr = RHSExp; |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 428 | |
| 429 | // Component access limited to variables (reject vec4.rg[1]). |
Nate Begeman | 506806b | 2008-02-19 01:11:03 +0000 | [diff] [blame] | 430 | if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr)) |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 431 | return Diag(LLoc, diag::err_ocuvector_component_access, |
| 432 | SourceRange(LLoc, RLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 433 | // FIXME: need to deal with const... |
| 434 | ResultType = VTy->getElementType(); |
| 435 | } else { |
| 436 | return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value, |
| 437 | RHSExp->getSourceRange()); |
| 438 | } |
| 439 | // C99 6.5.2.1p1 |
| 440 | if (!IndexExpr->getType()->isIntegerType()) |
| 441 | return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript, |
| 442 | IndexExpr->getSourceRange()); |
| 443 | |
| 444 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice, |
| 445 | // the following check catches trying to index a pointer to a function (e.g. |
| 446 | // void (*)(int)). Functions are not objects in C99. |
| 447 | if (!ResultType->isObjectType()) |
| 448 | return Diag(BaseExpr->getLocStart(), |
| 449 | diag::err_typecheck_subscript_not_object, |
| 450 | BaseExpr->getType().getAsString(), BaseExpr->getSourceRange()); |
| 451 | |
| 452 | return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc); |
| 453 | } |
| 454 | |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 455 | QualType Sema:: |
| 456 | CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc, |
| 457 | IdentifierInfo &CompName, SourceLocation CompLoc) { |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 458 | const OCUVectorType *vecType = baseType->getAsOCUVectorType(); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 459 | |
| 460 | // The vector accessor can't exceed the number of elements. |
| 461 | const char *compStr = CompName.getName(); |
| 462 | if (strlen(compStr) > vecType->getNumElements()) { |
| 463 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 464 | baseType.getAsString(), SourceRange(CompLoc)); |
| 465 | return QualType(); |
| 466 | } |
| 467 | // The component names must come from the same set. |
Chris Lattner | 9096b79 | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 468 | if (vecType->getPointAccessorIdx(*compStr) != -1) { |
| 469 | do |
| 470 | compStr++; |
| 471 | while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1); |
| 472 | } else if (vecType->getColorAccessorIdx(*compStr) != -1) { |
| 473 | do |
| 474 | compStr++; |
| 475 | while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1); |
| 476 | } else if (vecType->getTextureAccessorIdx(*compStr) != -1) { |
| 477 | do |
| 478 | compStr++; |
| 479 | while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1); |
| 480 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 481 | |
| 482 | if (*compStr) { |
| 483 | // We didn't get to the end of the string. This means the component names |
| 484 | // didn't come from the same set *or* we encountered an illegal name. |
| 485 | Diag(OpLoc, diag::err_ocuvector_component_name_illegal, |
| 486 | std::string(compStr,compStr+1), SourceRange(CompLoc)); |
| 487 | return QualType(); |
| 488 | } |
| 489 | // Each component accessor can't exceed the vector type. |
| 490 | compStr = CompName.getName(); |
| 491 | while (*compStr) { |
| 492 | if (vecType->isAccessorWithinNumElements(*compStr)) |
| 493 | compStr++; |
| 494 | else |
| 495 | break; |
| 496 | } |
| 497 | if (*compStr) { |
| 498 | // We didn't get to the end of the string. This means a component accessor |
| 499 | // exceeds the number of elements in the vector. |
| 500 | Diag(OpLoc, diag::err_ocuvector_component_exceeds_length, |
| 501 | baseType.getAsString(), SourceRange(CompLoc)); |
| 502 | return QualType(); |
| 503 | } |
| 504 | // The component accessor looks fine - now we need to compute the actual type. |
| 505 | // The vector type is implied by the component accessor. For example, |
| 506 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| 507 | unsigned CompSize = strlen(CompName.getName()); |
| 508 | if (CompSize == 1) |
| 509 | return vecType->getElementType(); |
Steve Naroff | 82113e3 | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 510 | |
| 511 | QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize); |
| 512 | // Now look up the TypeDefDecl from the vector type. Without this, |
| 513 | // diagostics look bad. We want OCU vector types to appear built-in. |
| 514 | for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) { |
| 515 | if (OCUVectorDecls[i]->getUnderlyingType() == VT) |
| 516 | return Context.getTypedefType(OCUVectorDecls[i]); |
| 517 | } |
| 518 | 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] | 519 | } |
| 520 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 521 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 522 | ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 523 | tok::TokenKind OpKind, SourceLocation MemberLoc, |
| 524 | IdentifierInfo &Member) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 525 | Expr *BaseExpr = static_cast<Expr *>(Base); |
| 526 | assert(BaseExpr && "no record expression"); |
Steve Naroff | 137e11d | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 527 | |
| 528 | // Perform default conversions. |
| 529 | DefaultFunctionArrayConversion(BaseExpr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 530 | |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 531 | QualType BaseType = BaseExpr->getType(); |
| 532 | assert(!BaseType.isNull() && "no type for member expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 533 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 534 | if (OpKind == tok::arrow) { |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 535 | if (const PointerType *PT = BaseType->getAsPointerType()) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 536 | BaseType = PT->getPointeeType(); |
| 537 | else |
| 538 | return Diag(OpLoc, diag::err_typecheck_member_reference_arrow, |
| 539 | SourceRange(MemberLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 540 | } |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 541 | // The base type is either a record or an OCUVectorType. |
Chris Lattner | e35a104 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 542 | if (const RecordType *RTy = BaseType->getAsRecordType()) { |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 543 | RecordDecl *RDecl = RTy->getDecl(); |
| 544 | if (RTy->isIncompleteType()) |
| 545 | return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(), |
| 546 | BaseExpr->getSourceRange()); |
| 547 | // The record definition is complete, now make sure the member is valid. |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 548 | FieldDecl *MemberDecl = RDecl->getMember(&Member); |
| 549 | if (!MemberDecl) |
Steve Naroff | 2cb6638 | 2007-07-26 03:11:44 +0000 | [diff] [blame] | 550 | return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(), |
| 551 | SourceRange(MemberLoc)); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 552 | |
| 553 | // Figure out the type of the member; see C99 6.5.2.3p3 |
Eli Friedman | aedabcf | 2008-02-07 05:24:51 +0000 | [diff] [blame] | 554 | // FIXME: Handle address space modifiers |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 555 | QualType MemberType = MemberDecl->getType(); |
| 556 | unsigned combinedQualifiers = |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 557 | MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers(); |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 558 | MemberType = MemberType.getQualifiedType(combinedQualifiers); |
| 559 | |
| 560 | return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, |
| 561 | MemberLoc, MemberType); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 562 | } else if (BaseType->isOCUVectorType() && OpKind == tok::period) { |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 563 | // Component access limited to variables (reject vec4.rg.g). |
Nate Begeman | 78a2a31 | 2008-03-17 17:22:18 +0000 | [diff] [blame] | 564 | if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr)) |
Steve Naroff | 8934552 | 2007-08-03 22:40:33 +0000 | [diff] [blame] | 565 | return Diag(OpLoc, diag::err_ocuvector_component_access, |
| 566 | SourceRange(MemberLoc)); |
Steve Naroff | 1b8a46c | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 567 | QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc); |
| 568 | if (ret.isNull()) |
| 569 | return true; |
Chris Lattner | a0d03a7 | 2007-08-03 17:31:20 +0000 | [diff] [blame] | 570 | return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc); |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 571 | } else if (BaseType->isObjCInterfaceType()) { |
| 572 | ObjCInterfaceDecl *IFace; |
| 573 | if (isa<ObjCInterfaceType>(BaseType.getCanonicalType())) |
| 574 | IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl(); |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 575 | else |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 576 | IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl(); |
| 577 | ObjCInterfaceDecl *clsDeclared; |
| 578 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared)) |
Fariborz Jahanian | 4af7249 | 2007-11-12 22:29:28 +0000 | [diff] [blame] | 579 | return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr, |
| 580 | OpKind==tok::arrow); |
| 581 | } |
| 582 | return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion, |
| 583 | SourceRange(MemberLoc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 586 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 587 | /// This provides the location of the left/right parens and a list of comma |
| 588 | /// locations. |
| 589 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 590 | ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc, |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 591 | ExprTy **args, unsigned NumArgs, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 592 | SourceLocation *CommaLocs, SourceLocation RParenLoc) { |
| 593 | Expr *Fn = static_cast<Expr *>(fn); |
| 594 | Expr **Args = reinterpret_cast<Expr**>(args); |
| 595 | assert(Fn && "no function call expression"); |
| 596 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 597 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 598 | // of arguments and function on error. |
| 599 | llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs, |
| 600 | Context.BoolTy, RParenLoc)); |
| 601 | |
| 602 | // Promote the function operand. |
| 603 | TheCall->setCallee(UsualUnaryConversions(Fn)); |
| 604 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 605 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall have |
| 606 | // type pointer to function". |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 607 | const PointerType *PT = Fn->getType()->getAsPointerType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 608 | if (PT == 0) |
| 609 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 610 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 611 | const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType(); |
| 612 | if (FuncT == 0) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 613 | return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function, |
| 614 | SourceRange(Fn->getLocStart(), RParenLoc)); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 615 | |
| 616 | // We know the result type of the call, set it. |
| 617 | TheCall->setType(FuncT->getResultType()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 618 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 619 | if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 620 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| 621 | // assignment, to the types of the corresponding parameter, ... |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 622 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 623 | unsigned NumArgsToCheck = NumArgs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 625 | // If too few arguments are available, don't make the call. |
| 626 | if (NumArgs < NumArgsInProto) |
| 627 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 628 | Fn->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 630 | // If too many are passed and not variadic, error on the extras and drop |
| 631 | // them. |
| 632 | if (NumArgs > NumArgsInProto) { |
| 633 | if (!Proto->isVariadic()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 634 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 635 | diag::err_typecheck_call_too_many_args, Fn->getSourceRange(), |
| 636 | SourceRange(Args[NumArgsInProto]->getLocStart(), |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 637 | Args[NumArgs-1]->getLocEnd())); |
| 638 | // This deletes the extra arguments. |
| 639 | TheCall->setNumArgs(NumArgsInProto); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 640 | } |
| 641 | NumArgsToCheck = NumArgsInProto; |
| 642 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 644 | // Continue to check argument types (even if we have too few/many args). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 645 | for (unsigned i = 0; i != NumArgsToCheck; i++) { |
| 646 | Expr *Arg = Args[i]; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 647 | QualType ProtoArgType = Proto->getArgType(i); |
| 648 | QualType ArgType = Arg->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 649 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 650 | // Compute implicit casts from the operand to the formal argument type. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 651 | AssignConvertType ConvTy = |
| 652 | CheckSingleAssignmentConstraints(ProtoArgType, Arg); |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 653 | TheCall->setArg(i, Arg); |
| 654 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 655 | if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType, |
| 656 | ArgType, Arg, "passing")) |
| 657 | return true; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 658 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 659 | |
| 660 | // If this is a variadic call, handle args passed through "...". |
| 661 | if (Proto->isVariadic()) { |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 662 | // Promote the arguments (C99 6.5.2.2p7). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 663 | for (unsigned i = NumArgsInProto; i != NumArgs; i++) { |
| 664 | Expr *Arg = Args[i]; |
| 665 | DefaultArgumentPromotion(Arg); |
| 666 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 667 | } |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 668 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 669 | } else { |
| 670 | assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!"); |
| 671 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 672 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 673 | for (unsigned i = 0; i != NumArgs; i++) { |
| 674 | Expr *Arg = Args[i]; |
| 675 | DefaultArgumentPromotion(Arg); |
| 676 | TheCall->setArg(i, Arg); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 677 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 678 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 679 | |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 680 | // Do special checking on direct calls to functions. |
| 681 | if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn)) |
| 682 | if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr())) |
| 683 | if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl())) |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 684 | if (CheckFunctionCall(FDecl, TheCall.get())) |
Anders Carlsson | e7e7aa2 | 2007-08-17 05:31:46 +0000 | [diff] [blame] | 685 | return true; |
Chris Lattner | 2e64c07 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 686 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 687 | return TheCall.take(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 691 | ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 692 | SourceLocation RParenLoc, ExprTy *InitExpr) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 693 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 694 | QualType literalType = QualType::getFromOpaquePtr(Ty); |
| 695 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 696 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 697 | Expr *literalExpr = static_cast<Expr*>(InitExpr); |
Anders Carlsson | 9374b85 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 698 | |
Steve Naroff | cb69fb7 | 2007-12-10 22:44:33 +0000 | [diff] [blame] | 699 | // FIXME: add more semantic analysis (C99 6.5.2.5). |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 700 | if (CheckInitializerTypes(literalExpr, literalType)) |
Steve Naroff | 92590f9 | 2008-01-09 20:58:06 +0000 | [diff] [blame] | 701 | return true; |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 702 | |
| 703 | bool isFileScope = !CurFunctionDecl && !CurMethodDecl; |
| 704 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | f0b2354 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 705 | if (CheckForConstantInitializer(literalExpr, literalType)) |
| 706 | return true; |
| 707 | } |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 708 | return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 712 | ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit, |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 713 | SourceLocation RBraceLoc) { |
Steve Naroff | e14e554 | 2007-09-02 02:04:30 +0000 | [diff] [blame] | 714 | Expr **InitList = reinterpret_cast<Expr**>(initlist); |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 715 | |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 716 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Steve Naroff | 1c9de71 | 2007-09-03 01:24:23 +0000 | [diff] [blame] | 717 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Anders Carlsson | 762b7c7 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 718 | |
Steve Naroff | 7c9d72d | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 719 | InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc); |
| 720 | e->setType(Context.VoidTy); // FIXME: just a place holder for now. |
| 721 | return e; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Chris Lattner | d1f26b3 | 2007-12-20 00:44:32 +0000 | [diff] [blame] | 724 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) { |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 725 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| 726 | |
| 727 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 728 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 729 | return Diag(R.getBegin(), |
| 730 | Ty->isVectorType() ? |
| 731 | diag::err_invalid_conversion_between_vectors : |
| 732 | diag::err_invalid_conversion_between_vector_and_integer, |
| 733 | VectorTy.getAsString().c_str(), |
| 734 | Ty.getAsString().c_str(), R); |
| 735 | } else |
| 736 | return Diag(R.getBegin(), |
| 737 | diag::err_invalid_conversion_between_vector_and_scalar, |
| 738 | VectorTy.getAsString().c_str(), |
| 739 | Ty.getAsString().c_str(), R); |
| 740 | |
| 741 | return false; |
| 742 | } |
| 743 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 744 | Action::ExprResult Sema:: |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 745 | ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 746 | SourceLocation RParenLoc, ExprTy *Op) { |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 747 | assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 748 | |
| 749 | Expr *castExpr = static_cast<Expr*>(Op); |
| 750 | QualType castType = QualType::getFromOpaquePtr(Ty); |
| 751 | |
Steve Naroff | 68adb48 | 2007-08-31 00:32:44 +0000 | [diff] [blame] | 752 | UsualUnaryConversions(castExpr); |
| 753 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 754 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 755 | // type needs to be scalar. |
Chris Lattner | db52673 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 756 | if (!castType->isVoidType()) { // Cast to void allows any expr type. |
Steve Naroff | f459ee5 | 2008-01-24 22:55:05 +0000 | [diff] [blame] | 757 | if (!castType->isScalarType() && !castType->isVectorType()) |
Chris Lattner | db52673 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 758 | return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar, |
| 759 | castType.getAsString(), SourceRange(LParenLoc, RParenLoc)); |
Steve Naroff | f459ee5 | 2008-01-24 22:55:05 +0000 | [diff] [blame] | 760 | if (!castExpr->getType()->isScalarType() && |
| 761 | !castExpr->getType()->isVectorType()) |
Chris Lattner | db52673 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 762 | return Diag(castExpr->getLocStart(), |
| 763 | diag::err_typecheck_expect_scalar_operand, |
| 764 | castExpr->getType().getAsString(),castExpr->getSourceRange()); |
Anders Carlsson | f257b4c | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 765 | |
| 766 | if (castExpr->getType()->isVectorType()) { |
| 767 | if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), |
| 768 | castExpr->getType(), castType)) |
| 769 | return true; |
| 770 | } else if (castType->isVectorType()) { |
| 771 | if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc), |
| 772 | castType, castExpr->getType())) |
| 773 | return true; |
Chris Lattner | db52673 | 2007-10-29 04:26:44 +0000 | [diff] [blame] | 774 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 775 | } |
| 776 | return new CastExpr(castType, castExpr, LParenLoc); |
| 777 | } |
| 778 | |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 779 | /// Note that lex is not null here, even if this is the gnu "x ?: y" extension. |
| 780 | /// In that case, lex = cond. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 781 | inline QualType Sema::CheckConditionalOperands( // C99 6.5.15 |
| 782 | Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) { |
| 783 | UsualUnaryConversions(cond); |
| 784 | UsualUnaryConversions(lex); |
| 785 | UsualUnaryConversions(rex); |
| 786 | QualType condT = cond->getType(); |
| 787 | QualType lexT = lex->getType(); |
| 788 | QualType rexT = rex->getType(); |
| 789 | |
| 790 | // first, check the condition. |
| 791 | if (!condT->isScalarType()) { // C99 6.5.15p2 |
| 792 | Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar, |
| 793 | condT.getAsString()); |
| 794 | return QualType(); |
| 795 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 796 | |
| 797 | // Now check the two expressions. |
| 798 | |
| 799 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 800 | // to find a common type: C99 6.5.15p3,5. |
| 801 | if (lexT->isArithmeticType() && rexT->isArithmeticType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 802 | UsualArithmeticConversions(lex, rex); |
| 803 | return lex->getType(); |
| 804 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 805 | |
| 806 | // If both operands are the same structure or union type, the result is that |
| 807 | // type. |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 808 | if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3 |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 809 | if (const RecordType *RHSRT = rexT->getAsRecordType()) |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 810 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 811 | // "If both the operands have structure or union type, the result has |
| 812 | // that type." This implies that CV qualifiers are dropped. |
| 813 | return lexT.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 814 | } |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 815 | |
| 816 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
| 817 | if (lexT->isVoidType() && rexT->isVoidType()) |
| 818 | return lexT.getUnqualifiedType(); |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 819 | |
| 820 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 821 | // the type of the other operand." |
| 822 | if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 823 | ImpCastExprToType(rex, lexT); // promote the null to a pointer. |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 824 | return lexT; |
| 825 | } |
| 826 | if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 827 | ImpCastExprToType(lex, rexT); // promote the null to a pointer. |
Steve Naroff | 12ebf27 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 828 | return rexT; |
| 829 | } |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 830 | // Handle the case where both operands are pointers before we handle null |
| 831 | // pointer constants in case both operands are null pointer constants. |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 832 | if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6 |
| 833 | if (const PointerType *RHSPT = rexT->getAsPointerType()) { |
| 834 | // get the "pointed to" types |
| 835 | QualType lhptee = LHSPT->getPointeeType(); |
| 836 | QualType rhptee = RHSPT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 837 | |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 838 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 839 | if (lhptee->isVoidType() && |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 840 | (rhptee->isObjectType() || rhptee->isIncompleteType())) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 841 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 842 | QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 843 | QualType destType = Context.getPointerType(destPointee); |
| 844 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 845 | ImpCastExprToType(rex, destType); // promote to void* |
| 846 | return destType; |
| 847 | } |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 848 | if (rhptee->isVoidType() && |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 849 | (lhptee->isObjectType() || lhptee->isIncompleteType())) { |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 850 | QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers()); |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 851 | QualType destType = Context.getPointerType(destPointee); |
| 852 | ImpCastExprToType(lex, destType); // add qualifiers if necessary |
| 853 | ImpCastExprToType(rex, destType); // promote to void* |
| 854 | return destType; |
| 855 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 856 | |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 857 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 858 | rhptee.getUnqualifiedType())) { |
Steve Naroff | 232324e | 2008-02-01 22:44:48 +0000 | [diff] [blame] | 859 | Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers, |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 860 | lexT.getAsString(), rexT.getAsString(), |
| 861 | lex->getSourceRange(), rex->getSourceRange()); |
Eli Friedman | 3328486 | 2008-01-30 17:02:03 +0000 | [diff] [blame] | 862 | // In this situation, we assume void* type. No especially good |
| 863 | // reason, but this is what gcc does, and we do have to pick |
| 864 | // to get a consistent AST. |
| 865 | QualType voidPtrTy = Context.getPointerType(Context.VoidTy); |
| 866 | ImpCastExprToType(lex, voidPtrTy); |
| 867 | ImpCastExprToType(rex, voidPtrTy); |
| 868 | return voidPtrTy; |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 869 | } |
| 870 | // The pointer types are compatible. |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 871 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 872 | // differently qualified versions of compatible types, the result type is |
| 873 | // a pointer to an appropriately qualified version of the *composite* |
| 874 | // type. |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 875 | // FIXME: Need to return the composite type. |
Eli Friedman | ca07c90 | 2008-02-10 22:59:36 +0000 | [diff] [blame] | 876 | // FIXME: Need to add qualifiers |
Chris Lattner | 0ac5163 | 2008-01-06 22:50:31 +0000 | [diff] [blame] | 877 | return lexT; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 878 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 879 | } |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 880 | |
Chris Lattner | 992ae93 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 881 | // Otherwise, the operands are not compatible. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 882 | Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands, |
| 883 | lexT.getAsString(), rexT.getAsString(), |
| 884 | lex->getSourceRange(), rex->getSourceRange()); |
| 885 | return QualType(); |
| 886 | } |
| 887 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 888 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 889 | /// in the case of a the GNU conditional expr extension. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 890 | Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 891 | SourceLocation ColonLoc, |
| 892 | ExprTy *Cond, ExprTy *LHS, |
| 893 | ExprTy *RHS) { |
| 894 | Expr *CondExpr = (Expr *) Cond; |
| 895 | Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS; |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 896 | |
| 897 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 898 | // was the condition. |
| 899 | bool isLHSNull = LHSExpr == 0; |
| 900 | if (isLHSNull) |
| 901 | LHSExpr = CondExpr; |
| 902 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 903 | QualType result = CheckConditionalOperands(CondExpr, LHSExpr, |
| 904 | RHSExpr, QuestionLoc); |
| 905 | if (result.isNull()) |
| 906 | return true; |
Chris Lattner | 98a425c | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 907 | return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr, |
| 908 | RHSExpr, result); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 911 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Steve Naroff | bbaed75 | 2008-01-29 02:42:22 +0000 | [diff] [blame] | 912 | /// do not have a prototype. Arguments that have type float are promoted to |
| 913 | /// double. All other argument types are converted by UsualUnaryConversions(). |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 914 | void Sema::DefaultArgumentPromotion(Expr *&Expr) { |
| 915 | QualType Ty = Expr->getType(); |
| 916 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 917 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 918 | if (Ty == Context.FloatTy) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 919 | ImpCastExprToType(Expr, Context.DoubleTy); |
Steve Naroff | bbaed75 | 2008-01-29 02:42:22 +0000 | [diff] [blame] | 920 | else |
| 921 | UsualUnaryConversions(Expr); |
Steve Naroff | db65e05 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 924 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| 925 | void Sema::DefaultFunctionArrayConversion(Expr *&e) { |
| 926 | QualType t = e->getType(); |
| 927 | assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 928 | |
Chris Lattner | f0c4a0a | 2007-07-31 16:56:34 +0000 | [diff] [blame] | 929 | if (const ReferenceType *ref = t->getAsReferenceType()) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 930 | ImpCastExprToType(e, ref->getReferenceeType()); // C++ [expr] |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 931 | t = e->getType(); |
| 932 | } |
| 933 | if (t->isFunctionType()) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 934 | ImpCastExprToType(e, Context.getPointerType(t)); |
Steve Naroff | ac26e9a | 2008-02-09 16:59:44 +0000 | [diff] [blame] | 935 | else if (const ArrayType *ary = t->getAsArrayType()) { |
Steve Naroff | 9ffeda1 | 2008-02-09 17:25:18 +0000 | [diff] [blame] | 936 | // Make sure we don't lose qualifiers when dealing with typedefs. Example: |
Steve Naroff | ac26e9a | 2008-02-09 16:59:44 +0000 | [diff] [blame] | 937 | // typedef int arr[10]; |
| 938 | // void test2() { |
| 939 | // const arr b; |
| 940 | // b[4] = 1; |
| 941 | // } |
| 942 | QualType ELT = ary->getElementType(); |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 943 | // FIXME: Handle ASQualType |
| 944 | ELT = ELT.getQualifiedType(t.getCVRQualifiers()|ELT.getCVRQualifiers()); |
Steve Naroff | ac26e9a | 2008-02-09 16:59:44 +0000 | [diff] [blame] | 945 | ImpCastExprToType(e, Context.getPointerType(ELT)); |
| 946 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 949 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 950 | /// operators (C99 6.3). The conversions of array and function types are |
| 951 | /// sometimes surpressed. For example, the array->pointer conversion doesn't |
| 952 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 953 | /// In these instances, this routine should *not* be called. |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 954 | Expr *Sema::UsualUnaryConversions(Expr *&Expr) { |
| 955 | QualType Ty = Expr->getType(); |
| 956 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 958 | if (const ReferenceType *Ref = Ty->getAsReferenceType()) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 959 | ImpCastExprToType(Expr, Ref->getReferenceeType()); // C++ [expr] |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 960 | Ty = Expr->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 961 | } |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 962 | if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2 |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 963 | ImpCastExprToType(Expr, Context.IntTy); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 964 | else |
Chris Lattner | 83bd5eb | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 965 | DefaultFunctionArrayConversion(Expr); |
| 966 | |
| 967 | return Expr; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 970 | /// UsualArithmeticConversions - Performs various conversions that are common to |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 971 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| 972 | /// routine returns the first non-arithmetic type found. The client is |
| 973 | /// responsible for emitting appropriate error diagnostics. |
Steve Naroff | e8419ca | 2008-01-15 22:21:49 +0000 | [diff] [blame] | 974 | /// FIXME: verify the conversion rules for "complex int" are consistent with GCC. |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 975 | QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr, |
| 976 | bool isCompAssign) { |
Steve Naroff | b2f9f55 | 2007-08-25 19:54:59 +0000 | [diff] [blame] | 977 | if (!isCompAssign) { |
| 978 | UsualUnaryConversions(lhsExpr); |
| 979 | UsualUnaryConversions(rhsExpr); |
| 980 | } |
Steve Naroff | 7438fdf | 2007-10-18 18:55:53 +0000 | [diff] [blame] | 981 | // For conversion purposes, we ignore any qualifiers. |
| 982 | // For example, "const float" and "float" are equivalent. |
Steve Naroff | 1ddb6f5 | 2007-11-10 19:45:54 +0000 | [diff] [blame] | 983 | QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType(); |
| 984 | QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 985 | |
| 986 | // If both types are identical, no conversion is needed. |
Steve Naroff | 7438fdf | 2007-10-18 18:55:53 +0000 | [diff] [blame] | 987 | if (lhs == rhs) |
| 988 | return lhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 989 | |
| 990 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 991 | // The caller can deal with this (e.g. pointer + int). |
| 992 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 993 | return lhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 994 | |
| 995 | // At this point, we have two different arithmetic types. |
| 996 | |
| 997 | // Handle complex types first (C99 6.3.1.8p1). |
| 998 | if (lhs->isComplexType() || rhs->isComplexType()) { |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 999 | // if we have an integer operand, the result is the complex type. |
Steve Naroff | e8419ca | 2008-01-15 22:21:49 +0000 | [diff] [blame] | 1000 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1001 | // convert the rhs to the lhs complex type. |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1002 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1003 | return lhs; |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 1004 | } |
Steve Naroff | e8419ca | 2008-01-15 22:21:49 +0000 | [diff] [blame] | 1005 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1006 | // convert the lhs to the rhs complex type. |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1007 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1008 | return rhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1009 | } |
Steve Naroff | 3cf497f | 2007-08-27 01:27:54 +0000 | [diff] [blame] | 1010 | // This handles complex/complex, complex/float, or float/complex. |
| 1011 | // When both operands are complex, the shorter operand is converted to the |
| 1012 | // type of the longer, and that is the type of the result. This corresponds |
| 1013 | // to what is done when combining two real floating-point operands. |
| 1014 | // The fun begins when size promotion occur across type domains. |
| 1015 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 1016 | // floating-point type, the less precise type is converted, within it's |
| 1017 | // real or complex domain, to the precision of the other type. For example, |
| 1018 | // when combining a "long double" with a "double _Complex", the |
| 1019 | // "double _Complex" is promoted to "long double _Complex". |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1020 | int result = Context.compareFloatingType(lhs, rhs); |
| 1021 | |
| 1022 | if (result > 0) { // The left side is bigger, convert rhs. |
Steve Naroff | 3b565d6 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1023 | rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs); |
| 1024 | if (!isCompAssign) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1025 | ImpCastExprToType(rhsExpr, rhs); |
Steve Naroff | 3b565d6 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1026 | } else if (result < 0) { // The right side is bigger, convert lhs. |
| 1027 | lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs); |
| 1028 | if (!isCompAssign) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1029 | ImpCastExprToType(lhsExpr, lhs); |
Steve Naroff | 3b565d6 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1030 | } |
| 1031 | // At this point, lhs and rhs have the same rank/size. Now, make sure the |
| 1032 | // domains match. This is a requirement for our implementation, C99 |
| 1033 | // does not require this promotion. |
| 1034 | if (lhs != rhs) { // Domains don't match, we have complex/float mix. |
| 1035 | if (lhs->isRealFloatingType()) { // handle "double, _Complex double". |
Steve Naroff | 3b6157f | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1036 | if (!isCompAssign) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1037 | ImpCastExprToType(lhsExpr, rhs); |
Steve Naroff | 3b6157f | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1038 | return rhs; |
Steve Naroff | 3b565d6 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1039 | } else { // handle "_Complex double, double". |
Steve Naroff | 3b6157f | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1040 | if (!isCompAssign) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1041 | ImpCastExprToType(rhsExpr, lhs); |
Steve Naroff | 3b6157f | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1042 | return lhs; |
Steve Naroff | 3b565d6 | 2007-08-27 21:32:55 +0000 | [diff] [blame] | 1043 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1044 | } |
Steve Naroff | 3b6157f | 2007-08-27 21:43:43 +0000 | [diff] [blame] | 1045 | return lhs; // The domain/size match exactly. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1046 | } |
| 1047 | // Now handle "real" floating types (i.e. float, double, long double). |
| 1048 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) { |
| 1049 | // if we have an integer operand, the result is the real floating type. |
Steve Naroff | e8419ca | 2008-01-15 22:21:49 +0000 | [diff] [blame] | 1050 | if (rhs->isIntegerType() || rhs->isComplexIntegerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1051 | // convert rhs to the lhs floating point type. |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1052 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1053 | return lhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1054 | } |
Steve Naroff | e8419ca | 2008-01-15 22:21:49 +0000 | [diff] [blame] | 1055 | if (lhs->isIntegerType() || lhs->isComplexIntegerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1056 | // convert lhs to the rhs floating point type. |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1057 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1058 | return rhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1059 | } |
| 1060 | // We have two real floating types, float/complex combos were handled above. |
| 1061 | // Convert the smaller operand to the bigger result. |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1062 | int result = Context.compareFloatingType(lhs, rhs); |
| 1063 | |
| 1064 | if (result > 0) { // convert the rhs |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1065 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1066 | return lhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1067 | } |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1068 | if (result < 0) { // convert the lhs |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1069 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs |
Steve Naroff | 45fc982 | 2007-08-27 15:30:22 +0000 | [diff] [blame] | 1070 | return rhs; |
| 1071 | } |
| 1072 | assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1073 | } |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 1074 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) { |
| 1075 | // Handle GCC complex int extension. |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 1076 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1077 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 1078 | |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1079 | if (lhsComplexInt && rhsComplexInt) { |
| 1080 | if (Context.maxIntegerType(lhsComplexInt->getElementType(), |
Eli Friedman | 94075c0 | 2008-02-08 01:24:30 +0000 | [diff] [blame] | 1081 | rhsComplexInt->getElementType()) == lhs) { |
| 1082 | // convert the rhs |
| 1083 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 1084 | return lhs; |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1085 | } |
| 1086 | if (!isCompAssign) |
Eli Friedman | 94075c0 | 2008-02-08 01:24:30 +0000 | [diff] [blame] | 1087 | ImpCastExprToType(lhsExpr, rhs); // convert the lhs |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1088 | return rhs; |
| 1089 | } else if (lhsComplexInt && rhs->isIntegerType()) { |
| 1090 | // convert the rhs to the lhs complex type. |
| 1091 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
| 1092 | return lhs; |
| 1093 | } else if (rhsComplexInt && lhs->isIntegerType()) { |
| 1094 | // convert the lhs to the rhs complex type. |
| 1095 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); |
| 1096 | return rhs; |
| 1097 | } |
Steve Naroff | 4300121 | 2008-01-15 19:36:10 +0000 | [diff] [blame] | 1098 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1099 | // Finally, we have two differing integer types. |
| 1100 | if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1101 | if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs); |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1102 | return lhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1103 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1104 | if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1105 | return rhs; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
| 1108 | // CheckPointerTypesForAssignment - This is a very tricky routine (despite |
| 1109 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| 1110 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 1111 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 1112 | // FIXME: add a couple examples in this comment. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1113 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1114 | Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) { |
| 1115 | QualType lhptee, rhptee; |
| 1116 | |
| 1117 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Chris Lattner | 7122514 | 2007-07-31 21:27:01 +0000 | [diff] [blame] | 1118 | lhptee = lhsType->getAsPointerType()->getPointeeType(); |
| 1119 | rhptee = rhsType->getAsPointerType()->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1120 | |
| 1121 | // make sure we operate on the canonical type |
| 1122 | lhptee = lhptee.getCanonicalType(); |
| 1123 | rhptee = rhptee.getCanonicalType(); |
| 1124 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1125 | AssignConvertType ConvTy = Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1126 | |
| 1127 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 1128 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 1129 | // qualifiers of the type *pointed to* by the right; |
Chris Lattner | 35fef52 | 2008-02-20 20:55:12 +0000 | [diff] [blame] | 1130 | // FIXME: Handle ASQualType |
| 1131 | if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) != |
| 1132 | rhptee.getCVRQualifiers()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1133 | ConvTy = CompatiblePointerDiscardsQualifiers; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1134 | |
| 1135 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 1136 | // incomplete type and the other is a pointer to a qualified or unqualified |
| 1137 | // version of void... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1138 | if (lhptee->isVoidType()) { |
| 1139 | if (rhptee->isObjectType() || rhptee->isIncompleteType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1140 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1141 | |
| 1142 | // As an extension, we allow cast to/from void* to function pointer. |
| 1143 | if (rhptee->isFunctionType()) |
| 1144 | return FunctionVoidPointer; |
| 1145 | } |
| 1146 | |
| 1147 | if (rhptee->isVoidType()) { |
| 1148 | if (lhptee->isObjectType() || lhptee->isIncompleteType()) |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1149 | return ConvTy; |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1150 | |
| 1151 | // As an extension, we allow cast to/from void* to function pointer. |
| 1152 | if (lhptee->isFunctionType()) |
| 1153 | return FunctionVoidPointer; |
| 1154 | } |
| 1155 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1156 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| 1157 | // unqualified versions of compatible types, ... |
Chris Lattner | 4ca3d77 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 1158 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 1159 | rhptee.getUnqualifiedType())) |
| 1160 | return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1161 | return ConvTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 1165 | /// has code to accommodate several GCC extensions when type checking |
| 1166 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 1167 | /// |
| 1168 | /// int a, *pint; |
| 1169 | /// short *pshort; |
| 1170 | /// struct foo *pfoo; |
| 1171 | /// |
| 1172 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 1173 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 1174 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 1175 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 1176 | /// |
| 1177 | /// As a result, the code for dealing with pointers is more complex than the |
| 1178 | /// C99 spec dictates. |
| 1179 | /// Note: the warning above turn into errors when -pedantic-errors is enabled. |
| 1180 | /// |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1181 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1182 | Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) { |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1183 | // Get canonical types. We're not formatting these types, just comparing |
| 1184 | // them. |
| 1185 | lhsType = lhsType.getCanonicalType(); |
| 1186 | rhsType = rhsType.getCanonicalType(); |
| 1187 | |
| 1188 | if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType()) |
Chris Lattner | fdd96d7 | 2008-01-07 17:51:46 +0000 | [diff] [blame] | 1189 | return Compatible; // Common case: fast path an exact match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1190 | |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1191 | if (lhsType->isReferenceType() || rhsType->isReferenceType()) { |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1192 | if (Context.referenceTypesAreCompatible(lhsType, rhsType)) |
Anders Carlsson | cebb8d6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 1193 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1194 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1195 | } |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1196 | |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1197 | if (lhsType->isObjCQualifiedIdType() |
| 1198 | || rhsType->isObjCQualifiedIdType()) { |
| 1199 | if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType)) |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1200 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1201 | return Incompatible; |
Fariborz Jahanian | 957442d | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 1202 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
| 1205 | // For OCUVector, allow vector splats; float -> <n x float> |
| 1206 | if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) { |
| 1207 | if (LV->getElementType().getTypePtr() == rhsType.getTypePtr()) |
| 1208 | return Compatible; |
| 1209 | } |
| 1210 | |
| 1211 | // If LHS and RHS are both vectors of integer or both vectors of floating |
| 1212 | // point types, and the total vector length is the same, allow the |
| 1213 | // conversion. This is a bitcast; no bits are changed but the result type |
| 1214 | // is different. |
| 1215 | if (getLangOptions().LaxVectorConversions && |
| 1216 | lhsType->isVectorType() && rhsType->isVectorType()) { |
| 1217 | if ((lhsType->isIntegerType() && rhsType->isIntegerType()) || |
| 1218 | (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) { |
Chris Lattner | 8cd0e93 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1219 | if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1220 | return Compatible; |
| 1221 | } |
Chris Lattner | db22bf4 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 1222 | } |
| 1223 | return Incompatible; |
| 1224 | } |
| 1225 | |
| 1226 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1227 | return Compatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1228 | |
| 1229 | if (lhsType->isPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1230 | if (rhsType->isIntegerType()) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1231 | return IntToPointer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1232 | |
| 1233 | if (rhsType->isPointerType()) |
| 1234 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1235 | return Incompatible; |
| 1236 | } |
| 1237 | |
| 1238 | if (rhsType->isPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1239 | // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer. |
| 1240 | if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy)) |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 1241 | return PointerToInt; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1242 | |
| 1243 | if (lhsType->isPointerType()) |
| 1244 | return CheckPointerTypesForAssignment(lhsType, rhsType); |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1245 | return Incompatible; |
Chris Lattner | 1853da2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
Steve Naroff | 85f0dc5 | 2007-10-15 20:41:53 +0000 | [diff] [blame] | 1249 | if (Context.tagTypesAreCompatible(lhsType, rhsType)) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1250 | return Compatible; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1251 | } |
| 1252 | return Incompatible; |
| 1253 | } |
| 1254 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1255 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1256 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) { |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1257 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 1258 | // a null pointer constant. |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1259 | if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType()) |
Fariborz Jahanian | a13effb | 2008-01-03 18:46:52 +0000 | [diff] [blame] | 1260 | && rExpr->isNullPointerConstant(Context)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1261 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | cdee22d | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 1262 | return Compatible; |
| 1263 | } |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1264 | // This check seems unnatural, however it is necessary to ensure the proper |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1265 | // conversion of functions/arrays. If the conversion were done for all |
Steve Naroff | 0acc9c9 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 1266 | // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1267 | // expressions that surpress this implicit conversion (&, sizeof). |
Chris Lattner | 5f505bf | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 1268 | // |
| 1269 | // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references |
| 1270 | // are better understood. |
| 1271 | if (!lhsType->isReferenceType()) |
| 1272 | DefaultFunctionArrayConversion(rExpr); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1273 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1274 | Sema::AssignConvertType result = |
| 1275 | CheckAssignmentConstraints(lhsType, rExpr->getType()); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1276 | |
| 1277 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 1278 | // type of the assignment expression. |
| 1279 | if (rExpr->getType() != lhsType) |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1280 | ImpCastExprToType(rExpr, lhsType); |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1281 | return result; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1284 | Sema::AssignConvertType |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1285 | Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) { |
| 1286 | return CheckAssignmentConstraints(lhsType, rhsType); |
| 1287 | } |
| 1288 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1289 | QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1290 | Diag(loc, diag::err_typecheck_invalid_operands, |
| 1291 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1292 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1293 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1294 | } |
| 1295 | |
| 1296 | inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex, |
| 1297 | Expr *&rex) { |
| 1298 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1299 | |
| 1300 | // make sure the vector types are identical. |
Nate Begeman | 78a2a31 | 2008-03-17 17:22:18 +0000 | [diff] [blame] | 1301 | if (lhsType.getCanonicalType() == rhsType.getCanonicalType()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1302 | return lhsType; |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1303 | |
| 1304 | // if the lhs is an ocu vector and the rhs is a scalar of the same type, |
| 1305 | // promote the rhs to the vector type. |
| 1306 | if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) { |
| 1307 | if (V->getElementType().getCanonicalType().getTypePtr() |
| 1308 | == rhsType.getCanonicalType().getTypePtr()) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1309 | ImpCastExprToType(rex, lhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1310 | return lhsType; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | // if the rhs is an ocu vector and the lhs is a scalar of the same type, |
| 1315 | // promote the lhs to the vector type. |
| 1316 | if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) { |
| 1317 | if (V->getElementType().getCanonicalType().getTypePtr() |
| 1318 | == lhsType.getCanonicalType().getTypePtr()) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1319 | ImpCastExprToType(lex, rhsType); |
Nate Begeman | ec2d106 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 1320 | return rhsType; |
| 1321 | } |
| 1322 | } |
| 1323 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1324 | // You cannot convert between vector values of different size. |
| 1325 | Diag(loc, diag::err_typecheck_vector_not_convertable, |
| 1326 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1327 | lex->getSourceRange(), rex->getSourceRange()); |
| 1328 | return QualType(); |
| 1329 | } |
| 1330 | |
| 1331 | inline QualType Sema::CheckMultiplyDivideOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1332 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1333 | { |
| 1334 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1335 | |
| 1336 | if (lhsType->isVectorType() || rhsType->isVectorType()) |
| 1337 | return CheckVectorOperands(loc, lex, rex); |
| 1338 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1339 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1340 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1341 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1342 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1343 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | inline QualType Sema::CheckRemainderOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1347 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1348 | { |
| 1349 | QualType lhsType = lex->getType(), rhsType = rex->getType(); |
| 1350 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1351 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1352 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1353 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1354 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1355 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | inline QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1359 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1360 | { |
| 1361 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1362 | return CheckVectorOperands(loc, lex, rex); |
| 1363 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1364 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1365 | |
| 1366 | // handle the common case first (both operands are arithmetic). |
| 1367 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1368 | return compType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1369 | |
| 1370 | if (lex->getType()->isPointerType() && rex->getType()->isIntegerType()) |
| 1371 | return lex->getType(); |
| 1372 | if (lex->getType()->isIntegerType() && rex->getType()->isPointerType()) |
| 1373 | return rex->getType(); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1374 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6 |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1378 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1379 | { |
| 1380 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1381 | return CheckVectorOperands(loc, lex, rex); |
| 1382 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1383 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1385 | // Enforce type constraints: C99 6.5.6p3. |
| 1386 | |
| 1387 | // Handle the common case first (both operands are arithmetic). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1388 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1389 | return compType; |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1390 | |
| 1391 | // Either ptr - int or ptr - ptr. |
| 1392 | if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) { |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1393 | QualType lpointee = LHSPTy->getPointeeType(); |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1394 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1395 | // The LHS must be an object type, not incomplete, function, etc. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1396 | if (!lpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1397 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1398 | if (lpointee->isVoidType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1399 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1400 | lex->getSourceRange(), rex->getSourceRange()); |
| 1401 | } else { |
| 1402 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1403 | lex->getType().getAsString(), lex->getSourceRange()); |
| 1404 | return QualType(); |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | // The result type of a pointer-int computation is the pointer type. |
| 1409 | if (rex->getType()->isIntegerType()) |
| 1410 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1411 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1412 | // Handle pointer-pointer subtractions. |
| 1413 | if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) { |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1414 | QualType rpointee = RHSPTy->getPointeeType(); |
| 1415 | |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1416 | // RHS must be an object type, unless void (GNU). |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1417 | if (!rpointee->isObjectType()) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1418 | // Handle the GNU void* extension. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1419 | if (rpointee->isVoidType()) { |
| 1420 | if (!lpointee->isVoidType()) |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1421 | Diag(loc, diag::ext_gnu_void_ptr, |
| 1422 | lex->getSourceRange(), rex->getSourceRange()); |
| 1423 | } else { |
| 1424 | Diag(loc, diag::err_typecheck_sub_ptr_object, |
| 1425 | rex->getType().getAsString(), rex->getSourceRange()); |
| 1426 | return QualType(); |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | // Pointee types must be compatible. |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1431 | if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(), |
| 1432 | rpointee.getUnqualifiedType())) { |
Chris Lattner | f6da291 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 1433 | Diag(loc, diag::err_typecheck_sub_ptr_compatible, |
| 1434 | lex->getType().getAsString(), rex->getType().getAsString(), |
| 1435 | lex->getSourceRange(), rex->getSourceRange()); |
| 1436 | return QualType(); |
| 1437 | } |
| 1438 | |
| 1439 | return Context.getPointerDiffType(); |
| 1440 | } |
| 1441 | } |
| 1442 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1443 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | inline QualType Sema::CheckShiftOperands( // C99 6.5.7 |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1447 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) { |
| 1448 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| 1449 | if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType()) |
| 1450 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1451 | |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1452 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 1453 | // promotions on each operand. C99 6.5.7p3 |
Chris Lattner | bb19bc4 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 1454 | if (!isCompAssign) |
| 1455 | UsualUnaryConversions(lex); |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1456 | UsualUnaryConversions(rex); |
| 1457 | |
| 1458 | // "The type of the result is that of the promoted left operand." |
| 1459 | return lex->getType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1462 | inline QualType Sema::CheckCompareOperands( // C99 6.5.8 |
| 1463 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1464 | { |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1465 | // C99 6.5.8p3 / C99 6.5.9p4 |
Steve Naroff | ecc4fa1 | 2007-08-10 18:26:40 +0000 | [diff] [blame] | 1466 | if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType()) |
| 1467 | UsualArithmeticConversions(lex, rex); |
| 1468 | else { |
| 1469 | UsualUnaryConversions(lex); |
| 1470 | UsualUnaryConversions(rex); |
| 1471 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1472 | QualType lType = lex->getType(); |
| 1473 | QualType rType = rex->getType(); |
| 1474 | |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1475 | // For non-floating point types, check for self-comparisons of the form |
| 1476 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 1477 | // often indicate logic errors in the program. |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 1478 | if (!lType->isFloatingType()) { |
Ted Kremenek | 87e30c5 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1479 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens())) |
| 1480 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens())) |
Ted Kremenek | cf8b77d | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 1481 | if (DRL->getDecl() == DRR->getDecl()) |
| 1482 | Diag(loc, diag::warn_selfcomparison); |
| 1483 | } |
| 1484 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1485 | if (isRelational) { |
| 1486 | if (lType->isRealType() && rType->isRealType()) |
| 1487 | return Context.IntTy; |
| 1488 | } else { |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1489 | // Check for comparisons of floating point operands using != and ==. |
Ted Kremenek | 486509e | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 1490 | if (lType->isFloatingType()) { |
| 1491 | assert (rType->isFloatingType()); |
Ted Kremenek | 30c6675 | 2007-11-25 00:58:00 +0000 | [diff] [blame] | 1492 | CheckFloatComparison(loc,lex,rex); |
Ted Kremenek | 7543914 | 2007-10-29 16:40:01 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1495 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| 1496 | return Context.IntTy; |
| 1497 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1498 | |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1499 | bool LHSIsNull = lex->isNullPointerConstant(Context); |
| 1500 | bool RHSIsNull = rex->isNullPointerConstant(Context); |
| 1501 | |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1502 | // All of the following pointer related warnings are GCC extensions, except |
| 1503 | // when handling null pointer constants. One day, we can consider making them |
| 1504 | // errors (when -pedantic-errors is enabled). |
Steve Naroff | c33c060 | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 1505 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1506 | QualType lpointee = lType->getAsPointerType()->getPointeeType(); |
| 1507 | QualType rpointee = rType->getAsPointerType()->getPointeeType(); |
| 1508 | |
Steve Naroff | 3b43562 | 2007-11-13 14:57:38 +0000 | [diff] [blame] | 1509 | if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2 |
Steve Naroff | 577f972 | 2008-01-29 18:58:14 +0000 | [diff] [blame] | 1510 | !lpointee->isVoidType() && !lpointee->isVoidType() && |
| 1511 | !Context.typesAreCompatible(lpointee.getUnqualifiedType(), |
Eli Friedman | 5072704 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 1512 | rpointee.getUnqualifiedType())) { |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1513 | Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers, |
| 1514 | lType.getAsString(), rType.getAsString(), |
| 1515 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1516 | } |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1517 | ImpCastExprToType(rex, lType); // promote the pointer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1518 | return Context.IntTy; |
| 1519 | } |
Ted Kremenek | 42730c5 | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1520 | if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType()) |
| 1521 | && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) { |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1522 | ImpCastExprToType(rex, lType); |
Fariborz Jahanian | 5319d9c | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 1523 | return Context.IntTy; |
| 1524 | } |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1525 | if (lType->isPointerType() && rType->isIntegerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1526 | if (!RHSIsNull) |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1527 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1528 | lType.getAsString(), rType.getAsString(), |
| 1529 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1530 | ImpCastExprToType(rex, lType); // promote the integer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1531 | return Context.IntTy; |
| 1532 | } |
| 1533 | if (lType->isIntegerType() && rType->isPointerType()) { |
Chris Lattner | 22be842 | 2007-08-26 01:10:14 +0000 | [diff] [blame] | 1534 | if (!LHSIsNull) |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1535 | Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer, |
| 1536 | lType.getAsString(), rType.getAsString(), |
| 1537 | lex->getSourceRange(), rex->getSourceRange()); |
Chris Lattner | e992d6c | 2008-01-16 19:17:22 +0000 | [diff] [blame] | 1538 | ImpCastExprToType(lex, rType); // promote the integer to pointer |
Steve Naroff | 4462cb0 | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 1539 | return Context.IntTy; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1540 | } |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1541 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1544 | inline QualType Sema::CheckBitwiseOperands( |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1545 | Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1546 | { |
| 1547 | if (lex->getType()->isVectorType() || rex->getType()->isVectorType()) |
| 1548 | return CheckVectorOperands(loc, lex, rex); |
| 1549 | |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1550 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1551 | |
| 1552 | if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType()) |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1553 | return compType; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1554 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
| 1558 | Expr *&lex, Expr *&rex, SourceLocation loc) |
| 1559 | { |
| 1560 | UsualUnaryConversions(lex); |
| 1561 | UsualUnaryConversions(rex); |
| 1562 | |
| 1563 | if (lex->getType()->isScalarType() || rex->getType()->isScalarType()) |
| 1564 | return Context.IntTy; |
Chris Lattner | 2c8bff7 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 1565 | return InvalidOperands(loc, lex, rex); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1 |
Steve Naroff | 0f32f43 | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 1569 | Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1570 | { |
| 1571 | QualType lhsType = lex->getType(); |
| 1572 | QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1573 | Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue(); |
| 1574 | |
| 1575 | switch (mlval) { // C99 6.5.16p2 |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1576 | case Expr::MLV_Valid: |
| 1577 | break; |
| 1578 | case Expr::MLV_ConstQualified: |
| 1579 | Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange()); |
| 1580 | return QualType(); |
| 1581 | case Expr::MLV_ArrayType: |
| 1582 | Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue, |
| 1583 | lhsType.getAsString(), lex->getSourceRange()); |
| 1584 | return QualType(); |
| 1585 | case Expr::MLV_NotObjectType: |
| 1586 | Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue, |
| 1587 | lhsType.getAsString(), lex->getSourceRange()); |
| 1588 | return QualType(); |
| 1589 | case Expr::MLV_InvalidExpression: |
| 1590 | Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue, |
| 1591 | lex->getSourceRange()); |
| 1592 | return QualType(); |
| 1593 | case Expr::MLV_IncompleteType: |
| 1594 | case Expr::MLV_IncompleteVoidType: |
| 1595 | Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue, |
| 1596 | lhsType.getAsString(), lex->getSourceRange()); |
| 1597 | return QualType(); |
| 1598 | case Expr::MLV_DuplicateVectorComponents: |
| 1599 | Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue, |
| 1600 | lex->getSourceRange()); |
| 1601 | return QualType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1602 | } |
Steve Naroff | 7cbb146 | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 1603 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1604 | AssignConvertType ConvTy; |
| 1605 | if (compoundType.isNull()) |
| 1606 | ConvTy = CheckSingleAssignmentConstraints(lhsType, rex); |
| 1607 | else |
| 1608 | ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType); |
| 1609 | |
| 1610 | if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType, |
| 1611 | rex, "assigning")) |
| 1612 | return QualType(); |
| 1613 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1614 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 1615 | // left operand unless the left operand has qualified type, in which case |
| 1616 | // it is the unqualified version of the type of the left operand. |
| 1617 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 1618 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 1619 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| 1620 | // oprdu. |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 1621 | return lhsType.getUnqualifiedType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | inline QualType Sema::CheckCommaOperands( // C99 6.5.17 |
| 1625 | Expr *&lex, Expr *&rex, SourceLocation loc) { |
| 1626 | UsualUnaryConversions(rex); |
| 1627 | return rex->getType(); |
| 1628 | } |
| 1629 | |
| 1630 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 1631 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
| 1632 | QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) { |
| 1633 | QualType resType = op->getType(); |
| 1634 | assert(!resType.isNull() && "no type for increment/decrement expression"); |
| 1635 | |
Steve Naroff | d30e193 | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1636 | // C99 6.5.2.4p1: We allow complex as a GCC extension. |
Steve Naroff | ce82758 | 2007-11-11 14:15:57 +0000 | [diff] [blame] | 1637 | if (const PointerType *pt = resType->getAsPointerType()) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1638 | if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2 |
| 1639 | Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type, |
| 1640 | resType.getAsString(), op->getSourceRange()); |
| 1641 | return QualType(); |
| 1642 | } |
Steve Naroff | d30e193 | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1643 | } else if (!resType->isRealType()) { |
| 1644 | if (resType->isComplexType()) |
| 1645 | // C99 does not support ++/-- on complex types. |
| 1646 | Diag(OpLoc, diag::ext_integer_increment_complex, |
| 1647 | resType.getAsString(), op->getSourceRange()); |
| 1648 | else { |
| 1649 | Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement, |
| 1650 | resType.getAsString(), op->getSourceRange()); |
| 1651 | return QualType(); |
| 1652 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1653 | } |
Steve Naroff | 6acc0f4 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 1654 | // At this point, we know we have a real, complex or pointer type. |
| 1655 | // Now make sure the operand is a modifiable lvalue. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1656 | Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue(); |
| 1657 | if (mlval != Expr::MLV_Valid) { |
| 1658 | // FIXME: emit a more precise diagnostic... |
| 1659 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr, |
| 1660 | op->getSourceRange()); |
| 1661 | return QualType(); |
| 1662 | } |
| 1663 | return resType; |
| 1664 | } |
| 1665 | |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1666 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1667 | /// This routine allows us to typecheck complex/recursive expressions |
| 1668 | /// where the declaration is needed for type checking. Here are some |
| 1669 | /// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2]. |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1670 | static ValueDecl *getPrimaryDecl(Expr *e) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1671 | switch (e->getStmtClass()) { |
| 1672 | case Stmt::DeclRefExprClass: |
| 1673 | return cast<DeclRefExpr>(e)->getDecl(); |
| 1674 | case Stmt::MemberExprClass: |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1675 | // Fields cannot be declared with a 'register' storage class. |
| 1676 | // &X->f is always ok, even if X is declared register. |
| 1677 | if (cast<MemberExpr>(e)->isArrow()) |
| 1678 | return 0; |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1679 | return getPrimaryDecl(cast<MemberExpr>(e)->getBase()); |
| 1680 | case Stmt::ArraySubscriptExprClass: { |
| 1681 | // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer. |
| 1682 | |
| 1683 | ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(e)->getBase()); |
Anders Carlsson | 655694e | 2008-02-01 16:01:31 +0000 | [diff] [blame] | 1684 | if (!VD || VD->getType()->isPointerType()) |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1685 | return 0; |
| 1686 | else |
| 1687 | return VD; |
| 1688 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1689 | case Stmt::UnaryOperatorClass: |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1690 | return getPrimaryDecl(cast<UnaryOperator>(e)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1691 | case Stmt::ParenExprClass: |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1692 | return getPrimaryDecl(cast<ParenExpr>(e)->getSubExpr()); |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1693 | case Stmt::ImplicitCastExprClass: |
| 1694 | // &X[4] when X is an array, has an implicit cast from array to pointer. |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1695 | return getPrimaryDecl(cast<ImplicitCastExpr>(e)->getSubExpr()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1696 | default: |
| 1697 | return 0; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | /// CheckAddressOfOperand - The operand of & must be either a function |
| 1702 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| 1703 | /// object cannot be declared with storage class register or be a bit field. |
| 1704 | /// Note: The usual conversions are *not* applied to the operand of the & |
| 1705 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| 1706 | QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) { |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 1707 | if (getLangOptions().C99) { |
| 1708 | // Implement C99-only parts of addressof rules. |
| 1709 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| 1710 | if (uOp->getOpcode() == UnaryOperator::Deref) |
| 1711 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 1712 | // (assuming the deref expression is valid). |
| 1713 | return uOp->getSubExpr()->getType(); |
| 1714 | } |
| 1715 | // Technically, there should be a check for array subscript |
| 1716 | // expressions here, but the result of one is always an lvalue anyway. |
| 1717 | } |
Anders Carlsson | 4b3db2b | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 1718 | ValueDecl *dcl = getPrimaryDecl(op); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1719 | Expr::isLvalueResult lval = op->isLvalue(); |
| 1720 | |
| 1721 | if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1 |
Chris Lattner | a324907 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 1722 | if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators |
| 1723 | // FIXME: emit more specific diag... |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1724 | Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof, |
| 1725 | op->getSourceRange()); |
| 1726 | return QualType(); |
| 1727 | } |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 1728 | } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1 |
| 1729 | if (MemExpr->getMemberDecl()->isBitField()) { |
| 1730 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 1731 | std::string("bit-field"), op->getSourceRange()); |
| 1732 | return QualType(); |
| 1733 | } |
| 1734 | // Check for Apple extension for accessing vector components. |
| 1735 | } else if (isa<ArraySubscriptExpr>(op) && |
| 1736 | cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) { |
| 1737 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 1738 | std::string("vector"), op->getSourceRange()); |
| 1739 | return QualType(); |
| 1740 | } else if (dcl) { // C99 6.5.3.2p1 |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1741 | // We have an lvalue with a decl. Make sure the decl is not declared |
| 1742 | // with the register storage-class specifier. |
| 1743 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| 1744 | if (vd->getStorageClass() == VarDecl::Register) { |
Steve Naroff | 73cf87e | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 1745 | Diag(OpLoc, diag::err_typecheck_address_of, |
| 1746 | std::string("register variable"), op->getSourceRange()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1747 | return QualType(); |
| 1748 | } |
| 1749 | } else |
| 1750 | assert(0 && "Unknown/unexpected decl type"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1751 | } |
| 1752 | // If the operand has type "type", the result has type "pointer to type". |
| 1753 | return Context.getPointerType(op->getType()); |
| 1754 | } |
| 1755 | |
| 1756 | QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) { |
| 1757 | UsualUnaryConversions(op); |
| 1758 | QualType qType = op->getType(); |
| 1759 | |
Chris Lattner | 7931f4a | 2007-07-31 16:53:04 +0000 | [diff] [blame] | 1760 | if (const PointerType *PT = qType->getAsPointerType()) { |
Steve Naroff | 9c6c359 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 1761 | // Note that per both C89 and C99, this is always legal, even |
| 1762 | // if ptype is an incomplete type or void. |
| 1763 | // It would be possible to warn about dereferencing a |
| 1764 | // void pointer, but it's completely well-defined, |
| 1765 | // and such a warning is unlikely to catch any mistakes. |
| 1766 | return PT->getPointeeType(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1767 | } |
| 1768 | Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer, |
| 1769 | qType.getAsString(), op->getSourceRange()); |
| 1770 | return QualType(); |
| 1771 | } |
| 1772 | |
| 1773 | static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode( |
| 1774 | tok::TokenKind Kind) { |
| 1775 | BinaryOperator::Opcode Opc; |
| 1776 | switch (Kind) { |
| 1777 | default: assert(0 && "Unknown binop!"); |
| 1778 | case tok::star: Opc = BinaryOperator::Mul; break; |
| 1779 | case tok::slash: Opc = BinaryOperator::Div; break; |
| 1780 | case tok::percent: Opc = BinaryOperator::Rem; break; |
| 1781 | case tok::plus: Opc = BinaryOperator::Add; break; |
| 1782 | case tok::minus: Opc = BinaryOperator::Sub; break; |
| 1783 | case tok::lessless: Opc = BinaryOperator::Shl; break; |
| 1784 | case tok::greatergreater: Opc = BinaryOperator::Shr; break; |
| 1785 | case tok::lessequal: Opc = BinaryOperator::LE; break; |
| 1786 | case tok::less: Opc = BinaryOperator::LT; break; |
| 1787 | case tok::greaterequal: Opc = BinaryOperator::GE; break; |
| 1788 | case tok::greater: Opc = BinaryOperator::GT; break; |
| 1789 | case tok::exclaimequal: Opc = BinaryOperator::NE; break; |
| 1790 | case tok::equalequal: Opc = BinaryOperator::EQ; break; |
| 1791 | case tok::amp: Opc = BinaryOperator::And; break; |
| 1792 | case tok::caret: Opc = BinaryOperator::Xor; break; |
| 1793 | case tok::pipe: Opc = BinaryOperator::Or; break; |
| 1794 | case tok::ampamp: Opc = BinaryOperator::LAnd; break; |
| 1795 | case tok::pipepipe: Opc = BinaryOperator::LOr; break; |
| 1796 | case tok::equal: Opc = BinaryOperator::Assign; break; |
| 1797 | case tok::starequal: Opc = BinaryOperator::MulAssign; break; |
| 1798 | case tok::slashequal: Opc = BinaryOperator::DivAssign; break; |
| 1799 | case tok::percentequal: Opc = BinaryOperator::RemAssign; break; |
| 1800 | case tok::plusequal: Opc = BinaryOperator::AddAssign; break; |
| 1801 | case tok::minusequal: Opc = BinaryOperator::SubAssign; break; |
| 1802 | case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break; |
| 1803 | case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break; |
| 1804 | case tok::ampequal: Opc = BinaryOperator::AndAssign; break; |
| 1805 | case tok::caretequal: Opc = BinaryOperator::XorAssign; break; |
| 1806 | case tok::pipeequal: Opc = BinaryOperator::OrAssign; break; |
| 1807 | case tok::comma: Opc = BinaryOperator::Comma; break; |
| 1808 | } |
| 1809 | return Opc; |
| 1810 | } |
| 1811 | |
| 1812 | static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode( |
| 1813 | tok::TokenKind Kind) { |
| 1814 | UnaryOperator::Opcode Opc; |
| 1815 | switch (Kind) { |
| 1816 | default: assert(0 && "Unknown unary op!"); |
| 1817 | case tok::plusplus: Opc = UnaryOperator::PreInc; break; |
| 1818 | case tok::minusminus: Opc = UnaryOperator::PreDec; break; |
| 1819 | case tok::amp: Opc = UnaryOperator::AddrOf; break; |
| 1820 | case tok::star: Opc = UnaryOperator::Deref; break; |
| 1821 | case tok::plus: Opc = UnaryOperator::Plus; break; |
| 1822 | case tok::minus: Opc = UnaryOperator::Minus; break; |
| 1823 | case tok::tilde: Opc = UnaryOperator::Not; break; |
| 1824 | case tok::exclaim: Opc = UnaryOperator::LNot; break; |
| 1825 | case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break; |
| 1826 | case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break; |
| 1827 | case tok::kw___real: Opc = UnaryOperator::Real; break; |
| 1828 | case tok::kw___imag: Opc = UnaryOperator::Imag; break; |
| 1829 | case tok::kw___extension__: Opc = UnaryOperator::Extension; break; |
| 1830 | } |
| 1831 | return Opc; |
| 1832 | } |
| 1833 | |
| 1834 | // Binary Operators. 'Tok' is the token for the operator. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1835 | Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1836 | ExprTy *LHS, ExprTy *RHS) { |
| 1837 | BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| 1838 | Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS; |
| 1839 | |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1840 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 1841 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1842 | |
| 1843 | QualType ResultTy; // Result type of the binary operator. |
| 1844 | QualType CompTy; // Computation type for compound assignments (e.g. '+=') |
| 1845 | |
| 1846 | switch (Opc) { |
| 1847 | default: |
| 1848 | assert(0 && "Unknown binary expr!"); |
| 1849 | case BinaryOperator::Assign: |
| 1850 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType()); |
| 1851 | break; |
| 1852 | case BinaryOperator::Mul: |
| 1853 | case BinaryOperator::Div: |
| 1854 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc); |
| 1855 | break; |
| 1856 | case BinaryOperator::Rem: |
| 1857 | ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc); |
| 1858 | break; |
| 1859 | case BinaryOperator::Add: |
| 1860 | ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc); |
| 1861 | break; |
| 1862 | case BinaryOperator::Sub: |
| 1863 | ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc); |
| 1864 | break; |
| 1865 | case BinaryOperator::Shl: |
| 1866 | case BinaryOperator::Shr: |
| 1867 | ResultTy = CheckShiftOperands(lhs, rhs, TokLoc); |
| 1868 | break; |
| 1869 | case BinaryOperator::LE: |
| 1870 | case BinaryOperator::LT: |
| 1871 | case BinaryOperator::GE: |
| 1872 | case BinaryOperator::GT: |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1873 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1874 | break; |
| 1875 | case BinaryOperator::EQ: |
| 1876 | case BinaryOperator::NE: |
Chris Lattner | 254f3bc | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 1877 | ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1878 | break; |
| 1879 | case BinaryOperator::And: |
| 1880 | case BinaryOperator::Xor: |
| 1881 | case BinaryOperator::Or: |
| 1882 | ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc); |
| 1883 | break; |
| 1884 | case BinaryOperator::LAnd: |
| 1885 | case BinaryOperator::LOr: |
| 1886 | ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc); |
| 1887 | break; |
| 1888 | case BinaryOperator::MulAssign: |
| 1889 | case BinaryOperator::DivAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1890 | CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1891 | if (!CompTy.isNull()) |
| 1892 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1893 | break; |
| 1894 | case BinaryOperator::RemAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1895 | CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1896 | if (!CompTy.isNull()) |
| 1897 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1898 | break; |
| 1899 | case BinaryOperator::AddAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1900 | CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1901 | if (!CompTy.isNull()) |
| 1902 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1903 | break; |
| 1904 | case BinaryOperator::SubAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1905 | CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1906 | if (!CompTy.isNull()) |
| 1907 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1908 | break; |
| 1909 | case BinaryOperator::ShlAssign: |
| 1910 | case BinaryOperator::ShrAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1911 | CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1912 | if (!CompTy.isNull()) |
| 1913 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1914 | break; |
| 1915 | case BinaryOperator::AndAssign: |
| 1916 | case BinaryOperator::XorAssign: |
| 1917 | case BinaryOperator::OrAssign: |
Steve Naroff | 8f70836 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 1918 | CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1919 | if (!CompTy.isNull()) |
| 1920 | ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy); |
| 1921 | break; |
| 1922 | case BinaryOperator::Comma: |
| 1923 | ResultTy = CheckCommaOperands(lhs, rhs, TokLoc); |
| 1924 | break; |
| 1925 | } |
| 1926 | if (ResultTy.isNull()) |
| 1927 | return true; |
| 1928 | if (CompTy.isNull()) |
Chris Lattner | f420df1 | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 1929 | return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1930 | else |
Chris Lattner | f420df1 | 2007-08-28 18:36:55 +0000 | [diff] [blame] | 1931 | return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
| 1934 | // Unary Operators. 'Tok' is the token for the operator. |
Steve Naroff | 87d58b4 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1935 | Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1936 | ExprTy *input) { |
| 1937 | Expr *Input = (Expr*)input; |
| 1938 | UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op); |
| 1939 | QualType resultType; |
| 1940 | switch (Opc) { |
| 1941 | default: |
| 1942 | assert(0 && "Unimplemented unary expr!"); |
| 1943 | case UnaryOperator::PreInc: |
| 1944 | case UnaryOperator::PreDec: |
| 1945 | resultType = CheckIncrementDecrementOperand(Input, OpLoc); |
| 1946 | break; |
| 1947 | case UnaryOperator::AddrOf: |
| 1948 | resultType = CheckAddressOfOperand(Input, OpLoc); |
| 1949 | break; |
| 1950 | case UnaryOperator::Deref: |
Steve Naroff | ccc26a7 | 2007-12-18 04:06:57 +0000 | [diff] [blame] | 1951 | DefaultFunctionArrayConversion(Input); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1952 | resultType = CheckIndirectionOperand(Input, OpLoc); |
| 1953 | break; |
| 1954 | case UnaryOperator::Plus: |
| 1955 | case UnaryOperator::Minus: |
| 1956 | UsualUnaryConversions(Input); |
| 1957 | resultType = Input->getType(); |
| 1958 | if (!resultType->isArithmeticType()) // C99 6.5.3.3p1 |
| 1959 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1960 | resultType.getAsString()); |
| 1961 | break; |
| 1962 | case UnaryOperator::Not: // bitwise complement |
| 1963 | UsualUnaryConversions(Input); |
| 1964 | resultType = Input->getType(); |
Steve Naroff | d30e193 | 2007-08-24 17:20:07 +0000 | [diff] [blame] | 1965 | // C99 6.5.3.3p1. We allow complex as a GCC extension. |
| 1966 | if (!resultType->isIntegerType()) { |
| 1967 | if (resultType->isComplexType()) |
| 1968 | // C99 does not support '~' for complex conjugation. |
| 1969 | Diag(OpLoc, diag::ext_integer_complement_complex, |
| 1970 | resultType.getAsString()); |
| 1971 | else |
| 1972 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1973 | resultType.getAsString()); |
| 1974 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1975 | break; |
| 1976 | case UnaryOperator::LNot: // logical negation |
| 1977 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| 1978 | DefaultFunctionArrayConversion(Input); |
| 1979 | resultType = Input->getType(); |
| 1980 | if (!resultType->isScalarType()) // C99 6.5.3.3p1 |
| 1981 | return Diag(OpLoc, diag::err_typecheck_unary_expr, |
| 1982 | resultType.getAsString()); |
| 1983 | // LNot always has type int. C99 6.5.3.3p5. |
| 1984 | resultType = Context.IntTy; |
| 1985 | break; |
| 1986 | case UnaryOperator::SizeOf: |
| 1987 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true); |
| 1988 | break; |
| 1989 | case UnaryOperator::AlignOf: |
| 1990 | resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false); |
| 1991 | break; |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1992 | case UnaryOperator::Real: |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1993 | case UnaryOperator::Imag: |
Chris Lattner | 5110ad5 | 2007-08-24 21:41:10 +0000 | [diff] [blame] | 1994 | resultType = CheckRealImagOperand(Input, OpLoc); |
Chris Lattner | 03931a7 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 1995 | break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1996 | case UnaryOperator::Extension: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1997 | resultType = Input->getType(); |
| 1998 | break; |
| 1999 | } |
| 2000 | if (resultType.isNull()) |
| 2001 | return true; |
| 2002 | return new UnaryOperator(Input, Opc, resultType, OpLoc); |
| 2003 | } |
| 2004 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2005 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| 2006 | Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2007 | SourceLocation LabLoc, |
| 2008 | IdentifierInfo *LabelII) { |
| 2009 | // Look up the record for this label identifier. |
| 2010 | LabelStmt *&LabelDecl = LabelMap[LabelII]; |
| 2011 | |
| 2012 | // If we haven't seen this label yet, create a forward reference. |
| 2013 | if (LabelDecl == 0) |
| 2014 | LabelDecl = new LabelStmt(LabLoc, LabelII, 0); |
| 2015 | |
| 2016 | // 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] | 2017 | return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl, |
| 2018 | Context.getPointerType(Context.VoidTy)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2021 | Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2022 | SourceLocation RPLoc) { // "({..})" |
| 2023 | Stmt *SubStmt = static_cast<Stmt*>(substmt); |
| 2024 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 2025 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 2026 | |
| 2027 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 2028 | // example, it is not possible to goto into a stmt expression apparently. |
| 2029 | // More semantic analysis is needed. |
| 2030 | |
| 2031 | // FIXME: the last statement in the compount stmt has its value used. We |
| 2032 | // should not warn about it being unused. |
| 2033 | |
| 2034 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 2035 | // as the type of the stmtexpr. |
| 2036 | QualType Ty = Context.VoidTy; |
| 2037 | |
| 2038 | if (!Compound->body_empty()) |
| 2039 | if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back())) |
| 2040 | Ty = LastExpr->getType(); |
| 2041 | |
| 2042 | return new StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 2043 | } |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2044 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2045 | Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc, |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2046 | SourceLocation TypeLoc, |
| 2047 | TypeTy *argty, |
| 2048 | OffsetOfComponent *CompPtr, |
| 2049 | unsigned NumComponents, |
| 2050 | SourceLocation RPLoc) { |
| 2051 | QualType ArgTy = QualType::getFromOpaquePtr(argty); |
| 2052 | assert(!ArgTy.isNull() && "Missing type argument!"); |
| 2053 | |
| 2054 | // We must have at least one component that refers to the type, and the first |
| 2055 | // one is known to be a field designator. Verify that the ArgTy represents |
| 2056 | // a struct/union/class. |
| 2057 | if (!ArgTy->isRecordType()) |
| 2058 | return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString()); |
| 2059 | |
| 2060 | // Otherwise, create a compound literal expression as the base, and |
| 2061 | // iteratively process the offsetof designators. |
Steve Naroff | be37fc0 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 2062 | Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2063 | |
Chris Lattner | b37522e | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 2064 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 2065 | // GCC extension, diagnose them. |
| 2066 | if (NumComponents != 1) |
| 2067 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator, |
| 2068 | SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd)); |
| 2069 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2070 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 2071 | const OffsetOfComponent &OC = CompPtr[i]; |
| 2072 | if (OC.isBrackets) { |
| 2073 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 2074 | const ArrayType *AT = Res->getType()->getAsArrayType(); |
| 2075 | if (!AT) { |
| 2076 | delete Res; |
| 2077 | return Diag(OC.LocEnd, diag::err_offsetof_array_type, |
| 2078 | Res->getType().getAsString()); |
| 2079 | } |
| 2080 | |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2081 | // FIXME: C++: Verify that operator[] isn't overloaded. |
| 2082 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2083 | // C99 6.5.2.1p1 |
| 2084 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 2085 | if (!Idx->getType()->isIntegerType()) |
| 2086 | return Diag(Idx->getLocStart(), diag::err_typecheck_subscript, |
| 2087 | Idx->getSourceRange()); |
| 2088 | |
| 2089 | Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd); |
| 2090 | continue; |
| 2091 | } |
| 2092 | |
| 2093 | const RecordType *RC = Res->getType()->getAsRecordType(); |
| 2094 | if (!RC) { |
| 2095 | delete Res; |
| 2096 | return Diag(OC.LocEnd, diag::err_offsetof_record_type, |
| 2097 | Res->getType().getAsString()); |
| 2098 | } |
| 2099 | |
| 2100 | // Get the decl corresponding to this. |
| 2101 | RecordDecl *RD = RC->getDecl(); |
| 2102 | FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo); |
| 2103 | if (!MemberDecl) |
| 2104 | return Diag(BuiltinLoc, diag::err_typecheck_no_member, |
| 2105 | OC.U.IdentInfo->getName(), |
| 2106 | SourceRange(OC.LocStart, OC.LocEnd)); |
Chris Lattner | 2af6a80 | 2007-08-30 17:59:59 +0000 | [diff] [blame] | 2107 | |
| 2108 | // FIXME: C++: Verify that MemberDecl isn't a static field. |
| 2109 | // FIXME: Verify that MemberDecl isn't a bitfield. |
Eli Friedman | 76b4983 | 2008-02-06 22:48:16 +0000 | [diff] [blame] | 2110 | // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't |
| 2111 | // matter here. |
| 2112 | Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType()); |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
| 2115 | return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(), |
| 2116 | BuiltinLoc); |
| 2117 | } |
| 2118 | |
| 2119 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2120 | Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc, |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2121 | TypeTy *arg1, TypeTy *arg2, |
| 2122 | SourceLocation RPLoc) { |
| 2123 | QualType argT1 = QualType::getFromOpaquePtr(arg1); |
| 2124 | QualType argT2 = QualType::getFromOpaquePtr(arg2); |
| 2125 | |
| 2126 | assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)"); |
| 2127 | |
Chris Lattner | 0d9bcea | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 2128 | return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc); |
Steve Naroff | 63bad2d | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Steve Naroff | 5cbb02f | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 2131 | Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond, |
Steve Naroff | 93c5301 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 2132 | ExprTy *expr1, ExprTy *expr2, |
| 2133 | SourceLocation RPLoc) { |
| 2134 | Expr *CondExpr = static_cast<Expr*>(cond); |
| 2135 | Expr *LHSExpr = static_cast<Expr*>(expr1); |
| 2136 | Expr *RHSExpr = static_cast<Expr*>(expr2); |
| 2137 | |
| 2138 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 2139 | |
| 2140 | // The conditional expression is required to be a constant expression. |
| 2141 | llvm::APSInt condEval(32); |
| 2142 | SourceLocation ExpLoc; |
| 2143 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| 2144 | return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant, |
| 2145 | CondExpr->getSourceRange()); |
| 2146 | |
| 2147 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| 2148 | QualType resType = condEval.getZExtValue() ? LHSExpr->getType() : |
| 2149 | RHSExpr->getType(); |
| 2150 | return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc); |
| 2151 | } |
| 2152 | |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2153 | /// ExprsMatchFnType - return true if the Exprs in array Args have |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2154 | /// QualTypes that match the QualTypes of the arguments of the FnType. |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2155 | /// The number of arguments has already been validated to match the number of |
| 2156 | /// arguments in FnType. |
| 2157 | static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) { |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2158 | unsigned NumParams = FnType->getNumArgs(); |
| 2159 | for (unsigned i = 0; i != NumParams; ++i) |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2160 | if (Args[i]->getType().getCanonicalType() != |
| 2161 | FnType->getArgType(i).getCanonicalType()) |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2162 | return false; |
| 2163 | return true; |
| 2164 | } |
| 2165 | |
| 2166 | Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs, |
| 2167 | SourceLocation *CommaLocs, |
| 2168 | SourceLocation BuiltinLoc, |
| 2169 | SourceLocation RParenLoc) { |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2170 | // __builtin_overload requires at least 2 arguments |
| 2171 | if (NumArgs < 2) |
| 2172 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2173 | SourceRange(BuiltinLoc, RParenLoc)); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2174 | |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2175 | // The first argument is required to be a constant expression. It tells us |
| 2176 | // the number of arguments to pass to each of the functions to be overloaded. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2177 | Expr **Args = reinterpret_cast<Expr**>(args); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2178 | Expr *NParamsExpr = Args[0]; |
| 2179 | llvm::APSInt constEval(32); |
| 2180 | SourceLocation ExpLoc; |
| 2181 | if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc)) |
| 2182 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2183 | NParamsExpr->getSourceRange()); |
| 2184 | |
| 2185 | // Verify that the number of parameters is > 0 |
| 2186 | unsigned NumParams = constEval.getZExtValue(); |
| 2187 | if (NumParams == 0) |
| 2188 | return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant, |
| 2189 | NParamsExpr->getSourceRange()); |
| 2190 | // Verify that we have at least 1 + NumParams arguments to the builtin. |
| 2191 | if ((NumParams + 1) > NumArgs) |
| 2192 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args, |
| 2193 | SourceRange(BuiltinLoc, RParenLoc)); |
| 2194 | |
| 2195 | // Figure out the return type, by matching the args to one of the functions |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2196 | // listed after the parameters. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2197 | OverloadExpr *OE = 0; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2198 | for (unsigned i = NumParams + 1; i < NumArgs; ++i) { |
| 2199 | // UsualUnaryConversions will convert the function DeclRefExpr into a |
| 2200 | // pointer to function. |
| 2201 | Expr *Fn = UsualUnaryConversions(Args[i]); |
| 2202 | FunctionTypeProto *FnType = 0; |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2203 | if (const PointerType *PT = Fn->getType()->getAsPointerType()) { |
| 2204 | QualType PointeeType = PT->getPointeeType().getCanonicalType(); |
| 2205 | FnType = dyn_cast<FunctionTypeProto>(PointeeType); |
| 2206 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2207 | |
| 2208 | // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no |
| 2209 | // parameters, and the number of parameters must match the value passed to |
| 2210 | // the builtin. |
| 2211 | if (!FnType || (FnType->getNumArgs() != NumParams)) |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2212 | return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype, |
| 2213 | Fn->getSourceRange()); |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2214 | |
| 2215 | // Scan the parameter list for the FunctionType, checking the QualType of |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2216 | // each parameter against the QualTypes of the arguments to the builtin. |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2217 | // If they match, return a new OverloadExpr. |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2218 | if (ExprsMatchFnType(Args+1, FnType)) { |
| 2219 | if (OE) |
| 2220 | return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match, |
| 2221 | OE->getFn()->getSourceRange()); |
| 2222 | // Remember our match, and continue processing the remaining arguments |
| 2223 | // to catch any errors. |
| 2224 | OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(), |
| 2225 | BuiltinLoc, RParenLoc); |
| 2226 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2227 | } |
Nate Begeman | c6078c9 | 2008-01-31 05:38:29 +0000 | [diff] [blame] | 2228 | // Return the newly created OverloadExpr node, if we succeded in matching |
| 2229 | // exactly one of the candidate functions. |
| 2230 | if (OE) |
| 2231 | return OE; |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2232 | |
| 2233 | // If we didn't find a matching function Expr in the __builtin_overload list |
| 2234 | // the return an error. |
| 2235 | std::string typeNames; |
Nate Begeman | bd881ef | 2008-01-30 20:50:20 +0000 | [diff] [blame] | 2236 | for (unsigned i = 0; i != NumParams; ++i) { |
| 2237 | if (i != 0) typeNames += ", "; |
| 2238 | typeNames += Args[i+1]->getType().getAsString(); |
| 2239 | } |
Nate Begeman | 9f3bfb7 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 2240 | |
| 2241 | return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames, |
| 2242 | SourceRange(BuiltinLoc, RParenLoc)); |
| 2243 | } |
| 2244 | |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2245 | Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| 2246 | ExprTy *expr, TypeTy *type, |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2247 | SourceLocation RPLoc) { |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2248 | Expr *E = static_cast<Expr*>(expr); |
| 2249 | QualType T = QualType::getFromOpaquePtr(type); |
| 2250 | |
| 2251 | InitBuiltinVaListType(); |
| 2252 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2253 | if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType()) |
| 2254 | != Compatible) |
Anders Carlsson | 3676033 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2255 | return Diag(E->getLocStart(), |
| 2256 | diag::err_first_argument_to_va_arg_not_of_type_va_list, |
| 2257 | E->getType().getAsString(), |
| 2258 | E->getSourceRange()); |
| 2259 | |
| 2260 | // FIXME: Warn if a non-POD type is passed in. |
| 2261 | |
| 2262 | return new VAArgExpr(BuiltinLoc, E, T, RPLoc); |
| 2263 | } |
| 2264 | |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2265 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 2266 | SourceLocation Loc, |
| 2267 | QualType DstType, QualType SrcType, |
| 2268 | Expr *SrcExpr, const char *Flavor) { |
| 2269 | // Decode the result (notice that AST's are still created for extensions). |
| 2270 | bool isInvalid = false; |
| 2271 | unsigned DiagKind; |
| 2272 | switch (ConvTy) { |
| 2273 | default: assert(0 && "Unknown conversion type"); |
| 2274 | case Compatible: return false; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2275 | case PointerToInt: |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2276 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 2277 | break; |
Chris Lattner | d951b7b | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 2278 | case IntToPointer: |
| 2279 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 2280 | break; |
Chris Lattner | 005ed75 | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 2281 | case IncompatiblePointer: |
| 2282 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| 2283 | break; |
| 2284 | case FunctionVoidPointer: |
| 2285 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 2286 | break; |
| 2287 | case CompatiblePointerDiscardsQualifiers: |
| 2288 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 2289 | break; |
| 2290 | case Incompatible: |
| 2291 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 2292 | isInvalid = true; |
| 2293 | break; |
| 2294 | } |
| 2295 | |
| 2296 | Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor, |
| 2297 | SrcExpr->getSourceRange()); |
| 2298 | return isInvalid; |
| 2299 | } |
| 2300 | |