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