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