blob: 5341ee7faa3f34f31a71942b2b0346d350f3e356 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek588e5eb2007-11-25 00:58:00 +000015#include "SemaUtil.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000016#include "clang/AST/ASTContext.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/AST/Expr.h"
Chris Lattner04421082008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Naroff563477d2007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner925e60d2007-12-28 05:29:59 +000024#include "llvm/ADT/OwningPtr.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner59907c42007-08-10 20:18:51 +000026#include "llvm/ADT/StringExtras.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
Steve Narofff69936d2007-09-16 03:34:24 +000029/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Reid Spencer5f016e22007-07-11 17:01:13 +000030/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
31/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
32/// multiple tokens. However, the common case is that StringToks points to one
33/// string.
34///
35Action::ExprResult
Steve Narofff69936d2007-09-16 03:34:24 +000036Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Reid Spencer5f016e22007-07-11 17:01:13 +000037 assert(NumStringToks && "Must have at least one string!");
38
39 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
40 if (Literal.hadError)
41 return ExprResult(true);
42
43 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
44 for (unsigned i = 0; i != NumStringToks; ++i)
45 StringTokLocs.push_back(StringToks[i].getLocation());
Chris Lattnera7ad98f2008-02-11 00:02:17 +000046
47 // Verify that pascal strings aren't too large.
Anders Carlssonee98ac52007-10-15 02:50:23 +000048 if (Literal.Pascal && Literal.GetStringLength() > 256)
49 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
50 SourceRange(StringToks[0].getLocation(),
51 StringToks[NumStringToks-1].getLocation()));
Reid Spencer5f016e22007-07-11 17:01:13 +000052
Chris Lattnera7ad98f2008-02-11 00:02:17 +000053 QualType StrTy = Context.CharTy;
54 // FIXME: handle wchar_t
55 if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
56
57 // Get an array type for the string, according to C99 6.4.5. This includes
58 // the nul terminator character as well as the string length for pascal
59 // strings.
60 StrTy = Context.getConstantArrayType(StrTy,
61 llvm::APInt(32, Literal.GetStringLength()+1),
62 ArrayType::Normal, 0);
63
Reid Spencer5f016e22007-07-11 17:01:13 +000064 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
65 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera7ad98f2008-02-11 00:02:17 +000066 Literal.AnyWide, StrTy,
Anders Carlssonee98ac52007-10-15 02:50:23 +000067 StringToks[0].getLocation(),
Reid Spencer5f016e22007-07-11 17:01:13 +000068 StringToks[NumStringToks-1].getLocation());
69}
70
71
Steve Naroff08d92e42007-09-15 18:49:24 +000072/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Reid Spencer5f016e22007-07-11 17:01:13 +000073/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroff0d755ad2008-03-19 23:46:26 +000074/// identifier is used in a function call context.
Steve Naroff08d92e42007-09-15 18:49:24 +000075Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Reid Spencer5f016e22007-07-11 17:01:13 +000076 IdentifierInfo &II,
77 bool HasTrailingLParen) {
Chris Lattner8a934232008-03-31 00:36:02 +000078 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroffb327ce02008-04-02 14:35:35 +000079 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattner8a934232008-03-31 00:36:02 +000080
81 // If this reference is in an Objective-C method, then ivar lookup happens as
82 // well.
83 if (CurMethodDecl) {
Steve Naroffe8043c32008-04-01 23:04:06 +000084 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattner8a934232008-03-31 00:36:02 +000085 // There are two cases to handle here. 1) scoped lookup could have failed,
86 // in which case we should look for an ivar. 2) scoped lookup could have
87 // found a decl, but that decl is outside the current method (i.e. a global
88 // variable). In these two cases, we do a lookup for an ivar with this
89 // name, if the lookup suceeds, we replace it our current decl.
Steve Naroffe8043c32008-04-01 23:04:06 +000090 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattner8a934232008-03-31 00:36:02 +000091 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface(), *DeclClass;
92 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, DeclClass)) {
93 // FIXME: This should use a new expr for a direct reference, don't turn
94 // this into Self->ivar, just return a BareIVarExpr or something.
95 IdentifierInfo &II = Context.Idents.get("self");
96 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
97 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
98 static_cast<Expr*>(SelfExpr.Val), true, true);
99 }
100 }
101 }
102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 if (D == 0) {
104 // Otherwise, this could be an implicitly declared function reference (legal
105 // in C90, extension in C99).
106 if (HasTrailingLParen &&
Chris Lattner8a934232008-03-31 00:36:02 +0000107 !getLangOptions().CPlusPlus) // Not in C++.
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 D = ImplicitlyDefineFunction(Loc, II, S);
109 else {
110 // If this name wasn't predeclared and if this is not a function call,
111 // diagnose the problem.
112 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
113 }
114 }
Chris Lattner8a934232008-03-31 00:36:02 +0000115
Steve Naroffe1223f72007-08-28 03:03:08 +0000116 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattner7e669b22008-02-29 16:48:43 +0000117 // check if referencing an identifier with __attribute__((deprecated)).
118 if (VD->getAttr<DeprecatedAttr>())
119 Diag(Loc, diag::warn_deprecated, VD->getName());
120
Steve Naroff53a32342007-08-28 18:45:29 +0000121 // Only create DeclRefExpr's for valid Decl's.
Steve Naroff5912a352007-08-28 20:14:24 +0000122 if (VD->isInvalidDecl())
Steve Naroffe1223f72007-08-28 03:03:08 +0000123 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroffe1223f72007-08-28 03:03:08 +0000125 }
Chris Lattner8a934232008-03-31 00:36:02 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 if (isa<TypedefDecl>(D))
128 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian5ef404f2007-12-05 18:16:33 +0000130 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +0000131 if (isa<NamespaceDecl>(D))
132 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Reid Spencer5f016e22007-07-11 17:01:13 +0000133
134 assert(0 && "Invalid decl");
Chris Lattnereddbe032007-07-21 04:57:45 +0000135 abort();
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Steve Narofff69936d2007-09-16 03:34:24 +0000138Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson22742662007-07-21 05:21:51 +0000139 tok::TokenKind Kind) {
140 PreDefinedExpr::IdentType IT;
141
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 switch (Kind) {
Chris Lattner1423ea42008-01-12 18:39:25 +0000143 default: assert(0 && "Unknown simple primary expr!");
144 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
145 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
146 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 }
Chris Lattner1423ea42008-01-12 18:39:25 +0000148
149 // Verify that this is in a function context.
Chris Lattner8f978d52008-01-12 19:32:28 +0000150 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner1423ea42008-01-12 18:39:25 +0000151 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson22742662007-07-21 05:21:51 +0000152
Chris Lattnerfa28b302008-01-12 08:14:25 +0000153 // Pre-defined identifiers are of type char[x], where x is the length of the
154 // string.
Chris Lattner8f978d52008-01-12 19:32:28 +0000155 unsigned Length;
156 if (CurFunctionDecl)
157 Length = CurFunctionDecl->getIdentifier()->getLength();
158 else
Fariborz Jahanianfaf5e772008-01-17 17:37:26 +0000159 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattner1423ea42008-01-12 18:39:25 +0000160
Chris Lattner8f978d52008-01-12 19:32:28 +0000161 llvm::APInt LengthI(32, Length + 1);
Chris Lattner1423ea42008-01-12 18:39:25 +0000162 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner8f978d52008-01-12 19:32:28 +0000163 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnerfa28b302008-01-12 08:14:25 +0000164 return new PreDefinedExpr(Loc, ResTy, IT);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165}
166
Steve Narofff69936d2007-09-16 03:34:24 +0000167Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 llvm::SmallString<16> CharBuffer;
169 CharBuffer.resize(Tok.getLength());
170 const char *ThisTokBegin = &CharBuffer[0];
171 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
172
173 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
174 Tok.getLocation(), PP);
175 if (Literal.hadError())
176 return ExprResult(true);
Chris Lattnerfc62bfd2008-03-01 08:32:21 +0000177
178 QualType type = getLangOptions().CPlusPlus ? Context.CharTy : Context.IntTy;
179
180 return new CharacterLiteral(Literal.getValue(), type, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000181}
182
Steve Narofff69936d2007-09-16 03:34:24 +0000183Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 // fast path for a single digit (which is quite common). A single digit
185 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
186 if (Tok.getLength() == 1) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000187 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000188
Chris Lattner98be4942008-03-05 18:54:05 +0000189 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattnerf0467b32008-04-02 04:24:33 +0000190 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Reid Spencer5f016e22007-07-11 17:01:13 +0000191 Context.IntTy,
192 Tok.getLocation()));
193 }
194 llvm::SmallString<512> IntegerBuffer;
195 IntegerBuffer.resize(Tok.getLength());
196 const char *ThisTokBegin = &IntegerBuffer[0];
197
198 // Get the spelling of the token, which eliminates trigraphs, etc.
199 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
200 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
201 Tok.getLocation(), PP);
202 if (Literal.hadError)
203 return ExprResult(true);
204
Chris Lattner5d661452007-08-26 03:42:43 +0000205 Expr *Res;
206
207 if (Literal.isFloatingLiteral()) {
Chris Lattner525a0502007-09-22 18:29:59 +0000208 QualType Ty;
209 const llvm::fltSemantics *Format;
Chris Lattner525a0502007-09-22 18:29:59 +0000210
211 if (Literal.isFloat) {
212 Ty = Context.FloatTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000213 Format = Context.Target.getFloatFormat();
214 } else if (!Literal.isLong) {
Chris Lattner525a0502007-09-22 18:29:59 +0000215 Ty = Context.DoubleTy;
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000216 Format = Context.Target.getDoubleFormat();
217 } else {
218 Ty = Context.LongDoubleTy;
219 Format = Context.Target.getLongDoubleFormat();
Chris Lattner525a0502007-09-22 18:29:59 +0000220 }
221
Ted Kremenek720c4ec2007-11-29 00:56:49 +0000222 // isExact will be set by GetFloatValue().
223 bool isExact = false;
224
225 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
226 Ty, Tok.getLocation());
227
Chris Lattner5d661452007-08-26 03:42:43 +0000228 } else if (!Literal.isIntegerLiteral()) {
229 return ExprResult(true);
230 } else {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000231 QualType Ty;
Reid Spencer5f016e22007-07-11 17:01:13 +0000232
Neil Boothb9449512007-08-29 22:00:19 +0000233 // long long is a C99 feature.
234 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth79859c32007-08-29 22:13:52 +0000235 Literal.isLongLong)
Neil Boothb9449512007-08-29 22:00:19 +0000236 Diag(Tok.getLocation(), diag::ext_longlong);
237
Reid Spencer5f016e22007-07-11 17:01:13 +0000238 // Get the value in the widest-possible width.
Chris Lattner98be4942008-03-05 18:54:05 +0000239 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000240
241 if (Literal.GetIntegerValue(ResultVal)) {
242 // If this value didn't fit into uintmax_t, warn and force to ull.
243 Diag(Tok.getLocation(), diag::warn_integer_too_large);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000244 Ty = Context.UnsignedLongLongTy;
245 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner98be4942008-03-05 18:54:05 +0000246 "long long is not intmax_t?");
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 } else {
248 // If this value fits into a ULL, try to figure out what else it fits into
249 // according to the rules of C99 6.4.4.1p5.
250
251 // Octal, Hexadecimal, and integers with a U suffix are allowed to
252 // be an unsigned int.
253 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
254
255 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000256 unsigned Width = 0;
Chris Lattner97c51562007-08-23 21:58:08 +0000257 if (!Literal.isLong && !Literal.isLongLong) {
258 // Are int/unsigned possibilities?
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000259 unsigned IntSize = Context.Target.getIntWidth();
260
Reid Spencer5f016e22007-07-11 17:01:13 +0000261 // Does it fit in a unsigned int?
262 if (ResultVal.isIntN(IntSize)) {
263 // Does it fit in a signed int?
264 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000265 Ty = Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000266 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000267 Ty = Context.UnsignedIntTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000268 Width = IntSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000269 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000270 }
271
272 // Are long/unsigned long possibilities?
Chris Lattnerf0467b32008-04-02 04:24:33 +0000273 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000274 unsigned LongSize = Context.Target.getLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000275
276 // Does it fit in a unsigned long?
277 if (ResultVal.isIntN(LongSize)) {
278 // Does it fit in a signed long?
279 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000280 Ty = Context.LongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000282 Ty = Context.UnsignedLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000283 Width = LongSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000284 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000285 }
286
287 // Finally, check long long if needed.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000288 if (Ty.isNull()) {
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000289 unsigned LongLongSize = Context.Target.getLongLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000290
291 // Does it fit in a unsigned long long?
292 if (ResultVal.isIntN(LongLongSize)) {
293 // Does it fit in a signed long long?
294 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000295 Ty = Context.LongLongTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000296 else if (AllowUnsigned)
Chris Lattnerf0467b32008-04-02 04:24:33 +0000297 Ty = Context.UnsignedLongLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000298 Width = LongLongSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 }
300 }
301
302 // If we still couldn't decide a type, we probably have something that
303 // does not fit in a signed long long, but has no U suffix.
Chris Lattnerf0467b32008-04-02 04:24:33 +0000304 if (Ty.isNull()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000305 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattnerf0467b32008-04-02 04:24:33 +0000306 Ty = Context.UnsignedLongLongTy;
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000307 Width = Context.Target.getLongLongWidth();
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 }
Chris Lattner8cbcb0e2008-05-09 05:59:00 +0000309
310 if (ResultVal.getBitWidth() != Width)
311 ResultVal.trunc(Width);
Reid Spencer5f016e22007-07-11 17:01:13 +0000312 }
313
Chris Lattnerf0467b32008-04-02 04:24:33 +0000314 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Chris Lattner5d661452007-08-26 03:42:43 +0000316
317 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
318 if (Literal.isImaginary)
319 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
320
321 return Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000322}
323
Steve Narofff69936d2007-09-16 03:34:24 +0000324Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 ExprTy *Val) {
Chris Lattnerf0467b32008-04-02 04:24:33 +0000326 Expr *E = (Expr *)Val;
327 assert((E != 0) && "ActOnParenExpr() missing expr");
328 return new ParenExpr(L, R, E);
Reid Spencer5f016e22007-07-11 17:01:13 +0000329}
330
331/// The UsualUnaryConversions() function is *not* called by this routine.
332/// See C99 6.3.2.1p[2-4] for more details.
333QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
334 SourceLocation OpLoc, bool isSizeof) {
335 // C99 6.5.3.4p1:
336 if (isa<FunctionType>(exprType) && isSizeof)
337 // alignof(function) is allowed.
338 Diag(OpLoc, diag::ext_sizeof_function_type);
339 else if (exprType->isVoidType())
340 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
341 else if (exprType->isIncompleteType()) {
342 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
343 diag::err_alignof_incomplete_type,
344 exprType.getAsString());
345 return QualType(); // error
346 }
347 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
348 return Context.getSizeType();
349}
350
351Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000352ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 SourceLocation LPLoc, TypeTy *Ty,
354 SourceLocation RPLoc) {
355 // If error parsing type, ignore.
356 if (Ty == 0) return true;
357
358 // Verify that this is a valid expression.
359 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
360
361 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
362
363 if (resultType.isNull())
364 return true;
365 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
366}
367
Chris Lattner5d794252007-08-24 21:41:10 +0000368QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattnerdbb36972007-08-24 21:16:53 +0000369 DefaultFunctionArrayConversion(V);
370
Chris Lattnercc26ed72007-08-26 05:39:26 +0000371 // These operators return the element type of a complex type.
Chris Lattnerdbb36972007-08-24 21:16:53 +0000372 if (const ComplexType *CT = V->getType()->getAsComplexType())
373 return CT->getElementType();
Chris Lattnercc26ed72007-08-26 05:39:26 +0000374
375 // Otherwise they pass through real integer and floating point types here.
376 if (V->getType()->isArithmeticType())
377 return V->getType();
378
379 // Reject anything else.
380 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
381 return QualType();
Chris Lattnerdbb36972007-08-24 21:16:53 +0000382}
383
384
Reid Spencer5f016e22007-07-11 17:01:13 +0000385
Steve Narofff69936d2007-09-16 03:34:24 +0000386Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000387 tok::TokenKind Kind,
388 ExprTy *Input) {
389 UnaryOperator::Opcode Opc;
390 switch (Kind) {
391 default: assert(0 && "Unknown unary op!");
392 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
393 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
394 }
395 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
396 if (result.isNull())
397 return true;
398 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
399}
400
401Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000402ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000403 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner727a80d2007-07-15 23:59:53 +0000404 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner12d9ff62007-07-16 00:14:47 +0000405
406 // Perform default conversions.
407 DefaultFunctionArrayConversion(LHSExp);
408 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner727a80d2007-07-15 23:59:53 +0000409
Chris Lattner12d9ff62007-07-16 00:14:47 +0000410 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000411
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000413 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 // in the subscript position. As a result, we need to derive the array base
415 // and index from the expression types.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000416 Expr *BaseExpr, *IndexExpr;
417 QualType ResultType;
Chris Lattnerbefee482007-07-31 16:53:04 +0000418 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner12d9ff62007-07-16 00:14:47 +0000419 BaseExpr = LHSExp;
420 IndexExpr = RHSExp;
421 // FIXME: need to deal with const...
422 ResultType = PTy->getPointeeType();
Chris Lattnerbefee482007-07-31 16:53:04 +0000423 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000424 // Handle the uncommon case of "123[Ptr]".
Chris Lattner12d9ff62007-07-16 00:14:47 +0000425 BaseExpr = RHSExp;
426 IndexExpr = LHSExp;
427 // FIXME: need to deal with const...
428 ResultType = PTy->getPointeeType();
Chris Lattnerc8629632007-07-31 19:29:30 +0000429 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
430 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner12d9ff62007-07-16 00:14:47 +0000431 IndexExpr = RHSExp;
Steve Naroff608e0ee2007-08-03 22:40:33 +0000432
433 // Component access limited to variables (reject vec4.rg[1]).
Nate Begeman8a997642008-05-09 06:41:27 +0000434 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
435 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begeman213541a2008-04-18 23:10:10 +0000436 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff608e0ee2007-08-03 22:40:33 +0000437 SourceRange(LLoc, RLoc));
Chris Lattner12d9ff62007-07-16 00:14:47 +0000438 // FIXME: need to deal with const...
439 ResultType = VTy->getElementType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 } else {
Chris Lattner727a80d2007-07-15 23:59:53 +0000441 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
442 RHSExp->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 }
444 // C99 6.5.2.1p1
Chris Lattner12d9ff62007-07-16 00:14:47 +0000445 if (!IndexExpr->getType()->isIntegerType())
446 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
447 IndexExpr->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000448
Chris Lattner12d9ff62007-07-16 00:14:47 +0000449 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
450 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattnerd805bec2008-04-02 06:59:01 +0000451 // void (*)(int)) and pointers to incomplete types. Functions are not
452 // objects in C99.
Chris Lattner12d9ff62007-07-16 00:14:47 +0000453 if (!ResultType->isObjectType())
454 return Diag(BaseExpr->getLocStart(),
455 diag::err_typecheck_subscript_not_object,
456 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
457
458 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000459}
460
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000461QualType Sema::
Nate Begeman213541a2008-04-18 23:10:10 +0000462CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000463 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begeman213541a2008-04-18 23:10:10 +0000464 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Nate Begeman8a997642008-05-09 06:41:27 +0000465
466 // This flag determines whether or not the component is to be treated as a
467 // special name, or a regular GLSL-style component access.
468 bool SpecialComponent = false;
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000469
470 // The vector accessor can't exceed the number of elements.
471 const char *compStr = CompName.getName();
472 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begeman213541a2008-04-18 23:10:10 +0000473 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000474 baseType.getAsString(), SourceRange(CompLoc));
475 return QualType();
476 }
Nate Begeman8a997642008-05-09 06:41:27 +0000477
478 // Check that we've found one of the special components, or that the component
479 // names must come from the same set.
480 if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") ||
481 !strcmp(compStr, "e") || !strcmp(compStr, "o")) {
482 SpecialComponent = true;
483 } else if (vecType->getPointAccessorIdx(*compStr) != -1) {
Chris Lattner88dca042007-08-02 22:33:49 +0000484 do
485 compStr++;
486 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
487 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
488 do
489 compStr++;
490 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
491 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
492 do
493 compStr++;
494 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
495 }
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000496
Nate Begeman8a997642008-05-09 06:41:27 +0000497 if (!SpecialComponent && *compStr) {
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000498 // We didn't get to the end of the string. This means the component names
499 // didn't come from the same set *or* we encountered an illegal name.
Nate Begeman213541a2008-04-18 23:10:10 +0000500 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000501 std::string(compStr,compStr+1), SourceRange(CompLoc));
502 return QualType();
503 }
504 // Each component accessor can't exceed the vector type.
505 compStr = CompName.getName();
506 while (*compStr) {
507 if (vecType->isAccessorWithinNumElements(*compStr))
508 compStr++;
509 else
510 break;
511 }
Nate Begeman8a997642008-05-09 06:41:27 +0000512 if (!SpecialComponent && *compStr) {
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000513 // We didn't get to the end of the string. This means a component accessor
514 // exceeds the number of elements in the vector.
Nate Begeman213541a2008-04-18 23:10:10 +0000515 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000516 baseType.getAsString(), SourceRange(CompLoc));
517 return QualType();
518 }
Nate Begeman8a997642008-05-09 06:41:27 +0000519
520 // If we have a special component name, verify that the current vector length
521 // is an even number, since all special component names return exactly half
522 // the elements.
523 if (SpecialComponent && (vecType->getNumElements() & 1U)) {
524 return QualType();
525 }
526
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000527 // The component accessor looks fine - now we need to compute the actual type.
528 // The vector type is implied by the component accessor. For example,
529 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
Nate Begeman8a997642008-05-09 06:41:27 +0000530 // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2.
531 unsigned CompSize = SpecialComponent ? vecType->getNumElements() / 2
532 : strlen(CompName.getName());
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000533 if (CompSize == 1)
534 return vecType->getElementType();
Steve Naroffbea0b342007-07-29 16:33:31 +0000535
Nate Begeman213541a2008-04-18 23:10:10 +0000536 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroffbea0b342007-07-29 16:33:31 +0000537 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begeman213541a2008-04-18 23:10:10 +0000538 // diagostics look bad. We want extended vector types to appear built-in.
539 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
540 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
541 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroffbea0b342007-07-29 16:33:31 +0000542 }
543 return VT; // should never get here (a typedef type should always be found).
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000544}
545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000547ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 tok::TokenKind OpKind, SourceLocation MemberLoc,
549 IdentifierInfo &Member) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000550 Expr *BaseExpr = static_cast<Expr *>(Base);
551 assert(BaseExpr && "no record expression");
Steve Naroff3cc4af82007-12-16 21:42:28 +0000552
553 // Perform default conversions.
554 DefaultFunctionArrayConversion(BaseExpr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000556 QualType BaseType = BaseExpr->getType();
557 assert(!BaseType.isNull() && "no type for member expression");
Reid Spencer5f016e22007-07-11 17:01:13 +0000558
Reid Spencer5f016e22007-07-11 17:01:13 +0000559 if (OpKind == tok::arrow) {
Chris Lattnerbefee482007-07-31 16:53:04 +0000560 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000561 BaseType = PT->getPointeeType();
562 else
563 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
564 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
Nate Begeman213541a2008-04-18 23:10:10 +0000566 // The base type is either a record or an ExtVectorType.
Chris Lattnerc8629632007-07-31 19:29:30 +0000567 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000568 RecordDecl *RDecl = RTy->getDecl();
569 if (RTy->isIncompleteType())
570 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
571 BaseExpr->getSourceRange());
572 // The record definition is complete, now make sure the member is valid.
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000573 FieldDecl *MemberDecl = RDecl->getMember(&Member);
574 if (!MemberDecl)
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000575 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
576 SourceRange(MemberLoc));
Eli Friedman51019072008-02-06 22:48:16 +0000577
578 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedman64ec0cc2008-02-07 05:24:51 +0000579 // FIXME: Handle address space modifiers
Eli Friedman51019072008-02-06 22:48:16 +0000580 QualType MemberType = MemberDecl->getType();
581 unsigned combinedQualifiers =
Chris Lattnerf46699c2008-02-20 20:55:12 +0000582 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman51019072008-02-06 22:48:16 +0000583 MemberType = MemberType.getQualifiedType(combinedQualifiers);
584
585 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
586 MemberLoc, MemberType);
Nate Begeman213541a2008-04-18 23:10:10 +0000587 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff608e0ee2007-08-03 22:40:33 +0000588 // Component access limited to variables (reject vec4.rg.g).
Nate Begeman8a997642008-05-09 06:41:27 +0000589 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr) &&
590 !isa<ExtVectorElementExpr>(BaseExpr))
Nate Begeman213541a2008-04-18 23:10:10 +0000591 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff608e0ee2007-08-03 22:40:33 +0000592 SourceRange(MemberLoc));
Nate Begeman213541a2008-04-18 23:10:10 +0000593 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroffe1b31fe2007-07-27 22:15:19 +0000594 if (ret.isNull())
595 return true;
Nate Begeman213541a2008-04-18 23:10:10 +0000596 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597 } else if (BaseType->isObjCInterfaceType()) {
598 ObjCInterfaceDecl *IFace;
599 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
600 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000601 else
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
603 ObjCInterfaceDecl *clsDeclared;
604 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian232220c2007-11-12 22:29:28 +0000605 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
606 OpKind==tok::arrow);
607 }
608 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
609 SourceRange(MemberLoc));
Reid Spencer5f016e22007-07-11 17:01:13 +0000610}
611
Steve Narofff69936d2007-09-16 03:34:24 +0000612/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +0000613/// This provides the location of the left/right parens and a list of comma
614/// locations.
615Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000616ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner925e60d2007-12-28 05:29:59 +0000617 ExprTy **args, unsigned NumArgs,
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner74c469f2007-07-21 03:03:59 +0000619 Expr *Fn = static_cast<Expr *>(fn);
620 Expr **Args = reinterpret_cast<Expr**>(args);
621 assert(Fn && "no function call expression");
Chris Lattner04421082008-04-08 04:40:51 +0000622 FunctionDecl *FDecl = NULL;
Chris Lattner04421082008-04-08 04:40:51 +0000623
624 // Promote the function operand.
625 UsualUnaryConversions(Fn);
626
627 // If we're directly calling a function, get the declaration for
628 // that function.
629 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
630 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
631 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
632
Chris Lattner925e60d2007-12-28 05:29:59 +0000633 // Make the call expr early, before semantic checks. This guarantees cleanup
634 // of arguments and function on error.
Chris Lattner8123a952008-04-10 02:22:51 +0000635 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner925e60d2007-12-28 05:29:59 +0000636 Context.BoolTy, RParenLoc));
637
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
639 // type pointer to function".
Chris Lattner925e60d2007-12-28 05:29:59 +0000640 const PointerType *PT = Fn->getType()->getAsPointerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 if (PT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000642 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
643 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000644 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
645 if (FuncT == 0)
Chris Lattner74c469f2007-07-21 03:03:59 +0000646 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
647 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner925e60d2007-12-28 05:29:59 +0000648
649 // We know the result type of the call, set it.
650 TheCall->setType(FuncT->getResultType());
Reid Spencer5f016e22007-07-11 17:01:13 +0000651
Chris Lattner925e60d2007-12-28 05:29:59 +0000652 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
654 // assignment, to the types of the corresponding parameter, ...
Chris Lattner925e60d2007-12-28 05:29:59 +0000655 unsigned NumArgsInProto = Proto->getNumArgs();
656 unsigned NumArgsToCheck = NumArgs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000657
Chris Lattner04421082008-04-08 04:40:51 +0000658 // If too few arguments are available (and we don't have default
659 // arguments for the remaining parameters), don't make the call.
660 if (NumArgs < NumArgsInProto) {
Chris Lattner8123a952008-04-10 02:22:51 +0000661 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner04421082008-04-08 04:40:51 +0000662 // Use default arguments for missing arguments
663 NumArgsToCheck = NumArgsInProto;
Chris Lattner8123a952008-04-10 02:22:51 +0000664 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner04421082008-04-08 04:40:51 +0000665 } else
666 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
667 Fn->getSourceRange());
668 }
669
Chris Lattner925e60d2007-12-28 05:29:59 +0000670 // If too many are passed and not variadic, error on the extras and drop
671 // them.
672 if (NumArgs > NumArgsInProto) {
673 if (!Proto->isVariadic()) {
Chris Lattnerd472b312007-07-21 03:09:58 +0000674 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner74c469f2007-07-21 03:03:59 +0000675 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnerd472b312007-07-21 03:09:58 +0000676 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner925e60d2007-12-28 05:29:59 +0000677 Args[NumArgs-1]->getLocEnd()));
678 // This deletes the extra arguments.
679 TheCall->setNumArgs(NumArgsInProto);
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 }
681 NumArgsToCheck = NumArgsInProto;
682 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000683
Reid Spencer5f016e22007-07-11 17:01:13 +0000684 // Continue to check argument types (even if we have too few/many args).
Chris Lattner925e60d2007-12-28 05:29:59 +0000685 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner5cf216b2008-01-04 18:04:52 +0000686 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner04421082008-04-08 04:40:51 +0000687
688 Expr *Arg;
689 if (i < NumArgs)
690 Arg = Args[i];
691 else
692 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner5cf216b2008-01-04 18:04:52 +0000693 QualType ArgType = Arg->getType();
Steve Naroff700204c2007-07-24 21:46:40 +0000694
Chris Lattner925e60d2007-12-28 05:29:59 +0000695 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner5cf216b2008-01-04 18:04:52 +0000696 AssignConvertType ConvTy =
697 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner925e60d2007-12-28 05:29:59 +0000698 TheCall->setArg(i, Arg);
699
Chris Lattner5cf216b2008-01-04 18:04:52 +0000700 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
701 ArgType, Arg, "passing"))
702 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000704
705 // If this is a variadic call, handle args passed through "...".
706 if (Proto->isVariadic()) {
Steve Naroffb291ab62007-08-28 23:30:39 +0000707 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner925e60d2007-12-28 05:29:59 +0000708 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
709 Expr *Arg = Args[i];
710 DefaultArgumentPromotion(Arg);
711 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000712 }
Steve Naroffb291ab62007-08-28 23:30:39 +0000713 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000714 } else {
715 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
716
Steve Naroffb291ab62007-08-28 23:30:39 +0000717 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner925e60d2007-12-28 05:29:59 +0000718 for (unsigned i = 0; i != NumArgs; i++) {
719 Expr *Arg = Args[i];
720 DefaultArgumentPromotion(Arg);
721 TheCall->setArg(i, Arg);
Steve Naroffb291ab62007-08-28 23:30:39 +0000722 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000724
Chris Lattner59907c42007-08-10 20:18:51 +0000725 // Do special checking on direct calls to functions.
Chris Lattner04421082008-04-08 04:40:51 +0000726 if (FDecl && CheckFunctionCall(FDecl, TheCall.get()))
727 return true;
Chris Lattner59907c42007-08-10 20:18:51 +0000728
Chris Lattner925e60d2007-12-28 05:29:59 +0000729 return TheCall.take();
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
731
732Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000733ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroffaff1edd2007-07-19 21:32:11 +0000734 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Narofff69936d2007-09-16 03:34:24 +0000735 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Naroff4aa88f82007-07-19 01:06:55 +0000736 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroffaff1edd2007-07-19 21:32:11 +0000737 // FIXME: put back this assert when initializers are worked out.
Steve Narofff69936d2007-09-16 03:34:24 +0000738 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroffaff1edd2007-07-19 21:32:11 +0000739 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlssond35c8322007-12-05 07:24:19 +0000740
Steve Naroff2fdc3742007-12-10 22:44:33 +0000741 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Naroffd0091aa2008-01-10 22:15:12 +0000742 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff58d18212008-01-09 20:58:06 +0000743 return true;
Steve Naroffe9b12192008-01-14 18:19:28 +0000744
745 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
746 if (isFileScope) { // 6.5.2.5p3
Steve Naroffd0091aa2008-01-10 22:15:12 +0000747 if (CheckForConstantInitializer(literalExpr, literalType))
748 return true;
749 }
Steve Naroffe9b12192008-01-14 18:19:28 +0000750 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Naroff4aa88f82007-07-19 01:06:55 +0000751}
752
753Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000754ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000755 SourceLocation RBraceLoc) {
Steve Narofff0090632007-09-02 02:04:30 +0000756 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000757
Steve Naroff08d92e42007-09-15 18:49:24 +0000758 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroffd35005e2007-09-03 01:24:23 +0000759 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson66b5a8a2007-08-31 04:56:16 +0000760
Chris Lattnerf0467b32008-04-02 04:24:33 +0000761 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
762 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
763 return E;
Steve Naroff4aa88f82007-07-19 01:06:55 +0000764}
765
Chris Lattnerfe23e212007-12-20 00:44:32 +0000766bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssona64db8f2007-11-27 05:51:55 +0000767 assert(VectorTy->isVectorType() && "Not a vector type!");
768
769 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner98be4942008-03-05 18:54:05 +0000770 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssona64db8f2007-11-27 05:51:55 +0000771 return Diag(R.getBegin(),
772 Ty->isVectorType() ?
773 diag::err_invalid_conversion_between_vectors :
774 diag::err_invalid_conversion_between_vector_and_integer,
775 VectorTy.getAsString().c_str(),
776 Ty.getAsString().c_str(), R);
777 } else
778 return Diag(R.getBegin(),
779 diag::err_invalid_conversion_between_vector_and_scalar,
780 VectorTy.getAsString().c_str(),
781 Ty.getAsString().c_str(), R);
782
783 return false;
784}
785
Steve Naroff4aa88f82007-07-19 01:06:55 +0000786Action::ExprResult Sema::
Steve Narofff69936d2007-09-16 03:34:24 +0000787ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Reid Spencer5f016e22007-07-11 17:01:13 +0000788 SourceLocation RParenLoc, ExprTy *Op) {
Steve Narofff69936d2007-09-16 03:34:24 +0000789 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff16beff82007-07-16 23:25:18 +0000790
791 Expr *castExpr = static_cast<Expr*>(Op);
792 QualType castType = QualType::getFromOpaquePtr(Ty);
793
Steve Naroff711602b2007-08-31 00:32:44 +0000794 UsualUnaryConversions(castExpr);
795
Chris Lattner75af4802007-07-18 16:00:06 +0000796 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
797 // type needs to be scalar.
Chris Lattner3da2db42007-10-29 04:26:44 +0000798 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Naroff9412d682008-01-24 22:55:05 +0000799 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000800 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
801 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Naroff9412d682008-01-24 22:55:05 +0000802 if (!castExpr->getType()->isScalarType() &&
803 !castExpr->getType()->isVectorType())
Chris Lattner3da2db42007-10-29 04:26:44 +0000804 return Diag(castExpr->getLocStart(),
805 diag::err_typecheck_expect_scalar_operand,
806 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssona64db8f2007-11-27 05:51:55 +0000807
808 if (castExpr->getType()->isVectorType()) {
809 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
810 castExpr->getType(), castType))
811 return true;
812 } else if (castType->isVectorType()) {
813 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
814 castType, castExpr->getType()))
815 return true;
Chris Lattner3da2db42007-10-29 04:26:44 +0000816 }
Steve Naroff16beff82007-07-16 23:25:18 +0000817 }
818 return new CastExpr(castType, castExpr, LParenLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000819}
820
Chris Lattnera21ddb32007-11-26 01:40:58 +0000821/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
822/// In that case, lex = cond.
Reid Spencer5f016e22007-07-11 17:01:13 +0000823inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff49b45262007-07-13 16:58:59 +0000824 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000825 UsualUnaryConversions(cond);
826 UsualUnaryConversions(lex);
827 UsualUnaryConversions(rex);
828 QualType condT = cond->getType();
829 QualType lexT = lex->getType();
830 QualType rexT = rex->getType();
831
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 // first, check the condition.
Steve Naroff49b45262007-07-13 16:58:59 +0000833 if (!condT->isScalarType()) { // C99 6.5.15p2
834 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
835 condT.getAsString());
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 return QualType();
837 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000838
839 // Now check the two expressions.
840
841 // If both operands have arithmetic type, do the usual arithmetic conversions
842 // to find a common type: C99 6.5.15p3,5.
843 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffa4332e22007-07-17 00:58:39 +0000844 UsualArithmeticConversions(lex, rex);
845 return lex->getType();
846 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000847
848 // If both operands are the same structure or union type, the result is that
849 // type.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000850 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner70d67a92008-01-06 22:42:25 +0000851 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattnera21ddb32007-11-26 01:40:58 +0000852 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner70d67a92008-01-06 22:42:25 +0000853 // "If both the operands have structure or union type, the result has
854 // that type." This implies that CV qualifiers are dropped.
855 return lexT.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000856 }
Chris Lattner70d67a92008-01-06 22:42:25 +0000857
858 // C99 6.5.15p5: "If both operands have void type, the result has void type."
859 if (lexT->isVoidType() && rexT->isVoidType())
860 return lexT.getUnqualifiedType();
Steve Naroffb6d54e52008-01-08 01:11:38 +0000861
862 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
863 // the type of the other operand."
864 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000865 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000866 return lexT;
867 }
868 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +0000869 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroffb6d54e52008-01-08 01:11:38 +0000870 return rexT;
871 }
Chris Lattnerbd57d362008-01-06 22:50:31 +0000872 // Handle the case where both operands are pointers before we handle null
873 // pointer constants in case both operands are null pointer constants.
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000874 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
875 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
876 // get the "pointed to" types
877 QualType lhptee = LHSPT->getPointeeType();
878 QualType rhptee = RHSPT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000879
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000880 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
881 if (lhptee->isVoidType() &&
Chris Lattnerd805bec2008-04-02 06:59:01 +0000882 rhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000883 // Figure out necessary qualifiers (C99 6.5.15p6)
884 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000885 QualType destType = Context.getPointerType(destPointee);
886 ImpCastExprToType(lex, destType); // add qualifiers if necessary
887 ImpCastExprToType(rex, destType); // promote to void*
888 return destType;
889 }
Chris Lattnerd805bec2008-04-02 06:59:01 +0000890 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattnerf46699c2008-02-20 20:55:12 +0000891 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmana541d532008-02-10 22:59:36 +0000892 QualType destType = Context.getPointerType(destPointee);
893 ImpCastExprToType(lex, destType); // add qualifiers if necessary
894 ImpCastExprToType(rex, destType); // promote to void*
895 return destType;
896 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000897
Steve Naroffec0550f2007-10-15 20:41:53 +0000898 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
899 rhptee.getUnqualifiedType())) {
Steve Naroffc0ff1ca2008-02-01 22:44:48 +0000900 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000901 lexT.getAsString(), rexT.getAsString(),
902 lex->getSourceRange(), rex->getSourceRange());
Eli Friedmanb1284ac2008-01-30 17:02:03 +0000903 // In this situation, we assume void* type. No especially good
904 // reason, but this is what gcc does, and we do have to pick
905 // to get a consistent AST.
906 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
907 ImpCastExprToType(lex, voidPtrTy);
908 ImpCastExprToType(rex, voidPtrTy);
909 return voidPtrTy;
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000910 }
911 // The pointer types are compatible.
Chris Lattner73d0d4f2007-08-30 17:45:32 +0000912 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
913 // differently qualified versions of compatible types, the result type is
914 // a pointer to an appropriately qualified version of the *composite*
915 // type.
Chris Lattnerbd57d362008-01-06 22:50:31 +0000916 // FIXME: Need to return the composite type.
Eli Friedmana541d532008-02-10 22:59:36 +0000917 // FIXME: Need to add qualifiers
Chris Lattnerbd57d362008-01-06 22:50:31 +0000918 return lexT;
Reid Spencer5f016e22007-07-11 17:01:13 +0000919 }
920 }
Chris Lattner2dcb6bb2007-07-31 21:27:01 +0000921
Chris Lattner70d67a92008-01-06 22:42:25 +0000922 // Otherwise, the operands are not compatible.
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff49b45262007-07-13 16:58:59 +0000924 lexT.getAsString(), rexT.getAsString(),
925 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 return QualType();
927}
928
Steve Narofff69936d2007-09-16 03:34:24 +0000929/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Reid Spencer5f016e22007-07-11 17:01:13 +0000930/// in the case of a the GNU conditional expr extension.
Steve Narofff69936d2007-09-16 03:34:24 +0000931Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 SourceLocation ColonLoc,
933 ExprTy *Cond, ExprTy *LHS,
934 ExprTy *RHS) {
Chris Lattner26824902007-07-16 21:39:03 +0000935 Expr *CondExpr = (Expr *) Cond;
936 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000937
938 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
939 // was the condition.
940 bool isLHSNull = LHSExpr == 0;
941 if (isLHSNull)
942 LHSExpr = CondExpr;
943
Chris Lattner26824902007-07-16 21:39:03 +0000944 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
945 RHSExpr, QuestionLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000946 if (result.isNull())
947 return true;
Chris Lattnera21ddb32007-11-26 01:40:58 +0000948 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
949 RHSExpr, result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000950}
951
Steve Naroffb291ab62007-08-28 23:30:39 +0000952/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffb3c2b882008-01-29 02:42:22 +0000953/// do not have a prototype. Arguments that have type float are promoted to
954/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner925e60d2007-12-28 05:29:59 +0000955void Sema::DefaultArgumentPromotion(Expr *&Expr) {
956 QualType Ty = Expr->getType();
957 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffb291ab62007-08-28 23:30:39 +0000958
Chris Lattner925e60d2007-12-28 05:29:59 +0000959 if (Ty == Context.FloatTy)
Chris Lattner1e0a3902008-01-16 19:17:22 +0000960 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffb3c2b882008-01-29 02:42:22 +0000961 else
962 UsualUnaryConversions(Expr);
Steve Naroffb291ab62007-08-28 23:30:39 +0000963}
964
Steve Narofffa2eaab2007-07-15 02:02:06 +0000965/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattnerf0467b32008-04-02 04:24:33 +0000966void Sema::DefaultFunctionArrayConversion(Expr *&E) {
967 QualType Ty = E->getType();
968 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendling08ad47c2007-07-17 03:52:31 +0000969
Chris Lattnerf0467b32008-04-02 04:24:33 +0000970 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +0000971 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattnerf0467b32008-04-02 04:24:33 +0000972 Ty = E->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000973 }
Chris Lattnerf0467b32008-04-02 04:24:33 +0000974 if (Ty->isFunctionType())
975 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattnere6327742008-04-02 05:18:44 +0000976 else if (Ty->isArrayType())
977 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
979
Nate Begemane2ce1d92008-01-17 17:46:27 +0000980/// UsualUnaryConversions - Performs various conversions that are common to most
Reid Spencer5f016e22007-07-11 17:01:13 +0000981/// operators (C99 6.3). The conversions of array and function types are
982/// sometimes surpressed. For example, the array->pointer conversion doesn't
983/// apply if the array is an argument to the sizeof or address (&) operators.
984/// In these instances, this routine should *not* be called.
Chris Lattner925e60d2007-12-28 05:29:59 +0000985Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
986 QualType Ty = Expr->getType();
987 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000988
Chris Lattner925e60d2007-12-28 05:29:59 +0000989 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattner423a3c92008-04-02 17:45:06 +0000990 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner925e60d2007-12-28 05:29:59 +0000991 Ty = Expr->getType();
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000992 }
Chris Lattner925e60d2007-12-28 05:29:59 +0000993 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattner1e0a3902008-01-16 19:17:22 +0000994 ImpCastExprToType(Expr, Context.IntTy);
Steve Naroffc80b4ee2007-07-16 21:54:35 +0000995 else
Chris Lattner925e60d2007-12-28 05:29:59 +0000996 DefaultFunctionArrayConversion(Expr);
997
998 return Expr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000999}
1000
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001001/// UsualArithmeticConversions - Performs various conversions that are common to
Reid Spencer5f016e22007-07-11 17:01:13 +00001002/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
1003/// routine returns the first non-arithmetic type found. The client is
1004/// responsible for emitting appropriate error diagnostics.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001005/// FIXME: verify the conversion rules for "complex int" are consistent with
1006/// GCC.
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001007QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
1008 bool isCompAssign) {
Steve Naroff8702a0f2007-08-25 19:54:59 +00001009 if (!isCompAssign) {
1010 UsualUnaryConversions(lhsExpr);
1011 UsualUnaryConversions(rhsExpr);
1012 }
Steve Naroff3187e202007-10-18 18:55:53 +00001013 // For conversion purposes, we ignore any qualifiers.
1014 // For example, "const float" and "float" are equivalent.
Steve Narofff68a63f2007-11-10 19:45:54 +00001015 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
1016 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001017
1018 // If both types are identical, no conversion is needed.
Steve Naroff3187e202007-10-18 18:55:53 +00001019 if (lhs == rhs)
1020 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001021
1022 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1023 // The caller can deal with this (e.g. pointer + int).
Steve Naroffa4332e22007-07-17 00:58:39 +00001024 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001025 return lhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001026
1027 // At this point, we have two different arithmetic types.
1028
1029 // Handle complex types first (C99 6.3.1.8p1).
1030 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff02f62a92008-01-15 19:36:10 +00001031 // if we have an integer operand, the result is the complex type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001032 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001033 // convert the rhs to the lhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001034 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001035 return lhs;
Steve Naroff02f62a92008-01-15 19:36:10 +00001036 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001037 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001038 // convert the lhs to the rhs complex type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001039 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001040 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001041 }
Steve Narofff1448a02007-08-27 01:27:54 +00001042 // This handles complex/complex, complex/float, or float/complex.
1043 // When both operands are complex, the shorter operand is converted to the
1044 // type of the longer, and that is the type of the result. This corresponds
1045 // to what is done when combining two real floating-point operands.
1046 // The fun begins when size promotion occur across type domains.
1047 // From H&S 6.3.4: When one operand is complex and the other is a real
1048 // floating-point type, the less precise type is converted, within it's
1049 // real or complex domain, to the precision of the other type. For example,
1050 // when combining a "long double" with a "double _Complex", the
1051 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnera75cea32008-04-06 23:38:49 +00001052 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001053
1054 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff55fe4552007-08-27 21:32:55 +00001055 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1056 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001057 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001058 } else if (result < 0) { // The right side is bigger, convert lhs.
1059 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1060 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001061 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff55fe4552007-08-27 21:32:55 +00001062 }
1063 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1064 // domains match. This is a requirement for our implementation, C99
1065 // does not require this promotion.
1066 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1067 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff29960362007-08-27 21:43:43 +00001068 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001069 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff29960362007-08-27 21:43:43 +00001070 return rhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001071 } else { // handle "_Complex double, double".
Steve Naroff29960362007-08-27 21:43:43 +00001072 if (!isCompAssign)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001073 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff29960362007-08-27 21:43:43 +00001074 return lhs;
Steve Naroff55fe4552007-08-27 21:32:55 +00001075 }
Steve Naroffa4332e22007-07-17 00:58:39 +00001076 }
Steve Naroff29960362007-08-27 21:43:43 +00001077 return lhs; // The domain/size match exactly.
Reid Spencer5f016e22007-07-11 17:01:13 +00001078 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 // Now handle "real" floating types (i.e. float, double, long double).
1080 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1081 // if we have an integer operand, the result is the real floating type.
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001082 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001083 // convert rhs to the lhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001084 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001085 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001086 }
Steve Naroffdfb9bbb2008-01-15 22:21:49 +00001087 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001088 // convert lhs to the rhs floating point type.
Chris Lattner1e0a3902008-01-16 19:17:22 +00001089 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001090 return rhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001091 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001092 // We have two real floating types, float/complex combos were handled above.
1093 // Convert the smaller operand to the bigger result.
Chris Lattnera75cea32008-04-06 23:38:49 +00001094 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Narofffb0d4962007-08-27 15:30:22 +00001095
1096 if (result > 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001097 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001098 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001099 }
Steve Narofffb0d4962007-08-27 15:30:22 +00001100 if (result < 0) { // convert the lhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001101 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Narofffb0d4962007-08-27 15:30:22 +00001102 return rhs;
1103 }
1104 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Reid Spencer5f016e22007-07-11 17:01:13 +00001105 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001106 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1107 // Handle GCC complex int extension.
Steve Naroff02f62a92008-01-15 19:36:10 +00001108 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001109 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff02f62a92008-01-15 19:36:10 +00001110
Eli Friedman8e54ad02008-02-08 01:19:44 +00001111 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001112 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1113 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001114 // convert the rhs
1115 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1116 return lhs;
Eli Friedman8e54ad02008-02-08 01:19:44 +00001117 }
1118 if (!isCompAssign)
Eli Friedman8c4e5db2008-02-08 01:24:30 +00001119 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman8e54ad02008-02-08 01:19:44 +00001120 return rhs;
1121 } else if (lhsComplexInt && rhs->isIntegerType()) {
1122 // convert the rhs to the lhs complex type.
1123 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1124 return lhs;
1125 } else if (rhsComplexInt && lhs->isIntegerType()) {
1126 // convert the lhs to the rhs complex type.
1127 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1128 return rhs;
1129 }
Steve Naroff02f62a92008-01-15 19:36:10 +00001130 }
Steve Narofffa2eaab2007-07-15 02:02:06 +00001131 // Finally, we have two differing integer types.
Chris Lattner7cfeb082008-04-06 23:55:33 +00001132 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattner1e0a3902008-01-16 19:17:22 +00001133 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001134 return lhs;
Steve Naroffa4332e22007-07-17 00:58:39 +00001135 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001136 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001137 return rhs;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138}
1139
1140// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1141// being closely modeled after the C99 spec:-). The odd characteristic of this
1142// routine is it effectively iqnores the qualifiers on the top level pointee.
1143// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1144// FIXME: add a couple examples in this comment.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001145Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001146Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1147 QualType lhptee, rhptee;
1148
1149 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner2dcb6bb2007-07-31 21:27:01 +00001150 lhptee = lhsType->getAsPointerType()->getPointeeType();
1151 rhptee = rhsType->getAsPointerType()->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001152
1153 // make sure we operate on the canonical type
1154 lhptee = lhptee.getCanonicalType();
1155 rhptee = rhptee.getCanonicalType();
1156
Chris Lattner5cf216b2008-01-04 18:04:52 +00001157 AssignConvertType ConvTy = Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
1159 // C99 6.5.16.1p1: This following citation is common to constraints
1160 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1161 // qualifiers of the type *pointed to* by the right;
Chris Lattnerf46699c2008-02-20 20:55:12 +00001162 // FIXME: Handle ASQualType
1163 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1164 rhptee.getCVRQualifiers())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001165 ConvTy = CompatiblePointerDiscardsQualifiers;
Reid Spencer5f016e22007-07-11 17:01:13 +00001166
1167 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1168 // incomplete type and the other is a pointer to a qualified or unqualified
1169 // version of void...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001170 if (lhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001171 if (rhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001172 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001173
1174 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001175 assert(rhptee->isFunctionType());
1176 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001177 }
1178
1179 if (rhptee->isVoidType()) {
Chris Lattnerd805bec2008-04-02 06:59:01 +00001180 if (lhptee->isIncompleteOrObjectType())
Chris Lattner5cf216b2008-01-04 18:04:52 +00001181 return ConvTy;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001182
1183 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattnerd805bec2008-04-02 06:59:01 +00001184 assert(lhptee->isFunctionType());
1185 return FunctionVoidPointer;
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001186 }
1187
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1189 // unqualified versions of compatible types, ...
Chris Lattnerbfe639e2008-01-03 22:56:36 +00001190 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1191 rhptee.getUnqualifiedType()))
1192 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner5cf216b2008-01-04 18:04:52 +00001193 return ConvTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194}
1195
1196/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1197/// has code to accommodate several GCC extensions when type checking
1198/// pointers. Here are some objectionable examples that GCC considers warnings:
1199///
1200/// int a, *pint;
1201/// short *pshort;
1202/// struct foo *pfoo;
1203///
1204/// pint = pshort; // warning: assignment from incompatible pointer type
1205/// a = pint; // warning: assignment makes integer from pointer without a cast
1206/// pint = a; // warning: assignment makes pointer from integer without a cast
1207/// pint = pfoo; // warning: assignment from incompatible pointer type
1208///
1209/// As a result, the code for dealing with pointers is more complex than the
1210/// C99 spec dictates.
Reid Spencer5f016e22007-07-11 17:01:13 +00001211///
Chris Lattner5cf216b2008-01-04 18:04:52 +00001212Sema::AssignConvertType
Reid Spencer5f016e22007-07-11 17:01:13 +00001213Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnerfc144e22008-01-04 23:18:45 +00001214 // Get canonical types. We're not formatting these types, just comparing
1215 // them.
1216 lhsType = lhsType.getCanonicalType();
1217 rhsType = rhsType.getCanonicalType();
1218
1219 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerd2656dd2008-01-07 17:51:46 +00001220 return Compatible; // Common case: fast path an exact match.
Steve Naroff700204c2007-07-24 21:46:40 +00001221
Anders Carlsson793680e2007-10-12 23:56:29 +00001222 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattner8f8fc7b2008-04-07 06:52:53 +00001223 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlsson793680e2007-10-12 23:56:29 +00001224 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001225 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001226 }
Chris Lattnerfc144e22008-01-04 23:18:45 +00001227
Chris Lattnereca7be62008-04-07 05:30:13 +00001228 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1229 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001230 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001231 return Incompatible;
Fariborz Jahanian411f3732007-12-19 17:45:58 +00001232 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001233
Chris Lattner78eca282008-04-07 06:49:41 +00001234 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begeman213541a2008-04-18 23:10:10 +00001235 // For ExtVector, allow vector splats; float -> <n x float>
1236 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnere8b3e962008-01-04 23:32:24 +00001237 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1238 return Compatible;
1239 }
1240
1241 // If LHS and RHS are both vectors of integer or both vectors of floating
1242 // point types, and the total vector length is the same, allow the
1243 // conversion. This is a bitcast; no bits are changed but the result type
1244 // is different.
1245 if (getLangOptions().LaxVectorConversions &&
1246 lhsType->isVectorType() && rhsType->isVectorType()) {
1247 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1248 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner98be4942008-03-05 18:54:05 +00001249 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begeman4119d1a2007-12-30 02:59:45 +00001250 return Compatible;
1251 }
Chris Lattnere8b3e962008-01-04 23:32:24 +00001252 }
1253 return Incompatible;
1254 }
1255
1256 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 return Compatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001258
Chris Lattner78eca282008-04-07 06:49:41 +00001259 if (isa<PointerType>(lhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 if (rhsType->isIntegerType())
Chris Lattnerb7b61152008-01-04 18:22:42 +00001261 return IntToPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262
Chris Lattner78eca282008-04-07 06:49:41 +00001263 if (isa<PointerType>(rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001265 return Incompatible;
1266 }
1267
Chris Lattner78eca282008-04-07 06:49:41 +00001268 if (isa<PointerType>(rhsType)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Chris Lattner78eca282008-04-07 06:49:41 +00001270 if (lhsType->isIntegerType() && lhsType != Context.BoolTy)
Chris Lattnerb7b61152008-01-04 18:22:42 +00001271 return PointerToInt;
Reid Spencer5f016e22007-07-11 17:01:13 +00001272
Chris Lattner78eca282008-04-07 06:49:41 +00001273 if (isa<PointerType>(lhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001274 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnerfc144e22008-01-04 23:18:45 +00001275 return Incompatible;
Chris Lattnerfc144e22008-01-04 23:18:45 +00001276 }
1277
1278 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner78eca282008-04-07 06:49:41 +00001279 if (Context.typesAreCompatible(lhsType, rhsType))
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 return Compatible;
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 }
1282 return Incompatible;
1283}
1284
Chris Lattner5cf216b2008-01-04 18:04:52 +00001285Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001286Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff529a4ad2007-11-27 17:58:44 +00001287 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1288 // a null pointer constant.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001289 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian9d3185e2008-01-03 18:46:52 +00001290 && rExpr->isNullPointerConstant(Context)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001291 ImpCastExprToType(rExpr, lhsType);
Steve Naroff529a4ad2007-11-27 17:58:44 +00001292 return Compatible;
1293 }
Chris Lattner943140e2007-10-16 02:55:40 +00001294 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroff90045e82007-07-13 23:32:42 +00001295 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff08d92e42007-09-15 18:49:24 +00001296 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroff90045e82007-07-13 23:32:42 +00001297 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner943140e2007-10-16 02:55:40 +00001298 //
1299 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1300 // are better understood.
1301 if (!lhsType->isReferenceType())
1302 DefaultFunctionArrayConversion(rExpr);
Steve Narofff1120de2007-08-24 22:33:52 +00001303
Chris Lattner5cf216b2008-01-04 18:04:52 +00001304 Sema::AssignConvertType result =
1305 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Narofff1120de2007-08-24 22:33:52 +00001306
1307 // C99 6.5.16.1p2: The value of the right operand is converted to the
1308 // type of the assignment expression.
1309 if (rExpr->getType() != lhsType)
Chris Lattner1e0a3902008-01-16 19:17:22 +00001310 ImpCastExprToType(rExpr, lhsType);
Steve Narofff1120de2007-08-24 22:33:52 +00001311 return result;
Steve Naroff90045e82007-07-13 23:32:42 +00001312}
1313
Chris Lattner5cf216b2008-01-04 18:04:52 +00001314Sema::AssignConvertType
Steve Naroff90045e82007-07-13 23:32:42 +00001315Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1316 return CheckAssignmentConstraints(lhsType, rhsType);
1317}
1318
Chris Lattnerca5eede2007-12-12 05:47:28 +00001319QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 Diag(loc, diag::err_typecheck_invalid_operands,
1321 lex->getType().getAsString(), rex->getType().getAsString(),
1322 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnerca5eede2007-12-12 05:47:28 +00001323 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001324}
1325
Steve Naroff49b45262007-07-13 16:58:59 +00001326inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1327 Expr *&rex) {
Nate Begeman1330b0e2008-04-04 01:30:25 +00001328 // For conversion purposes, we ignore any qualifiers.
1329 // For example, "const float" and "float" are equivalent.
1330 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1331 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001332
1333 // make sure the vector types are identical.
Nate Begeman1330b0e2008-04-04 01:30:25 +00001334 if (lhsType == rhsType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 return lhsType;
Nate Begeman4119d1a2007-12-30 02:59:45 +00001336
Nate Begeman213541a2008-04-18 23:10:10 +00001337 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begeman4119d1a2007-12-30 02:59:45 +00001338 // promote the rhs to the vector type.
Nate Begeman213541a2008-04-18 23:10:10 +00001339 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001340 if (V->getElementType().getCanonicalType().getTypePtr()
1341 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001342 ImpCastExprToType(rex, lhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001343 return lhsType;
1344 }
1345 }
1346
Nate Begeman213541a2008-04-18 23:10:10 +00001347 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begeman4119d1a2007-12-30 02:59:45 +00001348 // promote the lhs to the vector type.
Nate Begeman213541a2008-04-18 23:10:10 +00001349 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begeman4119d1a2007-12-30 02:59:45 +00001350 if (V->getElementType().getCanonicalType().getTypePtr()
1351 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001352 ImpCastExprToType(lex, rhsType);
Nate Begeman4119d1a2007-12-30 02:59:45 +00001353 return rhsType;
1354 }
1355 }
1356
Reid Spencer5f016e22007-07-11 17:01:13 +00001357 // You cannot convert between vector values of different size.
1358 Diag(loc, diag::err_typecheck_vector_not_convertable,
1359 lex->getType().getAsString(), rex->getType().getAsString(),
1360 lex->getSourceRange(), rex->getSourceRange());
1361 return QualType();
1362}
1363
1364inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001365 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001366{
Steve Naroff90045e82007-07-13 23:32:42 +00001367 QualType lhsType = lex->getType(), rhsType = rex->getType();
1368
1369 if (lhsType->isVectorType() || rhsType->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 return CheckVectorOperands(loc, lex, rex);
Steve Naroff49b45262007-07-13 16:58:59 +00001371
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001372 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001373
Steve Naroffa4332e22007-07-17 00:58:39 +00001374 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001375 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001376 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001377}
1378
1379inline QualType Sema::CheckRemainderOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001380 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001381{
Steve Naroff90045e82007-07-13 23:32:42 +00001382 QualType lhsType = lex->getType(), rhsType = rex->getType();
1383
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001384 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001385
Steve Naroffa4332e22007-07-17 00:58:39 +00001386 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001387 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001388 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001389}
1390
1391inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001392 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001393{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001394 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff49b45262007-07-13 16:58:59 +00001395 return CheckVectorOperands(loc, lex, rex);
1396
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001397 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff3e5e5562007-07-16 22:23:01 +00001398
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 // handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001400 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001401 return compType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001402
Steve Naroffa4332e22007-07-17 00:58:39 +00001403 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1404 return lex->getType();
1405 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1406 return rex->getType();
Chris Lattnerca5eede2007-12-12 05:47:28 +00001407 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001408}
1409
Chris Lattnereca7be62008-04-07 05:30:13 +00001410// C99 6.5.6
1411QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1412 SourceLocation loc, bool isCompAssign) {
Steve Naroff3e5e5562007-07-16 22:23:01 +00001413 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001415
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001416 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001417
Chris Lattner6e4ab612007-12-09 21:53:25 +00001418 // Enforce type constraints: C99 6.5.6p3.
1419
1420 // Handle the common case first (both operands are arithmetic).
Steve Naroffa4332e22007-07-17 00:58:39 +00001421 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001422 return compType;
Chris Lattner6e4ab612007-12-09 21:53:25 +00001423
1424 // Either ptr - int or ptr - ptr.
1425 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff2565eef2008-01-29 18:58:14 +00001426 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001427
Chris Lattner6e4ab612007-12-09 21:53:25 +00001428 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff2565eef2008-01-29 18:58:14 +00001429 if (!lpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001430 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001431 if (lpointee->isVoidType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001432 Diag(loc, diag::ext_gnu_void_ptr,
1433 lex->getSourceRange(), rex->getSourceRange());
1434 } else {
1435 Diag(loc, diag::err_typecheck_sub_ptr_object,
1436 lex->getType().getAsString(), lex->getSourceRange());
1437 return QualType();
1438 }
1439 }
1440
1441 // The result type of a pointer-int computation is the pointer type.
1442 if (rex->getType()->isIntegerType())
1443 return lex->getType();
Steve Naroff3e5e5562007-07-16 22:23:01 +00001444
Chris Lattner6e4ab612007-12-09 21:53:25 +00001445 // Handle pointer-pointer subtractions.
1446 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman8e54ad02008-02-08 01:19:44 +00001447 QualType rpointee = RHSPTy->getPointeeType();
1448
Chris Lattner6e4ab612007-12-09 21:53:25 +00001449 // RHS must be an object type, unless void (GNU).
Steve Naroff2565eef2008-01-29 18:58:14 +00001450 if (!rpointee->isObjectType()) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001451 // Handle the GNU void* extension.
Steve Naroff2565eef2008-01-29 18:58:14 +00001452 if (rpointee->isVoidType()) {
1453 if (!lpointee->isVoidType())
Chris Lattner6e4ab612007-12-09 21:53:25 +00001454 Diag(loc, diag::ext_gnu_void_ptr,
1455 lex->getSourceRange(), rex->getSourceRange());
1456 } else {
1457 Diag(loc, diag::err_typecheck_sub_ptr_object,
1458 rex->getType().getAsString(), rex->getSourceRange());
1459 return QualType();
1460 }
1461 }
1462
1463 // Pointee types must be compatible.
Steve Naroff2565eef2008-01-29 18:58:14 +00001464 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1465 rpointee.getUnqualifiedType())) {
Chris Lattner6e4ab612007-12-09 21:53:25 +00001466 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1467 lex->getType().getAsString(), rex->getType().getAsString(),
1468 lex->getSourceRange(), rex->getSourceRange());
1469 return QualType();
1470 }
1471
1472 return Context.getPointerDiffType();
1473 }
1474 }
1475
Chris Lattnerca5eede2007-12-12 05:47:28 +00001476 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001477}
1478
Chris Lattnereca7be62008-04-07 05:30:13 +00001479// C99 6.5.7
1480QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1481 bool isCompAssign) {
Chris Lattnerca5eede2007-12-12 05:47:28 +00001482 // C99 6.5.7p2: Each of the operands shall have integer type.
1483 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1484 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001485
Chris Lattnerca5eede2007-12-12 05:47:28 +00001486 // Shifts don't perform usual arithmetic conversions, they just do integer
1487 // promotions on each operand. C99 6.5.7p3
Chris Lattner1dcf2c82007-12-13 07:28:16 +00001488 if (!isCompAssign)
1489 UsualUnaryConversions(lex);
Chris Lattnerca5eede2007-12-12 05:47:28 +00001490 UsualUnaryConversions(rex);
1491
1492 // "The type of the result is that of the promoted left operand."
1493 return lex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001494}
1495
Chris Lattnereca7be62008-04-07 05:30:13 +00001496// C99 6.5.8
1497QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1498 bool isRelational) {
Chris Lattnera5937dd2007-08-26 01:18:55 +00001499 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff30bf7712007-08-10 18:26:40 +00001500 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1501 UsualArithmeticConversions(lex, rex);
1502 else {
1503 UsualUnaryConversions(lex);
1504 UsualUnaryConversions(rex);
1505 }
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001506 QualType lType = lex->getType();
1507 QualType rType = rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001508
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001509 // For non-floating point types, check for self-comparisons of the form
1510 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1511 // often indicate logic errors in the program.
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001512 if (!lType->isFloatingType()) {
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001513 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1514 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenek3ca0bf22007-10-29 16:58:49 +00001515 if (DRL->getDecl() == DRR->getDecl())
1516 Diag(loc, diag::warn_selfcomparison);
1517 }
1518
Chris Lattnera5937dd2007-08-26 01:18:55 +00001519 if (isRelational) {
1520 if (lType->isRealType() && rType->isRealType())
1521 return Context.IntTy;
1522 } else {
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001523 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek72cb1ae2007-10-29 17:13:39 +00001524 if (lType->isFloatingType()) {
1525 assert (rType->isFloatingType());
Ted Kremenek588e5eb2007-11-25 00:58:00 +00001526 CheckFloatComparison(loc,lex,rex);
Ted Kremenek6a261552007-10-29 16:40:01 +00001527 }
1528
Chris Lattnera5937dd2007-08-26 01:18:55 +00001529 if (lType->isArithmeticType() && rType->isArithmeticType())
1530 return Context.IntTy;
1531 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001532
Chris Lattnerd28f8152007-08-26 01:10:14 +00001533 bool LHSIsNull = lex->isNullPointerConstant(Context);
1534 bool RHSIsNull = rex->isNullPointerConstant(Context);
1535
Chris Lattnera5937dd2007-08-26 01:18:55 +00001536 // All of the following pointer related warnings are GCC extensions, except
1537 // when handling null pointer constants. One day, we can consider making them
1538 // errors (when -pedantic-errors is enabled).
Steve Naroff77878cc2007-08-27 04:08:11 +00001539 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001540 QualType LCanPointeeTy =
1541 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1542 QualType RCanPointeeTy =
1543 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman8e54ad02008-02-08 01:19:44 +00001544
Steve Naroff66296cb2007-11-13 14:57:38 +00001545 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattnerbc896f52008-04-03 05:07:25 +00001546 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1547 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1548 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001549 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1550 lType.getAsString(), rType.getAsString(),
1551 lex->getSourceRange(), rex->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001552 }
Chris Lattner1e0a3902008-01-16 19:17:22 +00001553 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001554 return Context.IntTy;
1555 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001556 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
Chris Lattnereca7be62008-04-07 05:30:13 +00001557 && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattner1e0a3902008-01-16 19:17:22 +00001558 ImpCastExprToType(rex, lType);
Fariborz Jahanian7359f042007-12-20 01:06:58 +00001559 return Context.IntTy;
1560 }
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001561 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001562 if (!RHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001563 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1564 lType.getAsString(), rType.getAsString(),
1565 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001566 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001567 return Context.IntTy;
1568 }
1569 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattnerd28f8152007-08-26 01:10:14 +00001570 if (!LHSIsNull)
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001571 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1572 lType.getAsString(), rType.getAsString(),
1573 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1e0a3902008-01-16 19:17:22 +00001574 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroffe77fd3c2007-08-16 21:48:38 +00001575 return Context.IntTy;
Reid Spencer5f016e22007-07-11 17:01:13 +00001576 }
Chris Lattnerca5eede2007-12-12 05:47:28 +00001577 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001578}
1579
Reid Spencer5f016e22007-07-11 17:01:13 +00001580inline QualType Sema::CheckBitwiseOperands(
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001581 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Reid Spencer5f016e22007-07-11 17:01:13 +00001582{
Steve Naroff3e5e5562007-07-16 22:23:01 +00001583 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 return CheckVectorOperands(loc, lex, rex);
Steve Naroff90045e82007-07-13 23:32:42 +00001585
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001586 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Reid Spencer5f016e22007-07-11 17:01:13 +00001587
Steve Naroffa4332e22007-07-17 00:58:39 +00001588 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001589 return compType;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001590 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001591}
1592
1593inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff49b45262007-07-13 16:58:59 +00001594 Expr *&lex, Expr *&rex, SourceLocation loc)
Reid Spencer5f016e22007-07-11 17:01:13 +00001595{
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001596 UsualUnaryConversions(lex);
1597 UsualUnaryConversions(rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001598
Steve Naroffa4332e22007-07-17 00:58:39 +00001599 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 return Context.IntTy;
Chris Lattnerca5eede2007-12-12 05:47:28 +00001601 return InvalidOperands(loc, lex, rex);
Reid Spencer5f016e22007-07-11 17:01:13 +00001602}
1603
1604inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Narofff1120de2007-08-24 22:33:52 +00001605 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Reid Spencer5f016e22007-07-11 17:01:13 +00001606{
1607 QualType lhsType = lex->getType();
1608 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1610
1611 switch (mlval) { // C99 6.5.16p2
Chris Lattner5cf216b2008-01-04 18:04:52 +00001612 case Expr::MLV_Valid:
1613 break;
1614 case Expr::MLV_ConstQualified:
1615 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1616 return QualType();
1617 case Expr::MLV_ArrayType:
1618 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1619 lhsType.getAsString(), lex->getSourceRange());
1620 return QualType();
1621 case Expr::MLV_NotObjectType:
1622 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1623 lhsType.getAsString(), lex->getSourceRange());
1624 return QualType();
1625 case Expr::MLV_InvalidExpression:
1626 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1627 lex->getSourceRange());
1628 return QualType();
1629 case Expr::MLV_IncompleteType:
1630 case Expr::MLV_IncompleteVoidType:
1631 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1632 lhsType.getAsString(), lex->getSourceRange());
1633 return QualType();
1634 case Expr::MLV_DuplicateVectorComponents:
1635 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1636 lex->getSourceRange());
1637 return QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 }
Steve Naroffd1861fd2007-07-31 12:34:36 +00001639
Chris Lattner5cf216b2008-01-04 18:04:52 +00001640 AssignConvertType ConvTy;
1641 if (compoundType.isNull())
1642 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1643 else
1644 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1645
1646 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1647 rex, "assigning"))
1648 return QualType();
1649
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 // C99 6.5.16p3: The type of an assignment expression is the type of the
1651 // left operand unless the left operand has qualified type, in which case
1652 // it is the unqualified version of the type of the left operand.
1653 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1654 // is converted to the type of the assignment expression (above).
Chris Lattner73d0d4f2007-08-30 17:45:32 +00001655 // C++ 5.17p1: the type of the assignment expression is that of its left
1656 // oprdu.
Chris Lattner5cf216b2008-01-04 18:04:52 +00001657 return lhsType.getUnqualifiedType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001658}
1659
1660inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff49b45262007-07-13 16:58:59 +00001661 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001662 UsualUnaryConversions(rex);
1663 return rex->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001664}
1665
Steve Naroff49b45262007-07-13 16:58:59 +00001666/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1667/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Reid Spencer5f016e22007-07-11 17:01:13 +00001668QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff49b45262007-07-13 16:58:59 +00001669 QualType resType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 assert(!resType.isNull() && "no type for increment/decrement expression");
1671
Steve Naroff084f9ed2007-08-24 17:20:07 +00001672 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffd848a382007-11-11 14:15:57 +00001673 if (const PointerType *pt = resType->getAsPointerType()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1675 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1676 resType.getAsString(), op->getSourceRange());
1677 return QualType();
1678 }
Steve Naroff084f9ed2007-08-24 17:20:07 +00001679 } else if (!resType->isRealType()) {
1680 if (resType->isComplexType())
1681 // C99 does not support ++/-- on complex types.
1682 Diag(OpLoc, diag::ext_integer_increment_complex,
1683 resType.getAsString(), op->getSourceRange());
1684 else {
1685 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1686 resType.getAsString(), op->getSourceRange());
1687 return QualType();
1688 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 }
Steve Naroffdd10e022007-08-23 21:37:33 +00001690 // At this point, we know we have a real, complex or pointer type.
1691 // Now make sure the operand is a modifiable lvalue.
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1693 if (mlval != Expr::MLV_Valid) {
1694 // FIXME: emit a more precise diagnostic...
1695 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1696 op->getSourceRange());
1697 return QualType();
1698 }
1699 return resType;
1700}
1701
Anders Carlsson369dee42008-02-01 07:15:58 +00001702/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Reid Spencer5f016e22007-07-11 17:01:13 +00001703/// This routine allows us to typecheck complex/recursive expressions
1704/// where the declaration is needed for type checking. Here are some
1705/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattnerf0467b32008-04-02 04:24:33 +00001706static ValueDecl *getPrimaryDecl(Expr *E) {
1707 switch (E->getStmtClass()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 case Stmt::DeclRefExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001709 return cast<DeclRefExpr>(E)->getDecl();
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 case Stmt::MemberExprClass:
Chris Lattnerf82228f2007-11-16 17:46:48 +00001711 // Fields cannot be declared with a 'register' storage class.
1712 // &X->f is always ok, even if X is declared register.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001713 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnerf82228f2007-11-16 17:46:48 +00001714 return 0;
Chris Lattnerf0467b32008-04-02 04:24:33 +00001715 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson369dee42008-02-01 07:15:58 +00001716 case Stmt::ArraySubscriptExprClass: {
1717 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1718
Chris Lattnerf0467b32008-04-02 04:24:33 +00001719 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlssonf2a4b842008-02-01 16:01:31 +00001720 if (!VD || VD->getType()->isPointerType())
Anders Carlsson369dee42008-02-01 07:15:58 +00001721 return 0;
1722 else
1723 return VD;
1724 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 case Stmt::UnaryOperatorClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001726 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001727 case Stmt::ParenExprClass:
Chris Lattnerf0467b32008-04-02 04:24:33 +00001728 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnerf82228f2007-11-16 17:46:48 +00001729 case Stmt::ImplicitCastExprClass:
1730 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattnerf0467b32008-04-02 04:24:33 +00001731 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 default:
1733 return 0;
1734 }
1735}
1736
1737/// CheckAddressOfOperand - The operand of & must be either a function
1738/// designator or an lvalue designating an object. If it is an lvalue, the
1739/// object cannot be declared with storage class register or be a bit field.
1740/// Note: The usual conversions are *not* applied to the operand of the &
1741/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1742QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff08f19672008-01-13 17:10:08 +00001743 if (getLangOptions().C99) {
1744 // Implement C99-only parts of addressof rules.
1745 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1746 if (uOp->getOpcode() == UnaryOperator::Deref)
1747 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1748 // (assuming the deref expression is valid).
1749 return uOp->getSubExpr()->getType();
1750 }
1751 // Technically, there should be a check for array subscript
1752 // expressions here, but the result of one is always an lvalue anyway.
1753 }
Anders Carlsson369dee42008-02-01 07:15:58 +00001754 ValueDecl *dcl = getPrimaryDecl(op);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 Expr::isLvalueResult lval = op->isLvalue();
1756
1757 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnerf82228f2007-11-16 17:46:48 +00001758 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1759 // FIXME: emit more specific diag...
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1761 op->getSourceRange());
1762 return QualType();
1763 }
Steve Naroffbcb2b612008-02-29 23:30:25 +00001764 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1765 if (MemExpr->getMemberDecl()->isBitField()) {
1766 Diag(OpLoc, diag::err_typecheck_address_of,
1767 std::string("bit-field"), op->getSourceRange());
1768 return QualType();
1769 }
1770 // Check for Apple extension for accessing vector components.
1771 } else if (isa<ArraySubscriptExpr>(op) &&
1772 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1773 Diag(OpLoc, diag::err_typecheck_address_of,
1774 std::string("vector"), op->getSourceRange());
1775 return QualType();
1776 } else if (dcl) { // C99 6.5.3.2p1
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 // We have an lvalue with a decl. Make sure the decl is not declared
1778 // with the register storage-class specifier.
1779 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1780 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroffbcb2b612008-02-29 23:30:25 +00001781 Diag(OpLoc, diag::err_typecheck_address_of,
1782 std::string("register variable"), op->getSourceRange());
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 return QualType();
1784 }
1785 } else
1786 assert(0 && "Unknown/unexpected decl type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 }
1788 // If the operand has type "type", the result has type "pointer to type".
1789 return Context.getPointerType(op->getType());
1790}
1791
1792QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001793 UsualUnaryConversions(op);
1794 QualType qType = op->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001795
Chris Lattnerbefee482007-07-31 16:53:04 +00001796 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff08f19672008-01-13 17:10:08 +00001797 // Note that per both C89 and C99, this is always legal, even
1798 // if ptype is an incomplete type or void.
1799 // It would be possible to warn about dereferencing a
1800 // void pointer, but it's completely well-defined,
1801 // and such a warning is unlikely to catch any mistakes.
1802 return PT->getPointeeType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 }
1804 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1805 qType.getAsString(), op->getSourceRange());
1806 return QualType();
1807}
1808
1809static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1810 tok::TokenKind Kind) {
1811 BinaryOperator::Opcode Opc;
1812 switch (Kind) {
1813 default: assert(0 && "Unknown binop!");
1814 case tok::star: Opc = BinaryOperator::Mul; break;
1815 case tok::slash: Opc = BinaryOperator::Div; break;
1816 case tok::percent: Opc = BinaryOperator::Rem; break;
1817 case tok::plus: Opc = BinaryOperator::Add; break;
1818 case tok::minus: Opc = BinaryOperator::Sub; break;
1819 case tok::lessless: Opc = BinaryOperator::Shl; break;
1820 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1821 case tok::lessequal: Opc = BinaryOperator::LE; break;
1822 case tok::less: Opc = BinaryOperator::LT; break;
1823 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1824 case tok::greater: Opc = BinaryOperator::GT; break;
1825 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1826 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1827 case tok::amp: Opc = BinaryOperator::And; break;
1828 case tok::caret: Opc = BinaryOperator::Xor; break;
1829 case tok::pipe: Opc = BinaryOperator::Or; break;
1830 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1831 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1832 case tok::equal: Opc = BinaryOperator::Assign; break;
1833 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1834 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1835 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1836 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1837 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1838 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1839 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1840 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1841 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1842 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1843 case tok::comma: Opc = BinaryOperator::Comma; break;
1844 }
1845 return Opc;
1846}
1847
1848static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1849 tok::TokenKind Kind) {
1850 UnaryOperator::Opcode Opc;
1851 switch (Kind) {
1852 default: assert(0 && "Unknown unary op!");
1853 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1854 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1855 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1856 case tok::star: Opc = UnaryOperator::Deref; break;
1857 case tok::plus: Opc = UnaryOperator::Plus; break;
1858 case tok::minus: Opc = UnaryOperator::Minus; break;
1859 case tok::tilde: Opc = UnaryOperator::Not; break;
1860 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1861 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1862 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1863 case tok::kw___real: Opc = UnaryOperator::Real; break;
1864 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1865 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1866 }
1867 return Opc;
1868}
1869
1870// Binary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001871Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 ExprTy *LHS, ExprTy *RHS) {
1873 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1874 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1875
Steve Narofff69936d2007-09-16 03:34:24 +00001876 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1877 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Reid Spencer5f016e22007-07-11 17:01:13 +00001878
1879 QualType ResultTy; // Result type of the binary operator.
1880 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1881
1882 switch (Opc) {
1883 default:
1884 assert(0 && "Unknown binary expr!");
1885 case BinaryOperator::Assign:
1886 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1887 break;
1888 case BinaryOperator::Mul:
1889 case BinaryOperator::Div:
1890 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1891 break;
1892 case BinaryOperator::Rem:
1893 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1894 break;
1895 case BinaryOperator::Add:
1896 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1897 break;
1898 case BinaryOperator::Sub:
1899 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1900 break;
1901 case BinaryOperator::Shl:
1902 case BinaryOperator::Shr:
1903 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1904 break;
1905 case BinaryOperator::LE:
1906 case BinaryOperator::LT:
1907 case BinaryOperator::GE:
1908 case BinaryOperator::GT:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001909 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001910 break;
1911 case BinaryOperator::EQ:
1912 case BinaryOperator::NE:
Chris Lattnera5937dd2007-08-26 01:18:55 +00001913 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Reid Spencer5f016e22007-07-11 17:01:13 +00001914 break;
1915 case BinaryOperator::And:
1916 case BinaryOperator::Xor:
1917 case BinaryOperator::Or:
1918 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1919 break;
1920 case BinaryOperator::LAnd:
1921 case BinaryOperator::LOr:
1922 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1923 break;
1924 case BinaryOperator::MulAssign:
1925 case BinaryOperator::DivAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001926 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 if (!CompTy.isNull())
1928 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1929 break;
1930 case BinaryOperator::RemAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001931 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001932 if (!CompTy.isNull())
1933 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1934 break;
1935 case BinaryOperator::AddAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001936 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001937 if (!CompTy.isNull())
1938 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1939 break;
1940 case BinaryOperator::SubAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001941 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 if (!CompTy.isNull())
1943 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1944 break;
1945 case BinaryOperator::ShlAssign:
1946 case BinaryOperator::ShrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001947 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001948 if (!CompTy.isNull())
1949 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1950 break;
1951 case BinaryOperator::AndAssign:
1952 case BinaryOperator::XorAssign:
1953 case BinaryOperator::OrAssign:
Steve Naroff9f5fa9b2007-08-24 19:07:16 +00001954 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 if (!CompTy.isNull())
1956 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1957 break;
1958 case BinaryOperator::Comma:
1959 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1960 break;
1961 }
1962 if (ResultTy.isNull())
1963 return true;
1964 if (CompTy.isNull())
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001965 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001966 else
Chris Lattner17d1b2a2007-08-28 18:36:55 +00001967 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001968}
1969
1970// Unary Operators. 'Tok' is the token for the operator.
Steve Narofff69936d2007-09-16 03:34:24 +00001971Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Reid Spencer5f016e22007-07-11 17:01:13 +00001972 ExprTy *input) {
1973 Expr *Input = (Expr*)input;
1974 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1975 QualType resultType;
1976 switch (Opc) {
1977 default:
1978 assert(0 && "Unimplemented unary expr!");
1979 case UnaryOperator::PreInc:
1980 case UnaryOperator::PreDec:
1981 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1982 break;
1983 case UnaryOperator::AddrOf:
1984 resultType = CheckAddressOfOperand(Input, OpLoc);
1985 break;
1986 case UnaryOperator::Deref:
Steve Naroff1ca9b112007-12-18 04:06:57 +00001987 DefaultFunctionArrayConversion(Input);
Reid Spencer5f016e22007-07-11 17:01:13 +00001988 resultType = CheckIndirectionOperand(Input, OpLoc);
1989 break;
1990 case UnaryOperator::Plus:
1991 case UnaryOperator::Minus:
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001992 UsualUnaryConversions(Input);
1993 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1995 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1996 resultType.getAsString());
1997 break;
1998 case UnaryOperator::Not: // bitwise complement
Steve Naroffc80b4ee2007-07-16 21:54:35 +00001999 UsualUnaryConversions(Input);
2000 resultType = Input->getType();
Steve Naroff084f9ed2007-08-24 17:20:07 +00002001 // C99 6.5.3.3p1. We allow complex as a GCC extension.
2002 if (!resultType->isIntegerType()) {
2003 if (resultType->isComplexType())
2004 // C99 does not support '~' for complex conjugation.
2005 Diag(OpLoc, diag::ext_integer_complement_complex,
2006 resultType.getAsString());
2007 else
2008 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2009 resultType.getAsString());
2010 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002011 break;
2012 case UnaryOperator::LNot: // logical negation
2013 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroffc80b4ee2007-07-16 21:54:35 +00002014 DefaultFunctionArrayConversion(Input);
2015 resultType = Input->getType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002016 if (!resultType->isScalarType()) // C99 6.5.3.3p1
2017 return Diag(OpLoc, diag::err_typecheck_unary_expr,
2018 resultType.getAsString());
2019 // LNot always has type int. C99 6.5.3.3p5.
2020 resultType = Context.IntTy;
2021 break;
2022 case UnaryOperator::SizeOf:
2023 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2024 break;
2025 case UnaryOperator::AlignOf:
2026 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2027 break;
Chris Lattnerdbb36972007-08-24 21:16:53 +00002028 case UnaryOperator::Real:
Chris Lattnerdbb36972007-08-24 21:16:53 +00002029 case UnaryOperator::Imag:
Chris Lattner5d794252007-08-24 21:41:10 +00002030 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattnerdbb36972007-08-24 21:16:53 +00002031 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 case UnaryOperator::Extension:
Reid Spencer5f016e22007-07-11 17:01:13 +00002033 resultType = Input->getType();
2034 break;
2035 }
2036 if (resultType.isNull())
2037 return true;
2038 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2039}
2040
Steve Naroff1b273c42007-09-16 14:56:35 +00002041/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2042Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Reid Spencer5f016e22007-07-11 17:01:13 +00002043 SourceLocation LabLoc,
2044 IdentifierInfo *LabelII) {
2045 // Look up the record for this label identifier.
2046 LabelStmt *&LabelDecl = LabelMap[LabelII];
2047
2048 // If we haven't seen this label yet, create a forward reference.
2049 if (LabelDecl == 0)
2050 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2051
2052 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattner6481a572007-08-03 17:31:20 +00002053 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2054 Context.getPointerType(Context.VoidTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00002055}
2056
Steve Naroff1b273c42007-09-16 14:56:35 +00002057Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattnerab18c4c2007-07-24 16:58:17 +00002058 SourceLocation RPLoc) { // "({..})"
2059 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2060 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2061 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2062
2063 // FIXME: there are a variety of strange constraints to enforce here, for
2064 // example, it is not possible to goto into a stmt expression apparently.
2065 // More semantic analysis is needed.
2066
2067 // FIXME: the last statement in the compount stmt has its value used. We
2068 // should not warn about it being unused.
2069
2070 // If there are sub stmts in the compound stmt, take the type of the last one
2071 // as the type of the stmtexpr.
2072 QualType Ty = Context.VoidTy;
2073
2074 if (!Compound->body_empty())
2075 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2076 Ty = LastExpr->getType();
2077
2078 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2079}
Steve Naroffd34e9152007-08-01 22:05:33 +00002080
Steve Naroff1b273c42007-09-16 14:56:35 +00002081Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002082 SourceLocation TypeLoc,
2083 TypeTy *argty,
2084 OffsetOfComponent *CompPtr,
2085 unsigned NumComponents,
2086 SourceLocation RPLoc) {
2087 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2088 assert(!ArgTy.isNull() && "Missing type argument!");
2089
2090 // We must have at least one component that refers to the type, and the first
2091 // one is known to be a field designator. Verify that the ArgTy represents
2092 // a struct/union/class.
2093 if (!ArgTy->isRecordType())
2094 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2095
2096 // Otherwise, create a compound literal expression as the base, and
2097 // iteratively process the offsetof designators.
Steve Naroffe9b12192008-01-14 18:19:28 +00002098 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002099
Chris Lattner9e2b75c2007-08-31 21:49:13 +00002100 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2101 // GCC extension, diagnose them.
2102 if (NumComponents != 1)
2103 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2104 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2105
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002106 for (unsigned i = 0; i != NumComponents; ++i) {
2107 const OffsetOfComponent &OC = CompPtr[i];
2108 if (OC.isBrackets) {
2109 // Offset of an array sub-field. TODO: Should we allow vector elements?
2110 const ArrayType *AT = Res->getType()->getAsArrayType();
2111 if (!AT) {
2112 delete Res;
2113 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2114 Res->getType().getAsString());
2115 }
2116
Chris Lattner704fe352007-08-30 17:59:59 +00002117 // FIXME: C++: Verify that operator[] isn't overloaded.
2118
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002119 // C99 6.5.2.1p1
2120 Expr *Idx = static_cast<Expr*>(OC.U.E);
2121 if (!Idx->getType()->isIntegerType())
2122 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2123 Idx->getSourceRange());
2124
2125 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2126 continue;
2127 }
2128
2129 const RecordType *RC = Res->getType()->getAsRecordType();
2130 if (!RC) {
2131 delete Res;
2132 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2133 Res->getType().getAsString());
2134 }
2135
2136 // Get the decl corresponding to this.
2137 RecordDecl *RD = RC->getDecl();
2138 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2139 if (!MemberDecl)
2140 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2141 OC.U.IdentInfo->getName(),
2142 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner704fe352007-08-30 17:59:59 +00002143
2144 // FIXME: C++: Verify that MemberDecl isn't a static field.
2145 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman51019072008-02-06 22:48:16 +00002146 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2147 // matter here.
2148 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002149 }
2150
2151 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2152 BuiltinLoc);
2153}
2154
2155
Steve Naroff1b273c42007-09-16 14:56:35 +00002156Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroffd34e9152007-08-01 22:05:33 +00002157 TypeTy *arg1, TypeTy *arg2,
2158 SourceLocation RPLoc) {
2159 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2160 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2161
2162 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2163
Chris Lattner73d0d4f2007-08-30 17:45:32 +00002164 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroffd34e9152007-08-01 22:05:33 +00002165}
2166
Steve Naroff1b273c42007-09-16 14:56:35 +00002167Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroffd04fdd52007-08-03 21:21:27 +00002168 ExprTy *expr1, ExprTy *expr2,
2169 SourceLocation RPLoc) {
2170 Expr *CondExpr = static_cast<Expr*>(cond);
2171 Expr *LHSExpr = static_cast<Expr*>(expr1);
2172 Expr *RHSExpr = static_cast<Expr*>(expr2);
2173
2174 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2175
2176 // The conditional expression is required to be a constant expression.
2177 llvm::APSInt condEval(32);
2178 SourceLocation ExpLoc;
2179 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2180 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2181 CondExpr->getSourceRange());
2182
2183 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2184 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2185 RHSExpr->getType();
2186 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2187}
2188
Nate Begeman67295d02008-01-30 20:50:20 +00002189/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begemane2ce1d92008-01-17 17:46:27 +00002190/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begeman67295d02008-01-30 20:50:20 +00002191/// The number of arguments has already been validated to match the number of
2192/// arguments in FnType.
2193static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begemane2ce1d92008-01-17 17:46:27 +00002194 unsigned NumParams = FnType->getNumArgs();
Nate Begemand6595fa2008-04-18 23:35:14 +00002195 for (unsigned i = 0; i != NumParams; ++i) {
2196 QualType ExprTy = Args[i]->getType().getCanonicalType();
2197 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2198
2199 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begemane2ce1d92008-01-17 17:46:27 +00002200 return false;
Nate Begemand6595fa2008-04-18 23:35:14 +00002201 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002202 return true;
2203}
2204
2205Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2206 SourceLocation *CommaLocs,
2207 SourceLocation BuiltinLoc,
2208 SourceLocation RParenLoc) {
Nate Begeman796ef3d2008-01-31 05:38:29 +00002209 // __builtin_overload requires at least 2 arguments
2210 if (NumArgs < 2)
2211 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2212 SourceRange(BuiltinLoc, RParenLoc));
Nate Begemane2ce1d92008-01-17 17:46:27 +00002213
Nate Begemane2ce1d92008-01-17 17:46:27 +00002214 // The first argument is required to be a constant expression. It tells us
2215 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002216 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begemane2ce1d92008-01-17 17:46:27 +00002217 Expr *NParamsExpr = Args[0];
2218 llvm::APSInt constEval(32);
2219 SourceLocation ExpLoc;
2220 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2221 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2222 NParamsExpr->getSourceRange());
2223
2224 // Verify that the number of parameters is > 0
2225 unsigned NumParams = constEval.getZExtValue();
2226 if (NumParams == 0)
2227 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2228 NParamsExpr->getSourceRange());
2229 // Verify that we have at least 1 + NumParams arguments to the builtin.
2230 if ((NumParams + 1) > NumArgs)
2231 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2232 SourceRange(BuiltinLoc, RParenLoc));
2233
2234 // Figure out the return type, by matching the args to one of the functions
Nate Begeman67295d02008-01-30 20:50:20 +00002235 // listed after the parameters.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002236 OverloadExpr *OE = 0;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002237 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2238 // UsualUnaryConversions will convert the function DeclRefExpr into a
2239 // pointer to function.
2240 Expr *Fn = UsualUnaryConversions(Args[i]);
2241 FunctionTypeProto *FnType = 0;
Nate Begeman67295d02008-01-30 20:50:20 +00002242 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2243 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2244 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2245 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002246
2247 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2248 // parameters, and the number of parameters must match the value passed to
2249 // the builtin.
2250 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begeman67295d02008-01-30 20:50:20 +00002251 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2252 Fn->getSourceRange());
Nate Begemane2ce1d92008-01-17 17:46:27 +00002253
2254 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begeman67295d02008-01-30 20:50:20 +00002255 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begemane2ce1d92008-01-17 17:46:27 +00002256 // If they match, return a new OverloadExpr.
Nate Begeman796ef3d2008-01-31 05:38:29 +00002257 if (ExprsMatchFnType(Args+1, FnType)) {
2258 if (OE)
2259 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2260 OE->getFn()->getSourceRange());
2261 // Remember our match, and continue processing the remaining arguments
2262 // to catch any errors.
2263 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2264 BuiltinLoc, RParenLoc);
2265 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002266 }
Nate Begeman796ef3d2008-01-31 05:38:29 +00002267 // Return the newly created OverloadExpr node, if we succeded in matching
2268 // exactly one of the candidate functions.
2269 if (OE)
2270 return OE;
Nate Begemane2ce1d92008-01-17 17:46:27 +00002271
2272 // If we didn't find a matching function Expr in the __builtin_overload list
2273 // the return an error.
2274 std::string typeNames;
Nate Begeman67295d02008-01-30 20:50:20 +00002275 for (unsigned i = 0; i != NumParams; ++i) {
2276 if (i != 0) typeNames += ", ";
2277 typeNames += Args[i+1]->getType().getAsString();
2278 }
Nate Begemane2ce1d92008-01-17 17:46:27 +00002279
2280 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2281 SourceRange(BuiltinLoc, RParenLoc));
2282}
2283
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002284Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2285 ExprTy *expr, TypeTy *type,
Chris Lattner5cf216b2008-01-04 18:04:52 +00002286 SourceLocation RPLoc) {
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002287 Expr *E = static_cast<Expr*>(expr);
2288 QualType T = QualType::getFromOpaquePtr(type);
2289
2290 InitBuiltinVaListType();
2291
Chris Lattner5cf216b2008-01-04 18:04:52 +00002292 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2293 != Compatible)
Anders Carlsson7c50aca2007-10-15 20:28:48 +00002294 return Diag(E->getLocStart(),
2295 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2296 E->getType().getAsString(),
2297 E->getSourceRange());
2298
2299 // FIXME: Warn if a non-POD type is passed in.
2300
2301 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2302}
2303
Chris Lattner5cf216b2008-01-04 18:04:52 +00002304bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2305 SourceLocation Loc,
2306 QualType DstType, QualType SrcType,
2307 Expr *SrcExpr, const char *Flavor) {
2308 // Decode the result (notice that AST's are still created for extensions).
2309 bool isInvalid = false;
2310 unsigned DiagKind;
2311 switch (ConvTy) {
2312 default: assert(0 && "Unknown conversion type");
2313 case Compatible: return false;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002314 case PointerToInt:
Chris Lattner5cf216b2008-01-04 18:04:52 +00002315 DiagKind = diag::ext_typecheck_convert_pointer_int;
2316 break;
Chris Lattnerb7b61152008-01-04 18:22:42 +00002317 case IntToPointer:
2318 DiagKind = diag::ext_typecheck_convert_int_pointer;
2319 break;
Chris Lattner5cf216b2008-01-04 18:04:52 +00002320 case IncompatiblePointer:
2321 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2322 break;
2323 case FunctionVoidPointer:
2324 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2325 break;
2326 case CompatiblePointerDiscardsQualifiers:
2327 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2328 break;
2329 case Incompatible:
2330 DiagKind = diag::err_typecheck_convert_incompatible;
2331 isInvalid = true;
2332 break;
2333 }
2334
2335 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2336 SrcExpr->getSourceRange());
2337 return isInvalid;
2338}
2339
Argyrios Kyrtzidis2d1c5d32008-04-27 13:50:30 +00002340
2341