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