blob: f8053a4fbe8d84ddf3c4247ed1dd3cf9bb8514bd [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattner5b183d82006-11-10 05:03:26 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Ted Kremenek43fb8b02007-11-25 00:58:00 +000015#include "SemaUtil.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000016#include "clang/AST/ASTContext.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
Steve Naroffd54978b2007-09-18 23:55:05 +000018#include "clang/Parse/DeclSpec.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000019#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000020#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000022#include "clang/Basic/TargetInfo.h"
Chris Lattner08464942007-12-28 05:29:59 +000023#include "llvm/ADT/OwningPtr.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000024#include "llvm/ADT/SmallString.h"
Chris Lattnerb87b1b32007-08-10 20:18:51 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace clang;
27
Steve Naroff83895f72007-09-16 03:34:24 +000028/// ActOnStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000029/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
30/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
31/// multiple tokens. However, the common case is that StringToks points to one
32/// string.
33///
34Action::ExprResult
Steve Naroff83895f72007-09-16 03:34:24 +000035Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000036 assert(NumStringToks && "Must have at least one string!");
37
Steve Naroff4f88b312007-03-13 22:37:02 +000038 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
39 if (Literal.hadError)
40 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000041
Chris Lattner23b7eb62007-06-15 23:05:46 +000042 llvm::SmallVector<SourceLocation, 4> StringTokLocs;
Chris Lattner5b183d82006-11-10 05:03:26 +000043 for (unsigned i = 0; i != NumStringToks; ++i)
44 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000045
46 // FIXME: handle wchar_t
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000047 QualType t;
48
49 if (Literal.Pascal)
50 t = Context.getPointerType(Context.UnsignedCharTy);
51 else
52 t = Context.getPointerType(Context.CharTy);
53
54 if (Literal.Pascal && Literal.GetStringLength() > 256)
55 return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
56 SourceRange(StringToks[0].getLocation(),
57 StringToks[NumStringToks-1].getLocation()));
Steve Narofff1e53692007-03-23 22:27:02 +000058
Chris Lattner5b183d82006-11-10 05:03:26 +000059 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000060 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Anders Carlssoncbfc4b82007-10-15 02:50:23 +000061 Literal.AnyWide, t,
62 StringToks[0].getLocation(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000063 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000064}
65
Chris Lattnere168f762006-11-10 05:29:30 +000066
Steve Naroff30d242c2007-09-15 18:49:24 +000067/// ActOnIdentifierExpr - The parser read an identifier in expression context,
Chris Lattnerac18be92006-11-20 06:49:47 +000068/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
69/// identifier is used in an function call context.
Steve Naroff30d242c2007-09-15 18:49:24 +000070Sema::ExprResult Sema::ActOnIdentifierExpr(Scope *S, SourceLocation Loc,
Chris Lattnerac18be92006-11-20 06:49:47 +000071 IdentifierInfo &II,
72 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000073 // Could be enum-constant or decl.
Steve Naroff2f742082007-09-16 16:16:00 +000074 ScopedDecl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000075 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000076 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000077 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000078 if (HasTrailingLParen &&
79 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000080 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000081 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000082 else {
Steve Naroffe46504b2007-11-12 14:29:37 +000083 if (CurMethodDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +000084 ObjCInterfaceDecl *IFace = CurMethodDecl->getClassInterface();
85 ObjCInterfaceDecl *clsDeclared;
86 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&II, clsDeclared)) {
Steve Narofff60782b2007-11-15 02:58:25 +000087 IdentifierInfo &II = Context.Idents.get("self");
88 ExprResult SelfExpr = ActOnIdentifierExpr(S, Loc, II, false);
89 return new ObjCIvarRefExpr(IV, IV->getType(), Loc,
90 static_cast<Expr*>(SelfExpr.Val), true, true);
91 }
Steve Naroffe46504b2007-11-12 14:29:37 +000092 }
Chris Lattnerac18be92006-11-20 06:49:47 +000093 // If this name wasn't predeclared and if this is not a function call,
94 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000095 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000096 }
Chris Lattner17ed4872006-11-20 04:58:19 +000097 }
Steve Naroff7e6f7c22007-08-28 03:03:08 +000098 if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
Steve Naroffcf871f52007-08-28 18:45:29 +000099 // Only create DeclRefExpr's for valid Decl's.
Steve Narofff93b6722007-08-28 20:14:24 +0000100 if (VD->isInvalidDecl())
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000101 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000102 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff7e6f7c22007-08-28 03:03:08 +0000103 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000104 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +0000105 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000106 if (isa<ObjCInterfaceDecl>(D))
Fariborz Jahanianb31a2f22007-12-05 18:16:33 +0000107 return Diag(Loc, diag::err_unexpected_interface, II.getName());
Steve Narofff1e53692007-03-23 22:27:02 +0000108
109 assert(0 && "Invalid decl");
Chris Lattnerbb0ab462007-07-21 04:57:45 +0000110 abort();
Chris Lattner17ed4872006-11-20 04:58:19 +0000111}
Chris Lattnere168f762006-11-10 05:29:30 +0000112
Steve Naroff83895f72007-09-16 03:34:24 +0000113Sema::ExprResult Sema::ActOnPreDefinedExpr(SourceLocation Loc,
Anders Carlsson625bfc82007-07-21 05:21:51 +0000114 tok::TokenKind Kind) {
115 PreDefinedExpr::IdentType IT;
116
Chris Lattnere168f762006-11-10 05:29:30 +0000117 switch (Kind) {
Chris Lattner317e6ba2008-01-12 18:39:25 +0000118 default: assert(0 && "Unknown simple primary expr!");
119 case tok::kw___func__: IT = PreDefinedExpr::Func; break; // [C99 6.4.2.2]
120 case tok::kw___FUNCTION__: IT = PreDefinedExpr::Function; break;
121 case tok::kw___PRETTY_FUNCTION__: IT = PreDefinedExpr::PrettyFunction; break;
Chris Lattnere168f762006-11-10 05:29:30 +0000122 }
Chris Lattner317e6ba2008-01-12 18:39:25 +0000123
124 // Verify that this is in a function context.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000125 if (CurFunctionDecl == 0 && CurMethodDecl == 0)
Chris Lattner317e6ba2008-01-12 18:39:25 +0000126 return Diag(Loc, diag::err_predef_outside_function);
Anders Carlsson625bfc82007-07-21 05:21:51 +0000127
Chris Lattnera81a0272008-01-12 08:14:25 +0000128 // Pre-defined identifiers are of type char[x], where x is the length of the
129 // string.
Chris Lattner0a8c2322008-01-12 19:32:28 +0000130 unsigned Length;
131 if (CurFunctionDecl)
132 Length = CurFunctionDecl->getIdentifier()->getLength();
133 else
134 Length = CurMethodDecl->getSelector().getName().size();
Chris Lattner317e6ba2008-01-12 18:39:25 +0000135
Chris Lattner0a8c2322008-01-12 19:32:28 +0000136 llvm::APInt LengthI(32, Length + 1);
Chris Lattner317e6ba2008-01-12 18:39:25 +0000137 QualType ResTy = Context.CharTy.getQualifiedType(QualType::Const);
Chris Lattner0a8c2322008-01-12 19:32:28 +0000138 ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0);
Chris Lattnera81a0272008-01-12 08:14:25 +0000139 return new PreDefinedExpr(Loc, ResTy, IT);
Chris Lattnere168f762006-11-10 05:29:30 +0000140}
141
Steve Naroff83895f72007-09-16 03:34:24 +0000142Sema::ExprResult Sema::ActOnCharacterConstant(const Token &Tok) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000143 llvm::SmallString<16> CharBuffer;
Steve Naroffae4143e2007-04-26 20:39:23 +0000144 CharBuffer.resize(Tok.getLength());
145 const char *ThisTokBegin = &CharBuffer[0];
146 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
147
148 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
149 Tok.getLocation(), PP);
150 if (Literal.hadError())
151 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000152 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
153 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000154}
155
Steve Naroff83895f72007-09-16 03:34:24 +0000156Action::ExprResult Sema::ActOnNumericConstant(const Token &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000157 // fast path for a single digit (which is quite common). A single digit
158 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
159 if (Tok.getLength() == 1) {
160 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Chris Lattner67ca9252007-05-21 01:08:44 +0000161
Chris Lattner9cf21c52007-09-04 02:45:27 +0000162 unsigned IntSize = static_cast<unsigned>(
163 Context.getTypeSize(Context.IntTy, Tok.getLocation()));
Chris Lattner23b7eb62007-06-15 23:05:46 +0000164 return ExprResult(new IntegerLiteral(llvm::APInt(IntSize, *t-'0'),
165 Context.IntTy,
Steve Naroff509fe022007-05-17 01:16:00 +0000166 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000167 }
Chris Lattner23b7eb62007-06-15 23:05:46 +0000168 llvm::SmallString<512> IntegerBuffer;
Steve Naroff8160ea22007-03-06 01:09:46 +0000169 IntegerBuffer.resize(Tok.getLength());
170 const char *ThisTokBegin = &IntegerBuffer[0];
171
Chris Lattner67ca9252007-05-21 01:08:44 +0000172 // Get the spelling of the token, which eliminates trigraphs, etc.
Steve Naroff8160ea22007-03-06 01:09:46 +0000173 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000174 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000175 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000176 if (Literal.hadError)
177 return ExprResult(true);
Chris Lattner67ca9252007-05-21 01:08:44 +0000178
Chris Lattner1c20a172007-08-26 03:42:43 +0000179 Expr *Res;
180
181 if (Literal.isFloatingLiteral()) {
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000182 QualType Ty;
183 const llvm::fltSemantics *Format;
184 uint64_t Size; unsigned Align;
185
186 if (Literal.isFloat) {
187 Ty = Context.FloatTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000188 Context.Target.getFloatInfo(Size, Align, Format,
189 Context.getFullLoc(Tok.getLocation()));
190
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000191 } else if (Literal.isLong) {
192 Ty = Context.LongDoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000193 Context.Target.getLongDoubleInfo(Size, Align, Format,
194 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000195 } else {
196 Ty = Context.DoubleTy;
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000197 Context.Target.getDoubleInfo(Size, Align, Format,
198 Context.getFullLoc(Tok.getLocation()));
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000199 }
200
Ted Kremenek3a2c9502007-11-29 00:56:49 +0000201 // isExact will be set by GetFloatValue().
202 bool isExact = false;
203
204 Res = new FloatingLiteral(Literal.GetFloatValue(*Format,&isExact), &isExact,
205 Ty, Tok.getLocation());
206
Chris Lattner1c20a172007-08-26 03:42:43 +0000207 } else if (!Literal.isIntegerLiteral()) {
208 return ExprResult(true);
209 } else {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000210 QualType t;
Chris Lattner67ca9252007-05-21 01:08:44 +0000211
Neil Boothac582c52007-08-29 22:00:19 +0000212 // long long is a C99 feature.
213 if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x &&
Neil Booth4a1ee052007-08-29 22:13:52 +0000214 Literal.isLongLong)
Neil Boothac582c52007-08-29 22:00:19 +0000215 Diag(Tok.getLocation(), diag::ext_longlong);
216
Chris Lattner67ca9252007-05-21 01:08:44 +0000217 // Get the value in the widest-possible width.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000218 llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(
219 Context.getFullLoc(Tok.getLocation())), 0);
Chris Lattner67ca9252007-05-21 01:08:44 +0000220
221 if (Literal.GetIntegerValue(ResultVal)) {
222 // If this value didn't fit into uintmax_t, warn and force to ull.
223 Diag(Tok.getLocation(), diag::warn_integer_too_large);
224 t = Context.UnsignedLongLongTy;
Chris Lattner4481b422007-07-14 01:29:45 +0000225 assert(Context.getTypeSize(t, Tok.getLocation()) ==
Chris Lattner67ca9252007-05-21 01:08:44 +0000226 ResultVal.getBitWidth() && "long long is not intmax_t?");
Steve Naroff09ef4742007-03-09 23:16:33 +0000227 } else {
Chris Lattner67ca9252007-05-21 01:08:44 +0000228 // If this value fits into a ULL, try to figure out what else it fits into
229 // according to the rules of C99 6.4.4.1p5.
230
231 // Octal, Hexadecimal, and integers with a U suffix are allowed to
232 // be an unsigned int.
233 bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10;
234
235 // Check from smallest to largest, picking the smallest type we can.
Chris Lattner7b939cf2007-08-23 21:58:08 +0000236 if (!Literal.isLong && !Literal.isLongLong) {
237 // Are int/unsigned possibilities?
Chris Lattner9cf21c52007-09-04 02:45:27 +0000238 unsigned IntSize = static_cast<unsigned>(
239 Context.getTypeSize(Context.IntTy,Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000240 // Does it fit in a unsigned int?
241 if (ResultVal.isIntN(IntSize)) {
242 // Does it fit in a signed int?
243 if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0)
244 t = Context.IntTy;
245 else if (AllowUnsigned)
246 t = Context.UnsignedIntTy;
247 }
248
249 if (!t.isNull())
250 ResultVal.trunc(IntSize);
251 }
252
253 // Are long/unsigned long possibilities?
254 if (t.isNull() && !Literal.isLongLong) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000255 unsigned LongSize = static_cast<unsigned>(
256 Context.getTypeSize(Context.LongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000257
258 // Does it fit in a unsigned long?
259 if (ResultVal.isIntN(LongSize)) {
260 // Does it fit in a signed long?
261 if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0)
262 t = Context.LongTy;
263 else if (AllowUnsigned)
264 t = Context.UnsignedLongTy;
265 }
266 if (!t.isNull())
267 ResultVal.trunc(LongSize);
268 }
269
270 // Finally, check long long if needed.
271 if (t.isNull()) {
Chris Lattner9cf21c52007-09-04 02:45:27 +0000272 unsigned LongLongSize = static_cast<unsigned>(
273 Context.getTypeSize(Context.LongLongTy, Tok.getLocation()));
Chris Lattner67ca9252007-05-21 01:08:44 +0000274
275 // Does it fit in a unsigned long long?
276 if (ResultVal.isIntN(LongLongSize)) {
277 // Does it fit in a signed long long?
278 if (!Literal.isUnsigned && ResultVal[LongLongSize-1] == 0)
279 t = Context.LongLongTy;
280 else if (AllowUnsigned)
281 t = Context.UnsignedLongLongTy;
282 }
283 }
284
285 // If we still couldn't decide a type, we probably have something that
286 // does not fit in a signed long long, but has no U suffix.
287 if (t.isNull()) {
288 Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed);
289 t = Context.UnsignedLongLongTy;
290 }
Steve Naroff09ef4742007-03-09 23:16:33 +0000291 }
Chris Lattner67ca9252007-05-21 01:08:44 +0000292
Chris Lattner1c20a172007-08-26 03:42:43 +0000293 Res = new IntegerLiteral(ResultVal, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000294 }
Chris Lattner1c20a172007-08-26 03:42:43 +0000295
296 // If this is an imaginary literal, create the ImaginaryLiteral wrapper.
297 if (Literal.isImaginary)
298 Res = new ImaginaryLiteral(Res, Context.getComplexType(Res->getType()));
299
300 return Res;
Chris Lattnere168f762006-11-10 05:29:30 +0000301}
302
Steve Naroff83895f72007-09-16 03:34:24 +0000303Action::ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R,
Chris Lattnere168f762006-11-10 05:29:30 +0000304 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000305 Expr *e = (Expr *)Val;
Steve Naroff83895f72007-09-16 03:34:24 +0000306 assert((e != 0) && "ActOnParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000307 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000308}
309
Steve Naroff71b59a92007-06-04 22:22:31 +0000310/// The UsualUnaryConversions() function is *not* called by this routine.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000311/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000312QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
313 SourceLocation OpLoc, bool isSizeof) {
314 // C99 6.5.3.4p1:
315 if (isa<FunctionType>(exprType) && isSizeof)
316 // alignof(function) is allowed.
317 Diag(OpLoc, diag::ext_sizeof_function_type);
318 else if (exprType->isVoidType())
319 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
320 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000321 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000322 diag::err_alignof_incomplete_type,
323 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000324 return QualType(); // error
325 }
326 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
327 return Context.getSizeType();
328}
329
Chris Lattnere168f762006-11-10 05:29:30 +0000330Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000331ActOnSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000332 SourceLocation LPLoc, TypeTy *Ty,
333 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000334 // If error parsing type, ignore.
335 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000336
337 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000338 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000339
Steve Naroff043d45d2007-05-15 02:32:35 +0000340 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
341
342 if (resultType.isNull())
343 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000344 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000345}
346
Chris Lattner74ed76b2007-08-24 21:41:10 +0000347QualType Sema::CheckRealImagOperand(Expr *&V, SourceLocation Loc) {
Chris Lattner30b5dd02007-08-24 21:16:53 +0000348 DefaultFunctionArrayConversion(V);
349
Chris Lattnere267f5d2007-08-26 05:39:26 +0000350 // These operators return the element type of a complex type.
Chris Lattner30b5dd02007-08-24 21:16:53 +0000351 if (const ComplexType *CT = V->getType()->getAsComplexType())
352 return CT->getElementType();
Chris Lattnere267f5d2007-08-26 05:39:26 +0000353
354 // Otherwise they pass through real integer and floating point types here.
355 if (V->getType()->isArithmeticType())
356 return V->getType();
357
358 // Reject anything else.
359 Diag(Loc, diag::err_realimag_invalid_type, V->getType().getAsString());
360 return QualType();
Chris Lattner30b5dd02007-08-24 21:16:53 +0000361}
362
363
Chris Lattnere168f762006-11-10 05:29:30 +0000364
Steve Naroff83895f72007-09-16 03:34:24 +0000365Action::ExprResult Sema::ActOnPostfixUnaryOp(SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000366 tok::TokenKind Kind,
367 ExprTy *Input) {
368 UnaryOperator::Opcode Opc;
369 switch (Kind) {
370 default: assert(0 && "Unknown unary op!");
371 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
372 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
373 }
Steve Naroff71ce2e02007-05-18 22:53:50 +0000374 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +0000375 if (result.isNull())
376 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000377 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000378}
379
380Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000381ActOnArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000382 ExprTy *Idx, SourceLocation RLoc) {
Chris Lattner5981db42007-07-15 23:59:53 +0000383 Expr *LHSExp = static_cast<Expr*>(Base), *RHSExp = static_cast<Expr*>(Idx);
Chris Lattner36d572b2007-07-16 00:14:47 +0000384
385 // Perform default conversions.
386 DefaultFunctionArrayConversion(LHSExp);
387 DefaultFunctionArrayConversion(RHSExp);
Chris Lattner5981db42007-07-15 23:59:53 +0000388
Chris Lattner36d572b2007-07-16 00:14:47 +0000389 QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000390
Steve Naroffc1aadb12007-03-28 21:49:40 +0000391 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
Chris Lattnerf17bd422007-08-30 17:45:32 +0000392 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000393 // in the subscript position. As a result, we need to derive the array base
394 // and index from the expression types.
Chris Lattner36d572b2007-07-16 00:14:47 +0000395 Expr *BaseExpr, *IndexExpr;
396 QualType ResultType;
Chris Lattnerc996b172007-07-31 16:53:04 +0000397 if (const PointerType *PTy = LHSTy->getAsPointerType()) {
Chris Lattner36d572b2007-07-16 00:14:47 +0000398 BaseExpr = LHSExp;
399 IndexExpr = RHSExp;
400 // FIXME: need to deal with const...
401 ResultType = PTy->getPointeeType();
Chris Lattnerc996b172007-07-31 16:53:04 +0000402 } else if (const PointerType *PTy = RHSTy->getAsPointerType()) {
Chris Lattneraee0cfd2007-07-16 00:23:25 +0000403 // Handle the uncommon case of "123[Ptr]".
Chris Lattner36d572b2007-07-16 00:14:47 +0000404 BaseExpr = RHSExp;
405 IndexExpr = LHSExp;
406 // FIXME: need to deal with const...
407 ResultType = PTy->getPointeeType();
Chris Lattner41977962007-07-31 19:29:30 +0000408 } else if (const VectorType *VTy = LHSTy->getAsVectorType()) {
409 BaseExpr = LHSExp; // vectors: V[123]
Chris Lattner36d572b2007-07-16 00:14:47 +0000410 IndexExpr = RHSExp;
Steve Naroff01047312007-08-03 22:40:33 +0000411
412 // Component access limited to variables (reject vec4.rg[1]).
413 if (!isa<DeclRefExpr>(BaseExpr))
414 return Diag(LLoc, diag::err_ocuvector_component_access,
415 SourceRange(LLoc, RLoc));
Chris Lattner36d572b2007-07-16 00:14:47 +0000416 // FIXME: need to deal with const...
417 ResultType = VTy->getElementType();
Steve Naroffb3096442007-06-09 03:47:53 +0000418 } else {
Chris Lattner5981db42007-07-15 23:59:53 +0000419 return Diag(LHSExp->getLocStart(), diag::err_typecheck_subscript_value,
420 RHSExp->getSourceRange());
Steve Naroffb3096442007-06-09 03:47:53 +0000421 }
Steve Naroffc1aadb12007-03-28 21:49:40 +0000422 // C99 6.5.2.1p1
Chris Lattner36d572b2007-07-16 00:14:47 +0000423 if (!IndexExpr->getType()->isIntegerType())
424 return Diag(IndexExpr->getLocStart(), diag::err_typecheck_subscript,
425 IndexExpr->getSourceRange());
Steve Naroffb29cdd52007-07-10 18:23:31 +0000426
Chris Lattner36d572b2007-07-16 00:14:47 +0000427 // C99 6.5.2.1p1: "shall have type "pointer to *object* type". In practice,
428 // the following check catches trying to index a pointer to a function (e.g.
429 // void (*)(int)). Functions are not objects in C99.
430 if (!ResultType->isObjectType())
431 return Diag(BaseExpr->getLocStart(),
432 diag::err_typecheck_subscript_not_object,
433 BaseExpr->getType().getAsString(), BaseExpr->getSourceRange());
434
435 return new ArraySubscriptExpr(LHSExp, RHSExp, ResultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000436}
437
Steve Narofff8fd09e2007-07-27 22:15:19 +0000438QualType Sema::
439CheckOCUVectorComponent(QualType baseType, SourceLocation OpLoc,
440 IdentifierInfo &CompName, SourceLocation CompLoc) {
Chris Lattner41977962007-07-31 19:29:30 +0000441 const OCUVectorType *vecType = baseType->getAsOCUVectorType();
Steve Narofff8fd09e2007-07-27 22:15:19 +0000442
443 // The vector accessor can't exceed the number of elements.
444 const char *compStr = CompName.getName();
445 if (strlen(compStr) > vecType->getNumElements()) {
446 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
447 baseType.getAsString(), SourceRange(CompLoc));
448 return QualType();
449 }
450 // The component names must come from the same set.
Chris Lattner7e152db2007-08-02 22:33:49 +0000451 if (vecType->getPointAccessorIdx(*compStr) != -1) {
452 do
453 compStr++;
454 while (*compStr && vecType->getPointAccessorIdx(*compStr) != -1);
455 } else if (vecType->getColorAccessorIdx(*compStr) != -1) {
456 do
457 compStr++;
458 while (*compStr && vecType->getColorAccessorIdx(*compStr) != -1);
459 } else if (vecType->getTextureAccessorIdx(*compStr) != -1) {
460 do
461 compStr++;
462 while (*compStr && vecType->getTextureAccessorIdx(*compStr) != -1);
463 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000464
465 if (*compStr) {
466 // We didn't get to the end of the string. This means the component names
467 // didn't come from the same set *or* we encountered an illegal name.
468 Diag(OpLoc, diag::err_ocuvector_component_name_illegal,
469 std::string(compStr,compStr+1), SourceRange(CompLoc));
470 return QualType();
471 }
472 // Each component accessor can't exceed the vector type.
473 compStr = CompName.getName();
474 while (*compStr) {
475 if (vecType->isAccessorWithinNumElements(*compStr))
476 compStr++;
477 else
478 break;
479 }
480 if (*compStr) {
481 // We didn't get to the end of the string. This means a component accessor
482 // exceeds the number of elements in the vector.
483 Diag(OpLoc, diag::err_ocuvector_component_exceeds_length,
484 baseType.getAsString(), SourceRange(CompLoc));
485 return QualType();
486 }
487 // The component accessor looks fine - now we need to compute the actual type.
488 // The vector type is implied by the component accessor. For example,
489 // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc.
490 unsigned CompSize = strlen(CompName.getName());
491 if (CompSize == 1)
492 return vecType->getElementType();
Steve Naroffddf5a1d2007-07-29 16:33:31 +0000493
494 QualType VT = Context.getOCUVectorType(vecType->getElementType(), CompSize);
495 // Now look up the TypeDefDecl from the vector type. Without this,
496 // diagostics look bad. We want OCU vector types to appear built-in.
497 for (unsigned i = 0, e = OCUVectorDecls.size(); i != e; ++i) {
498 if (OCUVectorDecls[i]->getUnderlyingType() == VT)
499 return Context.getTypedefType(OCUVectorDecls[i]);
500 }
501 return VT; // should never get here (a typedef type should always be found).
Steve Narofff8fd09e2007-07-27 22:15:19 +0000502}
503
Chris Lattnere168f762006-11-10 05:29:30 +0000504Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000505ActOnMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000506 tok::TokenKind OpKind, SourceLocation MemberLoc,
507 IdentifierInfo &Member) {
Steve Naroff185616f2007-07-26 03:11:44 +0000508 Expr *BaseExpr = static_cast<Expr *>(Base);
509 assert(BaseExpr && "no record expression");
Steve Naroffeaaae462007-12-16 21:42:28 +0000510
511 // Perform default conversions.
512 DefaultFunctionArrayConversion(BaseExpr);
Steve Naroffca8f7122007-04-01 01:41:35 +0000513
Steve Naroff185616f2007-07-26 03:11:44 +0000514 QualType BaseType = BaseExpr->getType();
515 assert(!BaseType.isNull() && "no type for member expression");
Steve Naroffca8f7122007-04-01 01:41:35 +0000516
Steve Narofff1e53692007-03-23 22:27:02 +0000517 if (OpKind == tok::arrow) {
Chris Lattnerc996b172007-07-31 16:53:04 +0000518 if (const PointerType *PT = BaseType->getAsPointerType())
Steve Naroff185616f2007-07-26 03:11:44 +0000519 BaseType = PT->getPointeeType();
520 else
521 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow,
522 SourceRange(MemberLoc));
Steve Narofff1e53692007-03-23 22:27:02 +0000523 }
Steve Narofff8fd09e2007-07-27 22:15:19 +0000524 // The base type is either a record or an OCUVectorType.
Chris Lattner41977962007-07-31 19:29:30 +0000525 if (const RecordType *RTy = BaseType->getAsRecordType()) {
Steve Naroff185616f2007-07-26 03:11:44 +0000526 RecordDecl *RDecl = RTy->getDecl();
527 if (RTy->isIncompleteType())
528 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RDecl->getName(),
529 BaseExpr->getSourceRange());
530 // The record definition is complete, now make sure the member is valid.
Steve Narofff8fd09e2007-07-27 22:15:19 +0000531 FieldDecl *MemberDecl = RDecl->getMember(&Member);
532 if (!MemberDecl)
Steve Naroff185616f2007-07-26 03:11:44 +0000533 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName(),
534 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000535 return new MemberExpr(BaseExpr, OpKind==tok::arrow, MemberDecl, MemberLoc);
536 } else if (BaseType->isOCUVectorType() && OpKind == tok::period) {
Steve Naroff01047312007-08-03 22:40:33 +0000537 // Component access limited to variables (reject vec4.rg.g).
538 if (!isa<DeclRefExpr>(BaseExpr))
539 return Diag(OpLoc, diag::err_ocuvector_component_access,
540 SourceRange(MemberLoc));
Steve Narofff8fd09e2007-07-27 22:15:19 +0000541 QualType ret = CheckOCUVectorComponent(BaseType, OpLoc, Member, MemberLoc);
542 if (ret.isNull())
543 return true;
Chris Lattnerd268a7a2007-08-03 17:31:20 +0000544 return new OCUVectorElementExpr(ret, BaseExpr, Member, MemberLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000545 } else if (BaseType->isObjCInterfaceType()) {
546 ObjCInterfaceDecl *IFace;
547 if (isa<ObjCInterfaceType>(BaseType.getCanonicalType()))
548 IFace = dyn_cast<ObjCInterfaceType>(BaseType)->getDecl();
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000549 else
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000550 IFace = dyn_cast<ObjCQualifiedInterfaceType>(BaseType)->getDecl();
551 ObjCInterfaceDecl *clsDeclared;
552 if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(&Member, clsDeclared))
Fariborz Jahanian21f54ee2007-11-12 22:29:28 +0000553 return new ObjCIvarRefExpr(IV, IV->getType(), MemberLoc, BaseExpr,
554 OpKind==tok::arrow);
555 }
556 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion,
557 SourceRange(MemberLoc));
Chris Lattnere168f762006-11-10 05:29:30 +0000558}
559
Steve Naroff83895f72007-09-16 03:34:24 +0000560/// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
Chris Lattnere168f762006-11-10 05:29:30 +0000561/// This provides the location of the left/right parens and a list of comma
562/// locations.
563Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000564ActOnCallExpr(ExprTy *fn, SourceLocation LParenLoc,
Chris Lattner08464942007-12-28 05:29:59 +0000565 ExprTy **args, unsigned NumArgs,
Chris Lattnere168f762006-11-10 05:29:30 +0000566 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Chris Lattner38dbdb22007-07-21 03:03:59 +0000567 Expr *Fn = static_cast<Expr *>(fn);
568 Expr **Args = reinterpret_cast<Expr**>(args);
569 assert(Fn && "no function call expression");
Steve Naroff8563f652007-05-28 19:25:56 +0000570
Chris Lattner08464942007-12-28 05:29:59 +0000571 // Make the call expr early, before semantic checks. This guarantees cleanup
572 // of arguments and function on error.
573 llvm::OwningPtr<CallExpr> TheCall(new CallExpr(Fn, Args, NumArgs,
574 Context.BoolTy, RParenLoc));
575
576 // Promote the function operand.
577 TheCall->setCallee(UsualUnaryConversions(Fn));
578
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000579 // C99 6.5.2.2p1 - "The expression that denotes the called function shall have
580 // type pointer to function".
Chris Lattner08464942007-12-28 05:29:59 +0000581 const PointerType *PT = Fn->getType()->getAsPointerType();
Chris Lattner3d01e4e2007-06-06 05:14:05 +0000582 if (PT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000583 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
584 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000585 const FunctionType *FuncT = PT->getPointeeType()->getAsFunctionType();
586 if (FuncT == 0)
Chris Lattner38dbdb22007-07-21 03:03:59 +0000587 return Diag(Fn->getLocStart(), diag::err_typecheck_call_not_function,
588 SourceRange(Fn->getLocStart(), RParenLoc));
Chris Lattner08464942007-12-28 05:29:59 +0000589
590 // We know the result type of the call, set it.
591 TheCall->setType(FuncT->getResultType());
Steve Naroffb8c289d2007-05-08 22:18:00 +0000592
Chris Lattner08464942007-12-28 05:29:59 +0000593 if (const FunctionTypeProto *Proto = dyn_cast<FunctionTypeProto>(FuncT)) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000594 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
595 // assignment, to the types of the corresponding parameter, ...
Chris Lattner08464942007-12-28 05:29:59 +0000596 unsigned NumArgsInProto = Proto->getNumArgs();
597 unsigned NumArgsToCheck = NumArgs;
Steve Naroff17f76e02007-05-03 21:03:48 +0000598
Chris Lattner08464942007-12-28 05:29:59 +0000599 // If too few arguments are available, don't make the call.
600 if (NumArgs < NumArgsInProto)
601 return Diag(RParenLoc, diag::err_typecheck_call_too_few_args,
602 Fn->getSourceRange());
Steve Naroff17f76e02007-05-03 21:03:48 +0000603
Chris Lattner08464942007-12-28 05:29:59 +0000604 // If too many are passed and not variadic, error on the extras and drop
605 // them.
606 if (NumArgs > NumArgsInProto) {
607 if (!Proto->isVariadic()) {
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000608 Diag(Args[NumArgsInProto]->getLocStart(),
Chris Lattner38dbdb22007-07-21 03:03:59 +0000609 diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
Chris Lattnera6f5ab52007-07-21 03:09:58 +0000610 SourceRange(Args[NumArgsInProto]->getLocStart(),
Chris Lattner08464942007-12-28 05:29:59 +0000611 Args[NumArgs-1]->getLocEnd()));
612 // This deletes the extra arguments.
613 TheCall->setNumArgs(NumArgsInProto);
Steve Naroff8563f652007-05-28 19:25:56 +0000614 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000615 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000616 }
Chris Lattner08464942007-12-28 05:29:59 +0000617
Steve Naroff17f76e02007-05-03 21:03:48 +0000618 // Continue to check argument types (even if we have too few/many args).
Chris Lattner08464942007-12-28 05:29:59 +0000619 for (unsigned i = 0; i != NumArgsToCheck; i++) {
620 Expr *Arg = Args[i];
Chris Lattner9bad62c2008-01-04 18:04:52 +0000621 QualType ProtoArgType = Proto->getArgType(i);
622 QualType ArgType = Arg->getType();
Steve Naroff44fd8ff2007-07-24 21:46:40 +0000623
Chris Lattner08464942007-12-28 05:29:59 +0000624 // Compute implicit casts from the operand to the formal argument type.
Chris Lattner9bad62c2008-01-04 18:04:52 +0000625 AssignConvertType ConvTy =
626 CheckSingleAssignmentConstraints(ProtoArgType, Arg);
Chris Lattner08464942007-12-28 05:29:59 +0000627 TheCall->setArg(i, Arg);
628
Chris Lattner9bad62c2008-01-04 18:04:52 +0000629 if (DiagnoseAssignmentResult(ConvTy, Arg->getLocStart(), ProtoArgType,
630 ArgType, Arg, "passing"))
631 return true;
Steve Naroff17f76e02007-05-03 21:03:48 +0000632 }
Chris Lattner08464942007-12-28 05:29:59 +0000633
634 // If this is a variadic call, handle args passed through "...".
635 if (Proto->isVariadic()) {
Steve Naroff0b661582007-08-28 23:30:39 +0000636 // Promote the arguments (C99 6.5.2.2p7).
Chris Lattner08464942007-12-28 05:29:59 +0000637 for (unsigned i = NumArgsInProto; i != NumArgs; i++) {
638 Expr *Arg = Args[i];
639 DefaultArgumentPromotion(Arg);
640 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000641 }
Steve Naroff0b661582007-08-28 23:30:39 +0000642 }
Chris Lattner08464942007-12-28 05:29:59 +0000643 } else {
644 assert(isa<FunctionTypeNoProto>(FuncT) && "Unknown FunctionType!");
645
Steve Naroff0b661582007-08-28 23:30:39 +0000646 // Promote the arguments (C99 6.5.2.2p6).
Chris Lattner08464942007-12-28 05:29:59 +0000647 for (unsigned i = 0; i != NumArgs; i++) {
648 Expr *Arg = Args[i];
649 DefaultArgumentPromotion(Arg);
650 TheCall->setArg(i, Arg);
Steve Naroff0b661582007-08-28 23:30:39 +0000651 }
Steve Naroffae4143e2007-04-26 20:39:23 +0000652 }
Chris Lattner08464942007-12-28 05:29:59 +0000653
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000654 // Do special checking on direct calls to functions.
655 if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
656 if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
657 if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
Chris Lattner08464942007-12-28 05:29:59 +0000658 if (CheckFunctionCall(FDecl, TheCall.get()))
Anders Carlsson98f07902007-08-17 05:31:46 +0000659 return true;
Chris Lattnerb87b1b32007-08-10 20:18:51 +0000660
Chris Lattner08464942007-12-28 05:29:59 +0000661 return TheCall.take();
Chris Lattnere168f762006-11-10 05:29:30 +0000662}
663
664Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000665ActOnCompoundLiteral(SourceLocation LParenLoc, TypeTy *Ty,
Steve Naroff57eb2c52007-07-19 21:32:11 +0000666 SourceLocation RParenLoc, ExprTy *InitExpr) {
Steve Naroff83895f72007-09-16 03:34:24 +0000667 assert((Ty != 0) && "ActOnCompoundLiteral(): missing type");
Steve Narofffbd09832007-07-19 01:06:55 +0000668 QualType literalType = QualType::getFromOpaquePtr(Ty);
Steve Naroff57eb2c52007-07-19 21:32:11 +0000669 // FIXME: put back this assert when initializers are worked out.
Steve Naroff83895f72007-09-16 03:34:24 +0000670 //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression");
Steve Naroff57eb2c52007-07-19 21:32:11 +0000671 Expr *literalExpr = static_cast<Expr*>(InitExpr);
Anders Carlsson2c1ec6d2007-12-05 07:24:19 +0000672
Steve Naroff91f78082007-12-10 22:44:33 +0000673 // FIXME: add more semantic analysis (C99 6.5.2.5).
Steve Naroff98f72032008-01-10 22:15:12 +0000674 if (CheckInitializerTypes(literalExpr, literalType))
Steve Naroff08ddb8c2008-01-09 20:58:06 +0000675 return true;
Steve Naroffd32419d2008-01-14 18:19:28 +0000676
677 bool isFileScope = !CurFunctionDecl && !CurMethodDecl;
678 if (isFileScope) { // 6.5.2.5p3
Steve Naroff98f72032008-01-10 22:15:12 +0000679 if (CheckForConstantInitializer(literalExpr, literalType))
680 return true;
681 }
Steve Naroffd32419d2008-01-14 18:19:28 +0000682 return new CompoundLiteralExpr(LParenLoc, literalType, literalExpr, isFileScope);
Steve Narofffbd09832007-07-19 01:06:55 +0000683}
684
685Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000686ActOnInitList(SourceLocation LBraceLoc, ExprTy **initlist, unsigned NumInit,
Anders Carlsson4692db02007-08-31 04:56:16 +0000687 SourceLocation RBraceLoc) {
Steve Naroff2fea1392007-09-02 02:04:30 +0000688 Expr **InitList = reinterpret_cast<Expr**>(initlist);
Anders Carlsson4692db02007-08-31 04:56:16 +0000689
Steve Naroff30d242c2007-09-15 18:49:24 +0000690 // Semantic analysis for initializers is done by ActOnDeclarator() and
Steve Naroff7d2c5ed2007-09-03 01:24:23 +0000691 // CheckInitializer() - it requires knowledge of the object being intialized.
Anders Carlsson4692db02007-08-31 04:56:16 +0000692
Steve Naroffb03f5942007-09-02 20:30:18 +0000693 InitListExpr *e = new InitListExpr(LBraceLoc, InitList, NumInit, RBraceLoc);
694 e->setType(Context.VoidTy); // FIXME: just a place holder for now.
695 return e;
Steve Narofffbd09832007-07-19 01:06:55 +0000696}
697
Chris Lattner6c9ffe92007-12-20 00:44:32 +0000698bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty) {
Anders Carlssonde71adf2007-11-27 05:51:55 +0000699 assert(VectorTy->isVectorType() && "Not a vector type!");
700
701 if (Ty->isVectorType() || Ty->isIntegerType()) {
702 if (Context.getTypeSize(VectorTy, SourceLocation()) !=
703 Context.getTypeSize(Ty, SourceLocation()))
704 return Diag(R.getBegin(),
705 Ty->isVectorType() ?
706 diag::err_invalid_conversion_between_vectors :
707 diag::err_invalid_conversion_between_vector_and_integer,
708 VectorTy.getAsString().c_str(),
709 Ty.getAsString().c_str(), R);
710 } else
711 return Diag(R.getBegin(),
712 diag::err_invalid_conversion_between_vector_and_scalar,
713 VectorTy.getAsString().c_str(),
714 Ty.getAsString().c_str(), R);
715
716 return false;
717}
718
Steve Narofffbd09832007-07-19 01:06:55 +0000719Action::ExprResult Sema::
Steve Naroff83895f72007-09-16 03:34:24 +0000720ActOnCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
Chris Lattnere168f762006-11-10 05:29:30 +0000721 SourceLocation RParenLoc, ExprTy *Op) {
Steve Naroff83895f72007-09-16 03:34:24 +0000722 assert((Ty != 0) && (Op != 0) && "ActOnCastExpr(): missing type or expr");
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000723
724 Expr *castExpr = static_cast<Expr*>(Op);
725 QualType castType = QualType::getFromOpaquePtr(Ty);
726
Steve Naroff43b8f7f2007-08-31 00:32:44 +0000727 UsualUnaryConversions(castExpr);
728
Chris Lattnerbd270732007-07-18 16:00:06 +0000729 // C99 6.5.4p2: the cast type needs to be void or scalar and the expression
730 // type needs to be scalar.
Chris Lattner3bc4d202007-10-29 04:26:44 +0000731 if (!castType->isVoidType()) { // Cast to void allows any expr type.
732 if (!castType->isScalarType())
733 return Diag(LParenLoc, diag::err_typecheck_cond_expect_scalar,
734 castType.getAsString(), SourceRange(LParenLoc, RParenLoc));
Anders Carlssonde71adf2007-11-27 05:51:55 +0000735 if (!castExpr->getType()->isScalarType())
Chris Lattner3bc4d202007-10-29 04:26:44 +0000736 return Diag(castExpr->getLocStart(),
737 diag::err_typecheck_expect_scalar_operand,
738 castExpr->getType().getAsString(),castExpr->getSourceRange());
Anders Carlssonde71adf2007-11-27 05:51:55 +0000739
740 if (castExpr->getType()->isVectorType()) {
741 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
742 castExpr->getType(), castType))
743 return true;
744 } else if (castType->isVectorType()) {
745 if (CheckVectorCast(SourceRange(LParenLoc, RParenLoc),
746 castType, castExpr->getType()))
747 return true;
Chris Lattner3bc4d202007-10-29 04:26:44 +0000748 }
Steve Naroff1a2cf6b2007-07-16 23:25:18 +0000749 }
750 return new CastExpr(castType, castExpr, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000751}
752
Steve Naroffa78c4642007-10-18 05:13:08 +0000753// promoteExprToType - a helper function to ensure we create exactly one
754// ImplicitCastExpr.
755static void promoteExprToType(Expr *&expr, QualType type) {
756 if (ImplicitCastExpr *impCast = dyn_cast<ImplicitCastExpr>(expr))
757 impCast->setType(type);
758 else
759 expr = new ImplicitCastExpr(type, expr);
760 return;
761}
762
Chris Lattner2ab40a62007-11-26 01:40:58 +0000763/// Note that lex is not null here, even if this is the gnu "x ?: y" extension.
764/// In that case, lex = cond.
Steve Narofff8a28c52007-05-15 20:29:32 +0000765inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
Steve Naroff7a5af782007-07-13 16:58:59 +0000766 Expr *&cond, Expr *&lex, Expr *&rex, SourceLocation questionLoc) {
Steve Naroff31090012007-07-16 21:54:35 +0000767 UsualUnaryConversions(cond);
768 UsualUnaryConversions(lex);
769 UsualUnaryConversions(rex);
770 QualType condT = cond->getType();
771 QualType lexT = lex->getType();
772 QualType rexT = rex->getType();
773
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000774 // first, check the condition.
Steve Naroff7a5af782007-07-13 16:58:59 +0000775 if (!condT->isScalarType()) { // C99 6.5.15p2
776 Diag(cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
777 condT.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000778 return QualType();
779 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000780
781 // Now check the two expressions.
782
783 // If both operands have arithmetic type, do the usual arithmetic conversions
784 // to find a common type: C99 6.5.15p3,5.
785 if (lexT->isArithmeticType() && rexT->isArithmeticType()) {
Steve Naroffdbd9e892007-07-17 00:58:39 +0000786 UsualArithmeticConversions(lex, rex);
787 return lex->getType();
788 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000789
790 // If both operands are the same structure or union type, the result is that
791 // type.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000792 if (const RecordType *LHSRT = lexT->getAsRecordType()) { // C99 6.5.15p3
Chris Lattnere2949f42008-01-06 22:42:25 +0000793 if (const RecordType *RHSRT = rexT->getAsRecordType())
Chris Lattner2ab40a62007-11-26 01:40:58 +0000794 if (LHSRT->getDecl() == RHSRT->getDecl())
Chris Lattnere2949f42008-01-06 22:42:25 +0000795 // "If both the operands have structure or union type, the result has
796 // that type." This implies that CV qualifiers are dropped.
797 return lexT.getUnqualifiedType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000798 }
Chris Lattnere2949f42008-01-06 22:42:25 +0000799
800 // C99 6.5.15p5: "If both operands have void type, the result has void type."
801 if (lexT->isVoidType() && rexT->isVoidType())
802 return lexT.getUnqualifiedType();
Steve Naroff039ad3c2008-01-08 01:11:38 +0000803
804 // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has
805 // the type of the other operand."
806 if (lexT->isPointerType() && rex->isNullPointerConstant(Context)) {
807 promoteExprToType(rex, lexT); // promote the null to a pointer.
808 return lexT;
809 }
810 if (rexT->isPointerType() && lex->isNullPointerConstant(Context)) {
811 promoteExprToType(lex, rexT); // promote the null to a pointer.
812 return rexT;
813 }
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000814 // Handle the case where both operands are pointers before we handle null
815 // pointer constants in case both operands are null pointer constants.
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000816 if (const PointerType *LHSPT = lexT->getAsPointerType()) { // C99 6.5.15p3,6
817 if (const PointerType *RHSPT = rexT->getAsPointerType()) {
818 // get the "pointed to" types
819 QualType lhptee = LHSPT->getPointeeType();
820 QualType rhptee = RHSPT->getPointeeType();
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000821
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000822 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
823 if (lhptee->isVoidType() &&
824 (rhptee->isObjectType() || rhptee->isIncompleteType()))
825 return lexT;
826 if (rhptee->isVoidType() &&
827 (lhptee->isObjectType() || lhptee->isIncompleteType()))
828 return rexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000829
Steve Naroff32e44c02007-10-15 20:41:53 +0000830 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
831 rhptee.getUnqualifiedType())) {
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000832 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
833 lexT.getAsString(), rexT.getAsString(),
834 lex->getSourceRange(), rex->getSourceRange());
835 return lexT; // FIXME: this is an _ext - is this return o.k?
836 }
837 // The pointer types are compatible.
Chris Lattnerf17bd422007-08-30 17:45:32 +0000838 // C99 6.5.15p6: If both operands are pointers to compatible types *or* to
839 // differently qualified versions of compatible types, the result type is
840 // a pointer to an appropriately qualified version of the *composite*
841 // type.
Chris Lattner4d3b0f52008-01-06 22:50:31 +0000842 // FIXME: Need to return the composite type.
843 return lexT;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000844 }
845 }
Chris Lattnere5a6cbd2007-07-31 21:27:01 +0000846
Chris Lattnere2949f42008-01-06 22:42:25 +0000847 // Otherwise, the operands are not compatible.
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000848 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
Steve Naroff7a5af782007-07-13 16:58:59 +0000849 lexT.getAsString(), rexT.getAsString(),
850 lex->getSourceRange(), rex->getSourceRange());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000851 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000852}
853
Steve Naroff83895f72007-09-16 03:34:24 +0000854/// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
Chris Lattnere168f762006-11-10 05:29:30 +0000855/// in the case of a the GNU conditional expr extension.
Steve Naroff83895f72007-09-16 03:34:24 +0000856Action::ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc,
Chris Lattnere168f762006-11-10 05:29:30 +0000857 SourceLocation ColonLoc,
858 ExprTy *Cond, ExprTy *LHS,
859 ExprTy *RHS) {
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000860 Expr *CondExpr = (Expr *) Cond;
861 Expr *LHSExpr = (Expr *) LHS, *RHSExpr = (Expr *) RHS;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000862
863 // If this is the gnu "x ?: y" extension, analyze the types as though the LHS
864 // was the condition.
865 bool isLHSNull = LHSExpr == 0;
866 if (isLHSNull)
867 LHSExpr = CondExpr;
868
Chris Lattnerdaaa9f22007-07-16 21:39:03 +0000869 QualType result = CheckConditionalOperands(CondExpr, LHSExpr,
870 RHSExpr, QuestionLoc);
Steve Narofff8a28c52007-05-15 20:29:32 +0000871 if (result.isNull())
872 return true;
Chris Lattner2ab40a62007-11-26 01:40:58 +0000873 return new ConditionalOperator(CondExpr, isLHSNull ? 0 : LHSExpr,
874 RHSExpr, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000875}
876
Steve Naroff0b661582007-08-28 23:30:39 +0000877/// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that
878/// do not have a prototype. Integer promotions are performed on each
879/// argument, and arguments that have type float are promoted to double.
Chris Lattner08464942007-12-28 05:29:59 +0000880void Sema::DefaultArgumentPromotion(Expr *&Expr) {
881 QualType Ty = Expr->getType();
882 assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type");
Steve Naroff0b661582007-08-28 23:30:39 +0000883
Chris Lattner08464942007-12-28 05:29:59 +0000884 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
885 promoteExprToType(Expr, Context.IntTy);
886 if (Ty == Context.FloatTy)
887 promoteExprToType(Expr, Context.DoubleTy);
Steve Naroff0b661582007-08-28 23:30:39 +0000888}
889
Steve Naroff81569d22007-07-15 02:02:06 +0000890/// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4).
Steve Naroff31090012007-07-16 21:54:35 +0000891void Sema::DefaultFunctionArrayConversion(Expr *&e) {
Steve Naroff81569d22007-07-15 02:02:06 +0000892 QualType t = e->getType();
Steve Naroffb8ea4fb2007-07-13 23:32:42 +0000893 assert(!t.isNull() && "DefaultFunctionArrayConversion - missing type");
Bill Wendlingdfc81072007-07-17 03:52:31 +0000894
Chris Lattnercd1d0862007-07-31 16:56:34 +0000895 if (const ReferenceType *ref = t->getAsReferenceType()) {
Bill Wendling354fb262007-07-17 04:16:47 +0000896 promoteExprToType(e, ref->getReferenceeType()); // C++ [expr]
897 t = e->getType();
898 }
Steve Naroff81569d22007-07-15 02:02:06 +0000899 if (t->isFunctionType())
Steve Naroff31090012007-07-16 21:54:35 +0000900 promoteExprToType(e, Context.getPointerType(t));
Chris Lattner41977962007-07-31 19:29:30 +0000901 else if (const ArrayType *ary = t->getAsArrayType())
Steve Naroff31090012007-07-16 21:54:35 +0000902 promoteExprToType(e, Context.getPointerType(ary->getElementType()));
Steve Naroff1926c832007-04-24 00:23:05 +0000903}
904
Steve Naroff71b59a92007-06-04 22:22:31 +0000905/// UsualUnaryConversion - Performs various conversions that are common to most
906/// operators (C99 6.3). The conversions of array and function types are
907/// sometimes surpressed. For example, the array->pointer conversion doesn't
908/// apply if the array is an argument to the sizeof or address (&) operators.
909/// In these instances, this routine should *not* be called.
Chris Lattner08464942007-12-28 05:29:59 +0000910Expr *Sema::UsualUnaryConversions(Expr *&Expr) {
911 QualType Ty = Expr->getType();
912 assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
Steve Naroff71b59a92007-06-04 22:22:31 +0000913
Chris Lattner08464942007-12-28 05:29:59 +0000914 if (const ReferenceType *Ref = Ty->getAsReferenceType()) {
915 promoteExprToType(Expr, Ref->getReferenceeType()); // C++ [expr]
916 Ty = Expr->getType();
Bill Wendling354fb262007-07-17 04:16:47 +0000917 }
Chris Lattner08464942007-12-28 05:29:59 +0000918 if (Ty->isPromotableIntegerType()) // C99 6.3.1.1p2
919 promoteExprToType(Expr, Context.IntTy);
Steve Naroff31090012007-07-16 21:54:35 +0000920 else
Chris Lattner08464942007-12-28 05:29:59 +0000921 DefaultFunctionArrayConversion(Expr);
922
923 return Expr;
Steve Naroff71b59a92007-06-04 22:22:31 +0000924}
925
Chris Lattnerf17bd422007-08-30 17:45:32 +0000926/// UsualArithmeticConversions - Performs various conversions that are common to
Steve Naroff1926c832007-04-24 00:23:05 +0000927/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
928/// routine returns the first non-arithmetic type found. The client is
929/// responsible for emitting appropriate error diagnostics.
Steve Naroffabefc392008-01-15 22:21:49 +0000930/// FIXME: verify the conversion rules for "complex int" are consistent with GCC.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000931QualType Sema::UsualArithmeticConversions(Expr *&lhsExpr, Expr *&rhsExpr,
932 bool isCompAssign) {
Steve Naroff46c72912007-08-25 19:54:59 +0000933 if (!isCompAssign) {
934 UsualUnaryConversions(lhsExpr);
935 UsualUnaryConversions(rhsExpr);
936 }
Steve Naroff1bb21df2007-10-18 18:55:53 +0000937 // For conversion purposes, we ignore any qualifiers.
938 // For example, "const float" and "float" are equivalent.
Steve Naroff257b4a22007-11-10 19:45:54 +0000939 QualType lhs = lhsExpr->getType().getCanonicalType().getUnqualifiedType();
940 QualType rhs = rhsExpr->getType().getCanonicalType().getUnqualifiedType();
Steve Naroff1926c832007-04-24 00:23:05 +0000941
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000942 // If both types are identical, no conversion is needed.
Steve Naroff1bb21df2007-10-18 18:55:53 +0000943 if (lhs == rhs)
944 return lhs;
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000945
946 // If either side is a non-arithmetic type (e.g. a pointer), we are done.
947 // The caller can deal with this (e.g. pointer + int).
Steve Naroffdbd9e892007-07-17 00:58:39 +0000948 if (!lhs->isArithmeticType() || !rhs->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000949 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000950
Chris Lattner0c46c5d2007-06-02 23:53:17 +0000951 // At this point, we have two different arithmetic types.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000952
953 // Handle complex types first (C99 6.3.1.8p1).
954 if (lhs->isComplexType() || rhs->isComplexType()) {
Steve Naroff6fcfd052008-01-15 19:36:10 +0000955 // if we have an integer operand, the result is the complex type.
Steve Naroffabefc392008-01-15 22:21:49 +0000956 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
957 // convert the rhs to the lhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000958 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
959 return lhs;
Steve Naroff6fcfd052008-01-15 19:36:10 +0000960 }
Steve Naroffabefc392008-01-15 22:21:49 +0000961 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
962 // convert the lhs to the rhs complex type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +0000963 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
964 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +0000965 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000966 // This handles complex/complex, complex/float, or float/complex.
967 // When both operands are complex, the shorter operand is converted to the
968 // type of the longer, and that is the type of the result. This corresponds
969 // to what is done when combining two real floating-point operands.
970 // The fun begins when size promotion occur across type domains.
971 // From H&S 6.3.4: When one operand is complex and the other is a real
972 // floating-point type, the less precise type is converted, within it's
973 // real or complex domain, to the precision of the other type. For example,
974 // when combining a "long double" with a "double _Complex", the
975 // "double _Complex" is promoted to "long double _Complex".
Steve Naroff7af82d42007-08-27 15:30:22 +0000976 int result = Context.compareFloatingType(lhs, rhs);
977
978 if (result > 0) { // The left side is bigger, convert rhs.
Steve Naroffe31313d2007-08-27 21:32:55 +0000979 rhs = Context.getFloatingTypeOfSizeWithinDomain(lhs, rhs);
980 if (!isCompAssign)
981 promoteExprToType(rhsExpr, rhs);
982 } else if (result < 0) { // The right side is bigger, convert lhs.
983 lhs = Context.getFloatingTypeOfSizeWithinDomain(rhs, lhs);
984 if (!isCompAssign)
985 promoteExprToType(lhsExpr, lhs);
986 }
987 // At this point, lhs and rhs have the same rank/size. Now, make sure the
988 // domains match. This is a requirement for our implementation, C99
989 // does not require this promotion.
990 if (lhs != rhs) { // Domains don't match, we have complex/float mix.
991 if (lhs->isRealFloatingType()) { // handle "double, _Complex double".
Steve Naroffa042db22007-08-27 21:43:43 +0000992 if (!isCompAssign)
993 promoteExprToType(lhsExpr, rhs);
994 return rhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000995 } else { // handle "_Complex double, double".
Steve Naroffa042db22007-08-27 21:43:43 +0000996 if (!isCompAssign)
997 promoteExprToType(rhsExpr, lhs);
998 return lhs;
Steve Naroffe31313d2007-08-27 21:32:55 +0000999 }
Steve Naroffdbd9e892007-07-17 00:58:39 +00001000 }
Steve Naroffa042db22007-08-27 21:43:43 +00001001 return lhs; // The domain/size match exactly.
Steve Naroffbf223ba2007-04-24 20:56:26 +00001002 }
Steve Naroffb01bbe32007-04-25 01:22:31 +00001003 // Now handle "real" floating types (i.e. float, double, long double).
1004 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
1005 // if we have an integer operand, the result is the real floating type.
Steve Naroffabefc392008-01-15 22:21:49 +00001006 if (rhs->isIntegerType() || rhs->isComplexIntegerType()) {
1007 // convert rhs to the lhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001008 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1009 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001010 }
Steve Naroffabefc392008-01-15 22:21:49 +00001011 if (lhs->isIntegerType() || lhs->isComplexIntegerType()) {
1012 // convert lhs to the rhs floating point type.
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001013 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
1014 return rhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001015 }
Steve Naroff81569d22007-07-15 02:02:06 +00001016 // We have two real floating types, float/complex combos were handled above.
1017 // Convert the smaller operand to the bigger result.
Steve Naroff7af82d42007-08-27 15:30:22 +00001018 int result = Context.compareFloatingType(lhs, rhs);
1019
1020 if (result > 0) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001021 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1022 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001023 }
Steve Naroff7af82d42007-08-27 15:30:22 +00001024 if (result < 0) { // convert the lhs
1025 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1026 return rhs;
1027 }
1028 assert(0 && "Sema::UsualArithmeticConversions(): illegal float comparison");
Steve Naroffb01bbe32007-04-25 01:22:31 +00001029 }
Steve Naroff6fcfd052008-01-15 19:36:10 +00001030 if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) {
1031 // Handle GCC complex int extension.
Steve Naroff6fcfd052008-01-15 19:36:10 +00001032 const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType();
1033 const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType();
1034
1035 if (lhsComplexInt && rhsComplexInt) {
1036 if (Context.maxIntegerType(lhsComplexInt->getElementType(),
1037 rhsComplexInt->getElementType()) == lhs) {
1038 if (!isCompAssign) promoteExprToType(rhsExpr, lhs); // convert the rhs
1039 return lhs;
1040 }
1041 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1042 return rhs;
1043 } else if (lhsComplexInt && rhs->isIntegerType()) {
1044 // convert the rhs to the lhs complex type.
1045 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1046 return lhs;
1047 } else if (rhsComplexInt && lhs->isIntegerType()) {
1048 // convert the lhs to the rhs complex type.
1049 if (!isCompAssign) promoteExprToType(lhsExpr, rhs);
1050 return rhs;
1051 }
1052 }
Steve Naroff81569d22007-07-15 02:02:06 +00001053 // Finally, we have two differing integer types.
Steve Naroffdbd9e892007-07-17 00:58:39 +00001054 if (Context.maxIntegerType(lhs, rhs) == lhs) { // convert the rhs
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001055 if (!isCompAssign) promoteExprToType(rhsExpr, lhs);
1056 return lhs;
Steve Naroffdbd9e892007-07-17 00:58:39 +00001057 }
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001058 if (!isCompAssign) promoteExprToType(lhsExpr, rhs); // convert the lhs
1059 return rhs;
Steve Narofff1e53692007-03-23 22:27:02 +00001060}
1061
Steve Naroff3f597292007-05-11 22:18:03 +00001062// CheckPointerTypesForAssignment - This is a very tricky routine (despite
1063// being closely modeled after the C99 spec:-). The odd characteristic of this
1064// routine is it effectively iqnores the qualifiers on the top level pointee.
1065// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
1066// FIXME: add a couple examples in this comment.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001067Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001068Sema::CheckPointerTypesForAssignment(QualType lhsType, QualType rhsType) {
Steve Naroff3f597292007-05-11 22:18:03 +00001069 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001070
1071 // get the "pointed to" type (ignoring qualifiers at the top level)
Chris Lattnere5a6cbd2007-07-31 21:27:01 +00001072 lhptee = lhsType->getAsPointerType()->getPointeeType();
1073 rhptee = rhsType->getAsPointerType()->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001074
1075 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +00001076 lhptee = lhptee.getCanonicalType();
1077 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +00001078
Chris Lattner9bad62c2008-01-04 18:04:52 +00001079 AssignConvertType ConvTy = Compatible;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001080
Steve Naroff3f597292007-05-11 22:18:03 +00001081 // C99 6.5.16.1p1: This following citation is common to constraints
1082 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
1083 // qualifiers of the type *pointed to* by the right;
1084 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
1085 rhptee.getQualifiers())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001086 ConvTy = CompatiblePointerDiscardsQualifiers;
Steve Naroff3f597292007-05-11 22:18:03 +00001087
1088 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
1089 // incomplete type and the other is a pointer to a qualified or unqualified
1090 // version of void...
Chris Lattner0a788432008-01-03 22:56:36 +00001091 if (lhptee->isVoidType()) {
1092 if (rhptee->isObjectType() || rhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001093 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001094
1095 // As an extension, we allow cast to/from void* to function pointer.
1096 if (rhptee->isFunctionType())
1097 return FunctionVoidPointer;
1098 }
1099
1100 if (rhptee->isVoidType()) {
1101 if (lhptee->isObjectType() || lhptee->isIncompleteType())
Chris Lattner9bad62c2008-01-04 18:04:52 +00001102 return ConvTy;
Chris Lattner0a788432008-01-03 22:56:36 +00001103
1104 // As an extension, we allow cast to/from void* to function pointer.
1105 if (lhptee->isFunctionType())
1106 return FunctionVoidPointer;
1107 }
1108
Steve Naroff3f597292007-05-11 22:18:03 +00001109 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
1110 // unqualified versions of compatible types, ...
Chris Lattner0a788432008-01-03 22:56:36 +00001111 if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(),
1112 rhptee.getUnqualifiedType()))
1113 return IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
Chris Lattner9bad62c2008-01-04 18:04:52 +00001114 return ConvTy;
Steve Naroff1f4d7272007-05-11 04:00:31 +00001115}
1116
Steve Naroff98cf3e92007-06-06 18:38:38 +00001117/// CheckAssignmentConstraints (C99 6.5.16) - This routine currently
Steve Naroff17f76e02007-05-03 21:03:48 +00001118/// has code to accommodate several GCC extensions when type checking
1119/// pointers. Here are some objectionable examples that GCC considers warnings:
1120///
1121/// int a, *pint;
1122/// short *pshort;
1123/// struct foo *pfoo;
1124///
1125/// pint = pshort; // warning: assignment from incompatible pointer type
1126/// a = pint; // warning: assignment makes integer from pointer without a cast
1127/// pint = a; // warning: assignment makes pointer from integer without a cast
1128/// pint = pfoo; // warning: assignment from incompatible pointer type
1129///
1130/// As a result, the code for dealing with pointers is more complex than the
1131/// C99 spec dictates.
1132/// Note: the warning above turn into errors when -pedantic-errors is enabled.
1133///
Chris Lattner9bad62c2008-01-04 18:04:52 +00001134Sema::AssignConvertType
Steve Naroff98cf3e92007-06-06 18:38:38 +00001135Sema::CheckAssignmentConstraints(QualType lhsType, QualType rhsType) {
Chris Lattnera52c2f22008-01-04 23:18:45 +00001136 // Get canonical types. We're not formatting these types, just comparing
1137 // them.
1138 lhsType = lhsType.getCanonicalType();
1139 rhsType = rhsType.getCanonicalType();
1140
1141 if (lhsType.getUnqualifiedType() == rhsType.getUnqualifiedType())
Chris Lattnerf5c973d2008-01-07 17:51:46 +00001142 return Compatible; // Common case: fast path an exact match.
Steve Naroff44fd8ff2007-07-24 21:46:40 +00001143
Anders Carlsson24ebce62007-10-12 23:56:29 +00001144 if (lhsType->isReferenceType() || rhsType->isReferenceType()) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001145 if (Context.referenceTypesAreCompatible(lhsType, rhsType))
Anders Carlsson24ebce62007-10-12 23:56:29 +00001146 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001147 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001148 }
Chris Lattnera52c2f22008-01-04 23:18:45 +00001149
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001150 if (lhsType->isObjCQualifiedIdType()
1151 || rhsType->isObjCQualifiedIdType()) {
1152 if (Context.ObjCQualifiedIdTypesAreCompatible(lhsType, rhsType))
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001153 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001154 return Incompatible;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001155 }
Chris Lattner881a2122008-01-04 23:32:24 +00001156
1157 if (lhsType->isVectorType() || rhsType->isVectorType()) {
1158 // For OCUVector, allow vector splats; float -> <n x float>
1159 if (const OCUVectorType *LV = lhsType->getAsOCUVectorType()) {
1160 if (LV->getElementType().getTypePtr() == rhsType.getTypePtr())
1161 return Compatible;
1162 }
1163
1164 // If LHS and RHS are both vectors of integer or both vectors of floating
1165 // point types, and the total vector length is the same, allow the
1166 // conversion. This is a bitcast; no bits are changed but the result type
1167 // is different.
1168 if (getLangOptions().LaxVectorConversions &&
1169 lhsType->isVectorType() && rhsType->isVectorType()) {
1170 if ((lhsType->isIntegerType() && rhsType->isIntegerType()) ||
1171 (lhsType->isRealFloatingType() && rhsType->isRealFloatingType())) {
1172 if (Context.getTypeSize(lhsType, SourceLocation()) ==
1173 Context.getTypeSize(rhsType, SourceLocation()))
Nate Begeman330aaa72007-12-30 02:59:45 +00001174 return Compatible;
1175 }
Chris Lattner881a2122008-01-04 23:32:24 +00001176 }
1177 return Incompatible;
1178 }
1179
1180 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001181 return Compatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001182
1183 if (lhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001184 if (rhsType->isIntegerType())
Chris Lattner940cfeb2008-01-04 18:22:42 +00001185 return IntToPointer;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001186
Steve Naroff3f597292007-05-11 22:18:03 +00001187 if (rhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001188 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001189 return Incompatible;
1190 }
1191
1192 if (rhsType->isPointerType()) {
Steve Naroff98cf3e92007-06-06 18:38:38 +00001193 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
1194 if ((lhsType->isIntegerType()) && (lhsType != Context.BoolTy))
Chris Lattner940cfeb2008-01-04 18:22:42 +00001195 return PointerToInt;
Steve Naroff98cf3e92007-06-06 18:38:38 +00001196
Steve Naroff3f597292007-05-11 22:18:03 +00001197 if (lhsType->isPointerType())
Steve Naroff98cf3e92007-06-06 18:38:38 +00001198 return CheckPointerTypesForAssignment(lhsType, rhsType);
Chris Lattnera52c2f22008-01-04 23:18:45 +00001199 return Incompatible;
Chris Lattnera52c2f22008-01-04 23:18:45 +00001200 }
1201
1202 if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
Steve Naroff32e44c02007-10-15 20:41:53 +00001203 if (Context.tagTypesAreCompatible(lhsType, rhsType))
Steve Naroff98cf3e92007-06-06 18:38:38 +00001204 return Compatible;
Bill Wendling216423b2007-05-30 06:30:29 +00001205 }
Steve Naroff98cf3e92007-06-06 18:38:38 +00001206 return Incompatible;
Steve Naroff9eb24652007-05-02 21:58:15 +00001207}
1208
Chris Lattner9bad62c2008-01-04 18:04:52 +00001209Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001210Sema::CheckSingleAssignmentConstraints(QualType lhsType, Expr *&rExpr) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001211 // C99 6.5.16.1p1: the left operand is a pointer and the right is
1212 // a null pointer constant.
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001213 if ((lhsType->isPointerType() || lhsType->isObjCQualifiedIdType())
Fariborz Jahanian5cc21a72008-01-03 18:46:52 +00001214 && rExpr->isNullPointerConstant(Context)) {
Steve Naroff0ee0b0a2007-11-27 17:58:44 +00001215 promoteExprToType(rExpr, lhsType);
1216 return Compatible;
1217 }
Chris Lattnere6dcd502007-10-16 02:55:40 +00001218 // This check seems unnatural, however it is necessary to ensure the proper
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001219 // conversion of functions/arrays. If the conversion were done for all
Steve Naroff30d242c2007-09-15 18:49:24 +00001220 // DeclExpr's (created by ActOnIdentifierExpr), it would mess up the unary
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001221 // expressions that surpress this implicit conversion (&, sizeof).
Chris Lattnere6dcd502007-10-16 02:55:40 +00001222 //
1223 // Suppress this for references: C99 8.5.3p5. FIXME: revisit when references
1224 // are better understood.
1225 if (!lhsType->isReferenceType())
1226 DefaultFunctionArrayConversion(rExpr);
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001227
Chris Lattner9bad62c2008-01-04 18:04:52 +00001228 Sema::AssignConvertType result =
1229 CheckAssignmentConstraints(lhsType, rExpr->getType());
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001230
1231 // C99 6.5.16.1p2: The value of the right operand is converted to the
1232 // type of the assignment expression.
1233 if (rExpr->getType() != lhsType)
1234 promoteExprToType(rExpr, lhsType);
1235 return result;
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001236}
1237
Chris Lattner9bad62c2008-01-04 18:04:52 +00001238Sema::AssignConvertType
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001239Sema::CheckCompoundAssignmentConstraints(QualType lhsType, QualType rhsType) {
1240 return CheckAssignmentConstraints(lhsType, rhsType);
1241}
1242
Chris Lattner5c11c412007-12-12 05:47:28 +00001243QualType Sema::InvalidOperands(SourceLocation loc, Expr *&lex, Expr *&rex) {
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001244 Diag(loc, diag::err_typecheck_invalid_operands,
1245 lex->getType().getAsString(), rex->getType().getAsString(),
1246 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner5c11c412007-12-12 05:47:28 +00001247 return QualType();
Steve Naroff6f49f5d2007-05-29 14:23:36 +00001248}
1249
Steve Naroff7a5af782007-07-13 16:58:59 +00001250inline QualType Sema::CheckVectorOperands(SourceLocation loc, Expr *&lex,
1251 Expr *&rex) {
Steve Naroff84ff4b42007-07-09 21:31:10 +00001252 QualType lhsType = lex->getType(), rhsType = rex->getType();
1253
1254 // make sure the vector types are identical.
1255 if (lhsType == rhsType)
1256 return lhsType;
Nate Begeman330aaa72007-12-30 02:59:45 +00001257
1258 // if the lhs is an ocu vector and the rhs is a scalar of the same type,
1259 // promote the rhs to the vector type.
1260 if (const OCUVectorType *V = lhsType->getAsOCUVectorType()) {
1261 if (V->getElementType().getCanonicalType().getTypePtr()
1262 == rhsType.getCanonicalType().getTypePtr()) {
1263 promoteExprToType(rex, lhsType);
1264 return lhsType;
1265 }
1266 }
1267
1268 // if the rhs is an ocu vector and the lhs is a scalar of the same type,
1269 // promote the lhs to the vector type.
1270 if (const OCUVectorType *V = rhsType->getAsOCUVectorType()) {
1271 if (V->getElementType().getCanonicalType().getTypePtr()
1272 == lhsType.getCanonicalType().getTypePtr()) {
1273 promoteExprToType(lex, rhsType);
1274 return rhsType;
1275 }
1276 }
1277
Steve Naroff84ff4b42007-07-09 21:31:10 +00001278 // You cannot convert between vector values of different size.
1279 Diag(loc, diag::err_typecheck_vector_not_convertable,
1280 lex->getType().getAsString(), rex->getType().getAsString(),
1281 lex->getSourceRange(), rex->getSourceRange());
1282 return QualType();
1283}
1284
Steve Naroff218bc2b2007-05-04 21:54:46 +00001285inline QualType Sema::CheckMultiplyDivideOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001286 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001287{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001288 QualType lhsType = lex->getType(), rhsType = rex->getType();
1289
1290 if (lhsType->isVectorType() || rhsType->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001291 return CheckVectorOperands(loc, lex, rex);
Steve Naroff7a5af782007-07-13 16:58:59 +00001292
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001293 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff5c10d4b2007-04-20 23:42:24 +00001294
Steve Naroffdbd9e892007-07-17 00:58:39 +00001295 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001296 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001297 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001298}
1299
Steve Naroff218bc2b2007-05-04 21:54:46 +00001300inline QualType Sema::CheckRemainderOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001301 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001302{
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001303 QualType lhsType = lex->getType(), rhsType = rex->getType();
1304
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001305 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001306
Steve Naroffdbd9e892007-07-17 00:58:39 +00001307 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001308 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001309 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001310}
1311
1312inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001313 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001314{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001315 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff7a5af782007-07-13 16:58:59 +00001316 return CheckVectorOperands(loc, lex, rex);
1317
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001318 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff94a5aca2007-07-16 22:23:01 +00001319
Steve Naroffe4718892007-04-27 18:30:00 +00001320 // handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001321 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001322 return compType;
Steve Naroff218bc2b2007-05-04 21:54:46 +00001323
Steve Naroffdbd9e892007-07-17 00:58:39 +00001324 if (lex->getType()->isPointerType() && rex->getType()->isIntegerType())
1325 return lex->getType();
1326 if (lex->getType()->isIntegerType() && rex->getType()->isPointerType())
1327 return rex->getType();
Chris Lattner5c11c412007-12-12 05:47:28 +00001328 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001329}
1330
Steve Naroff218bc2b2007-05-04 21:54:46 +00001331inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001332 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff218bc2b2007-05-04 21:54:46 +00001333{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001334 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001335 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001336
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001337 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001338
Chris Lattner4d62f422007-12-09 21:53:25 +00001339 // Enforce type constraints: C99 6.5.6p3.
1340
1341 // Handle the common case first (both operands are arithmetic).
Steve Naroffdbd9e892007-07-17 00:58:39 +00001342 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001343 return compType;
Chris Lattner4d62f422007-12-09 21:53:25 +00001344
1345 // Either ptr - int or ptr - ptr.
1346 if (const PointerType *LHSPTy = lex->getType()->getAsPointerType()) {
1347 // The LHS must be an object type, not incomplete, function, etc.
1348 if (!LHSPTy->getPointeeType()->isObjectType()) {
1349 // Handle the GNU void* extension.
1350 if (LHSPTy->getPointeeType()->isVoidType()) {
1351 Diag(loc, diag::ext_gnu_void_ptr,
1352 lex->getSourceRange(), rex->getSourceRange());
1353 } else {
1354 Diag(loc, diag::err_typecheck_sub_ptr_object,
1355 lex->getType().getAsString(), lex->getSourceRange());
1356 return QualType();
1357 }
1358 }
1359
1360 // The result type of a pointer-int computation is the pointer type.
1361 if (rex->getType()->isIntegerType())
1362 return lex->getType();
Steve Naroff94a5aca2007-07-16 22:23:01 +00001363
Chris Lattner4d62f422007-12-09 21:53:25 +00001364 // Handle pointer-pointer subtractions.
1365 if (const PointerType *RHSPTy = rex->getType()->getAsPointerType()) {
1366 // RHS must be an object type, unless void (GNU).
1367 if (!RHSPTy->getPointeeType()->isObjectType()) {
1368 // Handle the GNU void* extension.
1369 if (RHSPTy->getPointeeType()->isVoidType()) {
1370 if (!LHSPTy->getPointeeType()->isVoidType())
1371 Diag(loc, diag::ext_gnu_void_ptr,
1372 lex->getSourceRange(), rex->getSourceRange());
1373 } else {
1374 Diag(loc, diag::err_typecheck_sub_ptr_object,
1375 rex->getType().getAsString(), rex->getSourceRange());
1376 return QualType();
1377 }
1378 }
1379
1380 // Pointee types must be compatible.
1381 if (!Context.typesAreCompatible(LHSPTy->getPointeeType(),
1382 RHSPTy->getPointeeType())) {
1383 Diag(loc, diag::err_typecheck_sub_ptr_compatible,
1384 lex->getType().getAsString(), rex->getType().getAsString(),
1385 lex->getSourceRange(), rex->getSourceRange());
1386 return QualType();
1387 }
1388
1389 return Context.getPointerDiffType();
1390 }
1391 }
1392
Chris Lattner5c11c412007-12-12 05:47:28 +00001393 return InvalidOperands(loc, lex, rex);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001394}
1395
1396inline QualType Sema::CheckShiftOperands( // C99 6.5.7
Chris Lattner5c11c412007-12-12 05:47:28 +00001397 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign) {
1398 // C99 6.5.7p2: Each of the operands shall have integer type.
1399 if (!lex->getType()->isIntegerType() || !rex->getType()->isIntegerType())
1400 return InvalidOperands(loc, lex, rex);
Steve Naroff1926c832007-04-24 00:23:05 +00001401
Chris Lattner5c11c412007-12-12 05:47:28 +00001402 // Shifts don't perform usual arithmetic conversions, they just do integer
1403 // promotions on each operand. C99 6.5.7p3
Chris Lattner3c133402007-12-13 07:28:16 +00001404 if (!isCompAssign)
1405 UsualUnaryConversions(lex);
Chris Lattner5c11c412007-12-12 05:47:28 +00001406 UsualUnaryConversions(rex);
1407
1408 // "The type of the result is that of the promoted left operand."
1409 return lex->getType();
Steve Naroff26c8ea52007-03-21 21:08:52 +00001410}
1411
Chris Lattnerb620c342007-08-26 01:18:55 +00001412inline QualType Sema::CheckCompareOperands( // C99 6.5.8
1413 Expr *&lex, Expr *&rex, SourceLocation loc, bool isRelational)
Steve Naroff1926c832007-04-24 00:23:05 +00001414{
Chris Lattnerb620c342007-08-26 01:18:55 +00001415 // C99 6.5.8p3 / C99 6.5.9p4
Steve Naroff47fea352007-08-10 18:26:40 +00001416 if (lex->getType()->isArithmeticType() && rex->getType()->isArithmeticType())
1417 UsualArithmeticConversions(lex, rex);
1418 else {
1419 UsualUnaryConversions(lex);
1420 UsualUnaryConversions(rex);
1421 }
Steve Naroff31090012007-07-16 21:54:35 +00001422 QualType lType = lex->getType();
1423 QualType rType = rex->getType();
Steve Naroff1926c832007-04-24 00:23:05 +00001424
Ted Kremeneke2763b02007-10-29 17:13:39 +00001425 // For non-floating point types, check for self-comparisons of the form
1426 // x == x, x != x, x < x, etc. These always evaluate to a constant, and
1427 // often indicate logic errors in the program.
Ted Kremeneke451eae2007-10-29 16:58:49 +00001428 if (!lType->isFloatingType()) {
1429 if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(IgnoreParen(lex)))
1430 if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(IgnoreParen(rex)))
1431 if (DRL->getDecl() == DRR->getDecl())
1432 Diag(loc, diag::warn_selfcomparison);
1433 }
1434
Chris Lattnerb620c342007-08-26 01:18:55 +00001435 if (isRelational) {
1436 if (lType->isRealType() && rType->isRealType())
1437 return Context.IntTy;
1438 } else {
Ted Kremeneke2763b02007-10-29 17:13:39 +00001439 // Check for comparisons of floating point operands using != and ==.
Ted Kremeneke2763b02007-10-29 17:13:39 +00001440 if (lType->isFloatingType()) {
1441 assert (rType->isFloatingType());
Ted Kremenek43fb8b02007-11-25 00:58:00 +00001442 CheckFloatComparison(loc,lex,rex);
Ted Kremenekd4ecc6d2007-10-29 16:40:01 +00001443 }
1444
Chris Lattnerb620c342007-08-26 01:18:55 +00001445 if (lType->isArithmeticType() && rType->isArithmeticType())
1446 return Context.IntTy;
1447 }
Steve Naroffe4718892007-04-27 18:30:00 +00001448
Chris Lattner1895e582007-08-26 01:10:14 +00001449 bool LHSIsNull = lex->isNullPointerConstant(Context);
1450 bool RHSIsNull = rex->isNullPointerConstant(Context);
1451
Chris Lattnerb620c342007-08-26 01:18:55 +00001452 // All of the following pointer related warnings are GCC extensions, except
1453 // when handling null pointer constants. One day, we can consider making them
1454 // errors (when -pedantic-errors is enabled).
Steve Naroff808eb8f2007-08-27 04:08:11 +00001455 if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2
Steve Naroffb6666252007-11-13 14:57:38 +00001456
1457 if (!LHSIsNull && !RHSIsNull && // C99 6.5.9p2
1458 !lType->getAsPointerType()->getPointeeType()->isVoidType() &&
1459 !rType->getAsPointerType()->getPointeeType()->isVoidType() &&
Steve Naroff32e44c02007-10-15 20:41:53 +00001460 !Context.pointerTypesAreCompatible(lType.getUnqualifiedType(),
1461 rType.getUnqualifiedType())) {
Steve Naroffcdee44c2007-08-16 21:48:38 +00001462 Diag(loc, diag::ext_typecheck_comparison_of_distinct_pointers,
1463 lType.getAsString(), rType.getAsString(),
1464 lex->getSourceRange(), rex->getSourceRange());
Steve Naroff75c17232007-06-13 21:41:08 +00001465 }
Chris Lattner1895e582007-08-26 01:10:14 +00001466 promoteExprToType(rex, lType); // promote the pointer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001467 return Context.IntTy;
1468 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001469 if ((lType->isObjCQualifiedIdType() || rType->isObjCQualifiedIdType())
1470 && Context.ObjCQualifiedIdTypesAreCompatible(lType, rType, true)) {
Fariborz Jahanian134cbef2007-12-20 01:06:58 +00001471 promoteExprToType(rex, lType);
1472 return Context.IntTy;
1473 }
Steve Naroffcdee44c2007-08-16 21:48:38 +00001474 if (lType->isPointerType() && rType->isIntegerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001475 if (!RHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001476 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1477 lType.getAsString(), rType.getAsString(),
1478 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001479 promoteExprToType(rex, lType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001480 return Context.IntTy;
1481 }
1482 if (lType->isIntegerType() && rType->isPointerType()) {
Chris Lattner1895e582007-08-26 01:10:14 +00001483 if (!LHSIsNull)
Steve Naroffcdee44c2007-08-16 21:48:38 +00001484 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer,
1485 lType.getAsString(), rType.getAsString(),
1486 lex->getSourceRange(), rex->getSourceRange());
Chris Lattner1895e582007-08-26 01:10:14 +00001487 promoteExprToType(lex, rType); // promote the integer to pointer
Steve Naroffcdee44c2007-08-16 21:48:38 +00001488 return Context.IntTy;
Steve Naroff043d45d2007-05-15 02:32:35 +00001489 }
Chris Lattner5c11c412007-12-12 05:47:28 +00001490 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001491}
1492
Steve Naroff218bc2b2007-05-04 21:54:46 +00001493inline QualType Sema::CheckBitwiseOperands(
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001494 Expr *&lex, Expr *&rex, SourceLocation loc, bool isCompAssign)
Steve Naroff1926c832007-04-24 00:23:05 +00001495{
Steve Naroff94a5aca2007-07-16 22:23:01 +00001496 if (lex->getType()->isVectorType() || rex->getType()->isVectorType())
Steve Naroff84ff4b42007-07-09 21:31:10 +00001497 return CheckVectorOperands(loc, lex, rex);
Steve Naroffb8ea4fb2007-07-13 23:32:42 +00001498
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001499 QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign);
Steve Naroff1926c832007-04-24 00:23:05 +00001500
Steve Naroffdbd9e892007-07-17 00:58:39 +00001501 if (lex->getType()->isIntegerType() && rex->getType()->isIntegerType())
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001502 return compType;
Chris Lattner5c11c412007-12-12 05:47:28 +00001503 return InvalidOperands(loc, lex, rex);
Steve Naroff26c8ea52007-03-21 21:08:52 +00001504}
1505
Steve Naroff218bc2b2007-05-04 21:54:46 +00001506inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
Steve Naroff7a5af782007-07-13 16:58:59 +00001507 Expr *&lex, Expr *&rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +00001508{
Steve Naroff31090012007-07-16 21:54:35 +00001509 UsualUnaryConversions(lex);
1510 UsualUnaryConversions(rex);
Steve Naroffe4718892007-04-27 18:30:00 +00001511
Steve Naroffdbd9e892007-07-17 00:58:39 +00001512 if (lex->getType()->isScalarType() || rex->getType()->isScalarType())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001513 return Context.IntTy;
Chris Lattner5c11c412007-12-12 05:47:28 +00001514 return InvalidOperands(loc, lex, rex);
Steve Naroffae4143e2007-04-26 20:39:23 +00001515}
1516
Steve Naroff35d85152007-05-07 00:24:15 +00001517inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
Steve Naroff0c1c7ed2007-08-24 22:33:52 +00001518 Expr *lex, Expr *&rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +00001519{
Steve Naroff0af91202007-04-27 21:51:21 +00001520 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001521 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff9358c712007-05-27 23:58:33 +00001522 Expr::isModifiableLvalueResult mlval = lex->isModifiableLvalue();
1523
1524 switch (mlval) { // C99 6.5.16p2
Chris Lattner9bad62c2008-01-04 18:04:52 +00001525 case Expr::MLV_Valid:
1526 break;
1527 case Expr::MLV_ConstQualified:
1528 Diag(loc, diag::err_typecheck_assign_const, lex->getSourceRange());
1529 return QualType();
1530 case Expr::MLV_ArrayType:
1531 Diag(loc, diag::err_typecheck_array_not_modifiable_lvalue,
1532 lhsType.getAsString(), lex->getSourceRange());
1533 return QualType();
1534 case Expr::MLV_NotObjectType:
1535 Diag(loc, diag::err_typecheck_non_object_not_modifiable_lvalue,
1536 lhsType.getAsString(), lex->getSourceRange());
1537 return QualType();
1538 case Expr::MLV_InvalidExpression:
1539 Diag(loc, diag::err_typecheck_expression_not_modifiable_lvalue,
1540 lex->getSourceRange());
1541 return QualType();
1542 case Expr::MLV_IncompleteType:
1543 case Expr::MLV_IncompleteVoidType:
1544 Diag(loc, diag::err_typecheck_incomplete_type_not_modifiable_lvalue,
1545 lhsType.getAsString(), lex->getSourceRange());
1546 return QualType();
1547 case Expr::MLV_DuplicateVectorComponents:
1548 Diag(loc, diag::err_typecheck_duplicate_vector_components_not_mlvalue,
1549 lex->getSourceRange());
1550 return QualType();
Steve Naroff218bc2b2007-05-04 21:54:46 +00001551 }
Steve Naroffad373bd2007-07-31 12:34:36 +00001552
Chris Lattner9bad62c2008-01-04 18:04:52 +00001553 AssignConvertType ConvTy;
1554 if (compoundType.isNull())
1555 ConvTy = CheckSingleAssignmentConstraints(lhsType, rex);
1556 else
1557 ConvTy = CheckCompoundAssignmentConstraints(lhsType, rhsType);
1558
1559 if (DiagnoseAssignmentResult(ConvTy, loc, lhsType, rhsType,
1560 rex, "assigning"))
1561 return QualType();
1562
Steve Naroff98cf3e92007-06-06 18:38:38 +00001563 // C99 6.5.16p3: The type of an assignment expression is the type of the
1564 // left operand unless the left operand has qualified type, in which case
1565 // it is the unqualified version of the type of the left operand.
1566 // C99 6.5.16.1p2: In simple assignment, the value of the right operand
1567 // is converted to the type of the assignment expression (above).
Chris Lattnerf17bd422007-08-30 17:45:32 +00001568 // C++ 5.17p1: the type of the assignment expression is that of its left
1569 // oprdu.
Chris Lattner9bad62c2008-01-04 18:04:52 +00001570 return lhsType.getUnqualifiedType();
Steve Naroffae4143e2007-04-26 20:39:23 +00001571}
1572
Steve Naroff218bc2b2007-05-04 21:54:46 +00001573inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroff7a5af782007-07-13 16:58:59 +00001574 Expr *&lex, Expr *&rex, SourceLocation loc) {
Steve Naroff31090012007-07-16 21:54:35 +00001575 UsualUnaryConversions(rex);
1576 return rex->getType();
Steve Naroff95af0132007-03-30 23:47:58 +00001577}
1578
Steve Naroff7a5af782007-07-13 16:58:59 +00001579/// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine
1580/// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions.
Steve Naroff71ce2e02007-05-18 22:53:50 +00001581QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff7a5af782007-07-13 16:58:59 +00001582 QualType resType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001583 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +00001584
Steve Naroff9d139172007-08-24 17:20:07 +00001585 // C99 6.5.2.4p1: We allow complex as a GCC extension.
Steve Naroff5f9ae642007-11-11 14:15:57 +00001586 if (const PointerType *pt = resType->getAsPointerType()) {
Steve Naroff35d85152007-05-07 00:24:15 +00001587 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Steve Naroff71ce2e02007-05-18 22:53:50 +00001588 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
1589 resType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001590 return QualType();
1591 }
Steve Naroff9d139172007-08-24 17:20:07 +00001592 } else if (!resType->isRealType()) {
1593 if (resType->isComplexType())
1594 // C99 does not support ++/-- on complex types.
1595 Diag(OpLoc, diag::ext_integer_increment_complex,
1596 resType.getAsString(), op->getSourceRange());
1597 else {
1598 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
1599 resType.getAsString(), op->getSourceRange());
1600 return QualType();
1601 }
Steve Naroff46ba1eb2007-04-03 23:13:13 +00001602 }
Steve Naroff9e1e5512007-08-23 21:37:33 +00001603 // At this point, we know we have a real, complex or pointer type.
1604 // Now make sure the operand is a modifiable lvalue.
Steve Naroff9358c712007-05-27 23:58:33 +00001605 Expr::isModifiableLvalueResult mlval = op->isModifiableLvalue();
1606 if (mlval != Expr::MLV_Valid) {
1607 // FIXME: emit a more precise diagnostic...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001608 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr,
Chris Lattner84e160a2007-05-19 07:03:17 +00001609 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001610 return QualType();
1611 }
1612 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +00001613}
1614
Steve Naroff1926c832007-04-24 00:23:05 +00001615/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +00001616/// This routine allows us to typecheck complex/recursive expressions
1617/// where the declaration is needed for type checking. Here are some
1618/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +00001619static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +00001620 switch (e->getStmtClass()) {
1621 case Stmt::DeclRefExprClass:
1622 return cast<DeclRefExpr>(e)->getDecl();
1623 case Stmt::MemberExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001624 // Fields cannot be declared with a 'register' storage class.
1625 // &X->f is always ok, even if X is declared register.
1626 if (cast<MemberExpr>(e)->isArrow())
1627 return 0;
Steve Naroff1926c832007-04-24 00:23:05 +00001628 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001629 case Stmt::ArraySubscriptExprClass:
Chris Lattner48d52842007-11-16 17:46:48 +00001630 // &X[4] and &4[X] is invalid if X is invalid.
Steve Naroff1926c832007-04-24 00:23:05 +00001631 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +00001632 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001633 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001634 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +00001635 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Chris Lattner48d52842007-11-16 17:46:48 +00001636 case Stmt::ImplicitCastExprClass:
1637 // &X[4] when X is an array, has an implicit cast from array to pointer.
1638 return getPrimaryDeclaration(cast<ImplicitCastExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +00001639 default:
1640 return 0;
1641 }
1642}
1643
1644/// CheckAddressOfOperand - The operand of & must be either a function
1645/// designator or an lvalue designating an object. If it is an lvalue, the
1646/// object cannot be declared with storage class register or be a bit field.
1647/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +00001648/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +00001649QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001650 if (getLangOptions().C99) {
1651 // Implement C99-only parts of addressof rules.
1652 if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) {
1653 if (uOp->getOpcode() == UnaryOperator::Deref)
1654 // Per C99 6.5.3.2, the address of a deref always returns a valid result
1655 // (assuming the deref expression is valid).
1656 return uOp->getSubExpr()->getType();
1657 }
1658 // Technically, there should be a check for array subscript
1659 // expressions here, but the result of one is always an lvalue anyway.
1660 }
Steve Naroff1926c832007-04-24 00:23:05 +00001661 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff9358c712007-05-27 23:58:33 +00001662 Expr::isLvalueResult lval = op->isLvalue();
Steve Naroff47500512007-04-19 23:00:49 +00001663
Steve Naroff9358c712007-05-27 23:58:33 +00001664 if (lval != Expr::LV_Valid) { // C99 6.5.3.2p1
Chris Lattner48d52842007-11-16 17:46:48 +00001665 if (!dcl || !isa<FunctionDecl>(dcl)) {// allow function designators
1666 // FIXME: emit more specific diag...
Steve Naroff71ce2e02007-05-18 22:53:50 +00001667 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof,
1668 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001669 return QualType();
1670 }
Steve Naroff47500512007-04-19 23:00:49 +00001671 } else if (dcl) {
1672 // We have an lvalue with a decl. Make sure the decl is not declared
1673 // with the register storage-class specifier.
1674 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +00001675 if (vd->getStorageClass() == VarDecl::Register) {
Steve Naroff71ce2e02007-05-18 22:53:50 +00001676 Diag(OpLoc, diag::err_typecheck_address_of_register,
Chris Lattner84e160a2007-05-19 07:03:17 +00001677 op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001678 return QualType();
1679 }
Steve Narofff633d092007-04-25 19:01:39 +00001680 } else
1681 assert(0 && "Unknown/unexpected decl type");
1682
Steve Naroff47500512007-04-19 23:00:49 +00001683 // FIXME: add check for bitfields!
1684 }
1685 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +00001686 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +00001687}
1688
Steve Naroff35d85152007-05-07 00:24:15 +00001689QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff31090012007-07-16 21:54:35 +00001690 UsualUnaryConversions(op);
1691 QualType qType = op->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001692
Chris Lattnerc996b172007-07-31 16:53:04 +00001693 if (const PointerType *PT = qType->getAsPointerType()) {
Steve Naroff826e91a2008-01-13 17:10:08 +00001694 // Note that per both C89 and C99, this is always legal, even
1695 // if ptype is an incomplete type or void.
1696 // It would be possible to warn about dereferencing a
1697 // void pointer, but it's completely well-defined,
1698 // and such a warning is unlikely to catch any mistakes.
1699 return PT->getPointeeType();
Steve Naroff758ada12007-05-28 16:15:57 +00001700 }
1701 Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer,
Steve Naroffeb9da942007-05-30 00:06:37 +00001702 qType.getAsString(), op->getSourceRange());
Steve Naroff35d85152007-05-07 00:24:15 +00001703 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +00001704}
Steve Naroff218bc2b2007-05-04 21:54:46 +00001705
1706static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
1707 tok::TokenKind Kind) {
1708 BinaryOperator::Opcode Opc;
1709 switch (Kind) {
1710 default: assert(0 && "Unknown binop!");
1711 case tok::star: Opc = BinaryOperator::Mul; break;
1712 case tok::slash: Opc = BinaryOperator::Div; break;
1713 case tok::percent: Opc = BinaryOperator::Rem; break;
1714 case tok::plus: Opc = BinaryOperator::Add; break;
1715 case tok::minus: Opc = BinaryOperator::Sub; break;
1716 case tok::lessless: Opc = BinaryOperator::Shl; break;
1717 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
1718 case tok::lessequal: Opc = BinaryOperator::LE; break;
1719 case tok::less: Opc = BinaryOperator::LT; break;
1720 case tok::greaterequal: Opc = BinaryOperator::GE; break;
1721 case tok::greater: Opc = BinaryOperator::GT; break;
1722 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
1723 case tok::equalequal: Opc = BinaryOperator::EQ; break;
1724 case tok::amp: Opc = BinaryOperator::And; break;
1725 case tok::caret: Opc = BinaryOperator::Xor; break;
1726 case tok::pipe: Opc = BinaryOperator::Or; break;
1727 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
1728 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
1729 case tok::equal: Opc = BinaryOperator::Assign; break;
1730 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
1731 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
1732 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
1733 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
1734 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
1735 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
1736 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
1737 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
1738 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
1739 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
1740 case tok::comma: Opc = BinaryOperator::Comma; break;
1741 }
1742 return Opc;
1743}
1744
Steve Naroff35d85152007-05-07 00:24:15 +00001745static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
1746 tok::TokenKind Kind) {
1747 UnaryOperator::Opcode Opc;
1748 switch (Kind) {
1749 default: assert(0 && "Unknown unary op!");
1750 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
1751 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
1752 case tok::amp: Opc = UnaryOperator::AddrOf; break;
1753 case tok::star: Opc = UnaryOperator::Deref; break;
1754 case tok::plus: Opc = UnaryOperator::Plus; break;
1755 case tok::minus: Opc = UnaryOperator::Minus; break;
1756 case tok::tilde: Opc = UnaryOperator::Not; break;
1757 case tok::exclaim: Opc = UnaryOperator::LNot; break;
1758 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
1759 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
1760 case tok::kw___real: Opc = UnaryOperator::Real; break;
1761 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
Chris Lattnerd0f76512007-06-08 22:16:53 +00001762 case tok::kw___extension__: Opc = UnaryOperator::Extension; break;
Steve Naroff35d85152007-05-07 00:24:15 +00001763 }
1764 return Opc;
1765}
1766
Steve Naroff218bc2b2007-05-04 21:54:46 +00001767// Binary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001768Action::ExprResult Sema::ActOnBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
Steve Naroff218bc2b2007-05-04 21:54:46 +00001769 ExprTy *LHS, ExprTy *RHS) {
1770 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
1771 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
1772
Steve Naroff83895f72007-09-16 03:34:24 +00001773 assert((lhs != 0) && "ActOnBinOp(): missing left expression");
1774 assert((rhs != 0) && "ActOnBinOp(): missing right expression");
Steve Naroff218bc2b2007-05-04 21:54:46 +00001775
Chris Lattner256c21b2007-06-28 03:53:10 +00001776 QualType ResultTy; // Result type of the binary operator.
1777 QualType CompTy; // Computation type for compound assignments (e.g. '+=')
Steve Naroff218bc2b2007-05-04 21:54:46 +00001778
1779 switch (Opc) {
1780 default:
1781 assert(0 && "Unknown binary expr!");
1782 case BinaryOperator::Assign:
Chris Lattner256c21b2007-06-28 03:53:10 +00001783 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +00001784 break;
1785 case BinaryOperator::Mul:
1786 case BinaryOperator::Div:
Chris Lattner256c21b2007-06-28 03:53:10 +00001787 ResultTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001788 break;
1789 case BinaryOperator::Rem:
Chris Lattner256c21b2007-06-28 03:53:10 +00001790 ResultTy = CheckRemainderOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001791 break;
1792 case BinaryOperator::Add:
Chris Lattner256c21b2007-06-28 03:53:10 +00001793 ResultTy = CheckAdditionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001794 break;
1795 case BinaryOperator::Sub:
Chris Lattner256c21b2007-06-28 03:53:10 +00001796 ResultTy = CheckSubtractionOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001797 break;
1798 case BinaryOperator::Shl:
1799 case BinaryOperator::Shr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001800 ResultTy = CheckShiftOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001801 break;
1802 case BinaryOperator::LE:
1803 case BinaryOperator::LT:
1804 case BinaryOperator::GE:
1805 case BinaryOperator::GT:
Chris Lattnerb620c342007-08-26 01:18:55 +00001806 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, true);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001807 break;
1808 case BinaryOperator::EQ:
1809 case BinaryOperator::NE:
Chris Lattnerb620c342007-08-26 01:18:55 +00001810 ResultTy = CheckCompareOperands(lhs, rhs, TokLoc, false);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001811 break;
1812 case BinaryOperator::And:
1813 case BinaryOperator::Xor:
1814 case BinaryOperator::Or:
Chris Lattner256c21b2007-06-28 03:53:10 +00001815 ResultTy = CheckBitwiseOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001816 break;
1817 case BinaryOperator::LAnd:
1818 case BinaryOperator::LOr:
Chris Lattner256c21b2007-06-28 03:53:10 +00001819 ResultTy = CheckLogicalOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001820 break;
1821 case BinaryOperator::MulAssign:
1822 case BinaryOperator::DivAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001823 CompTy = CheckMultiplyDivideOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001824 if (!CompTy.isNull())
1825 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001826 break;
1827 case BinaryOperator::RemAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001828 CompTy = CheckRemainderOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001829 if (!CompTy.isNull())
1830 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001831 break;
1832 case BinaryOperator::AddAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001833 CompTy = CheckAdditionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001834 if (!CompTy.isNull())
1835 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001836 break;
1837 case BinaryOperator::SubAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001838 CompTy = CheckSubtractionOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001839 if (!CompTy.isNull())
1840 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001841 break;
1842 case BinaryOperator::ShlAssign:
1843 case BinaryOperator::ShrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001844 CompTy = CheckShiftOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001845 if (!CompTy.isNull())
1846 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001847 break;
1848 case BinaryOperator::AndAssign:
1849 case BinaryOperator::XorAssign:
1850 case BinaryOperator::OrAssign:
Steve Naroffbe4c4d12007-08-24 19:07:16 +00001851 CompTy = CheckBitwiseOperands(lhs, rhs, TokLoc, true);
Chris Lattner256c21b2007-06-28 03:53:10 +00001852 if (!CompTy.isNull())
1853 ResultTy = CheckAssignmentOperands(lhs, rhs, TokLoc, CompTy);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001854 break;
1855 case BinaryOperator::Comma:
Chris Lattner256c21b2007-06-28 03:53:10 +00001856 ResultTy = CheckCommaOperands(lhs, rhs, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001857 break;
1858 }
Chris Lattner256c21b2007-06-28 03:53:10 +00001859 if (ResultTy.isNull())
Steve Naroff218bc2b2007-05-04 21:54:46 +00001860 return true;
Chris Lattner256c21b2007-06-28 03:53:10 +00001861 if (CompTy.isNull())
Chris Lattnerc11005f2007-08-28 18:36:55 +00001862 return new BinaryOperator(lhs, rhs, Opc, ResultTy, TokLoc);
Chris Lattner256c21b2007-06-28 03:53:10 +00001863 else
Chris Lattnerc11005f2007-08-28 18:36:55 +00001864 return new CompoundAssignOperator(lhs, rhs, Opc, ResultTy, CompTy, TokLoc);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001865}
1866
Steve Naroff35d85152007-05-07 00:24:15 +00001867// Unary Operators. 'Tok' is the token for the operator.
Steve Naroff83895f72007-09-16 03:34:24 +00001868Action::ExprResult Sema::ActOnUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
Chris Lattner86554282007-06-08 22:32:33 +00001869 ExprTy *input) {
1870 Expr *Input = (Expr*)input;
Steve Naroff35d85152007-05-07 00:24:15 +00001871 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1872 QualType resultType;
1873 switch (Opc) {
1874 default:
1875 assert(0 && "Unimplemented unary expr!");
1876 case UnaryOperator::PreInc:
1877 case UnaryOperator::PreDec:
Chris Lattner86554282007-06-08 22:32:33 +00001878 resultType = CheckIncrementDecrementOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001879 break;
1880 case UnaryOperator::AddrOf:
Chris Lattner86554282007-06-08 22:32:33 +00001881 resultType = CheckAddressOfOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001882 break;
1883 case UnaryOperator::Deref:
Steve Naroffb7235642007-12-18 04:06:57 +00001884 DefaultFunctionArrayConversion(Input);
Chris Lattner86554282007-06-08 22:32:33 +00001885 resultType = CheckIndirectionOperand(Input, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001886 break;
1887 case UnaryOperator::Plus:
1888 case UnaryOperator::Minus:
Steve Naroff31090012007-07-16 21:54:35 +00001889 UsualUnaryConversions(Input);
1890 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001891 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001892 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1893 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001894 break;
1895 case UnaryOperator::Not: // bitwise complement
Steve Naroff31090012007-07-16 21:54:35 +00001896 UsualUnaryConversions(Input);
1897 resultType = Input->getType();
Steve Naroff9d139172007-08-24 17:20:07 +00001898 // C99 6.5.3.3p1. We allow complex as a GCC extension.
1899 if (!resultType->isIntegerType()) {
1900 if (resultType->isComplexType())
1901 // C99 does not support '~' for complex conjugation.
1902 Diag(OpLoc, diag::ext_integer_complement_complex,
1903 resultType.getAsString());
1904 else
1905 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1906 resultType.getAsString());
1907 }
Steve Naroff35d85152007-05-07 00:24:15 +00001908 break;
1909 case UnaryOperator::LNot: // logical negation
Steve Naroff71b59a92007-06-04 22:22:31 +00001910 // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5).
Steve Naroff31090012007-07-16 21:54:35 +00001911 DefaultFunctionArrayConversion(Input);
1912 resultType = Input->getType();
Steve Naroff35d85152007-05-07 00:24:15 +00001913 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001914 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1915 resultType.getAsString());
Chris Lattnerbe31ed82007-06-02 19:11:33 +00001916 // LNot always has type int. C99 6.5.3.3p5.
1917 resultType = Context.IntTy;
Steve Naroff35d85152007-05-07 00:24:15 +00001918 break;
1919 case UnaryOperator::SizeOf:
Chris Lattner86554282007-06-08 22:32:33 +00001920 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, true);
Steve Naroff043d45d2007-05-15 02:32:35 +00001921 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001922 case UnaryOperator::AlignOf:
Chris Lattner86554282007-06-08 22:32:33 +00001923 resultType = CheckSizeOfAlignOfOperand(Input->getType(), OpLoc, false);
1924 break;
Chris Lattner30b5dd02007-08-24 21:16:53 +00001925 case UnaryOperator::Real:
Chris Lattner30b5dd02007-08-24 21:16:53 +00001926 case UnaryOperator::Imag:
Chris Lattner74ed76b2007-08-24 21:41:10 +00001927 resultType = CheckRealImagOperand(Input, OpLoc);
Chris Lattner30b5dd02007-08-24 21:16:53 +00001928 break;
Chris Lattner86554282007-06-08 22:32:33 +00001929 case UnaryOperator::Extension:
Chris Lattner86554282007-06-08 22:32:33 +00001930 resultType = Input->getType();
Steve Naroff043d45d2007-05-15 02:32:35 +00001931 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001932 }
1933 if (resultType.isNull())
1934 return true;
Chris Lattner86554282007-06-08 22:32:33 +00001935 return new UnaryOperator(Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001936}
Chris Lattnereefa10e2007-05-28 06:56:27 +00001937
Steve Naroff66356bd2007-09-16 14:56:35 +00001938/// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo".
1939Sema::ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc,
Chris Lattnereefa10e2007-05-28 06:56:27 +00001940 SourceLocation LabLoc,
1941 IdentifierInfo *LabelII) {
1942 // Look up the record for this label identifier.
1943 LabelStmt *&LabelDecl = LabelMap[LabelII];
1944
1945 // If we haven't seen this label yet, create a forward reference.
1946 if (LabelDecl == 0)
1947 LabelDecl = new LabelStmt(LabLoc, LabelII, 0);
1948
1949 // Create the AST node. The address of a label always has type 'void*'.
Chris Lattnerd268a7a2007-08-03 17:31:20 +00001950 return new AddrLabelExpr(OpLoc, LabLoc, LabelDecl,
1951 Context.getPointerType(Context.VoidTy));
Chris Lattnereefa10e2007-05-28 06:56:27 +00001952}
1953
Steve Naroff66356bd2007-09-16 14:56:35 +00001954Sema::ExprResult Sema::ActOnStmtExpr(SourceLocation LPLoc, StmtTy *substmt,
Chris Lattner366727f2007-07-24 16:58:17 +00001955 SourceLocation RPLoc) { // "({..})"
1956 Stmt *SubStmt = static_cast<Stmt*>(substmt);
1957 assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!");
1958 CompoundStmt *Compound = cast<CompoundStmt>(SubStmt);
1959
1960 // FIXME: there are a variety of strange constraints to enforce here, for
1961 // example, it is not possible to goto into a stmt expression apparently.
1962 // More semantic analysis is needed.
1963
1964 // FIXME: the last statement in the compount stmt has its value used. We
1965 // should not warn about it being unused.
1966
1967 // If there are sub stmts in the compound stmt, take the type of the last one
1968 // as the type of the stmtexpr.
1969 QualType Ty = Context.VoidTy;
1970
1971 if (!Compound->body_empty())
1972 if (Expr *LastExpr = dyn_cast<Expr>(Compound->body_back()))
1973 Ty = LastExpr->getType();
1974
1975 return new StmtExpr(Compound, Ty, LPLoc, RPLoc);
1976}
Steve Naroff78864672007-08-01 22:05:33 +00001977
Steve Naroff66356bd2007-09-16 14:56:35 +00001978Sema::ExprResult Sema::ActOnBuiltinOffsetOf(SourceLocation BuiltinLoc,
Chris Lattnerf17bd422007-08-30 17:45:32 +00001979 SourceLocation TypeLoc,
1980 TypeTy *argty,
1981 OffsetOfComponent *CompPtr,
1982 unsigned NumComponents,
1983 SourceLocation RPLoc) {
1984 QualType ArgTy = QualType::getFromOpaquePtr(argty);
1985 assert(!ArgTy.isNull() && "Missing type argument!");
1986
1987 // We must have at least one component that refers to the type, and the first
1988 // one is known to be a field designator. Verify that the ArgTy represents
1989 // a struct/union/class.
1990 if (!ArgTy->isRecordType())
1991 return Diag(TypeLoc, diag::err_offsetof_record_type,ArgTy.getAsString());
1992
1993 // Otherwise, create a compound literal expression as the base, and
1994 // iteratively process the offsetof designators.
Steve Naroffd32419d2008-01-14 18:19:28 +00001995 Expr *Res = new CompoundLiteralExpr(SourceLocation(), ArgTy, 0, false);
Chris Lattnerf17bd422007-08-30 17:45:32 +00001996
Chris Lattner78502cf2007-08-31 21:49:13 +00001997 // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a
1998 // GCC extension, diagnose them.
1999 if (NumComponents != 1)
2000 Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator,
2001 SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd));
2002
Chris Lattnerf17bd422007-08-30 17:45:32 +00002003 for (unsigned i = 0; i != NumComponents; ++i) {
2004 const OffsetOfComponent &OC = CompPtr[i];
2005 if (OC.isBrackets) {
2006 // Offset of an array sub-field. TODO: Should we allow vector elements?
2007 const ArrayType *AT = Res->getType()->getAsArrayType();
2008 if (!AT) {
2009 delete Res;
2010 return Diag(OC.LocEnd, diag::err_offsetof_array_type,
2011 Res->getType().getAsString());
2012 }
2013
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002014 // FIXME: C++: Verify that operator[] isn't overloaded.
2015
Chris Lattnerf17bd422007-08-30 17:45:32 +00002016 // C99 6.5.2.1p1
2017 Expr *Idx = static_cast<Expr*>(OC.U.E);
2018 if (!Idx->getType()->isIntegerType())
2019 return Diag(Idx->getLocStart(), diag::err_typecheck_subscript,
2020 Idx->getSourceRange());
2021
2022 Res = new ArraySubscriptExpr(Res, Idx, AT->getElementType(), OC.LocEnd);
2023 continue;
2024 }
2025
2026 const RecordType *RC = Res->getType()->getAsRecordType();
2027 if (!RC) {
2028 delete Res;
2029 return Diag(OC.LocEnd, diag::err_offsetof_record_type,
2030 Res->getType().getAsString());
2031 }
2032
2033 // Get the decl corresponding to this.
2034 RecordDecl *RD = RC->getDecl();
2035 FieldDecl *MemberDecl = RD->getMember(OC.U.IdentInfo);
2036 if (!MemberDecl)
2037 return Diag(BuiltinLoc, diag::err_typecheck_no_member,
2038 OC.U.IdentInfo->getName(),
2039 SourceRange(OC.LocStart, OC.LocEnd));
Chris Lattner98dbf0a2007-08-30 17:59:59 +00002040
2041 // FIXME: C++: Verify that MemberDecl isn't a static field.
2042 // FIXME: Verify that MemberDecl isn't a bitfield.
2043
Chris Lattnerf17bd422007-08-30 17:45:32 +00002044 Res = new MemberExpr(Res, false, MemberDecl, OC.LocEnd);
2045 }
2046
2047 return new UnaryOperator(Res, UnaryOperator::OffsetOf, Context.getSizeType(),
2048 BuiltinLoc);
2049}
2050
2051
Steve Naroff66356bd2007-09-16 14:56:35 +00002052Sema::ExprResult Sema::ActOnTypesCompatibleExpr(SourceLocation BuiltinLoc,
Steve Naroff78864672007-08-01 22:05:33 +00002053 TypeTy *arg1, TypeTy *arg2,
2054 SourceLocation RPLoc) {
2055 QualType argT1 = QualType::getFromOpaquePtr(arg1);
2056 QualType argT2 = QualType::getFromOpaquePtr(arg2);
2057
2058 assert((!argT1.isNull() && !argT2.isNull()) && "Missing type argument(s)");
2059
Chris Lattnerf17bd422007-08-30 17:45:32 +00002060 return new TypesCompatibleExpr(Context.IntTy, BuiltinLoc, argT1, argT2,RPLoc);
Steve Naroff78864672007-08-01 22:05:33 +00002061}
2062
Steve Naroff66356bd2007-09-16 14:56:35 +00002063Sema::ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, ExprTy *cond,
Steve Naroff9efdabc2007-08-03 21:21:27 +00002064 ExprTy *expr1, ExprTy *expr2,
2065 SourceLocation RPLoc) {
2066 Expr *CondExpr = static_cast<Expr*>(cond);
2067 Expr *LHSExpr = static_cast<Expr*>(expr1);
2068 Expr *RHSExpr = static_cast<Expr*>(expr2);
2069
2070 assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)");
2071
2072 // The conditional expression is required to be a constant expression.
2073 llvm::APSInt condEval(32);
2074 SourceLocation ExpLoc;
2075 if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc))
2076 return Diag(ExpLoc, diag::err_typecheck_choose_expr_requires_constant,
2077 CondExpr->getSourceRange());
2078
2079 // If the condition is > zero, then the AST type is the same as the LSHExpr.
2080 QualType resType = condEval.getZExtValue() ? LHSExpr->getType() :
2081 RHSExpr->getType();
2082 return new ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, resType, RPLoc);
2083}
2084
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002085Sema::ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc,
2086 ExprTy *expr, TypeTy *type,
Chris Lattner9bad62c2008-01-04 18:04:52 +00002087 SourceLocation RPLoc) {
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002088 Expr *E = static_cast<Expr*>(expr);
2089 QualType T = QualType::getFromOpaquePtr(type);
2090
2091 InitBuiltinVaListType();
2092
Chris Lattner9bad62c2008-01-04 18:04:52 +00002093 if (CheckAssignmentConstraints(Context.getBuiltinVaListType(), E->getType())
2094 != Compatible)
Anders Carlsson7e13ab82007-10-15 20:28:48 +00002095 return Diag(E->getLocStart(),
2096 diag::err_first_argument_to_va_arg_not_of_type_va_list,
2097 E->getType().getAsString(),
2098 E->getSourceRange());
2099
2100 // FIXME: Warn if a non-POD type is passed in.
2101
2102 return new VAArgExpr(BuiltinLoc, E, T, RPLoc);
2103}
2104
Chris Lattner9bad62c2008-01-04 18:04:52 +00002105bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy,
2106 SourceLocation Loc,
2107 QualType DstType, QualType SrcType,
2108 Expr *SrcExpr, const char *Flavor) {
2109 // Decode the result (notice that AST's are still created for extensions).
2110 bool isInvalid = false;
2111 unsigned DiagKind;
2112 switch (ConvTy) {
2113 default: assert(0 && "Unknown conversion type");
2114 case Compatible: return false;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002115 case PointerToInt:
Chris Lattner9bad62c2008-01-04 18:04:52 +00002116 DiagKind = diag::ext_typecheck_convert_pointer_int;
2117 break;
Chris Lattner940cfeb2008-01-04 18:22:42 +00002118 case IntToPointer:
2119 DiagKind = diag::ext_typecheck_convert_int_pointer;
2120 break;
Chris Lattner9bad62c2008-01-04 18:04:52 +00002121 case IncompatiblePointer:
2122 DiagKind = diag::ext_typecheck_convert_incompatible_pointer;
2123 break;
2124 case FunctionVoidPointer:
2125 DiagKind = diag::ext_typecheck_convert_pointer_void_func;
2126 break;
2127 case CompatiblePointerDiscardsQualifiers:
2128 DiagKind = diag::ext_typecheck_convert_discards_qualifiers;
2129 break;
2130 case Incompatible:
2131 DiagKind = diag::err_typecheck_convert_incompatible;
2132 isInvalid = true;
2133 break;
2134 }
2135
2136 Diag(Loc, DiagKind, DstType.getAsString(), SrcType.getAsString(), Flavor,
2137 SrcExpr->getSourceRange());
2138 return isInvalid;
2139}
2140