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