blob: ef8e8ba24e055d8093de89edc5bd73edc696db91 [file] [log] [blame]
Chris Lattner5b183d82006-11-10 05:03:26 +00001//===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for expressions.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
Chris Lattnercb6a3822006-11-10 06:20:45 +000015#include "clang/AST/ASTContext.h"
Chris Lattner17ed4872006-11-20 04:58:19 +000016#include "clang/AST/Decl.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000017#include "clang/AST/Expr.h"
18#include "clang/Lex/Preprocessor.h"
Steve Naroff09ef4742007-03-09 23:16:33 +000019#include "clang/Lex/LiteralSupport.h"
Steve Narofff2fb89e2007-03-13 20:29:44 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000021#include "clang/Basic/Diagnostic.h"
Chris Lattnerac18be92006-11-20 06:49:47 +000022#include "clang/Basic/LangOptions.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000023#include "clang/Basic/TargetInfo.h"
24#include "llvm/ADT/SmallString.h"
Steve Naroff17f76e02007-05-03 21:03:48 +000025#include "llvm/ADT/StringExtras.h"
Chris Lattner5b183d82006-11-10 05:03:26 +000026using namespace llvm;
27using namespace clang;
28
Steve Naroffe4718892007-04-27 18:30:00 +000029// Sema.h avoids including Expr.h. As a result, all the Check* functions take
30// an unsigned which is really an enum. These typedefs provide a short hand
31// notiation for casting (to keep the lines within 80 columns:-)
32typedef BinaryOperator::Opcode BOP;
33typedef UnaryOperator::Opcode UOP;
34
Steve Naroffdf7855b2007-02-21 23:46:25 +000035/// ParseStringLiteral - The specified tokens were lexed as pasted string
Chris Lattner5b183d82006-11-10 05:03:26 +000036/// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string
37/// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from
38/// multiple tokens. However, the common case is that StringToks points to one
39/// string.
40///
41Action::ExprResult
Steve Naroffdf7855b2007-02-21 23:46:25 +000042Sema::ParseStringLiteral(const LexerToken *StringToks, unsigned NumStringToks) {
Chris Lattner5b183d82006-11-10 05:03:26 +000043 assert(NumStringToks && "Must have at least one string!");
44
Steve Naroff4f88b312007-03-13 22:37:02 +000045 StringLiteralParser Literal(StringToks, NumStringToks, PP, Context.Target);
46 if (Literal.hadError)
47 return ExprResult(true);
Chris Lattner5b183d82006-11-10 05:03:26 +000048
Chris Lattner5b183d82006-11-10 05:03:26 +000049 SmallVector<SourceLocation, 4> StringTokLocs;
50 for (unsigned i = 0; i != NumStringToks; ++i)
51 StringTokLocs.push_back(StringToks[i].getLocation());
Steve Narofff1e53692007-03-23 22:27:02 +000052
53 // FIXME: handle wchar_t
Steve Naroffe5aa9be2007-04-05 22:36:20 +000054 QualType t = Context.getPointerType(Context.CharTy);
Steve Narofff1e53692007-03-23 22:27:02 +000055
Chris Lattner5b183d82006-11-10 05:03:26 +000056 // FIXME: use factory.
Chris Lattner5b183d82006-11-10 05:03:26 +000057 // Pass &StringTokLocs[0], StringTokLocs.size() to factory!
Steve Naroff4f88b312007-03-13 22:37:02 +000058 return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
Steve Naroff53f07dc2007-05-17 21:49:33 +000059 Literal.AnyWide, t, StringToks[0].getLocation(),
60 StringToks[NumStringToks-1].getLocation());
Chris Lattner5b183d82006-11-10 05:03:26 +000061}
62
Chris Lattnere168f762006-11-10 05:29:30 +000063
Chris Lattnerac18be92006-11-20 06:49:47 +000064/// ParseIdentifierExpr - The parser read an identifier in expression context,
65/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
66/// identifier is used in an function call context.
67Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
68 IdentifierInfo &II,
69 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000070 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000071 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000072 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000073 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000074 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000075 if (HasTrailingLParen &&
76 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000077 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000078 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000079 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000080 // If this name wasn't predeclared and if this is not a function call,
81 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000082 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000083 }
Chris Lattner17ed4872006-11-20 04:58:19 +000084 }
85
Steve Naroff46ba1eb2007-04-03 23:13:13 +000086 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000087 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000088 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000089 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
90
91 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000092}
Chris Lattnere168f762006-11-10 05:29:30 +000093
Chris Lattner17ed4872006-11-20 04:58:19 +000094Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
95 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000096 switch (Kind) {
97 default:
98 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000099 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +0000100 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
101 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
102 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +0000103 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +0000104 }
105}
106
Steve Naroffae4143e2007-04-26 20:39:23 +0000107Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
108 SmallString<16> CharBuffer;
109 CharBuffer.resize(Tok.getLength());
110 const char *ThisTokBegin = &CharBuffer[0];
111 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
112
113 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
114 Tok.getLocation(), PP);
115 if (Literal.hadError())
116 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000117 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
118 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000119}
120
Steve Naroff8160ea22007-03-06 01:09:46 +0000121Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000122 // fast path for a single digit (which is quite common). A single digit
123 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
124 if (Tok.getLength() == 1) {
125 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Steve Naroff509fe022007-05-17 01:16:00 +0000126 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy,
127 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000128 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000129 SmallString<512> IntegerBuffer;
130 IntegerBuffer.resize(Tok.getLength());
131 const char *ThisTokBegin = &IntegerBuffer[0];
132
133 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
134 // - We know that ThisTokBuf points to a buffer that is big enough for the
135 // whole token and 'spelled' tokens can only shrink.
136 // - In practice, the local buffer is only used when the spelling doesn't
137 // match the original token (which is rare). The common case simply returns
138 // a pointer to a *constant* buffer (avoiding a copy).
139
140 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000141 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000142 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000143 if (Literal.hadError)
144 return ExprResult(true);
145
Steve Naroff09ef4742007-03-09 23:16:33 +0000146 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000147 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000148 if (Literal.hasSuffix()) {
149 if (Literal.isLong)
150 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
151 else if (Literal.isLongLong)
152 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
153 else
154 t = Context.UnsignedIntTy;
155 } else {
156 t = Context.IntTy; // implicit type is "int"
157 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000158 uintmax_t val;
159 if (Literal.GetIntegerValue(val)) {
Steve Naroff509fe022007-05-17 01:16:00 +0000160 return new IntegerLiteral(val, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000161 }
162 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000163 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000164 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000165 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000166 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000167}
168
169Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
170 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000171 Expr *e = (Expr *)Val;
172 assert((e != 0) && "ParseParenExpr() missing expr");
Steve Naroff53f07dc2007-05-17 21:49:33 +0000173 return new ParenExpr(L, R, e);
Chris Lattnere168f762006-11-10 05:29:30 +0000174}
175
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000176/// The UsualUnaryConversion() function is *not* called by this routine.
177/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000178QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
179 SourceLocation OpLoc, bool isSizeof) {
180 // C99 6.5.3.4p1:
181 if (isa<FunctionType>(exprType) && isSizeof)
182 // alignof(function) is allowed.
183 Diag(OpLoc, diag::ext_sizeof_function_type);
184 else if (exprType->isVoidType())
185 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
186 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000187 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000188 diag::err_alignof_incomplete_type,
189 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000190 return QualType(); // error
191 }
192 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
193 return Context.getSizeType();
194}
195
Chris Lattnere168f762006-11-10 05:29:30 +0000196Action::ExprResult Sema::
197ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000198 SourceLocation LPLoc, TypeTy *Ty,
199 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000200 // If error parsing type, ignore.
201 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000202
203 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000204 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000205
Steve Naroff043d45d2007-05-15 02:32:35 +0000206 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
207
208 if (resultType.isNull())
209 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000210 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000211}
212
213
214Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
215 tok::TokenKind Kind,
216 ExprTy *Input) {
217 UnaryOperator::Opcode Opc;
218 switch (Kind) {
219 default: assert(0 && "Unknown unary op!");
220 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
221 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
222 }
Steve Naroff35d85152007-05-07 00:24:15 +0000223 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
224 if (result.isNull())
225 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000226 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000227}
228
229Action::ExprResult Sema::
230ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
231 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000232 QualType t1 = ((Expr *)Base)->getType();
233 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000234
235 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000236 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000237
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000238 QualType canonT1 = t1.getCanonicalType();
239 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000240
Steve Naroffc1aadb12007-03-28 21:49:40 +0000241 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
242 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000243 // in the subscript position. As a result, we need to derive the array base
244 // and index from the expression types.
245
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000246 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000247 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
248 baseType = canonT1;
249 indexType = canonT2;
250 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
251 baseType = canonT2;
252 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000253 } else
254 return Diag(LLoc, diag::err_typecheck_subscript_value);
255
Steve Naroffc1aadb12007-03-28 21:49:40 +0000256 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000257 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000258 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000259
Steve Naroff95af0132007-03-30 23:47:58 +0000260 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000261 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000262 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
263 resultType = ary->getElementType();
264 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
265 resultType = ary->getPointeeType();
266 // in practice, the following check catches trying to index a pointer
267 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000268 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000269 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
270 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000271 }
Steve Naroff509fe022007-05-17 01:16:00 +0000272 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000273}
274
275Action::ExprResult Sema::
276ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
277 tok::TokenKind OpKind, SourceLocation MemberLoc,
278 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000279 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000280
281 assert(!qualifiedType.isNull() && "no type for member expression");
282
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000283 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000284
285 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000286 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
287 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000288 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000289 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000290 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
291 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000292 if (!isa<RecordType>(canonType))
293 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
294
295 // get the struct/union definition from the type.
296 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000297
Steve Naroff92e30f82007-04-02 22:35:25 +0000298 if (canonType->isIncompleteType())
299 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000300
Steve Naroff92e30f82007-04-02 22:35:25 +0000301 FieldDecl *MemberDecl = RD->getMember(&Member);
302 if (!MemberDecl)
303 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
304
305 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000306}
307
308/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
309/// This provides the location of the left/right parens and a list of comma
310/// locations.
311Action::ExprResult Sema::
312ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000313 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000314 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000315 QualType qType = ((Expr *)Fn)->getType();
316
317 assert(!qType.isNull() && "no type for function call expression");
318
Steve Naroff17f76e02007-05-03 21:03:48 +0000319 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000320
Steve Naroff17f76e02007-05-03 21:03:48 +0000321 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000322
Steve Naroff17f76e02007-05-03 21:03:48 +0000323 // If a prototype isn't declared, the parser implicitly defines a func decl
324 QualType resultType = funcT->getResultType();
325
326 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
327 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
328 // assignment, to the types of the corresponding parameter, ...
329
330 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000331 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000332
Steve Naroffb8c289d2007-05-08 22:18:00 +0000333 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000334 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000335 else if (NumArgsInCall > NumArgsInProto) {
336 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000337 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000338 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000339 }
340 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000341 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000342 QualType lhsType = proto->getArgType(i);
343 QualType rhsType = ((Expr **)Args)[i]->getType();
344
345 if (lhsType == rhsType) // common case, fast path...
346 continue;
347 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000348 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000349
350 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
351
352 // decode the result (notice that AST's are still created for extensions).
353 // FIXME: consider fancier error diagnostics (since this is quite common).
354 // #1: emit the actual prototype arg...requires adding source loc info.
355 // #2: pass Diag the offending argument type...requires hacking Diag.
356 switch (result) {
357 case Compatible:
358 break;
359 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000360 // check for null pointer constant (C99 6.3.2.3p3)
361 if (!((Expr **)Args)[i]->isNullPointerConstant())
362 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000363 break;
364 case IntFromPointer:
365 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
366 break;
367 case IncompatiblePointer:
368 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
369 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000370 case CompatiblePointerDiscardsQualifiers:
371 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
372 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000373 case Incompatible:
374 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
375 }
376 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000377 // Even if the types checked, bail if we had the wrong number of arguments.
378 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
379 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000380 }
Steve Naroff509fe022007-05-17 01:16:00 +0000381 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
382 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000383}
384
385Action::ExprResult Sema::
386ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
387 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000388 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000389 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroff509fe022007-05-17 01:16:00 +0000390 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000391}
392
Steve Narofff8a28c52007-05-15 20:29:32 +0000393inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
394 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000395 QualType cond = Cond->getType();
396 QualType lhs = LHS->getType();
397 QualType rhs = RHS->getType();
398
Steve Narofff8a28c52007-05-15 20:29:32 +0000399 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
400 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
401 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
402
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000403 cond = UsualUnaryConversion(cond);
404 lhs = UsualUnaryConversion(lhs);
405 rhs = UsualUnaryConversion(rhs);
406
407 // first, check the condition.
408 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff53f07dc2007-05-17 21:49:33 +0000409 Diag(Cond->getLocStart(), diag::err_typecheck_cond_expect_scalar,
Steve Naroff509fe022007-05-17 01:16:00 +0000410 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000411 return QualType();
412 }
413 // now check the two expressions.
414 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
415 return UsualArithmeticConversions(lhs, rhs);
416
417 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
418 (lhs->isUnionType() && rhs->isUnionType())) {
419 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
420 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
421
422 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
423 return lhs;
424 else {
425 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
426 lhs.getAsString(), rhs.getAsString());
427 return QualType();
428 }
429 }
430 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
431 QualType lhptee, rhptee;
432
433 // get the "pointed to" type
434 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
435 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
436
437 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
438 if (lhptee.getUnqualifiedType()->isVoidType() &&
439 (rhptee->isObjectType() || rhptee->isIncompleteType()))
440 return lhs;
441 if (rhptee.getUnqualifiedType()->isVoidType() &&
442 (lhptee->isObjectType() || lhptee->isIncompleteType()))
443 return rhs;
444
445 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
446 // *or* to differently qualified versions of compatible types, the result
447 // type is a pointer to an appropriately qualified version of the
448 // *composite* type.
449 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
450 rhptee.getUnqualifiedType())) {
451 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
452 lhs.getAsString(), rhs.getAsString());
453 return lhs; // FIXME: this is an _ext - is this return o.k?
454 }
455 }
456 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
457 return lhs;
458 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
459 return lhs;
460 if (rhs->isPointerType() && LHS->isNullPointerConstant()) // C99 6.5.15p3
461 return rhs;
462
463 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
464 lhs.getAsString(), rhs.getAsString());
465 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000466}
467
Chris Lattnere168f762006-11-10 05:29:30 +0000468/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
469/// in the case of a the GNU conditional expr extension.
470Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
471 SourceLocation ColonLoc,
472 ExprTy *Cond, ExprTy *LHS,
473 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000474 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
475 (Expr *)RHS, QuestionLoc);
476 if (result.isNull())
477 return true;
478 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000479}
480
Steve Naroff1926c832007-04-24 00:23:05 +0000481/// UsualUnaryConversion - Performs various conversions that are common to most
482/// operators (C99 6.3). The conversions of array and function types are
483/// sometimes surpressed. For example, the array->pointer conversion doesn't
484/// apply if the array is an argument to the sizeof or address (&) operators.
485/// In these instances, this routine should *not* be called.
486QualType Sema::UsualUnaryConversion(QualType t) {
487 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000488
Steve Naroff1926c832007-04-24 00:23:05 +0000489 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
490 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000491 if (t->isFunctionType()) // C99 6.3.2.1p4
492 return Context.getPointerType(t.getCanonicalType());
493 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
494 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000495 return t;
496}
497
498/// UsualArithmeticConversions - Performs various conversions that are common to
499/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
500/// routine returns the first non-arithmetic type found. The client is
501/// responsible for emitting appropriate error diagnostics.
502QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000503 QualType lhs = UsualUnaryConversion(t1);
504 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000505
506 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000507 if (!lhs->isArithmeticType())
508 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000509 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000510 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000511
Steve Narofff633d092007-04-25 19:01:39 +0000512 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000513 if (lhs == rhs)
514 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000515
Steve Naroffb01bbe32007-04-25 01:22:31 +0000516 // at this point, we have two different arithmetic types.
517
518 // Handle complex types first (C99 6.3.1.8p1).
519 if (lhs->isComplexType() || rhs->isComplexType()) {
520 // if we have an integer operand, the result is the complex type.
521 if (rhs->isIntegerType())
522 return lhs;
523 if (lhs->isIntegerType())
524 return rhs;
525
Steve Naroffe4718892007-04-27 18:30:00 +0000526 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000527 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000528 // Now handle "real" floating types (i.e. float, double, long double).
529 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
530 // if we have an integer operand, the result is the real floating type.
531 if (rhs->isIntegerType())
532 return lhs;
533 if (lhs->isIntegerType())
534 return rhs;
535
536 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000537 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000538 }
Steve Naroffe4718892007-04-27 18:30:00 +0000539 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000540}
541
Steve Naroff3f597292007-05-11 22:18:03 +0000542// CheckPointerTypesForAssignment - This is a very tricky routine (despite
543// being closely modeled after the C99 spec:-). The odd characteristic of this
544// routine is it effectively iqnores the qualifiers on the top level pointee.
545// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
546// FIXME: add a couple examples in this comment.
547QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
548 QualType rhsType,
549 AssignmentConversionResult &r) {
550 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000551
552 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000553 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
554 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000555
556 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000557 lhptee = lhptee.getCanonicalType();
558 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000559
Steve Naroff3f597292007-05-11 22:18:03 +0000560 // C99 6.5.16.1p1: This following citation is common to constraints
561 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
562 // qualifiers of the type *pointed to* by the right;
563 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
564 rhptee.getQualifiers())
565 r = CompatiblePointerDiscardsQualifiers;
566
567 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
568 // incomplete type and the other is a pointer to a qualified or unqualified
569 // version of void...
570 if (lhptee.getUnqualifiedType()->isVoidType() &&
571 (rhptee->isObjectType() || rhptee->isIncompleteType()))
572 ;
573 else if (rhptee.getUnqualifiedType()->isVoidType() &&
574 (lhptee->isObjectType() || lhptee->isIncompleteType()))
575 ;
576 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
577 // unqualified versions of compatible types, ...
578 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
579 rhptee.getUnqualifiedType()))
580 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
581 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000582}
583
Steve Naroff17f76e02007-05-03 21:03:48 +0000584/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
585/// has code to accommodate several GCC extensions when type checking
586/// pointers. Here are some objectionable examples that GCC considers warnings:
587///
588/// int a, *pint;
589/// short *pshort;
590/// struct foo *pfoo;
591///
592/// pint = pshort; // warning: assignment from incompatible pointer type
593/// a = pint; // warning: assignment makes integer from pointer without a cast
594/// pint = a; // warning: assignment makes pointer from integer without a cast
595/// pint = pfoo; // warning: assignment from incompatible pointer type
596///
597/// As a result, the code for dealing with pointers is more complex than the
598/// C99 spec dictates.
599/// Note: the warning above turn into errors when -pedantic-errors is enabled.
600///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000601QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000602 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000603 // this check seems unnatural, however it necessary to insure the proper
604 // conversion of functions/arrays. If the conversion where done for all
605 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
606 // unary expressions that surpress this implicit conversion (&, sizeof).
607 if (rhsType->isFunctionType() || rhsType->isArrayType())
608 rhsType = UsualUnaryConversion(rhsType);
609
Steve Naroff17f76e02007-05-03 21:03:48 +0000610 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000611 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
612 return lhsType;
613 else if (lhsType->isPointerType()) {
614 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000615 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000616 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000617 }
Steve Naroff3f597292007-05-11 22:18:03 +0000618 if (rhsType->isPointerType())
619 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000620 } else if (rhsType->isPointerType()) {
621 if (lhsType->isIntegerType()) {
622 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
623 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000624 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000625 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000626 }
Steve Naroff3f597292007-05-11 22:18:03 +0000627 if (lhsType->isPointerType())
628 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000629 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
630 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000631 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000632 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000633 r = Incompatible;
634 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000635}
636
Steve Naroff218bc2b2007-05-04 21:54:46 +0000637inline QualType Sema::CheckMultiplyDivideOperands(
638 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000639{
Steve Naroff1926c832007-04-24 00:23:05 +0000640 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000641
Steve Naroff218bc2b2007-05-04 21:54:46 +0000642 if (resType->isArithmeticType())
643 return resType;
644 Diag(loc, diag::err_typecheck_invalid_operands);
645 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000646}
647
Steve Naroff218bc2b2007-05-04 21:54:46 +0000648inline QualType Sema::CheckRemainderOperands(
649 Expr *lex, Expr *rex, SourceLocation loc)
650{
651 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
652
653 if (resType->isIntegerType())
654 return resType;
655 Diag(loc, diag::err_typecheck_invalid_operands);
656 return QualType();
657}
658
659inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
660 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000661{
Steve Naroffe4718892007-04-27 18:30:00 +0000662 QualType lhsType = lex->getType(), rhsType = rex->getType();
663 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
664
665 // handle the common case first (both operands are arithmetic).
666 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000667 return resType;
668
669 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
670 (lhsType->isIntegerType() && rhsType->isPointerType()))
671 return resType;
672 Diag(loc, diag::err_typecheck_invalid_operands);
673 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000674}
675
Steve Naroff218bc2b2007-05-04 21:54:46 +0000676inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
677 Expr *lex, Expr *rex, SourceLocation loc)
678{
679 QualType lhsType = lex->getType(), rhsType = rex->getType();
680 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
681
682 // handle the common case first (both operands are arithmetic).
683 if (resType->isArithmeticType())
684 return resType;
685 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
686 (lhsType->isPointerType() && rhsType->isPointerType()))
687 return resType;
688 Diag(loc, diag::err_typecheck_invalid_operands);
689 return QualType();
690}
691
692inline QualType Sema::CheckShiftOperands( // C99 6.5.7
693 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000694{
695 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
696
Steve Naroff218bc2b2007-05-04 21:54:46 +0000697 if (resType->isIntegerType())
698 return resType;
699 Diag(loc, diag::err_typecheck_invalid_operands);
700 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000701}
702
Steve Naroff218bc2b2007-05-04 21:54:46 +0000703inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
704 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000705{
706 QualType lType = lex->getType(), rType = rex->getType();
707
708 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000709 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000710
711 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000712 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000713
Steve Naroff043d45d2007-05-15 02:32:35 +0000714 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000715 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000716 return Context.IntTy;
717 }
718 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000719 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000720}
721
Steve Naroff218bc2b2007-05-04 21:54:46 +0000722inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
723 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000724{
725 QualType lType = lex->getType(), rType = rex->getType();
726
727 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000728 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000729 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000730 return Context.IntTy;
731
Steve Naroff043d45d2007-05-15 02:32:35 +0000732 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000733 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000734 return Context.IntTy;
735 }
736 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000737 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000738}
739
Steve Naroff218bc2b2007-05-04 21:54:46 +0000740inline QualType Sema::CheckBitwiseOperands(
741 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000742{
743 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
744
Steve Naroff218bc2b2007-05-04 21:54:46 +0000745 if (resType->isIntegerType())
746 return resType;
747 Diag(loc, diag::err_typecheck_invalid_operands);
748 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000749}
750
Steve Naroff218bc2b2007-05-04 21:54:46 +0000751inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
752 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000753{
Steve Naroffe4718892007-04-27 18:30:00 +0000754 QualType lhsType = UsualUnaryConversion(lex->getType());
755 QualType rhsType = UsualUnaryConversion(rex->getType());
756
Steve Naroff218bc2b2007-05-04 21:54:46 +0000757 if (lhsType->isScalarType() || rhsType->isScalarType())
758 return Context.IntTy;
759 Diag(loc, diag::err_typecheck_invalid_operands);
760 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000761}
762
Steve Naroff35d85152007-05-07 00:24:15 +0000763inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
764 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000765{
Steve Naroff0af91202007-04-27 21:51:21 +0000766 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000767 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000768 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000769
770 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000771 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000772 if (lhsType.isConstQualified()) {
773 Diag(loc, diag::err_typecheck_assign_const);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000774 hadError = true;
775 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
776 Diag(loc, diag::err_typecheck_assign_non_lvalue);
777 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000778 }
779 if (lhsType == rhsType) // common case, fast path...
780 return lhsType;
781
782 AssignmentConversionResult result;
783 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
784
785 // decode the result (notice that extensions still return a type).
786 switch (result) {
787 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000788 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000789 case Incompatible:
790 Diag(loc, diag::err_typecheck_assign_incompatible);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000791 hadError = true;
792 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000793 case PointerFromInt:
794 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000795 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000796 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000797 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000798 case IntFromPointer:
799 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000800 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000801 case IncompatiblePointer:
802 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000803 break;
804 case CompatiblePointerDiscardsQualifiers:
805 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
806 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000807 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000808 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000809}
810
Steve Naroff218bc2b2007-05-04 21:54:46 +0000811inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroffae4143e2007-04-26 20:39:23 +0000812 Expr *lex, Expr *rex, SourceLocation loc)
813{
Steve Naroff218bc2b2007-05-04 21:54:46 +0000814 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000815}
816
Steve Naroff35d85152007-05-07 00:24:15 +0000817QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000818 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000819 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000820
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000821 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000822 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
823 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000824 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
825 resType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000826 return QualType();
827 }
828 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000829 // FIXME: Allow Complex as a GCC extension.
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000830 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
831 resType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000832 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000833 }
Steve Naroff094046f2007-05-13 03:21:25 +0000834 // At this point, we know we have a real or pointer type. Now make sure
835 // the operand is a modifiable lvalue.
836 if (!op->isModifiableLvalue()) {
837 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr);
Steve Naroff35d85152007-05-07 00:24:15 +0000838 return QualType();
839 }
840 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000841}
842
Steve Naroff1926c832007-04-24 00:23:05 +0000843/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000844/// This routine allows us to typecheck complex/recursive expressions
845/// where the declaration is needed for type checking. Here are some
846/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000847static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000848 switch (e->getStmtClass()) {
849 case Stmt::DeclRefExprClass:
850 return cast<DeclRefExpr>(e)->getDecl();
851 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000852 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000853 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000854 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000855 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000856 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000857 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000858 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000859 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000860 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000861 default:
862 return 0;
863 }
864}
865
866/// CheckAddressOfOperand - The operand of & must be either a function
867/// designator or an lvalue designating an object. If it is an lvalue, the
868/// object cannot be declared with storage class register or be a bit field.
869/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000870/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000871QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000872 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000873
Steve Naroff5dd642e2007-05-14 18:14:51 +0000874 if (!op->isLvalue()) { // C99 6.5.3.2p1
875 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
876 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000877 else {
878 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
879 return QualType();
880 }
Steve Naroff47500512007-04-19 23:00:49 +0000881 } else if (dcl) {
882 // We have an lvalue with a decl. Make sure the decl is not declared
883 // with the register storage-class specifier.
884 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000885 if (vd->getStorageClass() == VarDecl::Register) {
886 Diag(OpLoc, diag::err_typecheck_address_of_register);
887 return QualType();
888 }
Steve Narofff633d092007-04-25 19:01:39 +0000889 } else
890 assert(0 && "Unknown/unexpected decl type");
891
Steve Naroff47500512007-04-19 23:00:49 +0000892 // FIXME: add check for bitfields!
893 }
894 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000895 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000896}
897
Steve Naroff35d85152007-05-07 00:24:15 +0000898QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
899 QualType qType = UsualUnaryConversion(op->getType());
900
Steve Naroff47500512007-04-19 23:00:49 +0000901 assert(!qType.isNull() && "no type for * expression");
902
Steve Naroff35d85152007-05-07 00:24:15 +0000903 if (PointerType *PT = dyn_cast<PointerType>(qType))
904 return PT->getPointeeType();
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000905 Diag(OpLoc, diag::err_typecheck_unary_expr, qType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000906 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000907}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000908
909static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
910 tok::TokenKind Kind) {
911 BinaryOperator::Opcode Opc;
912 switch (Kind) {
913 default: assert(0 && "Unknown binop!");
914 case tok::star: Opc = BinaryOperator::Mul; break;
915 case tok::slash: Opc = BinaryOperator::Div; break;
916 case tok::percent: Opc = BinaryOperator::Rem; break;
917 case tok::plus: Opc = BinaryOperator::Add; break;
918 case tok::minus: Opc = BinaryOperator::Sub; break;
919 case tok::lessless: Opc = BinaryOperator::Shl; break;
920 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
921 case tok::lessequal: Opc = BinaryOperator::LE; break;
922 case tok::less: Opc = BinaryOperator::LT; break;
923 case tok::greaterequal: Opc = BinaryOperator::GE; break;
924 case tok::greater: Opc = BinaryOperator::GT; break;
925 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
926 case tok::equalequal: Opc = BinaryOperator::EQ; break;
927 case tok::amp: Opc = BinaryOperator::And; break;
928 case tok::caret: Opc = BinaryOperator::Xor; break;
929 case tok::pipe: Opc = BinaryOperator::Or; break;
930 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
931 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
932 case tok::equal: Opc = BinaryOperator::Assign; break;
933 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
934 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
935 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
936 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
937 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
938 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
939 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
940 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
941 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
942 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
943 case tok::comma: Opc = BinaryOperator::Comma; break;
944 }
945 return Opc;
946}
947
Steve Naroff35d85152007-05-07 00:24:15 +0000948static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
949 tok::TokenKind Kind) {
950 UnaryOperator::Opcode Opc;
951 switch (Kind) {
952 default: assert(0 && "Unknown unary op!");
953 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
954 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
955 case tok::amp: Opc = UnaryOperator::AddrOf; break;
956 case tok::star: Opc = UnaryOperator::Deref; break;
957 case tok::plus: Opc = UnaryOperator::Plus; break;
958 case tok::minus: Opc = UnaryOperator::Minus; break;
959 case tok::tilde: Opc = UnaryOperator::Not; break;
960 case tok::exclaim: Opc = UnaryOperator::LNot; break;
961 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
962 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
963 case tok::kw___real: Opc = UnaryOperator::Real; break;
964 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
965 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
966 // FIXME: case tok::kw___extension__:
967 }
968 return Opc;
969}
970
Steve Naroff218bc2b2007-05-04 21:54:46 +0000971// Binary Operators. 'Tok' is the token for the operator.
972Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
973 ExprTy *LHS, ExprTy *RHS) {
974 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
975 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
976
977 assert((lhs != 0) && "ParseBinOp(): missing left expression");
978 assert((rhs != 0) && "ParseBinOp(): missing right expression");
979
980 QualType result;
981
982 switch (Opc) {
983 default:
984 assert(0 && "Unknown binary expr!");
985 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +0000986 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000987 break;
988 case BinaryOperator::Mul:
989 case BinaryOperator::Div:
990 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
991 break;
992 case BinaryOperator::Rem:
993 result = CheckRemainderOperands(lhs, rhs, TokLoc);
994 break;
995 case BinaryOperator::Add:
996 result = CheckAdditionOperands(lhs, rhs, TokLoc);
997 break;
998 case BinaryOperator::Sub:
999 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1000 break;
1001 case BinaryOperator::Shl:
1002 case BinaryOperator::Shr:
1003 result = CheckShiftOperands(lhs, rhs, TokLoc);
1004 break;
1005 case BinaryOperator::LE:
1006 case BinaryOperator::LT:
1007 case BinaryOperator::GE:
1008 case BinaryOperator::GT:
1009 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1010 break;
1011 case BinaryOperator::EQ:
1012 case BinaryOperator::NE:
1013 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1014 break;
1015 case BinaryOperator::And:
1016 case BinaryOperator::Xor:
1017 case BinaryOperator::Or:
1018 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1019 break;
1020 case BinaryOperator::LAnd:
1021 case BinaryOperator::LOr:
1022 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1023 break;
1024 case BinaryOperator::MulAssign:
1025 case BinaryOperator::DivAssign:
1026 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1027 if (result.isNull())
1028 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001029 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001030 break;
1031 case BinaryOperator::RemAssign:
1032 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1033 if (result.isNull())
1034 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001035 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001036 break;
1037 case BinaryOperator::AddAssign:
1038 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1039 if (result.isNull())
1040 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001041 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001042 break;
1043 case BinaryOperator::SubAssign:
1044 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1045 if (result.isNull())
1046 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001047 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001048 break;
1049 case BinaryOperator::ShlAssign:
1050 case BinaryOperator::ShrAssign:
1051 result = CheckShiftOperands(lhs, rhs, TokLoc);
1052 if (result.isNull())
1053 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001054 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001055 break;
1056 case BinaryOperator::AndAssign:
1057 case BinaryOperator::XorAssign:
1058 case BinaryOperator::OrAssign:
1059 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1060 if (result.isNull())
1061 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001062 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001063 break;
1064 case BinaryOperator::Comma:
1065 result = CheckCommaOperands(lhs, rhs, TokLoc);
1066 break;
1067 }
1068 if (result.isNull())
1069 return true;
1070 return new BinaryOperator(lhs, rhs, Opc, result);
1071}
1072
Steve Naroff35d85152007-05-07 00:24:15 +00001073// Unary Operators. 'Tok' is the token for the operator.
1074Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1075 ExprTy *Input) {
1076 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1077 QualType resultType;
1078 switch (Opc) {
1079 default:
1080 assert(0 && "Unimplemented unary expr!");
1081 case UnaryOperator::PreInc:
1082 case UnaryOperator::PreDec:
1083 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
1084 break;
1085 case UnaryOperator::AddrOf:
1086 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1087 break;
1088 case UnaryOperator::Deref:
1089 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1090 break;
1091 case UnaryOperator::Plus:
1092 case UnaryOperator::Minus:
1093 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1094 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001095 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1096 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001097 break;
1098 case UnaryOperator::Not: // bitwise complement
1099 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1100 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001101 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1102 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001103 break;
1104 case UnaryOperator::LNot: // logical negation
1105 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1106 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001107 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1108 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001109 break;
1110 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001111 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1112 true);
1113 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001114 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001115 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1116 false);
1117 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001118 }
1119 if (resultType.isNull())
1120 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001121 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001122}