blob: edd9284a39aa24ebaf7d818476251ee053c3f0c4 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek30c66752007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/AST/ASTContext.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/AST/Expr.h"
Chris Lattner3e254fb2008-04-08 04:40:51 +000018#include "clang/AST/ExprCXX.h"
Steve Naroffc39ca262007-09-18 23:55:05 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000020#include "clang/Lex/Preprocessor.h"
21#include "clang/Lex/LiteralSupport.h"
22#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner83bd5eb2007-12-28 05:29:59 +000024#include "llvm/ADT/OwningPtr.h"
Chris Lattner4b009652007-07-25 00:24:17 +000025#include "llvm/ADT/SmallString.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000026#include "llvm/ADT/StringExtras.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
Steve Naroff87d58b42007-09-16 03:34:24 +000029/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff87d58b42007-09-16 03:34:24 +000036Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnera6dcce32008-02-11 00:02:17 +000046
47 // Verify that pascal strings aren't too large.
Anders Carlsson55bfe0d2007-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()));
Chris Lattner4b009652007-07-25 00:24:17 +000052
Chris Lattnera6dcce32008-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
Chris Lattner4b009652007-07-25 00:24:17 +000064 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
65 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Chris Lattnera6dcce32008-02-11 00:02:17 +000066 Literal.AnyWide, StrTy,
Anders Carlsson55bfe0d2007-10-15 02:50:23 +000067 StringToks[0].getLocation(),
Chris Lattner4b009652007-07-25 00:24:17 +000068 StringToks[NumStringToks-1].getLocation());
69}
70
71
Steve Naroff0acc9c92007-09-15 18:49:24 +000072/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattner4b009652007-07-25 00:24:17 +000073/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
Steve Naroffe50e14c2008-03-19 23:46:26 +000074/// identifier is used in a function call context.
Steve Naroff0acc9c92007-09-15 18:49:24 +000075Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +000076 IdentifierInfo &II,
77 bool HasTrailingLParen) {
Chris Lattnerc72d22d2008-03-31 00:36:02 +000078 // Could be enum-constant, value decl, instance variable, etc.
Steve Naroff6384a012008-04-02 14:35:35 +000079 Decl *D = LookupDecl(&II, Decl::IDNS_Ordinary, S);
Chris Lattnerc72d22d2008-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 Naroffe57c21a2008-04-01 23:04:06 +000084 ScopedDecl *SD = dyn_cast_or_null<ScopedDecl>(D);
Chris Lattnerc72d22d2008-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 Naroffe57c21a2008-04-01 23:04:06 +000090 if (SD == 0 || SD->isDefinedOutsideFunctionOrMethod()) {
Chris Lattnerc72d22d2008-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
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc72d22d2008-03-31 00:36:02 +0000107 !getLangOptions().CPlusPlus) // Not in C++.
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattnerc72d22d2008-03-31 00:36:02 +0000115
Steve Naroff91b03f72007-08-28 03:03:08 +0000116 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Chris Lattneree4c3bf2008-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 Naroffcae537d2007-08-28 18:45:29 +0000121 // Only create DeclRefExpr's for valid Decl's.
Steve Naroffd1ad6ae2007-08-28 20:14:24 +0000122 if (VD->isInvalidDecl())
Steve Naroff91b03f72007-08-28 03:03:08 +0000123 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000124 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff91b03f72007-08-28 03:03:08 +0000125 }
Chris Lattnerc72d22d2008-03-31 00:36:02 +0000126
Chris Lattner4b009652007-07-25 00:24:17 +0000127 if (isa<TypedefDecl>(D))
128 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek42730c52008-01-07 19:49:32 +0000129 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanian3102df92007-12-05 18:16:33 +0000130 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +0000131 if (isa<NamespaceDecl>(D))
132 return Diag(Loc, diag::err_unexpected_namespace, II.getName());
Chris Lattner4b009652007-07-25 00:24:17 +0000133
134 assert(0 && "Invalid decl");
135 abort();
136}
137
Steve Naroff87d58b42007-09-16 03:34:24 +0000138Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Chris Lattner4b009652007-07-25 00:24:17 +0000139 tok::TokenKind Kind) {
140 PreDefinedExpr::IdentType IT;
141
142 switch (Kind) {
Chris Lattnere12ca5d2008-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;
Chris Lattner4b009652007-07-25 00:24:17 +0000147 }
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000148
149 // Verify that this is in a function context.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000150 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000151 return Diag(Loc, diag::err_predef_outside_function);
Chris Lattner4b009652007-07-25 00:24:17 +0000152
Chris Lattner7e637512008-01-12 08:14:25 +0000153 // Pre-defined identifiers are of type char[x], where x is the length of the
154 // string.
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000155 unsigned Length;
156 if (CurFunctionDecl)
157 Length = CurFunctionDecl->getIdentifier()->getLength();
158 else
Fariborz Jahaniandcecd5c2008-01-17 17:37:26 +0000159 Length = CurMethodDecl->getSynthesizedMethodSize();
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000160
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000161 llvm::APInt LengthI(32, Length + 1);
Chris Lattnere12ca5d2008-01-12 18:39:25 +0000162 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattnerfc9511c2008-01-12 19:32:28 +0000163 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattner7e637512008-01-12 08:14:25 +0000164 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattner4b009652007-07-25 00:24:17 +0000165}
166
Steve Naroff87d58b42007-09-16 03:34:24 +0000167Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner6b22fb72008-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());
Chris Lattner4b009652007-07-25 00:24:17 +0000181}
182
Steve Naroff87d58b42007-09-16 03:34:24 +0000183Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner48d7f382008-04-02 04:24:33 +0000187 const char *Ty = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000188
Chris Lattner8cd0e932008-03-05 18:54:05 +0000189 unsigned IntSize =static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner48d7f382008-04-02 04:24:33 +0000190 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *Ty-'0'),
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner1de66eb2007-08-26 03:42:43 +0000205 Expr *Res;
206
207 if (Literal.isFloatingLiteral()) {
Chris Lattner858eece2007-09-22 18:29:59 +0000208 QualType Ty;
209 const llvm::fltSemantics *Format;
Chris Lattner858eece2007-09-22 18:29:59 +0000210
211 if (Literal.isFloat) {
212 Ty = Context.FloatTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000213 Format = Context.Target.getFloatFormat();
214 } else if (!Literal.isLong) {
Chris Lattner858eece2007-09-22 18:29:59 +0000215 Ty = Context.DoubleTy;
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000216 Format = Context.Target.getDoubleFormat();
217 } else {
218 Ty = Context.LongDoubleTy;
219 Format = Context.Target.getLongDoubleFormat();
Chris Lattner858eece2007-09-22 18:29:59 +0000220 }
221
Ted Kremenekddedbe22007-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 Lattner1de66eb2007-08-26 03:42:43 +0000228 } else if (!Literal.isIntegerLiteral()) {
229 return ExprResult(true);
230 } else {
Chris Lattner48d7f382008-04-02 04:24:33 +0000231 QualType Ty;
Chris Lattner4b009652007-07-25 00:24:17 +0000232
Neil Booth7421e9c2007-08-29 22:00:19 +0000233 // long long is a C99 feature.
234 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth9bd47082007-08-29 22:13:52 +0000235 Literal.isLongLong)
Neil Booth7421e9c2007-08-29 22:00:19 +0000236 Diag(Tok.getLocation(), diag::ext_longlong);
237
Chris Lattner4b009652007-07-25 00:24:17 +0000238 // Get the value in the widest-possible width.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000239 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0);
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner48d7f382008-04-02 04:24:33 +0000244 Ty = Context.UnsignedLongLongTy;
245 assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() &&
Chris Lattner8cd0e932008-03-05 18:54:05 +0000246 "long long is not intmax_t?");
Chris Lattner4b009652007-07-25 00:24:17 +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 Lattner98540b62007-08-23 21:58:08 +0000256 if (!Literal.isLong && !Literal.isLongLong) {
257 // Are int/unsigned possibilities?
Chris Lattner8cd0e932008-03-05 18:54:05 +0000258 unsigned IntSize =
259 static_cast<unsigned>(Context.getTypeSize(Context.IntTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000260 // Does it fit in a unsigned int?
261 if (ResultVal.isIntN(IntSize)) {
262 // Does it fit in a signed int?
263 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000264 Ty = Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000265 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000266 Ty = Context.UnsignedIntTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000267 }
268
Chris Lattner48d7f382008-04-02 04:24:33 +0000269 if (!Ty.isNull())
Chris Lattner4b009652007-07-25 00:24:17 +0000270 ResultVal.trunc(IntSize);
271 }
272
273 // Are long/unsigned long possibilities?
Chris Lattner48d7f382008-04-02 04:24:33 +0000274 if (Ty.isNull() && !Literal.isLongLong) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000275 unsigned LongSize =
276 static_cast<unsigned>(Context.getTypeSize(Context.LongTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000277
278 // Does it fit in a unsigned long?
279 if (ResultVal.isIntN(LongSize)) {
280 // Does it fit in a signed long?
281 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000282 Ty = Context.LongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000283 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000284 Ty = Context.UnsignedLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000285 }
Chris Lattner48d7f382008-04-02 04:24:33 +0000286 if (!Ty.isNull())
Chris Lattner4b009652007-07-25 00:24:17 +0000287 ResultVal.trunc(LongSize);
288 }
289
290 // Finally, check long long if needed.
Chris Lattner48d7f382008-04-02 04:24:33 +0000291 if (Ty.isNull()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000292 unsigned LongLongSize =
293 static_cast<unsigned>(Context.getTypeSize(Context.LongLongTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000294
295 // Does it fit in a unsigned long long?
296 if (ResultVal.isIntN(LongLongSize)) {
297 // Does it fit in a signed long long?
298 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
Chris Lattner48d7f382008-04-02 04:24:33 +0000299 Ty = Context.LongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000300 else if (AllowUnsigned)
Chris Lattner48d7f382008-04-02 04:24:33 +0000301 Ty = Context.UnsignedLongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000302 }
303 }
304
305 // If we still couldn't decide a type, we probably have something that
306 // does not fit in a signed long long, but has no U suffix.
Chris Lattner48d7f382008-04-02 04:24:33 +0000307 if (Ty.isNull()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000308 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
Chris Lattner48d7f382008-04-02 04:24:33 +0000309 Ty = Context.UnsignedLongLongTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000310 }
311 }
312
Chris Lattner48d7f382008-04-02 04:24:33 +0000313 Res = new IntegerLiteral(ResultVal, Ty, Tok.getLocation());
Chris Lattner4b009652007-07-25 00:24:17 +0000314 }
Chris Lattner1de66eb2007-08-26 03:42:43 +0000315
316 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
317 if (Literal.isImaginary)
318 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
319
320 return Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000321}
322
Steve Naroff87d58b42007-09-16 03:34:24 +0000323Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattner4b009652007-07-25 00:24:17 +0000324 ExprTy *Val) {
Chris Lattner48d7f382008-04-02 04:24:33 +0000325 Expr *E = (Expr *)Val;
326 assert((E != 0) && "ActOnParenExpr() missing expr");
327 return new ParenExpr(L, R, E);
Chris Lattner4b009652007-07-25 00:24:17 +0000328}
329
330/// The UsualUnaryConversions() function is *not* called by this routine.
331/// See C99 6.3.2.1p[2-4] for more details.
332QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
333 SourceLocation OpLoc, bool isSizeof) {
334 // C99 6.5.3.4p1:
335 if (isa<FunctionType>(exprType) && isSizeof)
336 // alignof(function) is allowed.
337 Diag(OpLoc, diag::ext_sizeof_function_type);
338 else if (exprType->isVoidType())
339 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
340 else if (exprType->isIncompleteType()) {
341 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
342 diag::err_alignof_incomplete_type,
343 exprType.getAsString());
344 return QualType(); // error
345 }
346 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
347 return Context.getSizeType();
348}
349
350Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000351ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Chris Lattner4b009652007-07-25 00:24:17 +0000352 SourceLocation LPLoc, TypeTy *Ty,
353 SourceLocation RPLoc) {
354 // If error parsing type, ignore.
355 if (Ty == 0) return true;
356
357 // Verify that this is a valid expression.
358 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
359
360 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
361
362 if (resultType.isNull())
363 return true;
364 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
365}
366
Chris Lattner5110ad52007-08-24 21:41:10 +0000367QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner03931a72007-08-24 21:16:53 +0000368 DefaultFunctionArrayConversion(V);
369
Chris Lattnera16e42d2007-08-26 05:39:26 +0000370 // These operators return the element type of a complex type.
Chris Lattner03931a72007-08-24 21:16:53 +0000371 if (const ComplexType *CT = V->getType()->getAsComplexType())
372 return CT->getElementType();
Chris Lattnera16e42d2007-08-26 05:39:26 +0000373
374 // Otherwise they pass through real integer and floating point types here.
375 if (V->getType()->isArithmeticType())
376 return V->getType();
377
378 // Reject anything else.
379 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
380 return QualType();
Chris Lattner03931a72007-08-24 21:16:53 +0000381}
382
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384
Steve Naroff87d58b42007-09-16 03:34:24 +0000385Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000386 tok::TokenKind Kind,
387 ExprTy *Input) {
388 UnaryOperator::Opcode Opc;
389 switch (Kind) {
390 default: assert(0 && "Unknown unary op!");
391 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
392 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
393 }
394 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
395 if (result.isNull())
396 return true;
397 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
398}
399
400Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000401ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000402 ExprTy *Idx, SourceLocation RLoc) {
403 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
404
405 // Perform default conversions.
406 DefaultFunctionArrayConversion(LHSExp);
407 DefaultFunctionArrayConversion(RHSExp);
408
409 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
410
411 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000412 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Chris Lattner4b009652007-07-25 00:24:17 +0000413 // in the subscript position. As a result, we need to derive the array base
414 // and index from the expression types.
415 Expr *BaseExpr, *IndexExpr;
416 QualType ResultType;
Chris Lattner7931f4a2007-07-31 16:53:04 +0000417 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000418 BaseExpr = LHSExp;
419 IndexExpr = RHSExp;
420 // FIXME: need to deal with const...
421 ResultType = PTy->getPointeeType();
Chris Lattner7931f4a2007-07-31 16:53:04 +0000422 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Handle the uncommon case of "123[Ptr]".
424 BaseExpr = RHSExp;
425 IndexExpr = LHSExp;
426 // FIXME: need to deal with const...
427 ResultType = PTy->getPointeeType();
Chris Lattnere35a1042007-07-31 19:29:30 +0000428 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
429 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner4b009652007-07-25 00:24:17 +0000430 IndexExpr = RHSExp;
Steve Naroff89345522007-08-03 22:40:33 +0000431
432 // Component access limited to variables (reject vec4.rg[1]).
Nate Begeman506806b2008-02-19 01:11:03 +0000433 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000434 return Diag(LLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000435 SourceRange(LLoc, RLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000436 // FIXME: need to deal with const...
437 ResultType = VTy->getElementType();
438 } else {
439 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
440 RHSExp->getSourceRange());
441 }
442 // C99 6.5.2.1p1
443 if (!IndexExpr->getType()->isIntegerType())
444 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
445 IndexExpr->getSourceRange());
446
447 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
448 // the following check catches trying to index a pointer to a function (e.g.
Chris Lattner9db553e2008-04-02 06:59:01 +0000449 // void (*)(int)) and pointers to incomplete types. Functions are not
450 // objects in C99.
Chris Lattner4b009652007-07-25 00:24:17 +0000451 if (!ResultType->isObjectType())
452 return Diag(BaseExpr->getLocStart(),
453 diag::err_typecheck_subscript_not_object,
454 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
455
456 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
457}
458
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000459QualType Sema::
Nate Begemanaf6ed502008-04-18 23:10:10 +0000460CheckExtVectorComponent(QualType baseType, SourceLocation OpLoc,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000461 IdentifierInfo &CompName, SourceLocation CompLoc) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000462 const ExtVectorType *vecType = baseType->getAsExtVectorType();
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000463
464 // The vector accessor can't exceed the number of elements.
465 const char *compStr = CompName.getName();
466 if (strlen(compStr) > vecType->getNumElements()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000467 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000468 baseType.getAsString(), SourceRange(CompLoc));
469 return QualType();
470 }
471 // The component names must come from the same set.
Chris Lattner9096b792007-08-02 22:33:49 +0000472 if (vecType->getPointAccessorIdx(*compStr) != -1) {
473 do
474 compStr++;
475 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
476 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
477 do
478 compStr++;
479 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
480 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
481 do
482 compStr++;
483 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
484 }
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000485
486 if (*compStr) {
487 // We didn't get to the end of the string. This means the component names
488 // didn't come from the same set *or* we encountered an illegal name.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000489 Diag(OpLoc, diag::err_ext_vector_component_name_illegal,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000490 std::string(compStr,compStr+1), SourceRange(CompLoc));
491 return QualType();
492 }
493 // Each component accessor can't exceed the vector type.
494 compStr = CompName.getName();
495 while (*compStr) {
496 if (vecType->isAccessorWithinNumElements(*compStr))
497 compStr++;
498 else
499 break;
500 }
501 if (*compStr) {
502 // We didn't get to the end of the string. This means a component accessor
503 // exceeds the number of elements in the vector.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000504 Diag(OpLoc, diag::err_ext_vector_component_exceeds_length,
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000505 baseType.getAsString(), SourceRange(CompLoc));
506 return QualType();
507 }
508 // The component accessor looks fine - now we need to compute the actual type.
509 // The vector type is implied by the component accessor. For example,
510 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
511 unsigned CompSize = strlen(CompName.getName());
512 if (CompSize == 1)
513 return vecType->getElementType();
Steve Naroff82113e32007-07-29 16:33:31 +0000514
Nate Begemanaf6ed502008-04-18 23:10:10 +0000515 QualType VT = Context.getExtVectorType(vecType->getElementType(), CompSize);
Steve Naroff82113e32007-07-29 16:33:31 +0000516 // Now look up the TypeDefDecl from the vector type. Without this,
Nate Begemanaf6ed502008-04-18 23:10:10 +0000517 // diagostics look bad. We want extended vector types to appear built-in.
518 for (unsigned i = 0, E = ExtVectorDecls.size(); i != E; ++i) {
519 if (ExtVectorDecls[i]->getUnderlyingType() == VT)
520 return Context.getTypedefType(ExtVectorDecls[i]);
Steve Naroff82113e32007-07-29 16:33:31 +0000521 }
522 return VT; // should never get here (a typedef type should always be found).
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000523}
524
Chris Lattner4b009652007-07-25 00:24:17 +0000525Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000526ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000527 tok::TokenKind OpKind, SourceLocation MemberLoc,
528 IdentifierInfo &Member) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000529 Expr *BaseExpr = static_cast<Expr *>(Base);
530 assert(BaseExpr && "no record expression");
Steve Naroff137e11d2007-12-16 21:42:28 +0000531
532 // Perform default conversions.
533 DefaultFunctionArrayConversion(BaseExpr);
Chris Lattner4b009652007-07-25 00:24:17 +0000534
Steve Naroff2cb66382007-07-26 03:11:44 +0000535 QualType BaseType = BaseExpr->getType();
536 assert(!BaseType.isNull() && "no type for member expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000537
Chris Lattner4b009652007-07-25 00:24:17 +0000538 if (OpKind == tok::arrow) {
Chris Lattner7931f4a2007-07-31 16:53:04 +0000539 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff2cb66382007-07-26 03:11:44 +0000540 BaseType = PT->getPointeeType();
541 else
542 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
543 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000544 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000545 // The base type is either a record or an ExtVectorType.
Chris Lattnere35a1042007-07-31 19:29:30 +0000546 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff2cb66382007-07-26 03:11:44 +0000547 RecordDecl *RDecl = RTy->getDecl();
548 if (RTy->isIncompleteType())
549 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
550 BaseExpr->getSourceRange());
551 // The record definition is complete, now make sure the member is valid.
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000552 FieldDecl *MemberDecl = RDecl->getMember(&Member);
553 if (!MemberDecl)
Steve Naroff2cb66382007-07-26 03:11:44 +0000554 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
555 SourceRange(MemberLoc));
Eli Friedman76b49832008-02-06 22:48:16 +0000556
557 // Figure out the type of the member; see C99 6.5.2.3p3
Eli Friedmanaedabcf2008-02-07 05:24:51 +0000558 // FIXME: Handle address space modifiers
Eli Friedman76b49832008-02-06 22:48:16 +0000559 QualType MemberType = MemberDecl->getType();
560 unsigned combinedQualifiers =
Chris Lattner35fef522008-02-20 20:55:12 +0000561 MemberType.getCVRQualifiers() | BaseType.getCVRQualifiers();
Eli Friedman76b49832008-02-06 22:48:16 +0000562 MemberType = MemberType.getQualifiedType(combinedQualifiers);
563
564 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl,
565 MemberLoc, MemberType);
Nate Begemanaf6ed502008-04-18 23:10:10 +0000566 } else if (BaseType->isExtVectorType() && OpKind == tok::period) {
Steve Naroff89345522007-08-03 22:40:33 +0000567 // Component access limited to variables (reject vec4.rg.g).
Nate Begeman78a2a312008-03-17 17:22:18 +0000568 if (!isa<DeclRefExpr>(BaseExpr) && !isa<ArraySubscriptExpr>(BaseExpr))
Nate Begemanaf6ed502008-04-18 23:10:10 +0000569 return Diag(OpLoc, diag::err_ext_vector_component_access,
Steve Naroff89345522007-08-03 22:40:33 +0000570 SourceRange(MemberLoc));
Nate Begemanaf6ed502008-04-18 23:10:10 +0000571 QualType ret = CheckExtVectorComponent(BaseType, OpLoc, Member, MemberLoc);
Steve Naroff1b8a46c2007-07-27 22:15:19 +0000572 if (ret.isNull())
573 return true;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000574 return new ExtVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +0000575 } else if (BaseType->isObjCInterfaceType()) {
576 ObjCInterfaceDecl *IFace;
577 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
578 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000579 else
Ted Kremenek42730c52008-01-07 19:49:32 +0000580 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
581 ObjCInterfaceDecl *clsDeclared;
582 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian4af72492007-11-12 22:29:28 +0000583 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
584 OpKind==tok::arrow);
585 }
586 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
587 SourceRange(MemberLoc));
Chris Lattner4b009652007-07-25 00:24:17 +0000588}
589
Steve Naroff87d58b42007-09-16 03:34:24 +0000590/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattner4b009652007-07-25 00:24:17 +0000591/// This provides the location of the left/right parens and a list of comma
592/// locations.
593Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000594ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000595 ExprTy **args, unsigned NumArgs,
Chris Lattner4b009652007-07-25 00:24:17 +0000596 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
597 Expr *Fn = static_cast<Expr *>(fn);
598 Expr **Args = reinterpret_cast<Expr**>(args);
599 assert(Fn && "no function call expression");
Chris Lattner3e254fb2008-04-08 04:40:51 +0000600 FunctionDecl *FDecl = NULL;
Chris Lattner3e254fb2008-04-08 04:40:51 +0000601
602 // Promote the function operand.
603 UsualUnaryConversions(Fn);
604
605 // If we're directly calling a function, get the declaration for
606 // that function.
607 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
608 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
609 FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl());
610
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000611 // Make the call expr early, before semantic checks. This guarantees cleanup
612 // of arguments and function on error.
Chris Lattner97316c02008-04-10 02:22:51 +0000613 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000614 Context.BoolTy, RParenLoc));
615
Chris Lattner4b009652007-07-25 00:24:17 +0000616 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
617 // type pointer to function".
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000618 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner4b009652007-07-25 00:24:17 +0000619 if (PT == 0)
620 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
621 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000622 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
623 if (FuncT == 0)
Chris Lattner4b009652007-07-25 00:24:17 +0000624 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
625 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000626
627 // We know the result type of the call, set it.
628 TheCall->setType(FuncT->getResultType());
Chris Lattner4b009652007-07-25 00:24:17 +0000629
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000630 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Chris Lattner4b009652007-07-25 00:24:17 +0000631 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
632 // assignment, to the types of the corresponding parameter, ...
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000633 unsigned NumArgsInProto = Proto->getNumArgs();
634 unsigned NumArgsToCheck = NumArgs;
Chris Lattner4b009652007-07-25 00:24:17 +0000635
Chris Lattner3e254fb2008-04-08 04:40:51 +0000636 // If too few arguments are available (and we don't have default
637 // arguments for the remaining parameters), don't make the call.
638 if (NumArgs < NumArgsInProto) {
Chris Lattner97316c02008-04-10 02:22:51 +0000639 if (FDecl && NumArgs >= FDecl->getMinRequiredArguments()) {
Chris Lattner3e254fb2008-04-08 04:40:51 +0000640 // Use default arguments for missing arguments
641 NumArgsToCheck = NumArgsInProto;
Chris Lattner97316c02008-04-10 02:22:51 +0000642 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000643 } else
644 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
645 Fn->getSourceRange());
646 }
647
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000648 // If too many are passed and not variadic, error on the extras and drop
649 // them.
650 if (NumArgs > NumArgsInProto) {
651 if (!Proto->isVariadic()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000652 Diag(Args[NumArgsInProto]->getLocStart(),
653 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
654 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000655 Args[NumArgs-1]->getLocEnd()));
656 // This deletes the extra arguments.
657 TheCall->setNumArgs(NumArgsInProto);
Chris Lattner4b009652007-07-25 00:24:17 +0000658 }
659 NumArgsToCheck = NumArgsInProto;
660 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000661
Chris Lattner4b009652007-07-25 00:24:17 +0000662 // Continue to check argument types (even if we have too few/many args).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000663 for (unsigned i = 0; i != NumArgsToCheck; i++) {
Chris Lattner005ed752008-01-04 18:04:52 +0000664 QualType ProtoArgType = Proto->getArgType(i);
Chris Lattner3e254fb2008-04-08 04:40:51 +0000665
666 Expr *Arg;
667 if (i < NumArgs)
668 Arg = Args[i];
669 else
670 Arg = new CXXDefaultArgExpr(FDecl->getParamDecl(i));
Chris Lattner005ed752008-01-04 18:04:52 +0000671 QualType ArgType = Arg->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000672
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000673 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner005ed752008-01-04 18:04:52 +0000674 AssignConvertType ConvTy =
675 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000676 TheCall->setArg(i, Arg);
677
Chris Lattner005ed752008-01-04 18:04:52 +0000678 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
679 ArgType, Arg, "passing"))
680 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000681 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000682
683 // If this is a variadic call, handle args passed through "...".
684 if (Proto->isVariadic()) {
Steve Naroffdb65e052007-08-28 23:30:39 +0000685 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000686 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
687 Expr *Arg = Args[i];
688 DefaultArgumentPromotion(Arg);
689 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000690 }
Steve Naroffdb65e052007-08-28 23:30:39 +0000691 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000692 } else {
693 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
694
Steve Naroffdb65e052007-08-28 23:30:39 +0000695 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000696 for (unsigned i = 0; i != NumArgs; i++) {
697 Expr *Arg = Args[i];
698 DefaultArgumentPromotion(Arg);
699 TheCall->setArg(i, Arg);
Steve Naroffdb65e052007-08-28 23:30:39 +0000700 }
Chris Lattner4b009652007-07-25 00:24:17 +0000701 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000702
Chris Lattner2e64c072007-08-10 20:18:51 +0000703 // Do special checking on direct calls to functions.
Chris Lattner3e254fb2008-04-08 04:40:51 +0000704 if (FDecl && CheckFunctionCall(FDecl, TheCall.get()))
705 return true;
Chris Lattner2e64c072007-08-10 20:18:51 +0000706
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000707 return TheCall.take();
Chris Lattner4b009652007-07-25 00:24:17 +0000708}
709
710Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000711ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000712 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000713 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000714 QualType literalType = QualType::getFromOpaquePtr(Ty);
715 // FIXME: put back this assert when initializers are worked out.
Steve Naroff87d58b42007-09-16 03:34:24 +0000716 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Chris Lattner4b009652007-07-25 00:24:17 +0000717 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson9374b852007-12-05 07:24:19 +0000718
Steve Naroffcb69fb72007-12-10 22:44:33 +0000719 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Narofff0b23542008-01-10 22:15:12 +0000720 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff92590f92008-01-09 20:58:06 +0000721 return true;
Steve Naroffbe37fc02008-01-14 18:19:28 +0000722
723 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
724 if (isFileScope) { // 6.5.2.5p3
Steve Narofff0b23542008-01-10 22:15:12 +0000725 if (CheckForConstantInitializer(literalExpr, literalType))
726 return true;
727 }
Steve Naroffbe37fc02008-01-14 18:19:28 +0000728 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Chris Lattner4b009652007-07-25 00:24:17 +0000729}
730
731Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000732ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson762b7c72007-08-31 04:56:16 +0000733 SourceLocation RBraceLoc) {
Steve Naroffe14e5542007-09-02 02:04:30 +0000734 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson762b7c72007-08-31 04:56:16 +0000735
Steve Naroff0acc9c92007-09-15 18:49:24 +0000736 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff1c9de712007-09-03 01:24:23 +0000737 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson762b7c72007-08-31 04:56:16 +0000738
Chris Lattner48d7f382008-04-02 04:24:33 +0000739 InitListExpr *E = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
740 E->setType(Context.VoidTy); // FIXME: just a place holder for now.
741 return E;
Chris Lattner4b009652007-07-25 00:24:17 +0000742}
743
Chris Lattnerd1f26b32007-12-20 00:44:32 +0000744bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000745 assert(VectorTy->isVectorType() && "Not a vector type!");
746
747 if (Ty->isVectorType() || Ty->isIntegerType()) {
Chris Lattner8cd0e932008-03-05 18:54:05 +0000748 if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty))
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000749 return Diag(R.getBegin(),
750 Ty->isVectorType() ?
751 diag::err_invalid_conversion_between_vectors :
752 diag::err_invalid_conversion_between_vector_and_integer,
753 VectorTy.getAsString().c_str(),
754 Ty.getAsString().c_str(), R);
755 } else
756 return Diag(R.getBegin(),
757 diag::err_invalid_conversion_between_vector_and_scalar,
758 VectorTy.getAsString().c_str(),
759 Ty.getAsString().c_str(), R);
760
761 return false;
762}
763
Chris Lattner4b009652007-07-25 00:24:17 +0000764Action::ExprResult Sema::
Steve Naroff87d58b42007-09-16 03:34:24 +0000765ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattner4b009652007-07-25 00:24:17 +0000766 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff87d58b42007-09-16 03:34:24 +0000767 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Chris Lattner4b009652007-07-25 00:24:17 +0000768
769 Expr *castExpr = static_cast<Expr*>(Op);
770 QualType castType = QualType::getFromOpaquePtr(Ty);
771
Steve Naroff68adb482007-08-31 00:32:44 +0000772 UsualUnaryConversions(castExpr);
773
Chris Lattner4b009652007-07-25 00:24:17 +0000774 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
775 // type needs to be scalar.
Chris Lattnerdb526732007-10-29 04:26:44 +0000776 if (!castType->isVoidType()) { // Cast to void allows any expr type.
Steve Narofff459ee52008-01-24 22:55:05 +0000777 if (!castType->isScalarType() && !castType->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000778 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
779 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Steve Narofff459ee52008-01-24 22:55:05 +0000780 if (!castExpr->getType()->isScalarType() &&
781 !castExpr->getType()->isVectorType())
Chris Lattnerdb526732007-10-29 04:26:44 +0000782 return Diag(castExpr->getLocStart(),
783 diag::err_typecheck_expect_scalar_operand,
784 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonf257b4c2007-11-27 05:51:55 +0000785
786 if (castExpr->getType()->isVectorType()) {
787 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
788 castExpr->getType(), castType))
789 return true;
790 } else if (castType->isVectorType()) {
791 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
792 castType, castExpr->getType()))
793 return true;
Chris Lattnerdb526732007-10-29 04:26:44 +0000794 }
Chris Lattner4b009652007-07-25 00:24:17 +0000795 }
796 return new CastExpr(castType, castExpr, LParenLoc);
797}
798
Chris Lattner98a425c2007-11-26 01:40:58 +0000799/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
800/// In that case, lex = cond.
Chris Lattner4b009652007-07-25 00:24:17 +0000801inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
802 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
803 UsualUnaryConversions(cond);
804 UsualUnaryConversions(lex);
805 UsualUnaryConversions(rex);
806 QualType condT = cond->getType();
807 QualType lexT = lex->getType();
808 QualType rexT = rex->getType();
809
810 // first, check the condition.
811 if (!condT->isScalarType()) { // C99 6.5.15p2
812 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
813 condT.getAsString());
814 return QualType();
815 }
Chris Lattner992ae932008-01-06 22:42:25 +0000816
817 // Now check the two expressions.
818
819 // If both operands have arithmetic type, do the usual arithmetic conversions
820 // to find a common type: C99 6.5.15p3,5.
821 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000822 UsualArithmeticConversions(lex, rex);
823 return lex->getType();
824 }
Chris Lattner992ae932008-01-06 22:42:25 +0000825
826 // If both operands are the same structure or union type, the result is that
827 // type.
Chris Lattner71225142007-07-31 21:27:01 +0000828 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattner992ae932008-01-06 22:42:25 +0000829 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner98a425c2007-11-26 01:40:58 +0000830 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattner992ae932008-01-06 22:42:25 +0000831 // "If both the operands have structure or union type, the result has
832 // that type." This implies that CV qualifiers are dropped.
833 return lexT.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000834 }
Chris Lattner992ae932008-01-06 22:42:25 +0000835
836 // C99 6.5.15p5: "If both operands have void type, the result has void type."
837 if (lexT->isVoidType() && rexT->isVoidType())
838 return lexT.getUnqualifiedType();
Steve Naroff12ebf272008-01-08 01:11:38 +0000839
840 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
841 // the type of the other operand."
842 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000843 ImpCastExprToType(rex, lexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000844 return lexT;
845 }
846 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +0000847 ImpCastExprToType(lex, rexT); // promote the null to a pointer.
Steve Naroff12ebf272008-01-08 01:11:38 +0000848 return rexT;
849 }
Chris Lattner0ac51632008-01-06 22:50:31 +0000850 // Handle the case where both operands are pointers before we handle null
851 // pointer constants in case both operands are null pointer constants.
Chris Lattner71225142007-07-31 21:27:01 +0000852 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
853 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
854 // get the "pointed to" types
855 QualType lhptee = LHSPT->getPointeeType();
856 QualType rhptee = RHSPT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +0000857
Chris Lattner71225142007-07-31 21:27:01 +0000858 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
859 if (lhptee->isVoidType() &&
Chris Lattner9db553e2008-04-02 06:59:01 +0000860 rhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000861 // Figure out necessary qualifiers (C99 6.5.15p6)
862 QualType destPointee=lhptee.getQualifiedType(rhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000863 QualType destType = Context.getPointerType(destPointee);
864 ImpCastExprToType(lex, destType); // add qualifiers if necessary
865 ImpCastExprToType(rex, destType); // promote to void*
866 return destType;
867 }
Chris Lattner9db553e2008-04-02 06:59:01 +0000868 if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) {
Chris Lattner35fef522008-02-20 20:55:12 +0000869 QualType destPointee=rhptee.getQualifiedType(lhptee.getCVRQualifiers());
Eli Friedmanca07c902008-02-10 22:59:36 +0000870 QualType destType = Context.getPointerType(destPointee);
871 ImpCastExprToType(lex, destType); // add qualifiers if necessary
872 ImpCastExprToType(rex, destType); // promote to void*
873 return destType;
874 }
Chris Lattner4b009652007-07-25 00:24:17 +0000875
Steve Naroff85f0dc52007-10-15 20:41:53 +0000876 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
877 rhptee.getUnqualifiedType())) {
Steve Naroff232324e2008-02-01 22:44:48 +0000878 Diag(questionLoc, diag::warn_typecheck_cond_incompatible_pointers,
Chris Lattner71225142007-07-31 21:27:01 +0000879 lexT.getAsString(), rexT.getAsString(),
880 lex->getSourceRange(), rex->getSourceRange());
Eli Friedman33284862008-01-30 17:02:03 +0000881 // In this situation, we assume void* type. No especially good
882 // reason, but this is what gcc does, and we do have to pick
883 // to get a consistent AST.
884 QualType voidPtrTy = Context.getPointerType(Context.VoidTy);
885 ImpCastExprToType(lex, voidPtrTy);
886 ImpCastExprToType(rex, voidPtrTy);
887 return voidPtrTy;
Chris Lattner71225142007-07-31 21:27:01 +0000888 }
889 // The pointer types are compatible.
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000890 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
891 // differently qualified versions of compatible types, the result type is
892 // a pointer to an appropriately qualified version of the *composite*
893 // type.
Chris Lattner0ac51632008-01-06 22:50:31 +0000894 // FIXME: Need to return the composite type.
Eli Friedmanca07c902008-02-10 22:59:36 +0000895 // FIXME: Need to add qualifiers
Chris Lattner0ac51632008-01-06 22:50:31 +0000896 return lexT;
Chris Lattner4b009652007-07-25 00:24:17 +0000897 }
Chris Lattner4b009652007-07-25 00:24:17 +0000898 }
Chris Lattner71225142007-07-31 21:27:01 +0000899
Chris Lattner992ae932008-01-06 22:42:25 +0000900 // Otherwise, the operands are not compatible.
Chris Lattner4b009652007-07-25 00:24:17 +0000901 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
902 lexT.getAsString(), rexT.getAsString(),
903 lex->getSourceRange(), rex->getSourceRange());
904 return QualType();
905}
906
Steve Naroff87d58b42007-09-16 03:34:24 +0000907/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattner4b009652007-07-25 00:24:17 +0000908/// in the case of a the GNU conditional expr extension.
Steve Naroff87d58b42007-09-16 03:34:24 +0000909Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattner4b009652007-07-25 00:24:17 +0000910 SourceLocation ColonLoc,
911 ExprTy *Cond, ExprTy *LHS,
912 ExprTy *RHS) {
913 Expr *CondExpr = (Expr *) Cond;
914 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner98a425c2007-11-26 01:40:58 +0000915
916 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
917 // was the condition.
918 bool isLHSNull = LHSExpr == 0;
919 if (isLHSNull)
920 LHSExpr = CondExpr;
921
Chris Lattner4b009652007-07-25 00:24:17 +0000922 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
923 RHSExpr, QuestionLoc);
924 if (result.isNull())
925 return true;
Chris Lattner98a425c2007-11-26 01:40:58 +0000926 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
927 RHSExpr, result);
Chris Lattner4b009652007-07-25 00:24:17 +0000928}
929
Steve Naroffdb65e052007-08-28 23:30:39 +0000930/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
Steve Naroffbbaed752008-01-29 02:42:22 +0000931/// do not have a prototype. Arguments that have type float are promoted to
932/// double. All other argument types are converted by UsualUnaryConversions().
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000933void Sema::DefaultArgumentPromotion(Expr *&Expr) {
934 QualType Ty = Expr->getType();
935 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroffdb65e052007-08-28 23:30:39 +0000936
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000937 if (Ty == Context.FloatTy)
Chris Lattnere992d6c2008-01-16 19:17:22 +0000938 ImpCastExprToType(Expr, Context.DoubleTy);
Steve Naroffbbaed752008-01-29 02:42:22 +0000939 else
940 UsualUnaryConversions(Expr);
Steve Naroffdb65e052007-08-28 23:30:39 +0000941}
942
Chris Lattner4b009652007-07-25 00:24:17 +0000943/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Chris Lattner48d7f382008-04-02 04:24:33 +0000944void Sema::DefaultFunctionArrayConversion(Expr *&E) {
945 QualType Ty = E->getType();
946 assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000947
Chris Lattner48d7f382008-04-02 04:24:33 +0000948 if (const ReferenceType *ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +0000949 ImpCastExprToType(E, ref->getPointeeType()); // C++ [expr]
Chris Lattner48d7f382008-04-02 04:24:33 +0000950 Ty = E->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000951 }
Chris Lattner48d7f382008-04-02 04:24:33 +0000952 if (Ty->isFunctionType())
953 ImpCastExprToType(E, Context.getPointerType(Ty));
Chris Lattner19eb97e2008-04-02 05:18:44 +0000954 else if (Ty->isArrayType())
955 ImpCastExprToType(E, Context.getArrayDecayedType(Ty));
Chris Lattner4b009652007-07-25 00:24:17 +0000956}
957
Nate Begeman9f3bfb72008-01-17 17:46:27 +0000958/// UsualUnaryConversions - Performs various conversions that are common to most
Chris Lattner4b009652007-07-25 00:24:17 +0000959/// operators (C99 6.3). The conversions of array and function types are
960/// sometimes surpressed. For example, the array->pointer conversion doesn't
961/// apply if the array is an argument to the sizeof or address (&) operators.
962/// In these instances, this routine should *not* be called.
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000963Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
964 QualType Ty = Expr->getType();
965 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Chris Lattner4b009652007-07-25 00:24:17 +0000966
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000967 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
Chris Lattnera05f7d22008-04-02 17:45:06 +0000968 ImpCastExprToType(Expr, Ref->getPointeeType()); // C++ [expr]
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000969 Ty = Expr->getType();
Chris Lattner4b009652007-07-25 00:24:17 +0000970 }
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000971 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
Chris Lattnere992d6c2008-01-16 19:17:22 +0000972 ImpCastExprToType(Expr, Context.IntTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000973 else
Chris Lattner83bd5eb2007-12-28 05:29:59 +0000974 DefaultFunctionArrayConversion(Expr);
975
976 return Expr;
Chris Lattner4b009652007-07-25 00:24:17 +0000977}
978
Chris Lattner0d9bcea2007-08-30 17:45:32 +0000979/// UsualArithmeticConversions - Performs various conversions that are common to
Chris Lattner4b009652007-07-25 00:24:17 +0000980/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
981/// routine returns the first non-arithmetic type found. The client is
982/// responsible for emitting appropriate error diagnostics.
Chris Lattner48d7f382008-04-02 04:24:33 +0000983/// FIXME: verify the conversion rules for "complex int" are consistent with
984/// GCC.
Steve Naroff8f708362007-08-24 19:07:16 +0000985QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
986 bool isCompAssign) {
Steve Naroffb2f9f552007-08-25 19:54:59 +0000987 if (!isCompAssign) {
988 UsualUnaryConversions(lhsExpr);
989 UsualUnaryConversions(rhsExpr);
990 }
Steve Naroff7438fdf2007-10-18 18:55:53 +0000991 // For conversion purposes, we ignore any qualifiers.
992 // For example, "const float" and "float" are equivalent.
Steve Naroff1ddb6f52007-11-10 19:45:54 +0000993 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
994 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +0000995
996 // If both types are identical, no conversion is needed.
Steve Naroff7438fdf2007-10-18 18:55:53 +0000997 if (lhs == rhs)
998 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +0000999
1000 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
1001 // The caller can deal with this (e.g. pointer + int).
1002 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001003 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001004
1005 // At this point, we have two different arithmetic types.
1006
1007 // Handle complex types first (C99 6.3.1.8p1).
1008 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff43001212008-01-15 19:36:10 +00001009 // if we have an integer operand, the result is the complex type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001010 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001011 // convert the rhs to the lhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001012 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001013 return lhs;
Steve Naroff43001212008-01-15 19:36:10 +00001014 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001015 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001016 // convert the lhs to the rhs complex type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001017 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001018 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001019 }
Steve Naroff3cf497f2007-08-27 01:27:54 +00001020 // This handles complex/complex, complex/float, or float/complex.
1021 // When both operands are complex, the shorter operand is converted to the
1022 // type of the longer, and that is the type of the result. This corresponds
1023 // to what is done when combining two real floating-point operands.
1024 // The fun begins when size promotion occur across type domains.
1025 // From H&S 6.3.4: When one operand is complex and the other is a real
1026 // floating-point type, the less precise type is converted, within it's
1027 // real or complex domain, to the precision of the other type. For example,
1028 // when combining a "long double" with a "double _Complex", the
1029 // "double _Complex" is promoted to "long double _Complex".
Chris Lattnerd7135b42008-04-06 23:38:49 +00001030 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001031
1032 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroff3b565d62007-08-27 21:32:55 +00001033 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
1034 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001035 ImpCastExprToType(rhsExpr, rhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001036 } else if (result < 0) { // The right side is bigger, convert lhs.
1037 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
1038 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001039 ImpCastExprToType(lhsExpr, lhs);
Steve Naroff3b565d62007-08-27 21:32:55 +00001040 }
1041 // At this point, lhs and rhs have the same rank/size. Now, make sure the
1042 // domains match. This is a requirement for our implementation, C99
1043 // does not require this promotion.
1044 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
1045 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001046 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001047 ImpCastExprToType(lhsExpr, rhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001048 return rhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001049 } else { // handle "_Complex double, double".
Steve Naroff3b6157f2007-08-27 21:43:43 +00001050 if (!isCompAssign)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001051 ImpCastExprToType(rhsExpr, lhs);
Steve Naroff3b6157f2007-08-27 21:43:43 +00001052 return lhs;
Steve Naroff3b565d62007-08-27 21:32:55 +00001053 }
Chris Lattner4b009652007-07-25 00:24:17 +00001054 }
Steve Naroff3b6157f2007-08-27 21:43:43 +00001055 return lhs; // The domain/size match exactly.
Chris Lattner4b009652007-07-25 00:24:17 +00001056 }
1057 // Now handle "real" floating types (i.e. float, double, long double).
1058 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1059 // if we have an integer operand, the result is the real floating type.
Steve Naroffe8419ca2008-01-15 22:21:49 +00001060 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001061 // convert rhs to the lhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001062 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001063 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001064 }
Steve Naroffe8419ca2008-01-15 22:21:49 +00001065 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001066 // convert lhs to the rhs floating point type.
Chris Lattnere992d6c2008-01-16 19:17:22 +00001067 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001068 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001069 }
1070 // We have two real floating types, float/complex combos were handled above.
1071 // Convert the smaller operand to the bigger result.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001072 int result = Context.getFloatingTypeOrder(lhs, rhs);
Steve Naroff45fc9822007-08-27 15:30:22 +00001073
1074 if (result > 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001075 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001076 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001077 }
Steve Naroff45fc9822007-08-27 15:30:22 +00001078 if (result < 0) { // convert the lhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001079 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff45fc9822007-08-27 15:30:22 +00001080 return rhs;
1081 }
1082 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Chris Lattner4b009652007-07-25 00:24:17 +00001083 }
Steve Naroff43001212008-01-15 19:36:10 +00001084 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1085 // Handle GCC complex int extension.
Steve Naroff43001212008-01-15 19:36:10 +00001086 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
Eli Friedman50727042008-02-08 01:19:44 +00001087 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
Steve Naroff43001212008-01-15 19:36:10 +00001088
Eli Friedman50727042008-02-08 01:19:44 +00001089 if (lhsComplexInt && rhsComplexInt) {
Chris Lattner51285d82008-04-06 23:55:33 +00001090 if (Context.getIntegerTypeOrder(lhsComplexInt->getElementType(),
1091 rhsComplexInt->getElementType()) >= 0) {
Eli Friedman94075c02008-02-08 01:24:30 +00001092 // convert the rhs
1093 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1094 return lhs;
Eli Friedman50727042008-02-08 01:19:44 +00001095 }
1096 if (!isCompAssign)
Eli Friedman94075c02008-02-08 01:24:30 +00001097 ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Eli Friedman50727042008-02-08 01:19:44 +00001098 return rhs;
1099 } else if (lhsComplexInt && rhs->isIntegerType()) {
1100 // convert the rhs to the lhs complex type.
1101 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
1102 return lhs;
1103 } else if (rhsComplexInt && lhs->isIntegerType()) {
1104 // convert the lhs to the rhs complex type.
1105 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs);
1106 return rhs;
1107 }
Steve Naroff43001212008-01-15 19:36:10 +00001108 }
Chris Lattner4b009652007-07-25 00:24:17 +00001109 // Finally, we have two differing integer types.
Chris Lattner51285d82008-04-06 23:55:33 +00001110 if (Context.getIntegerTypeOrder(lhs, rhs) >= 0) { // convert the rhs
Chris Lattnere992d6c2008-01-16 19:17:22 +00001111 if (!isCompAssign) ImpCastExprToType(rhsExpr, lhs);
Steve Naroff8f708362007-08-24 19:07:16 +00001112 return lhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001113 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001114 if (!isCompAssign) ImpCastExprToType(lhsExpr, rhs); // convert the lhs
Steve Naroff8f708362007-08-24 19:07:16 +00001115 return rhs;
Chris Lattner4b009652007-07-25 00:24:17 +00001116}
1117
1118// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1119// being closely modeled after the C99 spec:-). The odd characteristic of this
1120// routine is it effectively iqnores the qualifiers on the top level pointee.
1121// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1122// FIXME: add a couple examples in this comment.
Chris Lattner005ed752008-01-04 18:04:52 +00001123Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001124Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
1125 QualType lhptee, rhptee;
1126
1127 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattner71225142007-07-31 21:27:01 +00001128 lhptee = lhsType->getAsPointerType()->getPointeeType();
1129 rhptee = rhsType->getAsPointerType()->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001130
1131 // make sure we operate on the canonical type
1132 lhptee = lhptee.getCanonicalType();
1133 rhptee = rhptee.getCanonicalType();
1134
Chris Lattner005ed752008-01-04 18:04:52 +00001135 AssignConvertType ConvTy = Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001136
1137 // C99 6.5.16.1p1: This following citation is common to constraints
1138 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1139 // qualifiers of the type *pointed to* by the right;
Chris Lattner35fef522008-02-20 20:55:12 +00001140 // FIXME: Handle ASQualType
1141 if ((lhptee.getCVRQualifiers() & rhptee.getCVRQualifiers()) !=
1142 rhptee.getCVRQualifiers())
Chris Lattner005ed752008-01-04 18:04:52 +00001143 ConvTy = CompatiblePointerDiscardsQualifiers;
Chris Lattner4b009652007-07-25 00:24:17 +00001144
1145 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1146 // incomplete type and the other is a pointer to a qualified or unqualified
1147 // version of void...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001148 if (lhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001149 if (rhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001150 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001151
1152 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001153 assert(rhptee->isFunctionType());
1154 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001155 }
1156
1157 if (rhptee->isVoidType()) {
Chris Lattner9db553e2008-04-02 06:59:01 +00001158 if (lhptee->isIncompleteOrObjectType())
Chris Lattner005ed752008-01-04 18:04:52 +00001159 return ConvTy;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001160
1161 // As an extension, we allow cast to/from void* to function pointer.
Chris Lattner9db553e2008-04-02 06:59:01 +00001162 assert(lhptee->isFunctionType());
1163 return FunctionVoidPointer;
Chris Lattner4ca3d772008-01-03 22:56:36 +00001164 }
1165
Chris Lattner4b009652007-07-25 00:24:17 +00001166 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1167 // unqualified versions of compatible types, ...
Chris Lattner4ca3d772008-01-03 22:56:36 +00001168 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1169 rhptee.getUnqualifiedType()))
1170 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner005ed752008-01-04 18:04:52 +00001171 return ConvTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001172}
1173
1174/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
1175/// has code to accommodate several GCC extensions when type checking
1176/// pointers. Here are some objectionable examples that GCC considers warnings:
1177///
1178/// int a, *pint;
1179/// short *pshort;
1180/// struct foo *pfoo;
1181///
1182/// pint = pshort; // warning: assignment from incompatible pointer type
1183/// a = pint; // warning: assignment makes integer from pointer without a cast
1184/// pint = a; // warning: assignment makes pointer from integer without a cast
1185/// pint = pfoo; // warning: assignment from incompatible pointer type
1186///
1187/// As a result, the code for dealing with pointers is more complex than the
1188/// C99 spec dictates.
Chris Lattner4b009652007-07-25 00:24:17 +00001189///
Chris Lattner005ed752008-01-04 18:04:52 +00001190Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001191Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattner1853da22008-01-04 23:18:45 +00001192 // Get canonical types. We're not formatting these types, just comparing
1193 // them.
1194 lhsType = lhsType.getCanonicalType();
1195 rhsType = rhsType.getCanonicalType();
1196
1197 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerfdd96d72008-01-07 17:51:46 +00001198 return Compatible; // Common case: fast path an exact match.
Chris Lattner4b009652007-07-25 00:24:17 +00001199
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001200 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Chris Lattnere1577e22008-04-07 06:52:53 +00001201 if (Context.typesAreCompatible(lhsType, rhsType))
Anders Carlssoncebb8d62007-10-12 23:56:29 +00001202 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001203 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001204 }
Chris Lattner1853da22008-01-04 23:18:45 +00001205
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001206 if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) {
1207 if (ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType, false))
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001208 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001209 return Incompatible;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001210 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001211
Chris Lattner390564e2008-04-07 06:49:41 +00001212 if (isa<VectorType>(lhsType) || isa<VectorType>(rhsType)) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001213 // For ExtVector, allow vector splats; float -> <n x float>
1214 if (const ExtVectorType *LV = dyn_cast<ExtVectorType>(lhsType)) {
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001215 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1216 return Compatible;
1217 }
1218
1219 // If LHS and RHS are both vectors of integer or both vectors of floating
1220 // point types, and the total vector length is the same, allow the
1221 // conversion. This is a bitcast; no bits are changed but the result type
1222 // is different.
1223 if (getLangOptions().LaxVectorConversions &&
1224 lhsType->isVectorType() && rhsType->isVectorType()) {
1225 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1226 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001227 if (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))
Nate Begemanec2d1062007-12-30 02:59:45 +00001228 return Compatible;
1229 }
Chris Lattnerdb22bf42008-01-04 23:32:24 +00001230 }
1231 return Incompatible;
1232 }
1233
1234 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Chris Lattner4b009652007-07-25 00:24:17 +00001235 return Compatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001236
Chris Lattner390564e2008-04-07 06:49:41 +00001237 if (isa<PointerType>(lhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001238 if (rhsType->isIntegerType())
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001239 return IntToPointer;
Chris Lattner4b009652007-07-25 00:24:17 +00001240
Chris Lattner390564e2008-04-07 06:49:41 +00001241 if (isa<PointerType>(rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001242 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001243 return Incompatible;
1244 }
1245
Chris Lattner390564e2008-04-07 06:49:41 +00001246 if (isa<PointerType>(rhsType)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001247 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
Chris Lattner390564e2008-04-07 06:49:41 +00001248 if (lhsType->isIntegerType() && lhsType != Context.BoolTy)
Chris Lattnerd951b7b2008-01-04 18:22:42 +00001249 return PointerToInt;
Chris Lattner4b009652007-07-25 00:24:17 +00001250
Chris Lattner390564e2008-04-07 06:49:41 +00001251 if (isa<PointerType>(lhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001252 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattner1853da22008-01-04 23:18:45 +00001253 return Incompatible;
Chris Lattner1853da22008-01-04 23:18:45 +00001254 }
1255
1256 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Chris Lattner390564e2008-04-07 06:49:41 +00001257 if (Context.typesAreCompatible(lhsType, rhsType))
Chris Lattner4b009652007-07-25 00:24:17 +00001258 return Compatible;
Chris Lattner4b009652007-07-25 00:24:17 +00001259 }
1260 return Incompatible;
1261}
1262
Chris Lattner005ed752008-01-04 18:04:52 +00001263Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001264Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroffcdee22d2007-11-27 17:58:44 +00001265 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1266 // a null pointer constant.
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahaniana13effb2008-01-03 18:46:52 +00001268 && rExpr->isNullPointerConstant(Context)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001269 ImpCastExprToType(rExpr, lhsType);
Steve Naroffcdee22d2007-11-27 17:58:44 +00001270 return Compatible;
1271 }
Chris Lattner5f505bf2007-10-16 02:55:40 +00001272 // This check seems unnatural, however it is necessary to ensure the proper
Chris Lattner4b009652007-07-25 00:24:17 +00001273 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff0acc9c92007-09-15 18:49:24 +00001274 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Chris Lattner4b009652007-07-25 00:24:17 +00001275 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattner5f505bf2007-10-16 02:55:40 +00001276 //
1277 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1278 // are better understood.
1279 if (!lhsType->isReferenceType())
1280 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0f32f432007-08-24 22:33:52 +00001281
Chris Lattner005ed752008-01-04 18:04:52 +00001282 Sema::AssignConvertType result =
1283 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0f32f432007-08-24 22:33:52 +00001284
1285 // C99 6.5.16.1p2: The value of the right operand is converted to the
1286 // type of the assignment expression.
1287 if (rExpr->getType() != lhsType)
Chris Lattnere992d6c2008-01-16 19:17:22 +00001288 ImpCastExprToType(rExpr, lhsType);
Steve Naroff0f32f432007-08-24 22:33:52 +00001289 return result;
Chris Lattner4b009652007-07-25 00:24:17 +00001290}
1291
Chris Lattner005ed752008-01-04 18:04:52 +00001292Sema::AssignConvertType
Chris Lattner4b009652007-07-25 00:24:17 +00001293Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1294 return CheckAssignmentConstraints(lhsType, rhsType);
1295}
1296
Chris Lattner2c8bff72007-12-12 05:47:28 +00001297QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Chris Lattner4b009652007-07-25 00:24:17 +00001298 Diag(loc, diag::err_typecheck_invalid_operands,
1299 lex->getType().getAsString(), rex->getType().getAsString(),
1300 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner2c8bff72007-12-12 05:47:28 +00001301 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001302}
1303
1304inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1305 Expr *&rex) {
Nate Begeman03105572008-04-04 01:30:25 +00001306 // For conversion purposes, we ignore any qualifiers.
1307 // For example, "const float" and "float" are equivalent.
1308 QualType lhsType = lex->getType().getCanonicalType().getUnqualifiedType();
1309 QualType rhsType = rex->getType().getCanonicalType().getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001310
1311 // make sure the vector types are identical.
Nate Begeman03105572008-04-04 01:30:25 +00001312 if (lhsType == rhsType)
Chris Lattner4b009652007-07-25 00:24:17 +00001313 return lhsType;
Nate Begemanec2d1062007-12-30 02:59:45 +00001314
Nate Begemanaf6ed502008-04-18 23:10:10 +00001315 // if the lhs is an extended vector and the rhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001316 // promote the rhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001317 if (const ExtVectorType *V = lhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001318 if (V->getElementType().getCanonicalType().getTypePtr()
1319 == rhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001320 ImpCastExprToType(rex, lhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001321 return lhsType;
1322 }
1323 }
1324
Nate Begemanaf6ed502008-04-18 23:10:10 +00001325 // if the rhs is an extended vector and the lhs is a scalar of the same type,
Nate Begemanec2d1062007-12-30 02:59:45 +00001326 // promote the lhs to the vector type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001327 if (const ExtVectorType *V = rhsType->getAsExtVectorType()) {
Nate Begemanec2d1062007-12-30 02:59:45 +00001328 if (V->getElementType().getCanonicalType().getTypePtr()
1329 == lhsType.getCanonicalType().getTypePtr()) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001330 ImpCastExprToType(lex, rhsType);
Nate Begemanec2d1062007-12-30 02:59:45 +00001331 return rhsType;
1332 }
1333 }
1334
Chris Lattner4b009652007-07-25 00:24:17 +00001335 // You cannot convert between vector values of different size.
1336 Diag(loc, diag::err_typecheck_vector_not_convertable,
1337 lex->getType().getAsString(), rex->getType().getAsString(),
1338 lex->getSourceRange(), rex->getSourceRange());
1339 return QualType();
1340}
1341
1342inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001343 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001344{
1345 QualType lhsType = lex->getType(), rhsType = rex->getType();
1346
1347 if (lhsType->isVectorType() || rhsType->isVectorType())
1348 return CheckVectorOperands(loc, lex, rex);
1349
Steve Naroff8f708362007-08-24 19:07:16 +00001350 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001351
Chris Lattner4b009652007-07-25 00:24:17 +00001352 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001353 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001354 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001355}
1356
1357inline QualType Sema::CheckRemainderOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001358 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001359{
1360 QualType lhsType = lex->getType(), rhsType = rex->getType();
1361
Steve Naroff8f708362007-08-24 19:07:16 +00001362 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001363
Chris Lattner4b009652007-07-25 00:24:17 +00001364 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001365 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001366 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001367}
1368
1369inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroff8f708362007-08-24 19:07:16 +00001370 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001371{
1372 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1373 return CheckVectorOperands(loc, lex, rex);
1374
Steve Naroff8f708362007-08-24 19:07:16 +00001375 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001376
1377 // handle the common case first (both operands are arithmetic).
1378 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001379 return compType;
Chris Lattner4b009652007-07-25 00:24:17 +00001380
1381 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1382 return lex->getType();
1383 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1384 return rex->getType();
Chris Lattner2c8bff72007-12-12 05:47:28 +00001385 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001386}
1387
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001388// C99 6.5.6
1389QualType Sema::CheckSubtractionOperands(Expr *&lex, Expr *&rex,
1390 SourceLocation loc, bool isCompAssign) {
Chris Lattner4b009652007-07-25 00:24:17 +00001391 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1392 return CheckVectorOperands(loc, lex, rex);
1393
Steve Naroff8f708362007-08-24 19:07:16 +00001394 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001395
Chris Lattnerf6da2912007-12-09 21:53:25 +00001396 // Enforce type constraints: C99 6.5.6p3.
1397
1398 // Handle the common case first (both operands are arithmetic).
Chris Lattner4b009652007-07-25 00:24:17 +00001399 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroff8f708362007-08-24 19:07:16 +00001400 return compType;
Chris Lattnerf6da2912007-12-09 21:53:25 +00001401
1402 // Either ptr - int or ptr - ptr.
1403 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
Steve Naroff577f9722008-01-29 18:58:14 +00001404 QualType lpointee = LHSPTy->getPointeeType();
Eli Friedman50727042008-02-08 01:19:44 +00001405
Chris Lattnerf6da2912007-12-09 21:53:25 +00001406 // The LHS must be an object type, not incomplete, function, etc.
Steve Naroff577f9722008-01-29 18:58:14 +00001407 if (!lpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001408 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001409 if (lpointee->isVoidType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001410 Diag(loc, diag::ext_gnu_void_ptr,
1411 lex->getSourceRange(), rex->getSourceRange());
1412 } else {
1413 Diag(loc, diag::err_typecheck_sub_ptr_object,
1414 lex->getType().getAsString(), lex->getSourceRange());
1415 return QualType();
1416 }
1417 }
1418
1419 // The result type of a pointer-int computation is the pointer type.
1420 if (rex->getType()->isIntegerType())
1421 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001422
Chris Lattnerf6da2912007-12-09 21:53:25 +00001423 // Handle pointer-pointer subtractions.
1424 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
Eli Friedman50727042008-02-08 01:19:44 +00001425 QualType rpointee = RHSPTy->getPointeeType();
1426
Chris Lattnerf6da2912007-12-09 21:53:25 +00001427 // RHS must be an object type, unless void (GNU).
Steve Naroff577f9722008-01-29 18:58:14 +00001428 if (!rpointee->isObjectType()) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001429 // Handle the GNU void* extension.
Steve Naroff577f9722008-01-29 18:58:14 +00001430 if (rpointee->isVoidType()) {
1431 if (!lpointee->isVoidType())
Chris Lattnerf6da2912007-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 rex->getType().getAsString(), rex->getSourceRange());
1437 return QualType();
1438 }
1439 }
1440
1441 // Pointee types must be compatible.
Steve Naroff577f9722008-01-29 18:58:14 +00001442 if (!Context.typesAreCompatible(lpointee.getUnqualifiedType(),
1443 rpointee.getUnqualifiedType())) {
Chris Lattnerf6da2912007-12-09 21:53:25 +00001444 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1445 lex->getType().getAsString(), rex->getType().getAsString(),
1446 lex->getSourceRange(), rex->getSourceRange());
1447 return QualType();
1448 }
1449
1450 return Context.getPointerDiffType();
1451 }
1452 }
1453
Chris Lattner2c8bff72007-12-12 05:47:28 +00001454 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001455}
1456
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001457// C99 6.5.7
1458QualType Sema::CheckShiftOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1459 bool isCompAssign) {
Chris Lattner2c8bff72007-12-12 05:47:28 +00001460 // C99 6.5.7p2: Each of the operands shall have integer type.
1461 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1462 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001463
Chris Lattner2c8bff72007-12-12 05:47:28 +00001464 // Shifts don't perform usual arithmetic conversions, they just do integer
1465 // promotions on each operand. C99 6.5.7p3
Chris Lattnerbb19bc42007-12-13 07:28:16 +00001466 if (!isCompAssign)
1467 UsualUnaryConversions(lex);
Chris Lattner2c8bff72007-12-12 05:47:28 +00001468 UsualUnaryConversions(rex);
1469
1470 // "The type of the result is that of the promoted left operand."
1471 return lex->getType();
Chris Lattner4b009652007-07-25 00:24:17 +00001472}
1473
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001474// C99 6.5.8
1475QualType Sema::CheckCompareOperands(Expr *&lex, Expr *&rex, SourceLocation loc,
1476 bool isRelational) {
Chris Lattner254f3bc2007-08-26 01:18:55 +00001477 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroffecc4fa12007-08-10 18:26:40 +00001478 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1479 UsualArithmeticConversions(lex, rex);
1480 else {
1481 UsualUnaryConversions(lex);
1482 UsualUnaryConversions(rex);
1483 }
Chris Lattner4b009652007-07-25 00:24:17 +00001484 QualType lType = lex->getType();
1485 QualType rType = rex->getType();
1486
Ted Kremenek486509e2007-10-29 17:13:39 +00001487 // For non-floating point types, check for self-comparisons of the form
1488 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1489 // often indicate logic errors in the program.
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001490 if (!lType->isFloatingType()) {
Ted Kremenek87e30c52008-01-17 16:57:34 +00001491 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex->IgnoreParens()))
1492 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex->IgnoreParens()))
Ted Kremenekcf8b77d2007-10-29 16:58:49 +00001493 if (DRL->getDecl() == DRR->getDecl())
1494 Diag(loc, diag::warn_selfcomparison);
1495 }
1496
Chris Lattner254f3bc2007-08-26 01:18:55 +00001497 if (isRelational) {
1498 if (lType->isRealType() && rType->isRealType())
1499 return Context.IntTy;
1500 } else {
Ted Kremenek486509e2007-10-29 17:13:39 +00001501 // Check for comparisons of floating point operands using != and ==.
Ted Kremenek486509e2007-10-29 17:13:39 +00001502 if (lType->isFloatingType()) {
1503 assert (rType->isFloatingType());
Ted Kremenek30c66752007-11-25 00:58:00 +00001504 CheckFloatComparison(loc,lex,rex);
Ted Kremenek75439142007-10-29 16:40:01 +00001505 }
1506
Chris Lattner254f3bc2007-08-26 01:18:55 +00001507 if (lType->isArithmeticType() && rType->isArithmeticType())
1508 return Context.IntTy;
1509 }
Chris Lattner4b009652007-07-25 00:24:17 +00001510
Chris Lattner22be8422007-08-26 01:10:14 +00001511 bool LHSIsNull = lex->isNullPointerConstant(Context);
1512 bool RHSIsNull = rex->isNullPointerConstant(Context);
1513
Chris Lattner254f3bc2007-08-26 01:18:55 +00001514 // All of the following pointer related warnings are GCC extensions, except
1515 // when handling null pointer constants. One day, we can consider making them
1516 // errors (when -pedantic-errors is enabled).
Steve Naroffc33c0602007-08-27 04:08:11 +00001517 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001518 QualType LCanPointeeTy =
1519 lType->getAsPointerType()->getPointeeType().getCanonicalType();
1520 QualType RCanPointeeTy =
1521 rType->getAsPointerType()->getPointeeType().getCanonicalType();
Eli Friedman50727042008-02-08 01:19:44 +00001522
Steve Naroff3b435622007-11-13 14:57:38 +00001523 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
Chris Lattner56a5cd62008-04-03 05:07:25 +00001524 !LCanPointeeTy->isVoidType() && !RCanPointeeTy->isVoidType() &&
1525 !Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(),
1526 RCanPointeeTy.getUnqualifiedType())) {
Steve Naroff4462cb02007-08-16 21:48:38 +00001527 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1528 lType.getAsString(), rType.getAsString(),
1529 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001530 }
Chris Lattnere992d6c2008-01-16 19:17:22 +00001531 ImpCastExprToType(rex, lType); // promote the pointer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001532 return Context.IntTy;
1533 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
Chris Lattnerfe1f4032008-04-07 05:30:13 +00001535 && ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Chris Lattnere992d6c2008-01-16 19:17:22 +00001536 ImpCastExprToType(rex, lType);
Fariborz Jahanian5319d9c2007-12-20 01:06:58 +00001537 return Context.IntTy;
1538 }
Steve Naroff4462cb02007-08-16 21:48:38 +00001539 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001540 if (!RHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001541 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1542 lType.getAsString(), rType.getAsString(),
1543 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001544 ImpCastExprToType(rex, lType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001545 return Context.IntTy;
1546 }
1547 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner22be8422007-08-26 01:10:14 +00001548 if (!LHSIsNull)
Steve Naroff4462cb02007-08-16 21:48:38 +00001549 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1550 lType.getAsString(), rType.getAsString(),
1551 lex->getSourceRange(), rex->getSourceRange());
Chris Lattnere992d6c2008-01-16 19:17:22 +00001552 ImpCastExprToType(lex, rType); // promote the integer to pointer
Steve Naroff4462cb02007-08-16 21:48:38 +00001553 return Context.IntTy;
Chris Lattner4b009652007-07-25 00:24:17 +00001554 }
Chris Lattner2c8bff72007-12-12 05:47:28 +00001555 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001556}
1557
Chris Lattner4b009652007-07-25 00:24:17 +00001558inline QualType Sema::CheckBitwiseOperands(
Steve Naroff8f708362007-08-24 19:07:16 +00001559 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Chris Lattner4b009652007-07-25 00:24:17 +00001560{
1561 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
1562 return CheckVectorOperands(loc, lex, rex);
1563
Steve Naroff8f708362007-08-24 19:07:16 +00001564 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Chris Lattner4b009652007-07-25 00:24:17 +00001565
1566 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroff8f708362007-08-24 19:07:16 +00001567 return compType;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001568 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001569}
1570
1571inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
1572 Expr *&lex, Expr *&rex, SourceLocation loc)
1573{
1574 UsualUnaryConversions(lex);
1575 UsualUnaryConversions(rex);
1576
1577 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
1578 return Context.IntTy;
Chris Lattner2c8bff72007-12-12 05:47:28 +00001579 return InvalidOperands(loc, lex, rex);
Chris Lattner4b009652007-07-25 00:24:17 +00001580}
1581
1582inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0f32f432007-08-24 22:33:52 +00001583 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Chris Lattner4b009652007-07-25 00:24:17 +00001584{
1585 QualType lhsType = lex->getType();
1586 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Chris Lattner4b009652007-07-25 00:24:17 +00001587 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1588
1589 switch (mlval) { // C99 6.5.16p2
Chris Lattner005ed752008-01-04 18:04:52 +00001590 case Expr::MLV_Valid:
1591 break;
1592 case Expr::MLV_ConstQualified:
1593 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1594 return QualType();
1595 case Expr::MLV_ArrayType:
1596 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1597 lhsType.getAsString(), lex->getSourceRange());
1598 return QualType();
1599 case Expr::MLV_NotObjectType:
1600 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1601 lhsType.getAsString(), lex->getSourceRange());
1602 return QualType();
1603 case Expr::MLV_InvalidExpression:
1604 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1605 lex->getSourceRange());
1606 return QualType();
1607 case Expr::MLV_IncompleteType:
1608 case Expr::MLV_IncompleteVoidType:
1609 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1610 lhsType.getAsString(), lex->getSourceRange());
1611 return QualType();
1612 case Expr::MLV_DuplicateVectorComponents:
1613 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1614 lex->getSourceRange());
1615 return QualType();
Chris Lattner4b009652007-07-25 00:24:17 +00001616 }
Steve Naroff7cbb1462007-07-31 12:34:36 +00001617
Chris Lattner005ed752008-01-04 18:04:52 +00001618 AssignConvertType ConvTy;
1619 if (compoundType.isNull())
1620 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1621 else
1622 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1623
1624 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1625 rex, "assigning"))
1626 return QualType();
1627
Chris Lattner4b009652007-07-25 00:24:17 +00001628 // C99 6.5.16p3: The type of an assignment expression is the type of the
1629 // left operand unless the left operand has qualified type, in which case
1630 // it is the unqualified version of the type of the left operand.
1631 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1632 // is converted to the type of the assignment expression (above).
Chris Lattner0d9bcea2007-08-30 17:45:32 +00001633 // C++ 5.17p1: the type of the assignment expression is that of its left
1634 // oprdu.
Chris Lattner005ed752008-01-04 18:04:52 +00001635 return lhsType.getUnqualifiedType();
Chris Lattner4b009652007-07-25 00:24:17 +00001636}
1637
1638inline QualType Sema::CheckCommaOperands( // C99 6.5.17
1639 Expr *&lex, Expr *&rex, SourceLocation loc) {
1640 UsualUnaryConversions(rex);
1641 return rex->getType();
1642}
1643
1644/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1645/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
1646QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
1647 QualType resType = op->getType();
1648 assert(!resType.isNull() && "no type for increment/decrement expression");
1649
Steve Naroffd30e1932007-08-24 17:20:07 +00001650 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroffce827582007-11-11 14:15:57 +00001651 if (const PointerType *pt = resType->getAsPointerType()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001652 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
1653 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1654 resType.getAsString(), op->getSourceRange());
1655 return QualType();
1656 }
Steve Naroffd30e1932007-08-24 17:20:07 +00001657 } else if (!resType->isRealType()) {
1658 if (resType->isComplexType())
1659 // C99 does not support ++/-- on complex types.
1660 Diag(OpLoc, diag::ext_integer_increment_complex,
1661 resType.getAsString(), op->getSourceRange());
1662 else {
1663 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1664 resType.getAsString(), op->getSourceRange());
1665 return QualType();
1666 }
Chris Lattner4b009652007-07-25 00:24:17 +00001667 }
Steve Naroff6acc0f42007-08-23 21:37:33 +00001668 // At this point, we know we have a real, complex or pointer type.
1669 // Now make sure the operand is a modifiable lvalue.
Chris Lattner4b009652007-07-25 00:24:17 +00001670 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1671 if (mlval != Expr::MLV_Valid) {
1672 // FIXME: emit a more precise diagnostic...
1673 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
1674 op->getSourceRange());
1675 return QualType();
1676 }
1677 return resType;
1678}
1679
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001680/// getPrimaryDecl - Helper function for CheckAddressOfOperand().
Chris Lattner4b009652007-07-25 00:24:17 +00001681/// This routine allows us to typecheck complex/recursive expressions
1682/// where the declaration is needed for type checking. Here are some
1683/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Chris Lattner48d7f382008-04-02 04:24:33 +00001684static ValueDecl *getPrimaryDecl(Expr *E) {
1685 switch (E->getStmtClass()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001686 case Stmt::DeclRefExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001687 return cast<DeclRefExpr>(E)->getDecl();
Chris Lattner4b009652007-07-25 00:24:17 +00001688 case Stmt::MemberExprClass:
Chris Lattnera3249072007-11-16 17:46:48 +00001689 // Fields cannot be declared with a 'register' storage class.
1690 // &X->f is always ok, even if X is declared register.
Chris Lattner48d7f382008-04-02 04:24:33 +00001691 if (cast<MemberExpr>(E)->isArrow())
Chris Lattnera3249072007-11-16 17:46:48 +00001692 return 0;
Chris Lattner48d7f382008-04-02 04:24:33 +00001693 return getPrimaryDecl(cast<MemberExpr>(E)->getBase());
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001694 case Stmt::ArraySubscriptExprClass: {
1695 // &X[4] and &4[X] is invalid if X is invalid and X is not a pointer.
1696
Chris Lattner48d7f382008-04-02 04:24:33 +00001697 ValueDecl *VD = getPrimaryDecl(cast<ArraySubscriptExpr>(E)->getBase());
Anders Carlsson655694e2008-02-01 16:01:31 +00001698 if (!VD || VD->getType()->isPointerType())
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001699 return 0;
1700 else
1701 return VD;
1702 }
Chris Lattner4b009652007-07-25 00:24:17 +00001703 case Stmt::UnaryOperatorClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001704 return getPrimaryDecl(cast<UnaryOperator>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001705 case Stmt::ParenExprClass:
Chris Lattner48d7f382008-04-02 04:24:33 +00001706 return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr());
Chris Lattnera3249072007-11-16 17:46:48 +00001707 case Stmt::ImplicitCastExprClass:
1708 // &X[4] when X is an array, has an implicit cast from array to pointer.
Chris Lattner48d7f382008-04-02 04:24:33 +00001709 return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr());
Chris Lattner4b009652007-07-25 00:24:17 +00001710 default:
1711 return 0;
1712 }
1713}
1714
1715/// CheckAddressOfOperand - The operand of & must be either a function
1716/// designator or an lvalue designating an object. If it is an lvalue, the
1717/// object cannot be declared with storage class register or be a bit field.
1718/// Note: The usual conversions are *not* applied to the operand of the &
1719/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
1720QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001721 if (getLangOptions().C99) {
1722 // Implement C99-only parts of addressof rules.
1723 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1724 if (uOp->getOpcode() == UnaryOperator::Deref)
1725 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1726 // (assuming the deref expression is valid).
1727 return uOp->getSubExpr()->getType();
1728 }
1729 // Technically, there should be a check for array subscript
1730 // expressions here, but the result of one is always an lvalue anyway.
1731 }
Anders Carlsson4b3db2b2008-02-01 07:15:58 +00001732 ValueDecl *dcl = getPrimaryDecl(op);
Chris Lattner4b009652007-07-25 00:24:17 +00001733 Expr::isLvalueResult lval = op->isLvalue();
1734
1735 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattnera3249072007-11-16 17:46:48 +00001736 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1737 // FIXME: emit more specific diag...
Chris Lattner4b009652007-07-25 00:24:17 +00001738 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1739 op->getSourceRange());
1740 return QualType();
1741 }
Steve Naroff73cf87e2008-02-29 23:30:25 +00001742 } else if (MemberExpr *MemExpr = dyn_cast<MemberExpr>(op)) { // C99 6.5.3.2p1
1743 if (MemExpr->getMemberDecl()->isBitField()) {
1744 Diag(OpLoc, diag::err_typecheck_address_of,
1745 std::string("bit-field"), op->getSourceRange());
1746 return QualType();
1747 }
1748 // Check for Apple extension for accessing vector components.
1749 } else if (isa<ArraySubscriptExpr>(op) &&
1750 cast<ArraySubscriptExpr>(op)->getBase()->getType()->isVectorType()) {
1751 Diag(OpLoc, diag::err_typecheck_address_of,
1752 std::string("vector"), op->getSourceRange());
1753 return QualType();
1754 } else if (dcl) { // C99 6.5.3.2p1
Chris Lattner4b009652007-07-25 00:24:17 +00001755 // We have an lvalue with a decl. Make sure the decl is not declared
1756 // with the register storage-class specifier.
1757 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
1758 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff73cf87e2008-02-29 23:30:25 +00001759 Diag(OpLoc, diag::err_typecheck_address_of,
1760 std::string("register variable"), op->getSourceRange());
Chris Lattner4b009652007-07-25 00:24:17 +00001761 return QualType();
1762 }
1763 } else
1764 assert(0 && "Unknown/unexpected decl type");
Chris Lattner4b009652007-07-25 00:24:17 +00001765 }
1766 // If the operand has type "type", the result has type "pointer to type".
1767 return Context.getPointerType(op->getType());
1768}
1769
1770QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
1771 UsualUnaryConversions(op);
1772 QualType qType = op->getType();
1773
Chris Lattner7931f4a2007-07-31 16:53:04 +00001774 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff9c6c3592008-01-13 17:10:08 +00001775 // Note that per both C89 and C99, this is always legal, even
1776 // if ptype is an incomplete type or void.
1777 // It would be possible to warn about dereferencing a
1778 // void pointer, but it's completely well-defined,
1779 // and such a warning is unlikely to catch any mistakes.
1780 return PT->getPointeeType();
Chris Lattner4b009652007-07-25 00:24:17 +00001781 }
1782 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
1783 qType.getAsString(), op->getSourceRange());
1784 return QualType();
1785}
1786
1787static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1788 tok::TokenKind Kind) {
1789 BinaryOperator::Opcode Opc;
1790 switch (Kind) {
1791 default: assert(0 && "Unknown binop!");
1792 case tok::star: Opc = BinaryOperator::Mul; break;
1793 case tok::slash: Opc = BinaryOperator::Div; break;
1794 case tok::percent: Opc = BinaryOperator::Rem; break;
1795 case tok::plus: Opc = BinaryOperator::Add; break;
1796 case tok::minus: Opc = BinaryOperator::Sub; break;
1797 case tok::lessless: Opc = BinaryOperator::Shl; break;
1798 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1799 case tok::lessequal: Opc = BinaryOperator::LE; break;
1800 case tok::less: Opc = BinaryOperator::LT; break;
1801 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1802 case tok::greater: Opc = BinaryOperator::GT; break;
1803 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1804 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1805 case tok::amp: Opc = BinaryOperator::And; break;
1806 case tok::caret: Opc = BinaryOperator::Xor; break;
1807 case tok::pipe: Opc = BinaryOperator::Or; break;
1808 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1809 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1810 case tok::equal: Opc = BinaryOperator::Assign; break;
1811 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1812 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1813 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1814 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1815 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1816 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1817 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1818 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1819 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1820 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1821 case tok::comma: Opc = BinaryOperator::Comma; break;
1822 }
1823 return Opc;
1824}
1825
1826static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1827 tok::TokenKind Kind) {
1828 UnaryOperator::Opcode Opc;
1829 switch (Kind) {
1830 default: assert(0 && "Unknown unary op!");
1831 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1832 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1833 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1834 case tok::star: Opc = UnaryOperator::Deref; break;
1835 case tok::plus: Opc = UnaryOperator::Plus; break;
1836 case tok::minus: Opc = UnaryOperator::Minus; break;
1837 case tok::tilde: Opc = UnaryOperator::Not; break;
1838 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1839 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1840 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1841 case tok::kw___real: Opc = UnaryOperator::Real; break;
1842 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
1843 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
1844 }
1845 return Opc;
1846}
1847
1848// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001849Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Chris Lattner4b009652007-07-25 00:24:17 +00001850 ExprTy *LHS, ExprTy *RHS) {
1851 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1852 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1853
Steve Naroff87d58b42007-09-16 03:34:24 +00001854 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1855 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Chris Lattner4b009652007-07-25 00:24:17 +00001856
1857 QualType ResultTy; // Result type of the binary operator.
1858 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
1859
1860 switch (Opc) {
1861 default:
1862 assert(0 && "Unknown binary expr!");
1863 case BinaryOperator::Assign:
1864 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
1865 break;
1866 case BinaryOperator::Mul:
1867 case BinaryOperator::Div:
1868 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1869 break;
1870 case BinaryOperator::Rem:
1871 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
1872 break;
1873 case BinaryOperator::Add:
1874 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
1875 break;
1876 case BinaryOperator::Sub:
1877 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
1878 break;
1879 case BinaryOperator::Shl:
1880 case BinaryOperator::Shr:
1881 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
1882 break;
1883 case BinaryOperator::LE:
1884 case BinaryOperator::LT:
1885 case BinaryOperator::GE:
1886 case BinaryOperator::GT:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001887 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001888 break;
1889 case BinaryOperator::EQ:
1890 case BinaryOperator::NE:
Chris Lattner254f3bc2007-08-26 01:18:55 +00001891 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Chris Lattner4b009652007-07-25 00:24:17 +00001892 break;
1893 case BinaryOperator::And:
1894 case BinaryOperator::Xor:
1895 case BinaryOperator::Or:
1896 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
1897 break;
1898 case BinaryOperator::LAnd:
1899 case BinaryOperator::LOr:
1900 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
1901 break;
1902 case BinaryOperator::MulAssign:
1903 case BinaryOperator::DivAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001904 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001905 if (!CompTy.isNull())
1906 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1907 break;
1908 case BinaryOperator::RemAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001909 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001910 if (!CompTy.isNull())
1911 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1912 break;
1913 case BinaryOperator::AddAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001914 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001915 if (!CompTy.isNull())
1916 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1917 break;
1918 case BinaryOperator::SubAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001919 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001920 if (!CompTy.isNull())
1921 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1922 break;
1923 case BinaryOperator::ShlAssign:
1924 case BinaryOperator::ShrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001925 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001926 if (!CompTy.isNull())
1927 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1928 break;
1929 case BinaryOperator::AndAssign:
1930 case BinaryOperator::XorAssign:
1931 case BinaryOperator::OrAssign:
Steve Naroff8f708362007-08-24 19:07:16 +00001932 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner4b009652007-07-25 00:24:17 +00001933 if (!CompTy.isNull())
1934 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
1935 break;
1936 case BinaryOperator::Comma:
1937 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
1938 break;
1939 }
1940 if (ResultTy.isNull())
1941 return true;
1942 if (CompTy.isNull())
Chris Lattnerf420df12007-08-28 18:36:55 +00001943 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001944 else
Chris Lattnerf420df12007-08-28 18:36:55 +00001945 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Chris Lattner4b009652007-07-25 00:24:17 +00001946}
1947
1948// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff87d58b42007-09-16 03:34:24 +00001949Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner4b009652007-07-25 00:24:17 +00001950 ExprTy *input) {
1951 Expr *Input = (Expr*)input;
1952 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1953 QualType resultType;
1954 switch (Opc) {
1955 default:
1956 assert(0 && "Unimplemented unary expr!");
1957 case UnaryOperator::PreInc:
1958 case UnaryOperator::PreDec:
1959 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
1960 break;
1961 case UnaryOperator::AddrOf:
1962 resultType = CheckAddressOfOperand(Input, OpLoc);
1963 break;
1964 case UnaryOperator::Deref:
Steve Naroffccc26a72007-12-18 04:06:57 +00001965 DefaultFunctionArrayConversion(Input);
Chris Lattner4b009652007-07-25 00:24:17 +00001966 resultType = CheckIndirectionOperand(Input, OpLoc);
1967 break;
1968 case UnaryOperator::Plus:
1969 case UnaryOperator::Minus:
1970 UsualUnaryConversions(Input);
1971 resultType = Input->getType();
1972 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
1973 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1974 resultType.getAsString());
1975 break;
1976 case UnaryOperator::Not: // bitwise complement
1977 UsualUnaryConversions(Input);
1978 resultType = Input->getType();
Steve Naroffd30e1932007-08-24 17:20:07 +00001979 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1980 if (!resultType->isIntegerType()) {
1981 if (resultType->isComplexType())
1982 // C99 does not support '~' for complex conjugation.
1983 Diag(OpLoc, diag::ext_integer_complement_complex,
1984 resultType.getAsString());
1985 else
1986 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1987 resultType.getAsString());
1988 }
Chris Lattner4b009652007-07-25 00:24:17 +00001989 break;
1990 case UnaryOperator::LNot: // logical negation
1991 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
1992 DefaultFunctionArrayConversion(Input);
1993 resultType = Input->getType();
1994 if (!resultType->isScalarType()) // C99 6.5.3.3p1
1995 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1996 resultType.getAsString());
1997 // LNot always has type int. C99 6.5.3.3p5.
1998 resultType = Context.IntTy;
1999 break;
2000 case UnaryOperator::SizeOf:
2001 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
2002 break;
2003 case UnaryOperator::AlignOf:
2004 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
2005 break;
Chris Lattner03931a72007-08-24 21:16:53 +00002006 case UnaryOperator::Real:
Chris Lattner03931a72007-08-24 21:16:53 +00002007 case UnaryOperator::Imag:
Chris Lattner5110ad52007-08-24 21:41:10 +00002008 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner03931a72007-08-24 21:16:53 +00002009 break;
Chris Lattner4b009652007-07-25 00:24:17 +00002010 case UnaryOperator::Extension:
Chris Lattner4b009652007-07-25 00:24:17 +00002011 resultType = Input->getType();
2012 break;
2013 }
2014 if (resultType.isNull())
2015 return true;
2016 return new UnaryOperator(Input, Opc, resultType, OpLoc);
2017}
2018
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002019/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
2020Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattner4b009652007-07-25 00:24:17 +00002021 SourceLocation LabLoc,
2022 IdentifierInfo *LabelII) {
2023 // Look up the record for this label identifier.
2024 LabelStmt *&LabelDecl = LabelMap[LabelII];
2025
2026 // If we haven't seen this label yet, create a forward reference.
2027 if (LabelDecl == 0)
2028 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
2029
2030 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnera0d03a72007-08-03 17:31:20 +00002031 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
2032 Context.getPointerType(Context.VoidTy));
Chris Lattner4b009652007-07-25 00:24:17 +00002033}
2034
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002035Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner4b009652007-07-25 00:24:17 +00002036 SourceLocation RPLoc) { // "({..})"
2037 Stmt *SubStmt = static_cast<Stmt*>(substmt);
2038 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
2039 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
2040
2041 // FIXME: there are a variety of strange constraints to enforce here, for
2042 // example, it is not possible to goto into a stmt expression apparently.
2043 // More semantic analysis is needed.
2044
2045 // FIXME: the last statement in the compount stmt has its value used. We
2046 // should not warn about it being unused.
2047
2048 // If there are sub stmts in the compound stmt, take the type of the last one
2049 // as the type of the stmtexpr.
2050 QualType Ty = Context.VoidTy;
2051
2052 if (!Compound->body_empty())
2053 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
2054 Ty = LastExpr->getType();
2055
2056 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
2057}
Steve Naroff63bad2d2007-08-01 22:05:33 +00002058
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002059Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002060 SourceLocation TypeLoc,
2061 TypeTy *argty,
2062 OffsetOfComponent *CompPtr,
2063 unsigned NumComponents,
2064 SourceLocation RPLoc) {
2065 QualType ArgTy = QualType::getFromOpaquePtr(argty);
2066 assert(!ArgTy.isNull() && "Missing type argument!");
2067
2068 // We must have at least one component that refers to the type, and the first
2069 // one is known to be a field designator. Verify that the ArgTy represents
2070 // a struct/union/class.
2071 if (!ArgTy->isRecordType())
2072 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
2073
2074 // Otherwise, create a compound literal expression as the base, and
2075 // iteratively process the offsetof designators.
Steve Naroffbe37fc02008-01-14 18:19:28 +00002076 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002077
Chris Lattnerb37522e2007-08-31 21:49:13 +00002078 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
2079 // GCC extension, diagnose them.
2080 if (NumComponents != 1)
2081 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2082 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2083
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002084 for (unsigned i = 0; i != NumComponents; ++i) {
2085 const OffsetOfComponent &OC = CompPtr[i];
2086 if (OC.isBrackets) {
2087 // Offset of an array sub-field. TODO: Should we allow vector elements?
2088 const ArrayType *AT = Res->getType()->getAsArrayType();
2089 if (!AT) {
2090 delete Res;
2091 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2092 Res->getType().getAsString());
2093 }
2094
Chris Lattner2af6a802007-08-30 17:59:59 +00002095 // FIXME: C++: Verify that operator[] isn't overloaded.
2096
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002097 // C99 6.5.2.1p1
2098 Expr *Idx = static_cast<Expr*>(OC.U.E);
2099 if (!Idx->getType()->isIntegerType())
2100 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2101 Idx->getSourceRange());
2102
2103 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2104 continue;
2105 }
2106
2107 const RecordType *RC = Res->getType()->getAsRecordType();
2108 if (!RC) {
2109 delete Res;
2110 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2111 Res->getType().getAsString());
2112 }
2113
2114 // Get the decl corresponding to this.
2115 RecordDecl *RD = RC->getDecl();
2116 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2117 if (!MemberDecl)
2118 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2119 OC.U.IdentInfo->getName(),
2120 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner2af6a802007-08-30 17:59:59 +00002121
2122 // FIXME: C++: Verify that MemberDecl isn't a static field.
2123 // FIXME: Verify that MemberDecl isn't a bitfield.
Eli Friedman76b49832008-02-06 22:48:16 +00002124 // MemberDecl->getType() doesn't get the right qualifiers, but it doesn't
2125 // matter here.
2126 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd, MemberDecl->getType());
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002127 }
2128
2129 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2130 BuiltinLoc);
2131}
2132
2133
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002134Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff63bad2d2007-08-01 22:05:33 +00002135 TypeTy *arg1, TypeTy *arg2,
2136 SourceLocation RPLoc) {
2137 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2138 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2139
2140 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2141
Chris Lattner0d9bcea2007-08-30 17:45:32 +00002142 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff63bad2d2007-08-01 22:05:33 +00002143}
2144
Steve Naroff5cbb02f2007-09-16 14:56:35 +00002145Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff93c53012007-08-03 21:21:27 +00002146 ExprTy *expr1, ExprTy *expr2,
2147 SourceLocation RPLoc) {
2148 Expr *CondExpr = static_cast<Expr*>(cond);
2149 Expr *LHSExpr = static_cast<Expr*>(expr1);
2150 Expr *RHSExpr = static_cast<Expr*>(expr2);
2151
2152 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2153
2154 // The conditional expression is required to be a constant expression.
2155 llvm::APSInt condEval(32);
2156 SourceLocation ExpLoc;
2157 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2158 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2159 CondExpr->getSourceRange());
2160
2161 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2162 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2163 RHSExpr->getType();
2164 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2165}
2166
Nate Begemanbd881ef2008-01-30 20:50:20 +00002167/// ExprsMatchFnType - return true if the Exprs in array Args have
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002168/// QualTypes that match the QualTypes of the arguments of the FnType.
Nate Begemanbd881ef2008-01-30 20:50:20 +00002169/// The number of arguments has already been validated to match the number of
2170/// arguments in FnType.
2171static bool ExprsMatchFnType(Expr **Args, const FunctionTypeProto *FnType) {
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002172 unsigned NumParams = FnType->getNumArgs();
Nate Begeman778fd3b2008-04-18 23:35:14 +00002173 for (unsigned i = 0; i != NumParams; ++i) {
2174 QualType ExprTy = Args[i]->getType().getCanonicalType();
2175 QualType ParmTy = FnType->getArgType(i).getCanonicalType();
2176
2177 if (ExprTy.getUnqualifiedType() != ParmTy.getUnqualifiedType())
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002178 return false;
Nate Begeman778fd3b2008-04-18 23:35:14 +00002179 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002180 return true;
2181}
2182
2183Sema::ExprResult Sema::ActOnOverloadExpr(ExprTy **args, unsigned NumArgs,
2184 SourceLocation *CommaLocs,
2185 SourceLocation BuiltinLoc,
2186 SourceLocation RParenLoc) {
Nate Begemanc6078c92008-01-31 05:38:29 +00002187 // __builtin_overload requires at least 2 arguments
2188 if (NumArgs < 2)
2189 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2190 SourceRange(BuiltinLoc, RParenLoc));
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002191
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002192 // The first argument is required to be a constant expression. It tells us
2193 // the number of arguments to pass to each of the functions to be overloaded.
Nate Begemanc6078c92008-01-31 05:38:29 +00002194 Expr **Args = reinterpret_cast<Expr**>(args);
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002195 Expr *NParamsExpr = Args[0];
2196 llvm::APSInt constEval(32);
2197 SourceLocation ExpLoc;
2198 if (!NParamsExpr->isIntegerConstantExpr(constEval, Context, &ExpLoc))
2199 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2200 NParamsExpr->getSourceRange());
2201
2202 // Verify that the number of parameters is > 0
2203 unsigned NumParams = constEval.getZExtValue();
2204 if (NumParams == 0)
2205 return Diag(ExpLoc, diag::err_overload_expr_requires_non_zero_constant,
2206 NParamsExpr->getSourceRange());
2207 // Verify that we have at least 1 + NumParams arguments to the builtin.
2208 if ((NumParams + 1) > NumArgs)
2209 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
2210 SourceRange(BuiltinLoc, RParenLoc));
2211
2212 // Figure out the return type, by matching the args to one of the functions
Nate Begemanbd881ef2008-01-30 20:50:20 +00002213 // listed after the parameters.
Nate Begemanc6078c92008-01-31 05:38:29 +00002214 OverloadExpr *OE = 0;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002215 for (unsigned i = NumParams + 1; i < NumArgs; ++i) {
2216 // UsualUnaryConversions will convert the function DeclRefExpr into a
2217 // pointer to function.
2218 Expr *Fn = UsualUnaryConversions(Args[i]);
2219 FunctionTypeProto *FnType = 0;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002220 if (const PointerType *PT = Fn->getType()->getAsPointerType()) {
2221 QualType PointeeType = PT->getPointeeType().getCanonicalType();
2222 FnType = dyn_cast<FunctionTypeProto>(PointeeType);
2223 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002224
2225 // The Expr type must be FunctionTypeProto, since FunctionTypeProto has no
2226 // parameters, and the number of parameters must match the value passed to
2227 // the builtin.
2228 if (!FnType || (FnType->getNumArgs() != NumParams))
Nate Begemanbd881ef2008-01-30 20:50:20 +00002229 return Diag(Fn->getExprLoc(), diag::err_overload_incorrect_fntype,
2230 Fn->getSourceRange());
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002231
2232 // Scan the parameter list for the FunctionType, checking the QualType of
Nate Begemanbd881ef2008-01-30 20:50:20 +00002233 // each parameter against the QualTypes of the arguments to the builtin.
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002234 // If they match, return a new OverloadExpr.
Nate Begemanc6078c92008-01-31 05:38:29 +00002235 if (ExprsMatchFnType(Args+1, FnType)) {
2236 if (OE)
2237 return Diag(Fn->getExprLoc(), diag::err_overload_multiple_match,
2238 OE->getFn()->getSourceRange());
2239 // Remember our match, and continue processing the remaining arguments
2240 // to catch any errors.
2241 OE = new OverloadExpr(Args, NumArgs, i, FnType->getResultType(),
2242 BuiltinLoc, RParenLoc);
2243 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002244 }
Nate Begemanc6078c92008-01-31 05:38:29 +00002245 // Return the newly created OverloadExpr node, if we succeded in matching
2246 // exactly one of the candidate functions.
2247 if (OE)
2248 return OE;
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002249
2250 // If we didn't find a matching function Expr in the __builtin_overload list
2251 // the return an error.
2252 std::string typeNames;
Nate Begemanbd881ef2008-01-30 20:50:20 +00002253 for (unsigned i = 0; i != NumParams; ++i) {
2254 if (i != 0) typeNames += ", ";
2255 typeNames += Args[i+1]->getType().getAsString();
2256 }
Nate Begeman9f3bfb72008-01-17 17:46:27 +00002257
2258 return Diag(BuiltinLoc, diag::err_overload_no_match, typeNames,
2259 SourceRange(BuiltinLoc, RParenLoc));
2260}
2261
Anders Carlsson36760332007-10-15 20:28:48 +00002262Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2263 ExprTy *expr, TypeTy *type,
Chris Lattner005ed752008-01-04 18:04:52 +00002264 SourceLocation RPLoc) {
Anders Carlsson36760332007-10-15 20:28:48 +00002265 Expr *E = static_cast<Expr*>(expr);
2266 QualType T = QualType::getFromOpaquePtr(type);
2267
2268 InitBuiltinVaListType();
2269
Chris Lattner005ed752008-01-04 18:04:52 +00002270 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2271 != Compatible)
Anders Carlsson36760332007-10-15 20:28:48 +00002272 return Diag(E->getLocStart(),
2273 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2274 E->getType().getAsString(),
2275 E->getSourceRange());
2276
2277 // FIXME: Warn if a non-POD type is passed in.
2278
2279 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2280}
2281
Chris Lattner005ed752008-01-04 18:04:52 +00002282bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2283 SourceLocation Loc,
2284 QualType DstType, QualType SrcType,
2285 Expr *SrcExpr, const char *Flavor) {
2286 // Decode the result (notice that AST's are still created for extensions).
2287 bool isInvalid = false;
2288 unsigned DiagKind;
2289 switch (ConvTy) {
2290 default: assert(0 && "Unknown conversion type");
2291 case Compatible: return false;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002292 case PointerToInt:
Chris Lattner005ed752008-01-04 18:04:52 +00002293 DiagKind = diag::ext_typecheck_convert_pointer_int;
2294 break;
Chris Lattnerd951b7b2008-01-04 18:22:42 +00002295 case IntToPointer:
2296 DiagKind = diag::ext_typecheck_convert_int_pointer;
2297 break;
Chris Lattner005ed752008-01-04 18:04:52 +00002298 case IncompatiblePointer:
2299 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2300 break;
2301 case FunctionVoidPointer:
2302 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2303 break;
2304 case CompatiblePointerDiscardsQualifiers:
2305 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2306 break;
2307 case Incompatible:
2308 DiagKind = diag::err_typecheck_convert_incompatible;
2309 isInvalid = true;
2310 break;
2311 }
2312
2313 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2314 SrcExpr->getSourceRange());
2315 return isInvalid;
2316}
2317
Argiris Kirtzidis03e6aaf2008-04-27 13:50:30 +00002318
2319