# HG changeset patch # User Shinji KONO # Date 1591105152 -32400 # Node ID 86e770a5ed65ac9b294fe3395e975eac5c5262a2 # Parent 9b4cb9bbd23d2656e987070910adc48e0c4d8858 ... diff -r 9b4cb9bbd23d -r 86e770a5ed65 clang/lib/Parse/ParseCbC.cpp --- a/clang/lib/Parse/ParseCbC.cpp Mon Jun 01 23:41:03 2020 +0900 +++ b/clang/lib/Parse/ParseCbC.cpp Tue Jun 02 22:39:12 2020 +0900 @@ -23,64 +23,74 @@ /// Because those namespaces are unnamed namespaces, we can't access them. /// So create this namespace and copy classes from those namespaces. namespace ExternalSpace { // from ParseExpr.cpp , ParseStmt.cpp - class CastExpressionIdValidator : public CorrectionCandidateCallback { - public: - CastExpressionIdValidator(bool AllowTypes, bool AllowNonTypes) - : AllowNonTypes(AllowNonTypes) { - WantTypeSpecifiers = AllowTypes; - } + class CastExpressionIdValidator final : public CorrectionCandidateCallback { + public: + CastExpressionIdValidator(Token Next, bool AllowTypes, bool AllowNonTypes) + : NextToken(Next), AllowNonTypes(AllowNonTypes) { + WantTypeSpecifiers = WantFunctionLikeCasts = AllowTypes; + } - bool ValidateCandidate(const TypoCorrection &candidate) override { - NamedDecl *ND = candidate.getCorrectionDecl(); - if (!ND) - return candidate.isKeyword(); + bool ValidateCandidate(const TypoCorrection &candidate) override { + NamedDecl *ND = candidate.getCorrectionDecl(); + if (!ND) + return candidate.isKeyword(); + + if (isa(ND)) + return WantTypeSpecifiers; - if (isa(ND)) - return WantTypeSpecifiers; - return AllowNonTypes; - } + if (!AllowNonTypes || !CorrectionCandidateCallback::ValidateCandidate(candidate)) + return false; + + if (!NextToken.isOneOf(tok::equal, tok::arrow, tok::period)) + return true; - std::unique_ptr clone() override { - return std::make_unique(*this); - } + for (auto *C : candidate) { + NamedDecl *ND = C->getUnderlyingDecl(); + if (isa(ND) && !isa(ND)) + return true; + } + return false; + } - private: - bool AllowNonTypes; - }; + std::unique_ptr clone() override { + return std::make_unique(*this); + } - class StatementFilterCCC : public CorrectionCandidateCallback { - public: - StatementFilterCCC(Token nextTok) : NextToken(nextTok) { - WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) || - nextTok.is(tok::identifier) || nextTok.is(tok::star) || - nextTok.is(tok::amp) || nextTok.is(tok::l_square); - WantExpressionKeywords = nextTok.is(tok::l_paren) || - nextTok.is(tok::identifier) || - nextTok.is(tok::arrow) || nextTok.is(tok::period); - WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) || - nextTok.is(tok::identifier) || - nextTok.is(tok::l_brace); - WantCXXNamedCasts = false; - } + private: + Token NextToken; + bool AllowNonTypes; + }; + + class StatementFilterCCC final : public CorrectionCandidateCallback { + public: + StatementFilterCCC(Token nextTok) : NextToken(nextTok) { + WantTypeSpecifiers = nextTok.isOneOf(tok::l_paren, tok::less, tok::l_square, + tok::identifier, tok::star, tok::amp); + WantExpressionKeywords = + nextTok.isOneOf(tok::l_paren, tok::identifier, tok::arrow, tok::period); + WantRemainingKeywords = + nextTok.isOneOf(tok::l_paren, tok::semi, tok::identifier, tok::l_brace); + WantCXXNamedCasts = false; + } - bool ValidateCandidate(const TypoCorrection &candidate) override { - if (FieldDecl *FD = candidate.getCorrectionDeclAs()) - return !candidate.getCorrectionSpecifier() || isa(FD); - if (NextToken.is(tok::equal)) - return candidate.getCorrectionDeclAs(); - if (NextToken.is(tok::period) && - candidate.getCorrectionDeclAs()) - return false; - return CorrectionCandidateCallback::ValidateCandidate(candidate); - } + bool ValidateCandidate(const TypoCorrection &candidate) override { + if (FieldDecl *FD = candidate.getCorrectionDeclAs()) + return !candidate.getCorrectionSpecifier() || isa(FD); + if (NextToken.is(tok::equal)) + return candidate.getCorrectionDeclAs(); + if (NextToken.is(tok::period) && + candidate.getCorrectionDeclAs()) + return false; + return CorrectionCandidateCallback::ValidateCandidate(candidate); + } - std::unique_ptr clone() override { + std::unique_ptr clone() override { return std::make_unique(*this); - } + } - private: - Token NextToken; - }; + private: + Token NextToken; + }; } /// goto with environment with out logjmp @@ -137,16 +147,17 @@ Diag(Tok, DiagID); return ExprError(); } + // std::unique_ptr AST(tooling::buildASTFromCode("({auto x = 1 + 1;})")); + EnterScope(Scope::DeclScope); StmtResult innerRes; SourceLocation Loc = Tok.getLocation(); - IdentifierInfo *__CbC_retII = CreateIdentifierInfo(__CBC_RETURN_NAME, Loc); + IdentifierInfo *__CbC_retII = CreateIdentifierInfo("__CbC_cont", Loc); IdentifierInfo *retcsII = CreateUniqueIdentifierInfo(__CBC_RET_CODE_BASE_NAME, Loc); Create__CbC_envStruct(Loc, AS_none); Actions.ActOnStartStmtExpr(); StmtResult CompoundStmtRes; - ParseScope CompoundScope(this, Scope::DeclScope); PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Loc,"in compound statement ('{}')"); StmtVector CompoundStmts; @@ -168,8 +179,11 @@ if (innerRes.isUsable()) CompoundStmts.push_back(innerRes.get()); + Sema::CompoundScopeRAII CompoundScope(Actions); CompoundStmtRes = Actions.ActOnCompoundStmt(Loc,Loc,CompoundStmts,true); - ConsumeToken(); // eat the '__return'. + // ConsumeToken(); + ConsumeAnyToken(); // eat the '__return'. + ExitScope(); return Actions.ActOnStmtExpr(getCurScope(),Loc, CompoundStmtRes.get(), Loc); } @@ -202,6 +216,7 @@ return ExprError(); } + EnterScope(Scope::DeclScope); StmtResult innerRes; SourceLocation Loc = Tok.getLocation(); IdentifierInfo *bufII = CreateIdentifierInfo(__CBC_BUF_NAME, Loc); @@ -212,7 +227,7 @@ IdentifierInfo *ret_pII = CreateIdentifierInfo(__CBC_STRUCT_POINTER_NAME, Loc); Create__CbC_envStruct(Loc, AS_none); Actions.ActOnStartStmtExpr(); - ParseScope CompoundScope(this, Scope::DeclScope); + PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),Loc,"in compound statement ('{}')"); StmtVector CompoundStmts; ExprResult Result(true); @@ -267,6 +282,7 @@ DeclaratorInfo.SetIdentifier(nullptr, Tok.getLocation()); DeclaratorInfo.AddTypeInfo(DeclaratorChunk::getPointer(star_DS.getTypeQualifiers(), Loc,star_DS.getConstSpecLoc(),star_DS.getVolatileSpecLoc(), star_DS.getRestrictSpecLoc(),star_DS.getAtomicSpecLoc(),star_DS.getUnalignedSpecLoc()),std::move(star_DS.getAttributes()),SourceLocation()); + ExitScope(); return Actions.ActOnCastExpr(getCurScope(), Loc, DeclaratorInfo, CastTy,Loc, Result.get()); } @@ -289,21 +305,31 @@ SourceLocation Loc = Tok.getLocation(); CXXScopeSpec SS; Next.startToken(); - Next.setKind(tok::period); + if (LHSisMemberAccess) + Next.setKind(tok::period); + else + Next.setKind(tok::equal); LHSToken.startToken(); LHSToken.setLocation(Loc); LHSToken.setIdentifierInfo(LHSII); - LHSToken.setKind(tok::annot_primary_expr); - ExternalSpace::StatementFilterCCC Validator(Next); - Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, LHSII, Loc, Next, SS.isEmpty() ? &Validator : nullptr); - if (LHSToken.getKind() != tok::annot_primary_expr) { + LHSToken.setKind(tok::identifier); + ExternalSpace::StatementFilterCCC SFCCC(Next); + Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, LHSII, Loc, Next, SS.isEmpty() ? &SFCCC : nullptr); + if (Classification.getKind() == Sema::NC_ContextIndependentExpr) { setExprAnnotation(LHSToken, Classification.getExpression()); + LHSToken.setAnnotationEndLoc(Loc); + PP.AnnotateCachedTokens(LHSToken); + LHS = getExprAnnotation(LHSToken); + } else if (Classification.getKind() == Sema::NC_NonType) { + Tok.setKind(tok::annot_non_type); + setNonTypeAnnotation(Tok, Classification.getNonTypeDecl()); + Tok.setLocation(Loc); + Tok.setAnnotationEndLoc(Loc); + NamedDecl *ND = getNonTypeAnnotation(Tok); + LHS = Actions.ActOnNameClassifiedAsNonType(getCurScope(), SS, ND, Loc, Tok); + } else { + assert(0); // may be error } - LHSToken.setAnnotationEndLoc(Loc); - PP.AnnotateCachedTokens(LHSToken); - - LHS = getExprAnnotation(LHSToken); - if (LHSisMemberAccess) LHS = LookupMemberAndBuildExpr(extraLHSII, LHS.get(), false); @@ -516,7 +542,7 @@ UnqualifiedId Name; CXXScopeSpec SS; SourceLocation TemplateKWLoc; - ExternalSpace::CastExpressionIdValidator Validator(false,true); + ExternalSpace::CastExpressionIdValidator Validator(Tok,false,true); Name.setIdentifier(II, Loc); return Actions.ActOnIdExpression(getCurScope(), SS, TemplateKWLoc, Name, false, IsAddressOfOperand, &Validator); } @@ -772,7 +798,7 @@ FnStmts.push_back(innerR.get()); ExprResult ljExpr,ljLHS; - ljExpr = AnonToExpr(CreateIdentifierInfo("__builtin_longjmp", Loc), tok::l_paren); + ljExpr = IIToExpr(CreateIdentifierInfo("__builtin_longjmp", Loc), tok::l_paren); ExprVector ljArgExprs; DeclSpec ljDS(AttrFactory); setTST(&ljDS, DeclSpec::TST_struct, structName); @@ -830,35 +856,25 @@ IITok.setLocation(Loc); IITok.setIdentifierInfo(II); IITok.setKind(tok::annot_primary_expr); + if (Classification.getKind() == Sema::NC_NonType) { + IITok.setKind(tok::annot_non_type); + setNonTypeAnnotation(IITok, Classification.getNonTypeDecl()); + IITok.setLocation(Loc); + IITok.setAnnotationEndLoc(Loc); + PP.AnnotateCachedTokens(IITok); + const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); + if (SS.isNotEmpty()) + AnnotateScopeToken(SS, !WasScopeAnnotation); + NamedDecl *ND = getNonTypeAnnotation(IITok); + ExprResult E; + return Actions.ActOnNameClassifiedAsNonType(getCurScope(), SS, ND, Loc, IITok); + } setExprAnnotation(IITok, Classification.getExpression()); IITok.setAnnotationEndLoc(Loc); PP.AnnotateCachedTokens(IITok); return getExprAnnotation(IITok); } -ExprResult Parser::AnonToExpr(IdentifierInfo *II, tok::TokenKind Kind){ - SourceLocation Loc = Tok.getLocation(); - Token Next,IITok; - Next.setKind(Kind); - ExternalSpace::StatementFilterCCC CCCValidator(Next); - CXXScopeSpec SS; - Sema::NameClassification Classification = Actions.ClassifyName(getCurScope(), SS, II, Loc, Next, &CCCValidator); - IITok.startToken(); - IITok.setKind(tok::annot_non_type); - setNonTypeAnnotation(IITok, Classification.getNonTypeDecl()); - IITok.setLocation(Loc); - IITok.setAnnotationEndLoc(Loc); - PP.AnnotateCachedTokens(IITok); - const bool WasScopeAnnotation = Tok.is(tok::annot_cxxscope); - if (SS.isNotEmpty()) - AnnotateScopeToken(SS, !WasScopeAnnotation); - NamedDecl *ND = getNonTypeAnnotation(IITok); - ExprResult E; - E = Actions.ActOnNameClassifiedAsNonType(getCurScope(), SS, ND, Loc, IITok); - setExprAnnotation(IITok, E); - return getExprAnnotation(IITok); -} - /// CreateComplexStmtRet - Create return value for complex statements. /// /// ({ /* some statements */