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