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