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