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