blob: b8175c3e03f61765f621f1a98768063cf64caab2 [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 Naroff509fe022007-05-17 01:16:00 +000059 Literal.AnyWide, t, SourceLocation()/*FIXME*/);
Chris Lattner5b183d82006-11-10 05:03:26 +000060}
61
Chris Lattnere168f762006-11-10 05:29:30 +000062
Chris Lattnerac18be92006-11-20 06:49:47 +000063/// ParseIdentifierExpr - The parser read an identifier in expression context,
64/// validate it per-C99 6.5.1. HasTrailingLParen indicates whether this
65/// identifier is used in an function call context.
66Sema::ExprResult Sema::ParseIdentifierExpr(Scope *S, SourceLocation Loc,
67 IdentifierInfo &II,
68 bool HasTrailingLParen) {
Chris Lattner17ed4872006-11-20 04:58:19 +000069 // Could be enum-constant or decl.
Chris Lattner9561a0b2007-01-28 08:20:04 +000070 Decl *D = LookupScopedDecl(&II, Decl::IDNS_Ordinary, Loc, S);
Chris Lattner17ed4872006-11-20 04:58:19 +000071 if (D == 0) {
Bill Wendling4073ed52007-02-13 01:51:42 +000072 // Otherwise, this could be an implicitly declared function reference (legal
Chris Lattner9561a0b2007-01-28 08:20:04 +000073 // in C90, extension in C99).
Chris Lattnerac18be92006-11-20 06:49:47 +000074 if (HasTrailingLParen &&
75 // Not in C++.
Steve Narofff1e53692007-03-23 22:27:02 +000076 !getLangOptions().CPlusPlus)
Chris Lattnerac18be92006-11-20 06:49:47 +000077 D = ImplicitlyDefineFunction(Loc, II, S);
Steve Naroff92e30f82007-04-02 22:35:25 +000078 else {
Chris Lattnerac18be92006-11-20 06:49:47 +000079 // If this name wasn't predeclared and if this is not a function call,
80 // diagnose the problem.
Steve Narofff1e53692007-03-23 22:27:02 +000081 return Diag(Loc, diag::err_undeclared_var_use, II.getName());
Steve Naroff92e30f82007-04-02 22:35:25 +000082 }
Chris Lattner17ed4872006-11-20 04:58:19 +000083 }
84
Steve Naroff46ba1eb2007-04-03 23:13:13 +000085 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
Steve Naroff509fe022007-05-17 01:16:00 +000086 return new DeclRefExpr(VD, VD->getType(), Loc);
Steve Naroff46ba1eb2007-04-03 23:13:13 +000087 if (isa<TypedefDecl>(D))
Steve Narofff1e53692007-03-23 22:27:02 +000088 return Diag(Loc, diag::err_unexpected_typedef, II.getName());
89
90 assert(0 && "Invalid decl");
Chris Lattner17ed4872006-11-20 04:58:19 +000091}
Chris Lattnere168f762006-11-10 05:29:30 +000092
Chris Lattner17ed4872006-11-20 04:58:19 +000093Sema::ExprResult Sema::ParseSimplePrimaryExpr(SourceLocation Loc,
94 tok::TokenKind Kind) {
Chris Lattnere168f762006-11-10 05:29:30 +000095 switch (Kind) {
96 default:
97 assert(0 && "Unknown simple primary expr!");
Steve Naroffe4718892007-04-27 18:30:00 +000098 // TODO: MOVE this to be some other callback.
Chris Lattnere168f762006-11-10 05:29:30 +000099 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
100 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
101 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner17ed4872006-11-20 04:58:19 +0000102 return 0;
Chris Lattnere168f762006-11-10 05:29:30 +0000103 }
104}
105
Steve Naroffae4143e2007-04-26 20:39:23 +0000106Sema::ExprResult Sema::ParseCharacterConstant(const LexerToken &Tok) {
107 SmallString<16> CharBuffer;
108 CharBuffer.resize(Tok.getLength());
109 const char *ThisTokBegin = &CharBuffer[0];
110 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
111
112 CharLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
113 Tok.getLocation(), PP);
114 if (Literal.hadError())
115 return ExprResult(true);
Steve Naroff509fe022007-05-17 01:16:00 +0000116 return new CharacterLiteral(Literal.getValue(), Context.IntTy,
117 Tok.getLocation());
Steve Naroffae4143e2007-04-26 20:39:23 +0000118}
119
Steve Naroff8160ea22007-03-06 01:09:46 +0000120Action::ExprResult Sema::ParseNumericConstant(const LexerToken &Tok) {
Steve Narofff2fb89e2007-03-13 20:29:44 +0000121 // fast path for a single digit (which is quite common). A single digit
122 // cannot have a trigraph, escaped newline, radix prefix, or type suffix.
123 if (Tok.getLength() == 1) {
124 const char *t = PP.getSourceManager().getCharacterData(Tok.getLocation());
Steve Naroff509fe022007-05-17 01:16:00 +0000125 return ExprResult(new IntegerLiteral(*t-'0', Context.IntTy,
126 Tok.getLocation()));
Steve Narofff2fb89e2007-03-13 20:29:44 +0000127 }
Steve Naroff8160ea22007-03-06 01:09:46 +0000128 SmallString<512> IntegerBuffer;
129 IntegerBuffer.resize(Tok.getLength());
130 const char *ThisTokBegin = &IntegerBuffer[0];
131
132 // Get the spelling of the token, which eliminates trigraphs, etc. Notes:
133 // - We know that ThisTokBuf points to a buffer that is big enough for the
134 // whole token and 'spelled' tokens can only shrink.
135 // - In practice, the local buffer is only used when the spelling doesn't
136 // match the original token (which is rare). The common case simply returns
137 // a pointer to a *constant* buffer (avoiding a copy).
138
139 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin);
Steve Naroff09ef4742007-03-09 23:16:33 +0000140 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
Steve Naroff451d8f162007-03-12 23:22:38 +0000141 Tok.getLocation(), PP);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000142 if (Literal.hadError)
143 return ExprResult(true);
144
Steve Naroff09ef4742007-03-09 23:16:33 +0000145 if (Literal.isIntegerLiteral()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000146 QualType t;
Steve Naroff09ef4742007-03-09 23:16:33 +0000147 if (Literal.hasSuffix()) {
148 if (Literal.isLong)
149 t = Literal.isUnsigned ? Context.UnsignedLongTy : Context.LongTy;
150 else if (Literal.isLongLong)
151 t = Literal.isUnsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
152 else
153 t = Context.UnsignedIntTy;
154 } else {
155 t = Context.IntTy; // implicit type is "int"
156 }
Steve Naroff451d8f162007-03-12 23:22:38 +0000157 uintmax_t val;
158 if (Literal.GetIntegerValue(val)) {
Steve Naroff509fe022007-05-17 01:16:00 +0000159 return new IntegerLiteral(val, t, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000160 }
161 } else if (Literal.isFloatingLiteral()) {
Steve Narofff1e53692007-03-23 22:27:02 +0000162 // FIXME: fill in the value and compute the real type...
Steve Naroff509fe022007-05-17 01:16:00 +0000163 return new FloatingLiteral(7.7, Context.FloatTy, Tok.getLocation());
Steve Naroff09ef4742007-03-09 23:16:33 +0000164 }
Steve Narofff2fb89e2007-03-13 20:29:44 +0000165 return ExprResult(true);
Chris Lattnere168f762006-11-10 05:29:30 +0000166}
167
168Action::ExprResult Sema::ParseParenExpr(SourceLocation L, SourceLocation R,
169 ExprTy *Val) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000170 Expr *e = (Expr *)Val;
171 assert((e != 0) && "ParseParenExpr() missing expr");
172 return e;
Chris Lattnere168f762006-11-10 05:29:30 +0000173}
174
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000175/// The UsualUnaryConversion() function is *not* called by this routine.
176/// See C99 6.3.2.1p[2-4] for more details.
Steve Naroff043d45d2007-05-15 02:32:35 +0000177QualType Sema::CheckSizeOfAlignOfOperand(QualType exprType,
178 SourceLocation OpLoc, bool isSizeof) {
179 // C99 6.5.3.4p1:
180 if (isa<FunctionType>(exprType) && isSizeof)
181 // alignof(function) is allowed.
182 Diag(OpLoc, diag::ext_sizeof_function_type);
183 else if (exprType->isVoidType())
184 Diag(OpLoc, diag::ext_sizeof_void_type, isSizeof ? "sizeof" : "__alignof");
185 else if (exprType->isIncompleteType()) {
Steve Naroff043d45d2007-05-15 02:32:35 +0000186 Diag(OpLoc, isSizeof ? diag::err_sizeof_incomplete_type :
Chris Lattner3dc3d772007-05-16 18:07:12 +0000187 diag::err_alignof_incomplete_type,
188 exprType.getAsString());
Steve Naroff043d45d2007-05-15 02:32:35 +0000189 return QualType(); // error
190 }
191 // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t.
192 return Context.getSizeType();
193}
194
Chris Lattnere168f762006-11-10 05:29:30 +0000195Action::ExprResult Sema::
196ParseSizeOfAlignOfTypeExpr(SourceLocation OpLoc, bool isSizeof,
Steve Naroff509fe022007-05-17 01:16:00 +0000197 SourceLocation LPLoc, TypeTy *Ty,
198 SourceLocation RPLoc) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000199 // If error parsing type, ignore.
200 if (Ty == 0) return true;
Chris Lattner6531c102007-01-23 22:29:49 +0000201
202 // Verify that this is a valid expression.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000203 QualType ArgTy = QualType::getFromOpaquePtr(Ty);
Chris Lattner6531c102007-01-23 22:29:49 +0000204
Steve Naroff043d45d2007-05-15 02:32:35 +0000205 QualType resultType = CheckSizeOfAlignOfOperand(ArgTy, OpLoc, isSizeof);
206
207 if (resultType.isNull())
208 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000209 return new SizeOfAlignOfTypeExpr(isSizeof, ArgTy, resultType, OpLoc, RPLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000210}
211
212
213Action::ExprResult Sema::ParsePostfixUnaryOp(SourceLocation OpLoc,
214 tok::TokenKind Kind,
215 ExprTy *Input) {
216 UnaryOperator::Opcode Opc;
217 switch (Kind) {
218 default: assert(0 && "Unknown unary op!");
219 case tok::plusplus: Opc = UnaryOperator::PostInc; break;
220 case tok::minusminus: Opc = UnaryOperator::PostDec; break;
221 }
Steve Naroff35d85152007-05-07 00:24:15 +0000222 QualType result = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
223 if (result.isNull())
224 return true;
Steve Naroff509fe022007-05-17 01:16:00 +0000225 return new UnaryOperator((Expr *)Input, Opc, result, OpLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000226}
227
228Action::ExprResult Sema::
229ParseArraySubscriptExpr(ExprTy *Base, SourceLocation LLoc,
230 ExprTy *Idx, SourceLocation RLoc) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000231 QualType t1 = ((Expr *)Base)->getType();
232 QualType t2 = ((Expr *)Idx)->getType();
Steve Narofff1e53692007-03-23 22:27:02 +0000233
234 assert(!t1.isNull() && "no type for array base expression");
Steve Naroffc1aadb12007-03-28 21:49:40 +0000235 assert(!t2.isNull() && "no type for array index expression");
Steve Narofff1e53692007-03-23 22:27:02 +0000236
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000237 QualType canonT1 = t1.getCanonicalType();
238 QualType canonT2 = t2.getCanonicalType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000239
Steve Naroffc1aadb12007-03-28 21:49:40 +0000240 // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent
241 // to the expression *((e1)+(e2)). This means the array "Base" may actually be
Steve Narofff1e53692007-03-23 22:27:02 +0000242 // in the subscript position. As a result, we need to derive the array base
243 // and index from the expression types.
244
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000245 QualType baseType, indexType;
Steve Naroffd50c88e2007-04-05 21:15:20 +0000246 if (isa<ArrayType>(canonT1) || isa<PointerType>(canonT1)) {
247 baseType = canonT1;
248 indexType = canonT2;
249 } else if (isa<ArrayType>(canonT2) || isa<PointerType>(canonT2)) { // uncommon
250 baseType = canonT2;
251 indexType = canonT1;
Steve Narofff1e53692007-03-23 22:27:02 +0000252 } else
253 return Diag(LLoc, diag::err_typecheck_subscript_value);
254
Steve Naroffc1aadb12007-03-28 21:49:40 +0000255 // C99 6.5.2.1p1
Steve Naroff1926c832007-04-24 00:23:05 +0000256 if (!indexType->isIntegerType())
Steve Narofff1e53692007-03-23 22:27:02 +0000257 return Diag(LLoc, diag::err_typecheck_subscript);
Steve Naroffc1aadb12007-03-28 21:49:40 +0000258
Steve Naroff95af0132007-03-30 23:47:58 +0000259 // FIXME: need to deal with const...
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000260 QualType resultType;
Steve Naroffc1aadb12007-03-28 21:49:40 +0000261 if (ArrayType *ary = dyn_cast<ArrayType>(baseType)) {
262 resultType = ary->getElementType();
263 } else if (PointerType *ary = dyn_cast<PointerType>(baseType)) {
264 resultType = ary->getPointeeType();
265 // in practice, the following check catches trying to index a pointer
266 // to a function (e.g. void (*)(int)). Functions are not objects in c99.
Steve Naroffbc2f0992007-03-30 20:09:34 +0000267 if (!resultType->isObjectType())
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000268 return Diag(LLoc, diag::err_typecheck_subscript_not_object,
269 baseType.getAsString());
Steve Naroffc1aadb12007-03-28 21:49:40 +0000270 }
Steve Naroff509fe022007-05-17 01:16:00 +0000271 return new ArraySubscriptExpr((Expr*)Base, (Expr*)Idx, resultType, RLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000272}
273
274Action::ExprResult Sema::
275ParseMemberReferenceExpr(ExprTy *Base, SourceLocation OpLoc,
276 tok::TokenKind OpKind, SourceLocation MemberLoc,
277 IdentifierInfo &Member) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000278 QualType qualifiedType = ((Expr *)Base)->getType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000279
280 assert(!qualifiedType.isNull() && "no type for member expression");
281
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000282 QualType canonType = qualifiedType.getCanonicalType();
Steve Narofff1e53692007-03-23 22:27:02 +0000283
284 if (OpKind == tok::arrow) {
Steve Naroffca8f7122007-04-01 01:41:35 +0000285 if (PointerType *PT = dyn_cast<PointerType>(canonType)) {
286 qualifiedType = PT->getPointeeType();
Steve Naroffd50c88e2007-04-05 21:15:20 +0000287 canonType = qualifiedType.getCanonicalType();
Steve Naroffca8f7122007-04-01 01:41:35 +0000288 } else
Steve Narofff1e53692007-03-23 22:27:02 +0000289 return Diag(OpLoc, diag::err_typecheck_member_reference_arrow);
290 }
Steve Naroff92e30f82007-04-02 22:35:25 +0000291 if (!isa<RecordType>(canonType))
292 return Diag(OpLoc, diag::err_typecheck_member_reference_structUnion);
293
294 // get the struct/union definition from the type.
295 RecordDecl *RD = cast<RecordType>(canonType)->getDecl();
Steve Naroffcc321422007-03-26 23:09:51 +0000296
Steve Naroff92e30f82007-04-02 22:35:25 +0000297 if (canonType->isIncompleteType())
298 return Diag(OpLoc, diag::err_typecheck_incomplete_tag, RD->getName());
Steve Naroffcc321422007-03-26 23:09:51 +0000299
Steve Naroff92e30f82007-04-02 22:35:25 +0000300 FieldDecl *MemberDecl = RD->getMember(&Member);
301 if (!MemberDecl)
302 return Diag(OpLoc, diag::err_typecheck_no_member, Member.getName());
303
304 return new MemberExpr((Expr*)Base, OpKind == tok::arrow, MemberDecl);
Chris Lattnere168f762006-11-10 05:29:30 +0000305}
306
307/// ParseCallExpr - Handle a call to Fn with the specified array of arguments.
308/// This provides the location of the left/right parens and a list of comma
309/// locations.
310Action::ExprResult Sema::
311ParseCallExpr(ExprTy *Fn, SourceLocation LParenLoc,
Steve Naroffb8c289d2007-05-08 22:18:00 +0000312 ExprTy **Args, unsigned NumArgsInCall,
Chris Lattnere168f762006-11-10 05:29:30 +0000313 SourceLocation *CommaLocs, SourceLocation RParenLoc) {
Steve Naroffae4143e2007-04-26 20:39:23 +0000314 QualType qType = ((Expr *)Fn)->getType();
315
316 assert(!qType.isNull() && "no type for function call expression");
317
Steve Naroff17f76e02007-05-03 21:03:48 +0000318 const FunctionType *funcT = dyn_cast<FunctionType>(qType.getCanonicalType());
Steve Naroffae4143e2007-04-26 20:39:23 +0000319
Steve Naroff17f76e02007-05-03 21:03:48 +0000320 assert(funcT && "ParseCallExpr(): not a function type");
Steve Naroffb8c289d2007-05-08 22:18:00 +0000321
Steve Naroff17f76e02007-05-03 21:03:48 +0000322 // If a prototype isn't declared, the parser implicitly defines a func decl
323 QualType resultType = funcT->getResultType();
324
325 if (const FunctionTypeProto *proto = dyn_cast<FunctionTypeProto>(funcT)) {
326 // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by
327 // assignment, to the types of the corresponding parameter, ...
328
329 unsigned NumArgsInProto = proto->getNumArgs();
Steve Naroffb8c289d2007-05-08 22:18:00 +0000330 unsigned NumArgsToCheck = NumArgsInCall;
Steve Naroff17f76e02007-05-03 21:03:48 +0000331
Steve Naroffb8c289d2007-05-08 22:18:00 +0000332 if (NumArgsInCall < NumArgsInProto)
Steve Naroff1f4d7272007-05-11 04:00:31 +0000333 Diag(LParenLoc, diag::err_typecheck_call_too_few_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000334 else if (NumArgsInCall > NumArgsInProto) {
335 if (!proto->isVariadic())
Steve Naroff1f4d7272007-05-11 04:00:31 +0000336 Diag(LParenLoc, diag::err_typecheck_call_too_many_args);
Steve Naroffb8c289d2007-05-08 22:18:00 +0000337 NumArgsToCheck = NumArgsInProto;
Steve Naroff17f76e02007-05-03 21:03:48 +0000338 }
339 // Continue to check argument types (even if we have too few/many args).
Steve Naroffb8c289d2007-05-08 22:18:00 +0000340 for (unsigned i = 0; i < NumArgsToCheck; i++) {
Steve Naroff17f76e02007-05-03 21:03:48 +0000341 QualType lhsType = proto->getArgType(i);
342 QualType rhsType = ((Expr **)Args)[i]->getType();
343
344 if (lhsType == rhsType) // common case, fast path...
345 continue;
346 AssignmentConversionResult result;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000347 UsualAssignmentConversions(lhsType, rhsType, result);
Steve Naroff17f76e02007-05-03 21:03:48 +0000348
349 SourceLocation l = (i == 0) ? LParenLoc : CommaLocs[i-1];
350
351 // decode the result (notice that AST's are still created for extensions).
352 // FIXME: consider fancier error diagnostics (since this is quite common).
353 // #1: emit the actual prototype arg...requires adding source loc info.
354 // #2: pass Diag the offending argument type...requires hacking Diag.
355 switch (result) {
356 case Compatible:
357 break;
358 case PointerFromInt:
Steve Naroff218bc2b2007-05-04 21:54:46 +0000359 // check for null pointer constant (C99 6.3.2.3p3)
360 if (!((Expr **)Args)[i]->isNullPointerConstant())
361 Diag(l, diag::ext_typecheck_passing_pointer_from_int, utostr(i+1));
Steve Naroff17f76e02007-05-03 21:03:48 +0000362 break;
363 case IntFromPointer:
364 Diag(l, diag::ext_typecheck_passing_int_from_pointer, utostr(i+1));
365 break;
366 case IncompatiblePointer:
367 Diag(l, diag::ext_typecheck_passing_incompatible_pointer, utostr(i+1));
368 break;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000369 case CompatiblePointerDiscardsQualifiers:
370 Diag(l, diag::ext_typecheck_passing_discards_qualifiers, utostr(i+1));
371 break;
Steve Naroff17f76e02007-05-03 21:03:48 +0000372 case Incompatible:
373 return Diag(l, diag::err_typecheck_passing_incompatible, utostr(i+1));
374 }
375 }
Steve Naroffb8c289d2007-05-08 22:18:00 +0000376 // Even if the types checked, bail if we had the wrong number of arguments.
377 if ((NumArgsInCall != NumArgsInProto) && !proto->isVariadic())
378 return true;
Steve Naroffae4143e2007-04-26 20:39:23 +0000379 }
Steve Naroff509fe022007-05-17 01:16:00 +0000380 return new CallExpr((Expr*)Fn, (Expr**)Args, NumArgsInCall, resultType,
381 RParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000382}
383
384Action::ExprResult Sema::
385ParseCastExpr(SourceLocation LParenLoc, TypeTy *Ty,
386 SourceLocation RParenLoc, ExprTy *Op) {
Chris Lattner0d8b1a12006-11-20 04:34:45 +0000387 // If error parsing type, ignore.
Steve Naroffae4143e2007-04-26 20:39:23 +0000388 assert((Ty != 0) && "ParseCastExpr(): missing type");
Steve Naroff509fe022007-05-17 01:16:00 +0000389 return new CastExpr(QualType::getFromOpaquePtr(Ty), (Expr*)Op, LParenLoc);
Chris Lattnere168f762006-11-10 05:29:30 +0000390}
391
Steve Narofff8a28c52007-05-15 20:29:32 +0000392inline QualType Sema::CheckConditionalOperands( // C99 6.5.15
393 Expr *Cond, Expr *LHS, Expr *RHS, SourceLocation questionLoc) {
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000394 QualType cond = Cond->getType();
395 QualType lhs = LHS->getType();
396 QualType rhs = RHS->getType();
397
Steve Narofff8a28c52007-05-15 20:29:32 +0000398 assert(!cond.isNull() && "ParseConditionalOp(): no conditional type");
399 assert(!lhs.isNull() && "ParseConditionalOp(): no lhs type");
400 assert(!rhs.isNull() && "ParseConditionalOp(): no rhs type");
401
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000402 cond = UsualUnaryConversion(cond);
403 lhs = UsualUnaryConversion(lhs);
404 rhs = UsualUnaryConversion(rhs);
405
406 // first, check the condition.
407 if (!cond->isScalarType()) { // C99 6.5.15p2
Steve Naroff509fe022007-05-17 01:16:00 +0000408 Diag(Cond->getSourceLocation(), diag::err_typecheck_cond_expect_scalar,
409 cond.getAsString());
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000410 return QualType();
411 }
412 // now check the two expressions.
413 if (lhs->isArithmeticType() && rhs->isArithmeticType()) // C99 6.5.15p3,5
414 return UsualArithmeticConversions(lhs, rhs);
415
416 if ((lhs->isStructureType() && rhs->isStructureType()) || // C99 6.5.15p3
417 (lhs->isUnionType() && rhs->isUnionType())) {
418 TagType *lTag = cast<TagType>(lhs.getCanonicalType());
419 TagType *rTag = cast<TagType>(rhs.getCanonicalType());
420
421 if (lTag->getDecl()->getIdentifier() == rTag->getDecl()->getIdentifier())
422 return lhs;
423 else {
424 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
425 lhs.getAsString(), rhs.getAsString());
426 return QualType();
427 }
428 }
429 if (lhs->isPointerType() && rhs->isPointerType()) { // C99 6.5.15p3,6
430 QualType lhptee, rhptee;
431
432 // get the "pointed to" type
433 lhptee = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
434 rhptee = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
435
436 // ignore qualifiers on void (C99 6.5.15p3, clause 6)
437 if (lhptee.getUnqualifiedType()->isVoidType() &&
438 (rhptee->isObjectType() || rhptee->isIncompleteType()))
439 return lhs;
440 if (rhptee.getUnqualifiedType()->isVoidType() &&
441 (lhptee->isObjectType() || lhptee->isIncompleteType()))
442 return rhs;
443
444 // FIXME: C99 6.5.15p6: If both operands are pointers to compatible types
445 // *or* to differently qualified versions of compatible types, the result
446 // type is a pointer to an appropriately qualified version of the
447 // *composite* type.
448 if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
449 rhptee.getUnqualifiedType())) {
450 Diag(questionLoc, diag::ext_typecheck_cond_incompatible_pointers,
451 lhs.getAsString(), rhs.getAsString());
452 return lhs; // FIXME: this is an _ext - is this return o.k?
453 }
454 }
455 if (lhs->isVoidType() && rhs->isVoidType()) // C99 6.5.15p3
456 return lhs;
457 if (lhs->isPointerType() && RHS->isNullPointerConstant()) // C99 6.5.15p3
458 return lhs;
459 if (rhs->isPointerType() && LHS->isNullPointerConstant()) // C99 6.5.15p3
460 return rhs;
461
462 Diag(questionLoc, diag::err_typecheck_cond_incompatible_operands,
463 lhs.getAsString(), rhs.getAsString());
464 return QualType();
Steve Narofff8a28c52007-05-15 20:29:32 +0000465}
466
Chris Lattnere168f762006-11-10 05:29:30 +0000467/// ParseConditionalOp - Parse a ?: operation. Note that 'LHS' may be null
468/// in the case of a the GNU conditional expr extension.
469Action::ExprResult Sema::ParseConditionalOp(SourceLocation QuestionLoc,
470 SourceLocation ColonLoc,
471 ExprTy *Cond, ExprTy *LHS,
472 ExprTy *RHS) {
Steve Narofff8a28c52007-05-15 20:29:32 +0000473 QualType result = CheckConditionalOperands((Expr *)Cond, (Expr *)LHS,
474 (Expr *)RHS, QuestionLoc);
475 if (result.isNull())
476 return true;
477 return new ConditionalOperator((Expr*)Cond, (Expr*)LHS, (Expr*)RHS, result);
Chris Lattnere168f762006-11-10 05:29:30 +0000478}
479
Steve Naroff1926c832007-04-24 00:23:05 +0000480/// UsualUnaryConversion - Performs various conversions that are common to most
481/// operators (C99 6.3). The conversions of array and function types are
482/// sometimes surpressed. For example, the array->pointer conversion doesn't
483/// apply if the array is an argument to the sizeof or address (&) operators.
484/// In these instances, this routine should *not* be called.
485QualType Sema::UsualUnaryConversion(QualType t) {
486 assert(!t.isNull() && "UsualUnaryConversion - missing type");
Steve Naroff4b7ce032007-04-20 22:26:17 +0000487
Steve Naroff1926c832007-04-24 00:23:05 +0000488 if (t->isPromotableIntegerType()) // C99 6.3.1.1p2
489 return Context.IntTy;
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000490 if (t->isFunctionType()) // C99 6.3.2.1p4
491 return Context.getPointerType(t.getCanonicalType());
492 if (const ArrayType *ary = dyn_cast<ArrayType>(t.getCanonicalType()))
493 return Context.getPointerType(ary->getElementType()); // C99 6.3.2.1p3
Steve Naroff1926c832007-04-24 00:23:05 +0000494 return t;
495}
496
497/// UsualArithmeticConversions - Performs various conversions that are common to
498/// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this
499/// routine returns the first non-arithmetic type found. The client is
500/// responsible for emitting appropriate error diagnostics.
501QualType Sema::UsualArithmeticConversions(QualType t1, QualType t2) {
Steve Naroffb01bbe32007-04-25 01:22:31 +0000502 QualType lhs = UsualUnaryConversion(t1);
503 QualType rhs = UsualUnaryConversion(t2);
Steve Naroff1926c832007-04-24 00:23:05 +0000504
505 // if either operand is not of arithmetic type, no conversion is possible.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000506 if (!lhs->isArithmeticType())
507 return lhs;
Steve Narofff633d092007-04-25 19:01:39 +0000508 if (!rhs->isArithmeticType())
Steve Naroffb01bbe32007-04-25 01:22:31 +0000509 return rhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000510
Steve Narofff633d092007-04-25 19:01:39 +0000511 // if both arithmetic types are identical, no conversion is needed.
Steve Naroffb01bbe32007-04-25 01:22:31 +0000512 if (lhs == rhs)
513 return lhs;
Steve Naroff1926c832007-04-24 00:23:05 +0000514
Steve Naroffb01bbe32007-04-25 01:22:31 +0000515 // at this point, we have two different arithmetic types.
516
517 // Handle complex types first (C99 6.3.1.8p1).
518 if (lhs->isComplexType() || rhs->isComplexType()) {
519 // if we have an integer operand, the result is the complex type.
520 if (rhs->isIntegerType())
521 return lhs;
522 if (lhs->isIntegerType())
523 return rhs;
524
Steve Naroffe4718892007-04-27 18:30:00 +0000525 return Context.maxComplexType(lhs, rhs);
Steve Naroffbf223ba2007-04-24 20:56:26 +0000526 }
Steve Naroffb01bbe32007-04-25 01:22:31 +0000527 // Now handle "real" floating types (i.e. float, double, long double).
528 if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) {
529 // if we have an integer operand, the result is the real floating type.
530 if (rhs->isIntegerType())
531 return lhs;
532 if (lhs->isIntegerType())
533 return rhs;
534
535 // we have two real floating types, float/complex combos were handled above.
Steve Naroffe4718892007-04-27 18:30:00 +0000536 return Context.maxFloatingType(lhs, rhs);
Steve Naroffb01bbe32007-04-25 01:22:31 +0000537 }
Steve Naroffe4718892007-04-27 18:30:00 +0000538 return Context.maxIntegerType(lhs, rhs);
Steve Narofff1e53692007-03-23 22:27:02 +0000539}
540
Steve Naroff3f597292007-05-11 22:18:03 +0000541// CheckPointerTypesForAssignment - This is a very tricky routine (despite
542// being closely modeled after the C99 spec:-). The odd characteristic of this
543// routine is it effectively iqnores the qualifiers on the top level pointee.
544// This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3].
545// FIXME: add a couple examples in this comment.
546QualType Sema::CheckPointerTypesForAssignment(QualType lhsType,
547 QualType rhsType,
548 AssignmentConversionResult &r) {
549 QualType lhptee, rhptee;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000550
551 // get the "pointed to" type (ignoring qualifiers at the top level)
Steve Naroff3f597292007-05-11 22:18:03 +0000552 lhptee = cast<PointerType>(lhsType.getCanonicalType())->getPointeeType();
553 rhptee = cast<PointerType>(rhsType.getCanonicalType())->getPointeeType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000554
555 // make sure we operate on the canonical type
Steve Naroff3f597292007-05-11 22:18:03 +0000556 lhptee = lhptee.getCanonicalType();
557 rhptee = rhptee.getCanonicalType();
Steve Naroff1f4d7272007-05-11 04:00:31 +0000558
Steve Naroff3f597292007-05-11 22:18:03 +0000559 // C99 6.5.16.1p1: This following citation is common to constraints
560 // 3 & 4 (below). ...and the type *pointed to* by the left has all the
561 // qualifiers of the type *pointed to* by the right;
562 if ((lhptee.getQualifiers() & rhptee.getQualifiers()) !=
563 rhptee.getQualifiers())
564 r = CompatiblePointerDiscardsQualifiers;
565
566 // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or
567 // incomplete type and the other is a pointer to a qualified or unqualified
568 // version of void...
569 if (lhptee.getUnqualifiedType()->isVoidType() &&
570 (rhptee->isObjectType() || rhptee->isIncompleteType()))
571 ;
572 else if (rhptee.getUnqualifiedType()->isVoidType() &&
573 (lhptee->isObjectType() || lhptee->isIncompleteType()))
574 ;
575 // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or
576 // unqualified versions of compatible types, ...
577 else if (!Type::typesAreCompatible(lhptee.getUnqualifiedType(),
578 rhptee.getUnqualifiedType()))
579 r = IncompatiblePointer; // this "trumps" PointerAssignDiscardsQualifiers
580 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000581}
582
Steve Naroff17f76e02007-05-03 21:03:48 +0000583/// UsualAssignmentConversions (C99 6.5.16) - This routine currently
584/// has code to accommodate several GCC extensions when type checking
585/// pointers. Here are some objectionable examples that GCC considers warnings:
586///
587/// int a, *pint;
588/// short *pshort;
589/// struct foo *pfoo;
590///
591/// pint = pshort; // warning: assignment from incompatible pointer type
592/// a = pint; // warning: assignment makes integer from pointer without a cast
593/// pint = a; // warning: assignment makes pointer from integer without a cast
594/// pint = pfoo; // warning: assignment from incompatible pointer type
595///
596/// As a result, the code for dealing with pointers is more complex than the
597/// C99 spec dictates.
598/// Note: the warning above turn into errors when -pedantic-errors is enabled.
599///
Steve Naroff218bc2b2007-05-04 21:54:46 +0000600QualType Sema::UsualAssignmentConversions(QualType lhsType, QualType rhsType,
Steve Naroff17f76e02007-05-03 21:03:48 +0000601 AssignmentConversionResult &r) {
Steve Naroffb891de32007-05-02 23:51:10 +0000602 // this check seems unnatural, however it necessary to insure the proper
603 // conversion of functions/arrays. If the conversion where done for all
604 // DeclExpr's (created by ParseIdentifierExpr), it would mess up the
605 // unary expressions that surpress this implicit conversion (&, sizeof).
606 if (rhsType->isFunctionType() || rhsType->isArrayType())
607 rhsType = UsualUnaryConversion(rhsType);
608
Steve Naroff17f76e02007-05-03 21:03:48 +0000609 r = Compatible;
Steve Naroff9eb24652007-05-02 21:58:15 +0000610 if (lhsType->isArithmeticType() && rhsType->isArithmeticType())
611 return lhsType;
612 else if (lhsType->isPointerType()) {
613 if (rhsType->isIntegerType()) {
Steve Naroff218bc2b2007-05-04 21:54:46 +0000614 r = PointerFromInt;
Steve Naroffb891de32007-05-02 23:51:10 +0000615 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000616 }
Steve Naroff3f597292007-05-11 22:18:03 +0000617 if (rhsType->isPointerType())
618 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff9eb24652007-05-02 21:58:15 +0000619 } else if (rhsType->isPointerType()) {
620 if (lhsType->isIntegerType()) {
621 // C99 6.5.16.1p1: the left operand is _Bool and the right is a pointer.
622 if (lhsType != Context.BoolTy)
Steve Naroff17f76e02007-05-03 21:03:48 +0000623 r = IntFromPointer;
Steve Naroffb891de32007-05-02 23:51:10 +0000624 return rhsType;
Steve Naroff9eb24652007-05-02 21:58:15 +0000625 }
Steve Naroff3f597292007-05-11 22:18:03 +0000626 if (lhsType->isPointerType())
627 return CheckPointerTypesForAssignment(lhsType, rhsType, r);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000628 } else if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) {
629 if (Type::tagTypesAreCompatible(lhsType, rhsType))
Steve Naroffb891de32007-05-02 23:51:10 +0000630 return rhsType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000631 }
Steve Naroff17f76e02007-05-03 21:03:48 +0000632 r = Incompatible;
633 return QualType();
Steve Naroff9eb24652007-05-02 21:58:15 +0000634}
635
Steve Naroff218bc2b2007-05-04 21:54:46 +0000636inline QualType Sema::CheckMultiplyDivideOperands(
637 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000638{
Steve Naroff1926c832007-04-24 00:23:05 +0000639 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
Steve Naroff5c10d4b2007-04-20 23:42:24 +0000640
Steve Naroff218bc2b2007-05-04 21:54:46 +0000641 if (resType->isArithmeticType())
642 return resType;
643 Diag(loc, diag::err_typecheck_invalid_operands);
644 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000645}
646
Steve Naroff218bc2b2007-05-04 21:54:46 +0000647inline QualType Sema::CheckRemainderOperands(
648 Expr *lex, Expr *rex, SourceLocation loc)
649{
650 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
651
652 if (resType->isIntegerType())
653 return resType;
654 Diag(loc, diag::err_typecheck_invalid_operands);
655 return QualType();
656}
657
658inline QualType Sema::CheckAdditionOperands( // C99 6.5.6
659 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000660{
Steve Naroffe4718892007-04-27 18:30:00 +0000661 QualType lhsType = lex->getType(), rhsType = rex->getType();
662 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
663
664 // handle the common case first (both operands are arithmetic).
665 if (resType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000666 return resType;
667
668 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
669 (lhsType->isIntegerType() && rhsType->isPointerType()))
670 return resType;
671 Diag(loc, diag::err_typecheck_invalid_operands);
672 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000673}
674
Steve Naroff218bc2b2007-05-04 21:54:46 +0000675inline QualType Sema::CheckSubtractionOperands( // C99 6.5.6
676 Expr *lex, Expr *rex, SourceLocation loc)
677{
678 QualType lhsType = lex->getType(), rhsType = rex->getType();
679 QualType resType = UsualArithmeticConversions(lhsType, rhsType);
680
681 // handle the common case first (both operands are arithmetic).
682 if (resType->isArithmeticType())
683 return resType;
684 if ((lhsType->isPointerType() && rhsType->isIntegerType()) ||
685 (lhsType->isPointerType() && rhsType->isPointerType()))
686 return resType;
687 Diag(loc, diag::err_typecheck_invalid_operands);
688 return QualType();
689}
690
691inline QualType Sema::CheckShiftOperands( // C99 6.5.7
692 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000693{
694 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
695
Steve Naroff218bc2b2007-05-04 21:54:46 +0000696 if (resType->isIntegerType())
697 return resType;
698 Diag(loc, diag::err_typecheck_invalid_operands);
699 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000700}
701
Steve Naroff218bc2b2007-05-04 21:54:46 +0000702inline QualType Sema::CheckRelationalOperands( // C99 6.5.8
703 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000704{
705 QualType lType = lex->getType(), rType = rex->getType();
706
707 if (lType->isRealType() && rType->isRealType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000708 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000709
710 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000711 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000712
Steve Naroff043d45d2007-05-15 02:32:35 +0000713 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000714 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000715 return Context.IntTy;
716 }
717 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000718 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000719}
720
Steve Naroff218bc2b2007-05-04 21:54:46 +0000721inline QualType Sema::CheckEqualityOperands( // C99 6.5.9
722 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000723{
724 QualType lType = lex->getType(), rType = rex->getType();
725
726 if (lType->isArithmeticType() && rType->isArithmeticType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000727 return Context.IntTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000728 if (lType->isPointerType() && rType->isPointerType())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000729 return Context.IntTy;
730
Steve Naroff043d45d2007-05-15 02:32:35 +0000731 if (lType->isIntegerType() || rType->isIntegerType()) { // GCC extension.
Steve Naroff218bc2b2007-05-04 21:54:46 +0000732 Diag(loc, diag::ext_typecheck_comparison_of_pointer_integer);
Steve Naroff043d45d2007-05-15 02:32:35 +0000733 return Context.IntTy;
734 }
735 Diag(loc, diag::err_typecheck_invalid_operands);
Steve Naroff218bc2b2007-05-04 21:54:46 +0000736 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000737}
738
Steve Naroff218bc2b2007-05-04 21:54:46 +0000739inline QualType Sema::CheckBitwiseOperands(
740 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000741{
742 QualType resType = UsualArithmeticConversions(lex->getType(), rex->getType());
743
Steve Naroff218bc2b2007-05-04 21:54:46 +0000744 if (resType->isIntegerType())
745 return resType;
746 Diag(loc, diag::err_typecheck_invalid_operands);
747 return QualType();
Steve Naroff26c8ea52007-03-21 21:08:52 +0000748}
749
Steve Naroff218bc2b2007-05-04 21:54:46 +0000750inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
751 Expr *lex, Expr *rex, SourceLocation loc)
Steve Naroff1926c832007-04-24 00:23:05 +0000752{
Steve Naroffe4718892007-04-27 18:30:00 +0000753 QualType lhsType = UsualUnaryConversion(lex->getType());
754 QualType rhsType = UsualUnaryConversion(rex->getType());
755
Steve Naroff218bc2b2007-05-04 21:54:46 +0000756 if (lhsType->isScalarType() || rhsType->isScalarType())
757 return Context.IntTy;
758 Diag(loc, diag::err_typecheck_invalid_operands);
759 return QualType();
Steve Naroffae4143e2007-04-26 20:39:23 +0000760}
761
Steve Naroff35d85152007-05-07 00:24:15 +0000762inline QualType Sema::CheckAssignmentOperands( // C99 6.5.16.1
763 Expr *lex, Expr *rex, SourceLocation loc, QualType compoundType)
Steve Naroffae4143e2007-04-26 20:39:23 +0000764{
Steve Naroff0af91202007-04-27 21:51:21 +0000765 QualType lhsType = lex->getType();
Steve Naroff35d85152007-05-07 00:24:15 +0000766 QualType rhsType = compoundType.isNull() ? rex->getType() : compoundType;
Steve Naroff1f4d7272007-05-11 04:00:31 +0000767 bool hadError = false;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000768
769 // this check is done first to give a more precise diagnostic.
Steve Naroff1f4d7272007-05-11 04:00:31 +0000770 // isModifiableLvalue() will also check for "const".
Steve Naroff218bc2b2007-05-04 21:54:46 +0000771 if (lhsType.isConstQualified()) {
772 Diag(loc, diag::err_typecheck_assign_const);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000773 hadError = true;
774 } else if (!lex->isModifiableLvalue()) { // C99 6.5.16p2
775 Diag(loc, diag::err_typecheck_assign_non_lvalue);
776 return QualType(); // no need to continue checking...
Steve Naroff218bc2b2007-05-04 21:54:46 +0000777 }
778 if (lhsType == rhsType) // common case, fast path...
779 return lhsType;
780
781 AssignmentConversionResult result;
782 QualType resType = UsualAssignmentConversions(lhsType, rhsType, result);
783
784 // decode the result (notice that extensions still return a type).
785 switch (result) {
786 case Compatible:
Steve Naroff1f4d7272007-05-11 04:00:31 +0000787 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000788 case Incompatible:
789 Diag(loc, diag::err_typecheck_assign_incompatible);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000790 hadError = true;
791 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000792 case PointerFromInt:
793 // check for null pointer constant (C99 6.3.2.3p3)
Steve Naroff35d85152007-05-07 00:24:15 +0000794 if (compoundType.isNull() && !rex->isNullPointerConstant())
Steve Naroff218bc2b2007-05-04 21:54:46 +0000795 Diag(loc, diag::ext_typecheck_assign_pointer_from_int);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000796 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000797 case IntFromPointer:
798 Diag(loc, diag::ext_typecheck_assign_int_from_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000799 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000800 case IncompatiblePointer:
801 Diag(loc, diag::ext_typecheck_assign_incompatible_pointer);
Steve Naroff1f4d7272007-05-11 04:00:31 +0000802 break;
803 case CompatiblePointerDiscardsQualifiers:
804 Diag(loc, diag::ext_typecheck_assign_discards_qualifiers);
805 break;
Steve Naroff218bc2b2007-05-04 21:54:46 +0000806 }
Steve Naroff1f4d7272007-05-11 04:00:31 +0000807 return hadError ? QualType() : resType;
Steve Naroffae4143e2007-04-26 20:39:23 +0000808}
809
Steve Naroff218bc2b2007-05-04 21:54:46 +0000810inline QualType Sema::CheckCommaOperands( // C99 6.5.17
Steve Naroffae4143e2007-04-26 20:39:23 +0000811 Expr *lex, Expr *rex, SourceLocation loc)
812{
Steve Naroff218bc2b2007-05-04 21:54:46 +0000813 return UsualUnaryConversion(rex->getType());
Steve Naroff95af0132007-03-30 23:47:58 +0000814}
815
Steve Naroff35d85152007-05-07 00:24:15 +0000816QualType Sema::CheckIncrementDecrementOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff094046f2007-05-13 03:21:25 +0000817 QualType resType = UsualArithmeticConversions(op->getType(), Context.IntTy);
Steve Naroff35d85152007-05-07 00:24:15 +0000818 assert(!resType.isNull() && "no type for increment/decrement expression");
Steve Naroffd50c88e2007-04-05 21:15:20 +0000819
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000820 // C99 6.5.2.4p1
Steve Naroff35d85152007-05-07 00:24:15 +0000821 if (const PointerType *pt = dyn_cast<PointerType>(resType)) {
822 if (!pt->getPointeeType()->isObjectType()) { // C99 6.5.2.4p2, 6.5.6p2
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000823 Diag(OpLoc, diag::err_typecheck_arithmetic_incomplete_type,
824 resType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000825 return QualType();
826 }
827 } else if (!resType->isRealType()) {
Steve Naroff95af0132007-03-30 23:47:58 +0000828 // FIXME: Allow Complex as a GCC extension.
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000829 Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement,
830 resType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000831 return QualType();
Steve Naroff46ba1eb2007-04-03 23:13:13 +0000832 }
Steve Naroff094046f2007-05-13 03:21:25 +0000833 // At this point, we know we have a real or pointer type. Now make sure
834 // the operand is a modifiable lvalue.
835 if (!op->isModifiableLvalue()) {
836 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_incr_decr);
Steve Naroff35d85152007-05-07 00:24:15 +0000837 return QualType();
838 }
839 return resType;
Steve Naroff26c8ea52007-03-21 21:08:52 +0000840}
841
Steve Naroff1926c832007-04-24 00:23:05 +0000842/// getPrimaryDeclaration - Helper function for CheckAddressOfOperand().
Steve Naroff47500512007-04-19 23:00:49 +0000843/// This routine allows us to typecheck complex/recursive expressions
844/// where the declaration is needed for type checking. Here are some
845/// examples: &s.xx, &s.zz[1].yy, &(1+2), &(XX), &"123"[2].
Steve Naroff1926c832007-04-24 00:23:05 +0000846static Decl *getPrimaryDeclaration(Expr *e) {
Steve Naroff47500512007-04-19 23:00:49 +0000847 switch (e->getStmtClass()) {
848 case Stmt::DeclRefExprClass:
849 return cast<DeclRefExpr>(e)->getDecl();
850 case Stmt::MemberExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000851 return getPrimaryDeclaration(cast<MemberExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000852 case Stmt::ArraySubscriptExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000853 return getPrimaryDeclaration(cast<ArraySubscriptExpr>(e)->getBase());
Steve Naroff47500512007-04-19 23:00:49 +0000854 case Stmt::CallExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000855 return getPrimaryDeclaration(cast<CallExpr>(e)->getCallee());
Steve Naroff47500512007-04-19 23:00:49 +0000856 case Stmt::UnaryOperatorClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000857 return getPrimaryDeclaration(cast<UnaryOperator>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000858 case Stmt::ParenExprClass:
Steve Naroff1926c832007-04-24 00:23:05 +0000859 return getPrimaryDeclaration(cast<ParenExpr>(e)->getSubExpr());
Steve Naroff47500512007-04-19 23:00:49 +0000860 default:
861 return 0;
862 }
863}
864
865/// CheckAddressOfOperand - The operand of & must be either a function
866/// designator or an lvalue designating an object. If it is an lvalue, the
867/// object cannot be declared with storage class register or be a bit field.
868/// Note: The usual conversions are *not* applied to the operand of the &
Steve Naroffa78fe7e2007-05-16 19:47:19 +0000869/// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue.
Steve Naroff35d85152007-05-07 00:24:15 +0000870QualType Sema::CheckAddressOfOperand(Expr *op, SourceLocation OpLoc) {
Steve Naroff1926c832007-04-24 00:23:05 +0000871 Decl *dcl = getPrimaryDeclaration(op);
Steve Naroff47500512007-04-19 23:00:49 +0000872
Steve Naroff5dd642e2007-05-14 18:14:51 +0000873 if (!op->isLvalue()) { // C99 6.5.3.2p1
874 if (dcl && isa<FunctionDecl>(dcl)) // allow function designators
875 ;
Steve Naroff35d85152007-05-07 00:24:15 +0000876 else {
877 Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof);
878 return QualType();
879 }
Steve Naroff47500512007-04-19 23:00:49 +0000880 } else if (dcl) {
881 // We have an lvalue with a decl. Make sure the decl is not declared
882 // with the register storage-class specifier.
883 if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) {
Steve Naroff35d85152007-05-07 00:24:15 +0000884 if (vd->getStorageClass() == VarDecl::Register) {
885 Diag(OpLoc, diag::err_typecheck_address_of_register);
886 return QualType();
887 }
Steve Narofff633d092007-04-25 19:01:39 +0000888 } else
889 assert(0 && "Unknown/unexpected decl type");
890
Steve Naroff47500512007-04-19 23:00:49 +0000891 // FIXME: add check for bitfields!
892 }
893 // If the operand has type "type", the result has type "pointer to type".
Steve Naroff35d85152007-05-07 00:24:15 +0000894 return Context.getPointerType(op->getType());
Steve Naroff47500512007-04-19 23:00:49 +0000895}
896
Steve Naroff35d85152007-05-07 00:24:15 +0000897QualType Sema::CheckIndirectionOperand(Expr *op, SourceLocation OpLoc) {
898 QualType qType = UsualUnaryConversion(op->getType());
899
Steve Naroff47500512007-04-19 23:00:49 +0000900 assert(!qType.isNull() && "no type for * expression");
901
Steve Naroff35d85152007-05-07 00:24:15 +0000902 if (PointerType *PT = dyn_cast<PointerType>(qType))
903 return PT->getPointeeType();
Chris Lattnerc04bd6a2007-05-16 18:09:54 +0000904 Diag(OpLoc, diag::err_typecheck_unary_expr, qType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +0000905 return QualType();
Steve Naroff1926c832007-04-24 00:23:05 +0000906}
Steve Naroff218bc2b2007-05-04 21:54:46 +0000907
908static inline BinaryOperator::Opcode ConvertTokenKindToBinaryOpcode(
909 tok::TokenKind Kind) {
910 BinaryOperator::Opcode Opc;
911 switch (Kind) {
912 default: assert(0 && "Unknown binop!");
913 case tok::star: Opc = BinaryOperator::Mul; break;
914 case tok::slash: Opc = BinaryOperator::Div; break;
915 case tok::percent: Opc = BinaryOperator::Rem; break;
916 case tok::plus: Opc = BinaryOperator::Add; break;
917 case tok::minus: Opc = BinaryOperator::Sub; break;
918 case tok::lessless: Opc = BinaryOperator::Shl; break;
919 case tok::greatergreater: Opc = BinaryOperator::Shr; break;
920 case tok::lessequal: Opc = BinaryOperator::LE; break;
921 case tok::less: Opc = BinaryOperator::LT; break;
922 case tok::greaterequal: Opc = BinaryOperator::GE; break;
923 case tok::greater: Opc = BinaryOperator::GT; break;
924 case tok::exclaimequal: Opc = BinaryOperator::NE; break;
925 case tok::equalequal: Opc = BinaryOperator::EQ; break;
926 case tok::amp: Opc = BinaryOperator::And; break;
927 case tok::caret: Opc = BinaryOperator::Xor; break;
928 case tok::pipe: Opc = BinaryOperator::Or; break;
929 case tok::ampamp: Opc = BinaryOperator::LAnd; break;
930 case tok::pipepipe: Opc = BinaryOperator::LOr; break;
931 case tok::equal: Opc = BinaryOperator::Assign; break;
932 case tok::starequal: Opc = BinaryOperator::MulAssign; break;
933 case tok::slashequal: Opc = BinaryOperator::DivAssign; break;
934 case tok::percentequal: Opc = BinaryOperator::RemAssign; break;
935 case tok::plusequal: Opc = BinaryOperator::AddAssign; break;
936 case tok::minusequal: Opc = BinaryOperator::SubAssign; break;
937 case tok::lesslessequal: Opc = BinaryOperator::ShlAssign; break;
938 case tok::greatergreaterequal: Opc = BinaryOperator::ShrAssign; break;
939 case tok::ampequal: Opc = BinaryOperator::AndAssign; break;
940 case tok::caretequal: Opc = BinaryOperator::XorAssign; break;
941 case tok::pipeequal: Opc = BinaryOperator::OrAssign; break;
942 case tok::comma: Opc = BinaryOperator::Comma; break;
943 }
944 return Opc;
945}
946
Steve Naroff35d85152007-05-07 00:24:15 +0000947static inline UnaryOperator::Opcode ConvertTokenKindToUnaryOpcode(
948 tok::TokenKind Kind) {
949 UnaryOperator::Opcode Opc;
950 switch (Kind) {
951 default: assert(0 && "Unknown unary op!");
952 case tok::plusplus: Opc = UnaryOperator::PreInc; break;
953 case tok::minusminus: Opc = UnaryOperator::PreDec; break;
954 case tok::amp: Opc = UnaryOperator::AddrOf; break;
955 case tok::star: Opc = UnaryOperator::Deref; break;
956 case tok::plus: Opc = UnaryOperator::Plus; break;
957 case tok::minus: Opc = UnaryOperator::Minus; break;
958 case tok::tilde: Opc = UnaryOperator::Not; break;
959 case tok::exclaim: Opc = UnaryOperator::LNot; break;
960 case tok::kw_sizeof: Opc = UnaryOperator::SizeOf; break;
961 case tok::kw___alignof: Opc = UnaryOperator::AlignOf; break;
962 case tok::kw___real: Opc = UnaryOperator::Real; break;
963 case tok::kw___imag: Opc = UnaryOperator::Imag; break;
964 case tok::ampamp: Opc = UnaryOperator::AddrLabel; break;
965 // FIXME: case tok::kw___extension__:
966 }
967 return Opc;
968}
969
Steve Naroff218bc2b2007-05-04 21:54:46 +0000970// Binary Operators. 'Tok' is the token for the operator.
971Action::ExprResult Sema::ParseBinOp(SourceLocation TokLoc, tok::TokenKind Kind,
972 ExprTy *LHS, ExprTy *RHS) {
973 BinaryOperator::Opcode Opc = ConvertTokenKindToBinaryOpcode(Kind);
974 Expr *lhs = (Expr *)LHS, *rhs = (Expr*)RHS;
975
976 assert((lhs != 0) && "ParseBinOp(): missing left expression");
977 assert((rhs != 0) && "ParseBinOp(): missing right expression");
978
979 QualType result;
980
981 switch (Opc) {
982 default:
983 assert(0 && "Unknown binary expr!");
984 case BinaryOperator::Assign:
Steve Naroff35d85152007-05-07 00:24:15 +0000985 result = CheckAssignmentOperands(lhs, rhs, TokLoc, QualType());
Steve Naroff218bc2b2007-05-04 21:54:46 +0000986 break;
987 case BinaryOperator::Mul:
988 case BinaryOperator::Div:
989 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
990 break;
991 case BinaryOperator::Rem:
992 result = CheckRemainderOperands(lhs, rhs, TokLoc);
993 break;
994 case BinaryOperator::Add:
995 result = CheckAdditionOperands(lhs, rhs, TokLoc);
996 break;
997 case BinaryOperator::Sub:
998 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
999 break;
1000 case BinaryOperator::Shl:
1001 case BinaryOperator::Shr:
1002 result = CheckShiftOperands(lhs, rhs, TokLoc);
1003 break;
1004 case BinaryOperator::LE:
1005 case BinaryOperator::LT:
1006 case BinaryOperator::GE:
1007 case BinaryOperator::GT:
1008 result = CheckRelationalOperands(lhs, rhs, TokLoc);
1009 break;
1010 case BinaryOperator::EQ:
1011 case BinaryOperator::NE:
1012 result = CheckEqualityOperands(lhs, rhs, TokLoc);
1013 break;
1014 case BinaryOperator::And:
1015 case BinaryOperator::Xor:
1016 case BinaryOperator::Or:
1017 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1018 break;
1019 case BinaryOperator::LAnd:
1020 case BinaryOperator::LOr:
1021 result = CheckLogicalOperands(lhs, rhs, TokLoc);
1022 break;
1023 case BinaryOperator::MulAssign:
1024 case BinaryOperator::DivAssign:
1025 result = CheckMultiplyDivideOperands(lhs, rhs, TokLoc);
1026 if (result.isNull())
1027 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001028 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001029 break;
1030 case BinaryOperator::RemAssign:
1031 result = CheckRemainderOperands(lhs, rhs, TokLoc);
1032 if (result.isNull())
1033 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001034 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001035 break;
1036 case BinaryOperator::AddAssign:
1037 result = CheckAdditionOperands(lhs, rhs, TokLoc);
1038 if (result.isNull())
1039 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001040 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001041 break;
1042 case BinaryOperator::SubAssign:
1043 result = CheckSubtractionOperands(lhs, rhs, TokLoc);
1044 if (result.isNull())
1045 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001046 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001047 break;
1048 case BinaryOperator::ShlAssign:
1049 case BinaryOperator::ShrAssign:
1050 result = CheckShiftOperands(lhs, rhs, TokLoc);
1051 if (result.isNull())
1052 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001053 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001054 break;
1055 case BinaryOperator::AndAssign:
1056 case BinaryOperator::XorAssign:
1057 case BinaryOperator::OrAssign:
1058 result = CheckBitwiseOperands(lhs, rhs, TokLoc);
1059 if (result.isNull())
1060 return true;
Steve Naroff35d85152007-05-07 00:24:15 +00001061 result = CheckAssignmentOperands(lhs, rhs, TokLoc, result);
Steve Naroff218bc2b2007-05-04 21:54:46 +00001062 break;
1063 case BinaryOperator::Comma:
1064 result = CheckCommaOperands(lhs, rhs, TokLoc);
1065 break;
1066 }
1067 if (result.isNull())
1068 return true;
1069 return new BinaryOperator(lhs, rhs, Opc, result);
1070}
1071
Steve Naroff35d85152007-05-07 00:24:15 +00001072// Unary Operators. 'Tok' is the token for the operator.
1073Action::ExprResult Sema::ParseUnaryOp(SourceLocation OpLoc, tok::TokenKind Op,
1074 ExprTy *Input) {
1075 UnaryOperator::Opcode Opc = ConvertTokenKindToUnaryOpcode(Op);
1076 QualType resultType;
1077 switch (Opc) {
1078 default:
1079 assert(0 && "Unimplemented unary expr!");
1080 case UnaryOperator::PreInc:
1081 case UnaryOperator::PreDec:
1082 resultType = CheckIncrementDecrementOperand((Expr *)Input, OpLoc);
1083 break;
1084 case UnaryOperator::AddrOf:
1085 resultType = CheckAddressOfOperand((Expr *)Input, OpLoc);
1086 break;
1087 case UnaryOperator::Deref:
1088 resultType = CheckIndirectionOperand((Expr *)Input, OpLoc);
1089 break;
1090 case UnaryOperator::Plus:
1091 case UnaryOperator::Minus:
1092 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1093 if (!resultType->isArithmeticType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001094 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1095 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001096 break;
1097 case UnaryOperator::Not: // bitwise complement
1098 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1099 if (!resultType->isIntegerType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001100 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1101 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001102 break;
1103 case UnaryOperator::LNot: // logical negation
1104 resultType = UsualUnaryConversion(((Expr *)Input)->getType());
1105 if (!resultType->isScalarType()) // C99 6.5.3.3p1
Chris Lattnerc04bd6a2007-05-16 18:09:54 +00001106 return Diag(OpLoc, diag::err_typecheck_unary_expr,
1107 resultType.getAsString());
Steve Naroff35d85152007-05-07 00:24:15 +00001108 break;
1109 case UnaryOperator::SizeOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001110 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1111 true);
1112 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001113 case UnaryOperator::AlignOf:
Steve Naroff043d45d2007-05-15 02:32:35 +00001114 resultType = CheckSizeOfAlignOfOperand(((Expr *)Input)->getType(), OpLoc,
1115 false);
1116 break;
Steve Naroff35d85152007-05-07 00:24:15 +00001117 }
1118 if (resultType.isNull())
1119 return true;
Steve Naroff509fe022007-05-17 01:16:00 +00001120 return new UnaryOperator((Expr *)Input, Opc, resultType, OpLoc);
Steve Naroff35d85152007-05-07 00:24:15 +00001121}