Mercurial > hg > CbC > CbC_llvm
comparison tools/clang/lib/Parse/ParseCbC.cpp @ 56:bdef5c940791
copy the previous function's return type to return value
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 23 Jan 2014 23:14:57 +0900 |
parents | d48478628b39 |
children | 88b0e1f890d7 |
comparison
equal
deleted
inserted
replaced
55:cdd58a41c04c | 56:bdef5c940791 |
---|---|
99 bool hasAmp(unsigned F) { return F & HasAmp; } | 99 bool hasAmp(unsigned F) { return F & HasAmp; } |
100 bool hasPeriod(unsigned F) { return F & HasPeriod; } | 100 bool hasPeriod(unsigned F) { return F & HasPeriod; } |
101 bool hasArrow(unsigned F) { return F & HasArrow; } | 101 bool hasArrow(unsigned F) { return F & HasArrow; } |
102 | 102 |
103 enum DeclarationFlags { | 103 enum DeclarationFlags { |
104 ArrayType = 0x01, | 104 ArrayType = 0x01, |
105 PointerType = 0x02, | 105 PointerType = 0x02, |
106 FunctionType = 0x04 | 106 CbCReturnFunc = 0x04, |
107 CopyParentType = 0x08 | |
107 }; | 108 }; |
108 | 109 |
109 bool isArray(unsigned F) { return F & ArrayType; } | 110 bool isArray(unsigned F) { return F & ArrayType; } |
110 bool isPointer(unsigned F) { return F & PointerType; } | 111 bool isPointer(unsigned F) { return F & PointerType; } |
111 bool isFunction(unsigned F) { return F & FunctionType; } | 112 bool isCbCRetFunc(unsigned F) { return F & CbCReturnFunc; } |
112 } | 113 bool shouldCopyParentsType(unsigned F) { return F & CopyParentType; } |
113 | 114 } |
114 | 115 |
115 void Parser::PrepareForGotoWithTheEnv(){ | 116 |
117 bool Parser::PrepareForGotoWithTheEnv(){ | |
116 StmtResult Res; | 118 StmtResult Res; |
117 SourceLocation Loc = Tok.getLocation(); | 119 SourceLocation Loc = Tok.getLocation(); |
118 | 120 |
119 IdentifierInfo *bufII, *retvalII, *structII, *__CbC_envII, *__CbC_retII,*envII, *ret_pII, *retcsII; | 121 IdentifierInfo *bufII, *retvalII, *structII, *__CbC_envII, *__CbC_retII,*envII, *ret_pII, *retcsII; |
120 bufII = CreateUniqueIdentifierInfo(__CBC_BUF_NAME, Loc); | 122 bufII = CreateUniqueIdentifierInfo(__CBC_BUF_NAME, Loc); |
124 __CbC_retII = CreateIdentifierInfo(__CBC_RETURN_NAME, Loc); | 126 __CbC_retII = CreateIdentifierInfo(__CBC_RETURN_NAME, Loc); |
125 envII = CreateIdentifierInfo(__CBC_STRUCT_ENV_NAME, Loc); | 127 envII = CreateIdentifierInfo(__CBC_STRUCT_ENV_NAME, Loc); |
126 ret_pII = CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc); | 128 ret_pII = CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc); |
127 retcsII = CreateUniqueIdentifierInfo(__CBC_RET_CODE_BASE_NAME, Loc); | 129 retcsII = CreateUniqueIdentifierInfo(__CBC_RET_CODE_BASE_NAME, Loc); |
128 | 130 |
129 CreateRetFunction(); | 131 if (CreateRetFunction()) { // error check : function type is void or not. |
130 Res = CreateDeclStmt(structII, CbCSpace::FunctionType, DeclSpec::TST___code); | 132 Diag(Tok, diag::err_cannot_use_goto_with_env); |
133 return true; | |
134 } | |
135 | |
136 Res = CreateDeclStmt(__CbC_retII, CbCSpace::CbCReturnFunc, DeclSpec::TST___code); | |
131 if (Res.isUsable()) | 137 if (Res.isUsable()) |
132 Stmtsp->push_back(Res.release()); | 138 Stmtsp->push_back(Res.release()); |
133 | 139 |
134 Res = CreateDeclStmt(__CbC_envII, 0, DeclSpec::TST_struct, structII); | 140 Res = CreateDeclStmt(__CbC_envII, 0, DeclSpec::TST_struct, structII); |
135 if (Res.isUsable()) | 141 if (Res.isUsable()) |
136 Stmtsp->push_back(Res.release()); | 142 Stmtsp->push_back(Res.release()); |
137 | 143 |
138 Res = CreateDeclStmt(bufII, CbCSpace::ArrayType, DeclSpec::TST_int); | 144 Res = CreateDeclStmt(bufII, 0, DeclSpec::TST_typename, CreateIdentifierInfo("jmp_buf", Loc)); |
139 if (Res.isUsable()) | 145 if (Res.isUsable()) |
140 Stmtsp->push_back(Res.release()); | 146 Stmtsp->push_back(Res.release()); |
141 | 147 |
142 Res = CreateDeclStmt(retvalII, 0, DeclSpec::TST_int); | 148 Res = CreateDeclStmt(retvalII, CbCSpace::CopyParentType); |
143 if (Res.isUsable()) | 149 if (Res.isUsable()) |
144 Stmtsp->push_back(Res.release()); | 150 Stmtsp->push_back(Res.release()); |
145 | 151 |
146 Res = CreateAssignmentStmt(__CbC_envII, retvalII, CbCSpace::HasPeriod, CbCSpace::HasAmp, ret_pII); | 152 Res = CreateAssignmentStmt(__CbC_envII, retvalII, CbCSpace::HasPeriod, CbCSpace::HasAmp, ret_pII); |
147 if (Res.isUsable()) | 153 if (Res.isUsable()) |
156 Stmtsp->push_back(Res.release()); | 162 Stmtsp->push_back(Res.release()); |
157 | 163 |
158 Res = CreateAssignmentStmt(__CbC_retII, retcsII); | 164 Res = CreateAssignmentStmt(__CbC_retII, retcsII); |
159 if (Res.isUsable()) | 165 if (Res.isUsable()) |
160 Stmtsp->push_back(Res.release()); | 166 Stmtsp->push_back(Res.release()); |
167 return false; | |
161 } | 168 } |
162 | 169 |
163 | 170 |
164 StmtResult Parser::CreateAssignmentStmt(IdentifierInfo* LHSII,IdentifierInfo* RHSII,unsigned LHSFlags,unsigned RHSFlags, | 171 StmtResult Parser::CreateAssignmentStmt(IdentifierInfo* LHSII,IdentifierInfo* RHSII,unsigned LHSFlags,unsigned RHSFlags, |
165 IdentifierInfo* extraLHSII,IdentifierInfo* extraRHSII){ | 172 IdentifierInfo* extraLHSII,IdentifierInfo* extraRHSII){ |
197 Expr = Actions.ActOnBinOp(getCurScope(), Loc,tok::equal,LHS.take(),RHS.take()); | 204 Expr = Actions.ActOnBinOp(getCurScope(), Loc,tok::equal,LHS.take(),RHS.take()); |
198 | 205 |
199 return Actions.ActOnExprStmt(Expr); | 206 return Actions.ActOnExprStmt(Expr); |
200 } | 207 } |
201 | 208 |
202 | 209 StmtResult Parser::CreateDeclStmt(IdentifierInfo *II, unsigned DeclFlags, DeclSpec::TST valueType, IdentifierInfo* Name, const char* size){ |
203 StmtResult Parser::CreateDeclStmt(IdentifierInfo *II, unsigned DeclFlags, DeclSpec::TST valueType, IdentifierInfo* Name){ | |
204 SourceLocation Loc = Tok.getLocation(); | 210 SourceLocation Loc = Tok.getLocation(); |
205 DeclGroupPtrTy DeclGPT; | 211 DeclGroupPtrTy DeclGPT; |
206 ParsingDeclSpec DS(*this); | 212 ParsingDeclSpec DS(*this); |
207 DeclSpec *DSp; | 213 DeclSpec *DSp; |
208 DSp = &DS; | 214 DSp = &DS; |
209 | 215 |
210 setTST(&DS, valueType, Name); | 216 setTST(&DS, valueType, Name); |
211 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Declarator::BlockContext)); | 217 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Declarator::BlockContext)); |
212 D.SetIdentifier(II, Loc); | 218 D.SetIdentifier(II, Loc); |
213 | 219 |
214 if (CbCSpace::isFunction(DeclFlags)) { | 220 if (CbCSpace::isCbCRetFunc(DeclFlags)) { |
215 D.setEllipsisLoc(SourceLocation()); | 221 D.setEllipsisLoc(SourceLocation()); |
216 bool hadGroupingParens = D.hasGroupingParens(); | 222 bool hadGroupingParens = D.hasGroupingParens(); |
217 D.setGroupingParens(true); | 223 D.setGroupingParens(true); |
218 IdentifierInfo* II = CreateIdentifierInfo(__CBC_RETURN_NAME, Loc); | |
219 D.SetRangeEnd(Loc); | 224 D.SetRangeEnd(Loc); |
220 DeclSpec FDS(AttrFactory); | 225 DeclSpec FDS(AttrFactory); |
221 | |
222 DS.Finish(Diags, PP); | 226 DS.Finish(Diags, PP); |
223 D.SetIdentifier(II, Loc); | |
224 | 227 |
225 D.AddTypeInfo(DeclaratorChunk::getPointer(FDS.getTypeQualifiers(), Loc, FDS.getConstSpecLoc(), FDS.getVolatileSpecLoc(), | 228 D.AddTypeInfo(DeclaratorChunk::getPointer(FDS.getTypeQualifiers(), Loc, FDS.getConstSpecLoc(), FDS.getVolatileSpecLoc(), |
226 FDS.getRestrictSpecLoc()), FDS.getAttributes(), SourceLocation()); | 229 FDS.getRestrictSpecLoc()), FDS.getAttributes(), SourceLocation()); |
227 D.setGroupingParens(hadGroupingParens); | 230 D.setGroupingParens(hadGroupingParens); |
228 | 231 |
239 SmallVector<ParsedType, 2> DynamicExceptions; | 242 SmallVector<ParsedType, 2> DynamicExceptions; |
240 SmallVector<SourceRange, 2> DynamicExceptionRanges; | 243 SmallVector<SourceRange, 2> DynamicExceptionRanges; |
241 ExprResult NoexceptExpr; | 244 ExprResult NoexceptExpr; |
242 ParsedAttributes FnAttrs(AttrFactory); | 245 ParsedAttributes FnAttrs(AttrFactory); |
243 TypeResult TrailingReturnType; | 246 TypeResult TrailingReturnType; |
247 | |
248 ParmVarDecl *Param; | |
249 FunctionDecl *CurFunctionDecl = Actions.getCurFunctionDecl(); | |
250 QualType CurFuncResQT = CurFunctionDecl->getResultType(); | |
251 TypeSourceInfo *CurFuncTI = Actions.Context.CreateTypeSourceInfo(CurFuncResQT); | |
252 | |
253 Param = CreateParam(); | |
254 Param->setTypeSourceInfo(CurFuncTI); | |
255 Param->setType(CurFuncResQT); | |
256 ParamInfo.push_back(DeclaratorChunk::ParamInfo(0, Loc, Param, 0)); | |
257 Param = CreateParam(0, 1, DeclSpec::TST_void); | |
258 ParamInfo.push_back(DeclaratorChunk::ParamInfo(0, Loc, Param, 0)); | |
259 HasProto = true; | |
260 | |
244 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,false,Loc,ParamInfo.data(), | 261 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,false,Loc,ParamInfo.data(), |
245 ParamInfo.size(),EllipsisLoc, Loc, FPDS.getTypeQualifiers(), | 262 ParamInfo.size(),EllipsisLoc, Loc, FPDS.getTypeQualifiers(), |
246 RefQualifierIsLValueRef,RefQualifierLoc, ConstQualifierLoc, | 263 RefQualifierIsLValueRef,RefQualifierLoc, ConstQualifierLoc, |
247 VolatileQualifierLoc,SourceLocation(),ESpecType, ESpecRange.getBegin(), | 264 VolatileQualifierLoc,SourceLocation(),ESpecType, ESpecRange.getBegin(), |
248 DynamicExceptions.data(),DynamicExceptionRanges.data(), | 265 DynamicExceptions.data(),DynamicExceptionRanges.data(), |
251 FnAttrs, Loc); | 268 FnAttrs, Loc); |
252 PrototypeScope.Exit(); | 269 PrototypeScope.Exit(); |
253 DSp = &FDS; | 270 DSp = &FDS; |
254 } | 271 } |
255 if (CbCSpace::isArray(DeclFlags)) | 272 if (CbCSpace::isArray(DeclFlags)) |
256 CreateArrayDecl(D, Loc); | 273 CreateArrayDecl(D, Loc, size); |
257 | 274 |
258 SmallVector<Decl *, 8> DeclsInGroup; | 275 SmallVector<Decl *, 8> DeclsInGroup; |
259 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); | 276 Decl *FirstDecl; |
277 | |
278 if (CbCSpace::shouldCopyParentsType(DeclFlags)) | |
279 FirstDecl = HandleDeclAndChangeDeclType(D); | |
280 else | |
281 FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); | |
282 | |
260 D.complete(FirstDecl); | 283 D.complete(FirstDecl); |
261 DeclsInGroup.push_back(FirstDecl); | 284 DeclsInGroup.push_back(FirstDecl); |
262 DeclGPT = Actions.FinalizeDeclaratorGroup(getCurScope(), *DSp, DeclsInGroup); | 285 DeclGPT = Actions.FinalizeDeclaratorGroup(getCurScope(), *DSp, DeclsInGroup); |
263 return Actions.ActOnDeclStmt(DeclGPT, Loc, Loc); | 286 return Actions.ActOnDeclStmt(DeclGPT, Loc, Loc); |
264 } | 287 } |
265 | 288 |
266 void Parser::CreateArrayDecl(ParsingDeclarator &D, SourceLocation Loc){ | 289 /* |
290 * This function imitated Parser::ParseDeclarationAfterDeclaratorAndAttributes() and Sema::ActOnDeclarator(). | |
291 * The origins get Type from Declarator but this function get Type from current function. | |
292 * It is useful for CbC to create statements for the continuation with the environments. | |
293 */ | |
294 Decl* Parser::HandleDeclAndChangeDeclType(Declarator &D) { | |
295 D.setFunctionDefinitionKind(FDK_Declaration); | |
296 DeclarationNameInfo NameInfo = Actions.GetNameForDeclarator(D); | |
297 DeclContext *DC = Actions.CurContext; | |
298 QualType R = Actions.getCurFunctionDecl()->getResultType(); | |
299 TypeSourceInfo *TInfo = Actions.Context.CreateTypeSourceInfo(R); | |
300 Scope *S = getCurScope(); | |
301 LookupResult Previous(Actions, NameInfo, Actions.LookupOrdinaryName, Actions.ForRedeclaration); | |
302 bool IsLinkageLookup = false; | |
303 bool CreateBuiltins = false; | |
304 | |
305 // If the declaration we're planning to build will be a function | |
306 // or object with linkage, then look for another declaration with | |
307 // linkage (C99 6.2.2p4-5 and C++ [basic.link]p6). | |
308 // | |
309 // If the declaration we're planning to build will be declared with | |
310 // external linkage in the translation unit, create any builtin with | |
311 // the same name. | |
312 if (R->isFunctionType()) { | |
313 IsLinkageLookup = true; | |
314 CreateBuiltins = | |
315 Actions.CurContext->getEnclosingNamespaceContext()->isTranslationUnit(); | |
316 } else if (Actions.CurContext->getRedeclContext()->isTranslationUnit()) | |
317 CreateBuiltins = true; | |
318 | |
319 if (IsLinkageLookup) | |
320 Previous.clear(Actions.LookupRedeclarationWithLinkage); | |
321 | |
322 Actions.LookupName(Previous, S, CreateBuiltins); | |
323 | |
324 // In C++, the previous declaration we find might be a tag type | |
325 // (class or enum). In this case, the new declaration will hide the | |
326 // tag type. Note that this does does not apply if we're declaring a | |
327 // typedef (C++ [dcl.typedef]p4). | |
328 if (Previous.isSingleTagDecl()) | |
329 Previous.clear(); | |
330 NamedDecl *New; | |
331 bool AddToScope = true; | |
332 if (R->isFunctionType()) { | |
333 New = Actions.ActOnFunctionDeclarator(S, D, DC, TInfo, Previous, | |
334 MultiTemplateParamsArg(), AddToScope); | |
335 } else { | |
336 New = Actions.ActOnVariableDeclarator(S, D, DC, TInfo, Previous, | |
337 MultiTemplateParamsArg(), AddToScope); | |
338 } | |
339 | |
340 if (New->getDeclName() && AddToScope) { | |
341 // Only make a locally-scoped extern declaration visible if it is the first | |
342 // declaration of this entity. Qualified lookup for such an entity should | |
343 // only find this declaration if there is no visible declaration of it. | |
344 bool AddToContext = !D.isRedeclaration() || !New->isLocalExternDecl(); | |
345 Actions.PushOnScopeChains(New, S, AddToContext); | |
346 if (!AddToContext) | |
347 Actions.CurContext->addHiddenDecl(New); | |
348 } | |
349 | |
350 return New; | |
351 } | |
352 | |
353 void Parser::CreateArrayDecl(ParsingDeclarator &D, SourceLocation Loc, const char* size){ | |
267 ExprResult ExprRes; | 354 ExprResult ExprRes; |
268 ParsedAttributesWithRange attrs(AttrFactory); | 355 ParsedAttributesWithRange attrs(AttrFactory); |
269 SmallString<128> SpellingBuffer; | 356 SmallString<128> SpellingBuffer; |
270 SpellingBuffer.resize(strlen(__JMP_BUF_SIZE) + 1); | 357 SpellingBuffer.resize(strlen(size) + 1); |
271 StringRef TokSpelling = StringRef(__JMP_BUF_SIZE, strlen(__JMP_BUF_SIZE)); | 358 StringRef TokSpelling = StringRef(size, strlen(size)); |
272 NumericLiteralParser Literal(TokSpelling, Loc, PP); | 359 NumericLiteralParser Literal(TokSpelling, Loc, PP); |
273 Expr *sizeRes; | 360 Expr *sizeRes; |
274 QualType Ty; | 361 QualType Ty; |
275 unsigned MaxWidth = Actions.Context.getTargetInfo().getIntMaxTWidth(); | 362 unsigned MaxWidth = Actions.Context.getTargetInfo().getIntMaxTWidth(); |
276 llvm::APInt ResultVal(MaxWidth, 0); | 363 llvm::APInt ResultVal(MaxWidth, 0); |
311 ParseScope CompoundScope(this, Scope::DeclScope); | 398 ParseScope CompoundScope(this, Scope::DeclScope); |
312 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Loc,"in create setjmp statement for CbC"); | 399 PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Loc,"in create setjmp statement for CbC"); |
313 StmtVector innerStmts; | 400 StmtVector innerStmts; |
314 StmtResult innerStmtRes; | 401 StmtResult innerStmtRes; |
315 ExprResult innerExprRes; | 402 ExprResult innerExprRes; |
316 DeclSpec CastDS(AttrFactory); | 403 innerExprRes = LookupAndDeclareName(CreateUniqueIdentifierInfo(__CBC_RETVAL_NAME, Loc)); |
317 | |
318 setTST(&CastDS, DeclSpec::TST_int); | |
319 Declarator CastDeclaratorInfo(CastDS, Declarator::TypeNameContext); | |
320 CastDeclaratorInfo.SetRangeEnd(Loc); | |
321 DeclSpec pointerDS(AttrFactory); | |
322 pointerDS.Finish(Diags, PP); | |
323 DeclaratorScopeObj DeclScopeObj(*this, CastDeclaratorInfo.getCXXScopeSpec()); | |
324 CastDeclaratorInfo.SetIdentifier(0, Loc); | |
325 CastDeclaratorInfo.AddTypeInfo(DeclaratorChunk::getPointer(pointerDS.getTypeQualifiers(), Loc, | |
326 pointerDS.getConstSpecLoc(), | |
327 pointerDS.getVolatileSpecLoc(), | |
328 pointerDS.getRestrictSpecLoc()), | |
329 pointerDS.getAttributes(),SourceLocation()); | |
330 | |
331 innerExprRes = LookupAndDeclareName(CreateIdentifierInfo(__CBC_ENVIRONMENT_NAME, Loc)); | |
332 innerExprRes = LookupMemberAndBuildExpr(CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc), innerExprRes.take(), false); | |
333 Expr *CastExpr = innerExprRes.take(); | |
334 TypeSourceInfo *castTInfo = Actions.GetTypeForDeclaratorCast(CastDeclaratorInfo, CastExpr->getType()); | |
335 Actions.checkUnusedDeclAttributes(CastDeclaratorInfo); | |
336 | |
337 innerExprRes = Actions.BuildCStyleCastExpr(Loc, castTInfo, Loc, CastExpr); | |
338 innerExprRes = Actions.ActOnParenExpr(Loc, Loc, innerExprRes.take()); | |
339 innerExprRes = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::star, innerExprRes.get()); | |
340 innerStmtRes = Actions.ActOnReturnStmt(Loc, innerExprRes.take()); | 404 innerStmtRes = Actions.ActOnReturnStmt(Loc, innerExprRes.take()); |
341 if (innerStmtRes.isUsable()) | 405 if (innerStmtRes.isUsable()) |
342 innerStmts.push_back(innerStmtRes.release()); | 406 innerStmts.push_back(innerStmtRes.release()); |
343 StmtRes = Actions.ActOnCompoundStmt(Loc, Loc,innerStmts, false); | 407 StmtRes = Actions.ActOnCompoundStmt(Loc, Loc,innerStmts, false); |
344 StmtResult ThenStmt(StmtRes); | 408 StmtResult ThenStmt(StmtRes); |
353 SourceLocation Loc = Tok.getLocation(); | 417 SourceLocation Loc = Tok.getLocation(); |
354 UnqualifiedId Name; | 418 UnqualifiedId Name; |
355 CXXScopeSpec SS; | 419 CXXScopeSpec SS; |
356 SourceLocation TemplateKWLoc; | 420 SourceLocation TemplateKWLoc; |
357 ExternalSpace::CastExpressionIdValidator Validator(false,true); | 421 ExternalSpace::CastExpressionIdValidator Validator(false,true); |
422 bool IsAddressOfOperand = false; | |
358 Name.setIdentifier(II, Loc); | 423 Name.setIdentifier(II, Loc); |
359 TemplateArgumentListInfo TemplateArgsBuffer; | 424 return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name, false, IsAddressOfOperand, &Validator); |
360 DeclarationNameInfo NameInfo; | |
361 const TemplateArgumentListInfo *TemplateArgs; | |
362 Actions.DecomposeUnqualifiedId(Name, TemplateArgsBuffer, NameInfo, TemplateArgs); | |
363 DeclarationName DName = NameInfo.getName(); | |
364 II = DName.getAsIdentifierInfo(); | |
365 SourceLocation NameLoc = NameInfo.getLoc(); | |
366 LookupResult R(Actions, NameInfo,Actions.LookupOrdinaryName); | |
367 Actions.LookupParsedName(R, getCurScope(), &SS, true); | |
368 | |
369 if (II && R.empty()) { | |
370 NamedDecl *D = Actions.ImplicitlyDefineFunction(NameLoc, *II, getCurScope()); | |
371 if (D) R.addDecl(D); | |
372 } | |
373 return Actions.BuildDeclarationNameExpr(SS, R, false); | |
374 } | 425 } |
375 | 426 |
376 ExprResult Parser::LookupMemberAndBuildExpr(IdentifierInfo *II, Expr* Base, bool IsArrow){ | 427 ExprResult Parser::LookupMemberAndBuildExpr(IdentifierInfo *II, Expr* Base, bool IsArrow){ |
377 SourceLocation Loc = Tok.getLocation(); | 428 SourceLocation Loc = Tok.getLocation(); |
378 ExprResult Res; | 429 ExprResult Res; |
493 os << curFuncName << ".." /* separator */ << Name; | 544 os << curFuncName << ".." /* separator */ << Name; |
494 II = CreateIdentifierInfo(os.str().c_str(), Loc); | 545 II = CreateIdentifierInfo(os.str().c_str(), Loc); |
495 return II; | 546 return II; |
496 } | 547 } |
497 | 548 |
498 void Parser::CreateRetFunction(){ | 549 bool Parser::CreateRetFunction(){ |
550 FunctionDecl *CurFunctionDecl = Actions.getCurFunctionDecl(); | |
551 QualType CurFuncResQT = CurFunctionDecl->getResultType(); | |
552 if (CurFuncResQT.getTypePtr()->isVoidType()) // this function cannot use continuation with the environment. | |
553 return true; | |
554 | |
499 Scope *SavedScope = getCurScope(); | 555 Scope *SavedScope = getCurScope(); |
500 DeclContext *SavedContext = Actions.CurContext; | 556 DeclContext *SavedContext = Actions.CurContext; |
557 TypeSourceInfo *CurFuncTI = Actions.Context.CreateTypeSourceInfo(CurFuncResQT); | |
501 sema::FunctionScopeInfo *SavedFSI = Actions.FunctionScopes.pop_back_val(); | 558 sema::FunctionScopeInfo *SavedFSI = Actions.FunctionScopes.pop_back_val(); |
502 DeclSpec::TST retvalType = DeclSpec::TST_int; | |
503 | 559 |
504 Actions.CurContext = static_cast<DeclContext *>(Actions.Context.getTranslationUnitDecl()); | 560 Actions.CurContext = static_cast<DeclContext *>(Actions.Context.getTranslationUnitDecl()); |
505 Scope *TopScope = getCurScope(); | 561 Scope *TopScope = getCurScope(); |
506 while(TopScope->getParent() != NULL) | 562 while(TopScope->getParent() != NULL) |
507 TopScope = TopScope->getParent(); | 563 TopScope = TopScope->getParent(); |
508 Actions.CurScope = TopScope; | 564 Actions.CurScope = TopScope; |
565 | |
509 DeclGroupPtrTy returnDecl = DeclGroupPtrTy(); | 566 DeclGroupPtrTy returnDecl = DeclGroupPtrTy(); |
510 SourceLocation Loc = Tok.getLocation(); | 567 SourceLocation Loc = Tok.getLocation(); |
568 Create__CbC_envStruct(Loc, AS_none); | |
511 ParsingDeclSpec PDS(*this); | 569 ParsingDeclSpec PDS(*this); |
512 setTST(&PDS, DeclSpec::TST___code); | 570 setTST(&PDS, DeclSpec::TST___code); |
513 ParsingDeclarator D(*this, PDS, static_cast<Declarator::TheContext>(Declarator::FileContext)); | 571 ParsingDeclarator D(*this, PDS, static_cast<Declarator::TheContext>(Declarator::FileContext)); |
514 D.SetIdentifier(CreateUniqueIdentifierInfo(__CBC_RET_CODE_BASE_NAME, Loc),Loc); | 572 D.SetIdentifier(CreateUniqueIdentifierInfo(__CBC_RET_CODE_BASE_NAME, Loc),Loc); |
515 ParseScope PrototypeScope(this,Scope::FunctionPrototypeScope|Scope::DeclScope|Scope::FunctionDeclarationScope); | 573 ParseScope PrototypeScope(this,Scope::FunctionPrototypeScope|Scope::DeclScope|Scope::FunctionDeclarationScope); |
524 SmallVector<ParsedType, 2> DynamicExceptions; | 582 SmallVector<ParsedType, 2> DynamicExceptions; |
525 SmallVector<SourceRange, 2> DynamicExceptionRanges; | 583 SmallVector<SourceRange, 2> DynamicExceptionRanges; |
526 ExprResult NoexceptExpr; | 584 ExprResult NoexceptExpr; |
527 ParsedAttributes FnAttrs(AttrFactory); | 585 ParsedAttributes FnAttrs(AttrFactory); |
528 TypeResult TrailingReturnType; | 586 TypeResult TrailingReturnType; |
529 | 587 ParmVarDecl *Param; |
530 | 588 |
531 IdentifierInfo *retvalII = CreateIdentifierInfo(__CBC_RETVAL_NAME, Loc); | 589 IdentifierInfo *retvalII = CreateIdentifierInfo(__CBC_RETVAL_NAME, Loc); |
532 // TODO : We should change retval type to a destination function's return type. | 590 Param = CreateParam(retvalII); |
533 CreateParam(retvalType, retvalII, 0, &ParamInfo); | 591 Param->setTypeSourceInfo(CurFuncTI); |
592 Param->setType(CurFuncResQT); | |
593 | |
594 ParamInfo.push_back(DeclaratorChunk::ParamInfo(retvalII, Loc, Param, 0)); | |
534 IdentifierInfo *envII = CreateIdentifierInfo(__CBC_STRUCT_ENV_NAME, Loc); | 595 IdentifierInfo *envII = CreateIdentifierInfo(__CBC_STRUCT_ENV_NAME, Loc); |
535 CreateParam(DeclSpec::TST_void, envII, 1, &ParamInfo); | 596 Param = CreateParam(envII, 1, DeclSpec::TST_void); |
597 ParamInfo.push_back(DeclaratorChunk::ParamInfo(envII, Loc, Param, 0)); | |
536 | 598 |
537 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, IsAmbiguous, Loc, ParamInfo.data(), ParamInfo.size(), EllipsisLoc, Loc, | 599 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, IsAmbiguous, Loc, ParamInfo.data(), ParamInfo.size(), EllipsisLoc, Loc, |
538 FDS.getTypeQualifiers(), RefQualifierIsLValueRef, RefQualifierLoc, ConstQualifierLoc, | 600 FDS.getTypeQualifiers(), RefQualifierIsLValueRef, RefQualifierLoc, ConstQualifierLoc, |
539 VolatileQualifierLoc, SourceLocation(), ESpecType, ESpecRange.getBegin(), | 601 VolatileQualifierLoc, SourceLocation(), ESpecType, ESpecRange.getBegin(), |
540 DynamicExceptions.data(), DynamicExceptionRanges.data(), DynamicExceptions.size(), | 602 DynamicExceptions.data(), DynamicExceptionRanges.data(), DynamicExceptions.size(), |
551 Actions.ActOnDefaultCtorInitializers(BodyRes); | 613 Actions.ActOnDefaultCtorInitializers(BodyRes); |
552 StmtResult FnBody; | 614 StmtResult FnBody; |
553 StmtVector FnStmts; | 615 StmtVector FnStmts; |
554 StmtResult innerR; | 616 StmtResult innerR; |
555 ExprResult retvalAssginmentExpr,LHS; | 617 ExprResult retvalAssginmentExpr,LHS; |
556 DeclSpec retvalTypeDS(AttrFactory); | |
557 setTST(&retvalTypeDS, retvalType); | |
558 | |
559 Declarator retvalTypeDInfo(retvalTypeDS, Declarator::TypeNameContext); | |
560 retvalTypeDInfo.SetRangeEnd(Loc); | |
561 DeclSpec starDS(AttrFactory); | |
562 starDS.Finish(Diags, PP); | |
563 retvalTypeDInfo.SetIdentifier(0, Loc); | |
564 retvalTypeDInfo.AddTypeInfo(DeclaratorChunk::getPointer(starDS.getTypeQualifiers(), Loc, | |
565 starDS.getConstSpecLoc(), | |
566 starDS.getVolatileSpecLoc(), | |
567 starDS.getRestrictSpecLoc()), | |
568 starDS.getAttributes(), | |
569 SourceLocation()); | |
570 | |
571 ExprVector ArgExprs; | 618 ExprVector ArgExprs; |
572 CommaLocsTy CommaLocs; | 619 CommaLocsTy CommaLocs; |
573 DeclSpec envDS(AttrFactory); | 620 DeclSpec envDS(AttrFactory); |
574 IdentifierInfo *structName = CreateIdentifierInfo(__CBC_STRUCT_NAME, Loc); | 621 IdentifierInfo *structName = CreateIdentifierInfo(__CBC_STRUCT_NAME, Loc); |
575 setTST(&envDS, DeclSpec::TST_struct, structName); | 622 setTST(&envDS, DeclSpec::TST_struct, structName); |
576 | 623 |
577 Declarator envDInfo(envDS, Declarator::TypeNameContext); | 624 Declarator envDInfo(envDS, Declarator::TypeNameContext); |
578 envDInfo.SetRangeEnd(Loc); | 625 envDInfo.SetRangeEnd(Loc); |
579 DeclSpec starDS2(AttrFactory); | 626 DeclSpec starDS(AttrFactory); |
580 starDS2.Finish(Diags, PP); | 627 starDS.Finish(Diags, PP); |
581 envDInfo.SetIdentifier(0,Loc); | 628 envDInfo.SetIdentifier(0,Loc); |
582 envDInfo.AddTypeInfo(DeclaratorChunk::getPointer(starDS2.getTypeQualifiers(), Loc, | 629 envDInfo.AddTypeInfo(DeclaratorChunk::getPointer(starDS.getTypeQualifiers(), Loc, |
583 starDS2.getConstSpecLoc(), | 630 starDS.getConstSpecLoc(), |
584 starDS2.getVolatileSpecLoc(), | 631 starDS.getVolatileSpecLoc(), |
585 starDS2.getRestrictSpecLoc()), | 632 starDS.getRestrictSpecLoc()), |
586 starDS2.getAttributes(), | 633 starDS.getAttributes(), |
587 SourceLocation()); | 634 SourceLocation()); |
588 ExprVector ArgExprs2; | 635 ExprVector ArgExprs2; |
589 LHS = LookupAndDeclareName(envII); | 636 LHS = LookupAndDeclareName(envII); |
590 ArgExprs2.push_back(LHS.release()); | 637 ArgExprs2.push_back(LHS.release()); |
591 LHS = Actions.ActOnParenListExpr(Loc, Loc, ArgExprs2); | 638 LHS = Actions.ActOnParenListExpr(Loc, Loc, ArgExprs2); |
597 ArgExprs.push_back(LHS.release()); | 644 ArgExprs.push_back(LHS.release()); |
598 LHS = Actions.ActOnParenListExpr(Loc, Loc, ArgExprs); | 645 LHS = Actions.ActOnParenListExpr(Loc, Loc, ArgExprs); |
599 LHS = LookupMemberAndBuildExpr(CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc), | 646 LHS = LookupMemberAndBuildExpr(CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc), |
600 LHS.take(), true); | 647 LHS.take(), true); |
601 Expr *ret_pCastExpr = LHS.take(); | 648 Expr *ret_pCastExpr = LHS.take(); |
602 TypeSourceInfo *castTInfo2 = Actions.GetTypeForDeclaratorCast(retvalTypeDInfo, ret_pCastExpr->getType()); | 649 DeclarationName noValDeclName; |
603 LHS = Actions.BuildCStyleCastExpr(Loc, castTInfo2, Loc, ret_pCastExpr); | 650 TypeSourceInfo *CurFuncTypesPointerTI = Actions.Context.CreateTypeSourceInfo(Actions.BuildPointerType(CurFuncResQT, Loc, noValDeclName)); |
651 LHS = Actions.BuildCStyleCastExpr(Loc, CurFuncTypesPointerTI, Loc, ret_pCastExpr); | |
604 LHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::star, LHS.get()); | 652 LHS = Actions.ActOnUnaryOp(getCurScope(), Loc, tok::star, LHS.get()); |
605 ExprResult RHS; | 653 ExprResult RHS; |
606 RHS = LookupAndDeclareName(retvalII); | 654 RHS = LookupAndDeclareName(retvalII); |
607 | 655 |
608 retvalAssginmentExpr = Actions.ActOnBinOp(getCurScope(), Loc, tok::equal, LHS.take(), RHS.take()); | 656 retvalAssginmentExpr = Actions.ActOnBinOp(getCurScope(), Loc, tok::equal, LHS.take(), RHS.take()); |
629 DeclSpec ljDS(AttrFactory); | 677 DeclSpec ljDS(AttrFactory); |
630 setTST(&ljDS, DeclSpec::TST_struct, structName); | 678 setTST(&ljDS, DeclSpec::TST_struct, structName); |
631 | 679 |
632 Declarator ljD(ljDS, Declarator::TypeNameContext); | 680 Declarator ljD(ljDS, Declarator::TypeNameContext); |
633 ljD.SetRangeEnd(Loc); | 681 ljD.SetRangeEnd(Loc); |
634 DeclSpec starDS3(AttrFactory); | 682 DeclSpec starDS2(AttrFactory); |
635 starDS3.Finish(Diags, PP); | 683 starDS2.Finish(Diags, PP); |
636 ljD.ExtendWithDeclSpec(starDS3); | 684 ljD.ExtendWithDeclSpec(starDS2); |
637 ljD.SetIdentifier(0, Loc); | 685 ljD.SetIdentifier(0, Loc); |
638 ljD.AddTypeInfo(DeclaratorChunk::getPointer(ljDS.getTypeQualifiers(), Loc, | 686 ljD.AddTypeInfo(DeclaratorChunk::getPointer(ljDS.getTypeQualifiers(), Loc, |
639 ljDS.getConstSpecLoc(), | 687 ljDS.getConstSpecLoc(), |
640 ljDS.getVolatileSpecLoc(), | 688 ljDS.getVolatileSpecLoc(), |
641 ljDS.getRestrictSpecLoc()), | 689 ljDS.getRestrictSpecLoc()), |
662 returnDecl = Actions.ConvertDeclToDeclGroup(TheDecl); | 710 returnDecl = Actions.ConvertDeclToDeclGroup(TheDecl); |
663 (&Actions.getASTConsumer())->HandleTopLevelDecl(returnDecl.get()); | 711 (&Actions.getASTConsumer())->HandleTopLevelDecl(returnDecl.get()); |
664 Actions.CurScope = SavedScope; | 712 Actions.CurScope = SavedScope; |
665 Actions.CurContext = SavedContext; | 713 Actions.CurContext = SavedContext; |
666 Actions.FunctionScopes.push_back(SavedFSI); | 714 Actions.FunctionScopes.push_back(SavedFSI); |
667 } | 715 return false; |
668 | 716 } |
669 void Parser::CreateParam(DeclSpec::TST T, IdentifierInfo *II, int pointerNum, SmallVector<DeclaratorChunk::ParamInfo, 16> *ParamInfo){ | 717 |
718 ParmVarDecl* Parser::CreateParam(IdentifierInfo *II, int pointerNum, DeclSpec::TST T){ | |
670 SourceLocation Loc = Tok.getLocation(); | 719 SourceLocation Loc = Tok.getLocation(); |
671 DeclSpec DS(AttrFactory); | 720 DeclSpec DS(AttrFactory); |
672 setTST(&DS, T); | 721 setTST(&DS, T); |
673 Declarator ParamDeclarator(DS, Declarator::PrototypeContext); | 722 Declarator ParamDeclarator(DS, Declarator::PrototypeContext); |
674 ParamDeclarator.SetIdentifier(II, Loc); | 723 ParamDeclarator.SetIdentifier(II, Loc); |
679 pointerDS.getConstSpecLoc(), | 728 pointerDS.getConstSpecLoc(), |
680 pointerDS.getVolatileSpecLoc(), | 729 pointerDS.getVolatileSpecLoc(), |
681 pointerDS.getRestrictSpecLoc()), | 730 pointerDS.getRestrictSpecLoc()), |
682 pointerDS.getAttributes(),SourceLocation()); | 731 pointerDS.getAttributes(),SourceLocation()); |
683 } | 732 } |
684 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParamDeclarator); | 733 ParmVarDecl *Param = dyn_cast<ParmVarDecl>(Actions.ActOnParamDeclarator(getCurScope(), ParamDeclarator)); |
685 ParamInfo->push_back(DeclaratorChunk::ParamInfo(II, ParamDeclarator.getIdentifierLoc(), Param, 0)); | 734 return Param; |
686 | 735 |
687 } | 736 } |
688 | 737 |
689 void Parser::setTST(DeclSpec *DS, DeclSpec::TST T, IdentifierInfo* Name){ | 738 void Parser::setTST(DeclSpec *DS, DeclSpec::TST T, IdentifierInfo* Name){ |
690 SourceLocation Loc = Tok.getLocation(); | 739 SourceLocation Loc = Tok.getLocation(); |
691 bool isInvalid = false; | 740 bool isInvalid = false; |
692 const char *PrevSpec = 0; | 741 const char *PrevSpec = 0; |
693 unsigned DiagID = 0; | 742 unsigned DiagID = 0; |
743 CXXScopeSpec SS; | |
694 DS->SetRangeStart(Loc); | 744 DS->SetRangeStart(Loc); |
695 DS->SetRangeEnd(Loc); | 745 DS->SetRangeEnd(Loc); |
696 if (T == DeclSpec::TST_struct) { | 746 if (T == DeclSpec::TST_struct) { |
697 ParsedAttributesWithRange attrs(AttrFactory); | 747 ParsedAttributesWithRange attrs(AttrFactory); |
698 CXXScopeSpec &SS = DS->getTypeSpecScope(); | |
699 DeclResult TagOrTempResult = true; | 748 DeclResult TagOrTempResult = true; |
700 bool Owned = false; | 749 bool Owned = false; |
701 bool IsDependent = false; | 750 bool IsDependent = false; |
702 MultiTemplateParamsArg TParams; | 751 MultiTemplateParamsArg TParams; |
703 TagOrTempResult = Actions.ActOnTag(getCurScope(), T, Sema::TUK_Reference, Loc, | 752 TagOrTempResult = Actions.ActOnTag(getCurScope(), T, Sema::TUK_Reference, Loc, |
706 TParams, Owned, IsDependent, | 755 TParams, Owned, IsDependent, |
707 SourceLocation(), false, | 756 SourceLocation(), false, |
708 clang::TypeResult()); | 757 clang::TypeResult()); |
709 isInvalid = DS->SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, TagOrTempResult.get(), Owned); | 758 isInvalid = DS->SetTypeSpecType(T, Loc, Loc, PrevSpec, DiagID, TagOrTempResult.get(), Owned); |
710 } | 759 } |
760 else if (T == DeclSpec::TST_typename) { | |
761 Token Next,TypeTok; | |
762 Next.setKind(tok::identifier); | |
763 ExternalSpace::StatementFilterCCC CCCValidator(Next); | |
764 Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, Name, Loc, Next, false, &CCCValidator); | |
765 TypeTok.startToken(); | |
766 TypeTok.setLocation(Loc); | |
767 TypeTok.setIdentifierInfo(Name); | |
768 TypeTok.setKind(tok::annot_typename); | |
769 setTypeAnnotation(TypeTok, Classification.getType()); | |
770 TypeTok.setAnnotationEndLoc(Loc); | |
771 PP.AnnotateCachedTokens(TypeTok); | |
772 if (TypeTok.getAnnotationValue()) { | |
773 ParsedType PT = getTypeAnnotation(TypeTok); | |
774 isInvalid = DS->SetTypeSpecType(T, Loc, PrevSpec, DiagID, PT); | |
775 } else | |
776 DS->SetTypeSpecError(); | |
777 } | |
711 else | 778 else |
712 isInvalid = DS->SetTypeSpecType(T, Loc, PrevSpec, DiagID); | 779 isInvalid = DS->SetTypeSpecType(T, Loc, PrevSpec, DiagID); |
713 | 780 |
714 DS->Finish(Diags, PP); | 781 DS->Finish(Diags, PP); |
715 if (isInvalid) { | 782 if (isInvalid) { |