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