Skip to content

Commit

Permalink
Correct parse AST to AST mapping for two-element static ref paths
Browse files Browse the repository at this point in the history
Signed-off-by: Matthew Ballance <matt.ballance@gmail.com>
  • Loading branch information
mballance committed Nov 9, 2023
1 parent 108233a commit c4df554
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/.antlr/PSSParser.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from /project/fun/zuspec/zuspec-fe-parser/packages/zuspec-parser/src/PSSParser.g4 by ANTLR 4.13.1
// Generated from /project/fun/zuspec/zuspec-parser/src/PSSParser.g4 by ANTLR 4.13.1
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.dfa.DFA;
import org.antlr.v4.runtime.*;
Expand Down
45 changes: 40 additions & 5 deletions src/AstBuilderInt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ antlrcpp::Any AstBuilderInt::visitIdentifier(PSSParser::IdentifierContext *ctx)
if (ctx->ESCAPED_ID()) {
id = m_factory->mkExprId(ctx->ESCAPED_ID()->getText(), true);
} else {
DEBUG("visitIdentifier: %s", ctx->ID()->getText().c_str());
id = m_factory->mkExprId(ctx->ID()->getText(), false);
}

Expand Down Expand Up @@ -2272,10 +2273,12 @@ ast::IFunctionParamDecl *AstBuilderInt::mkFunctionParamDecl(PSSParser::Function_

IExprId *AstBuilderInt::mkId(PSSParser::IdentifierContext *ctx) {
IExprId *id;


if (ctx->ESCAPED_ID()) {
id = m_factory->mkExprId(ctx->ESCAPED_ID()->getText(), true);
} else {
DEBUG("mkId: %s", ctx->ID()->getText().c_str());
id = m_factory->mkExprId(ctx->ID()->getText(), false);
}

Expand Down Expand Up @@ -2382,6 +2385,14 @@ ast::ITypeIdentifierElem *AstBuilderInt::mkTypeIdElem(
return elem;
}

ast::ITypeIdentifierElem *AstBuilderInt::mkTypeIdElem(
PSSParser::IdentifierContext *ctx) {
ast::ITypeIdentifierElem *elem = m_factory->mkTypeIdentifierElem(
mkId(ctx),
0);
return elem;
}

ast::IExpr *AstBuilderInt::mkExpr(
PSSParser::ExpressionContext *ctx) {
m_expr = 0;
Expand Down Expand Up @@ -2423,23 +2434,47 @@ ast::IExprRefPath *AstBuilderInt::mkExprRefPath(

ret = ref;
} else {
/*
* ref_path:
* static_ref_path ( TOK_DOT hierarchical_id )? bit_slice? // <-- We're here
* | (is_super=TOK_SUPER TOK_DOT)? hierarchical_id bit_slice?
*
* static_ref_path:
* static_ref_path_prefix (type_identifier_elem TOK_DOUBLE_COLON )* member_path_elem
*
* static_ref_path_prefix:
* (type_identifier_elem TOK_DOUBLE_COLON)
* | is_global=TOK_DOUBLE_COLON
*
* member_path_elem:
* identifier function_parameter_list? ( TOK_LSBRACE expression TOK_RSBRACE )?
*/

DEBUG("!hierarchical_id: ");
std::vector<PSSParser::Type_identifier_elemContext *> items =
ctx->static_ref_path()->type_identifier_elem();
if (!ctx->static_ref_path()->static_ref_path_prefix()->is_global && items.size() == 0 &&
!ctx->static_ref_path()->member_path_elem()->function_parameter_list()) {
// This is just a simple identifier reference
ast::IExprRefPathId *ref = m_factory->mkExprRefPathId(
mkId(ctx->static_ref_path()->member_path_elem()->identifier())
);
DEBUG("case1");
// static_ref_path_prefix member_path_elem
DEBUG("Non-function static reference");
ast::IExprRefPathStatic *ref = m_factory->mkExprRefPathStatic(false);
ref->getBase().push_back(ast::ITypeIdentifierElemUP(
mkTypeIdElem(ctx->static_ref_path()->static_ref_path_prefix()->type_identifier_elem())
));
ref->getBase().push_back(ast::ITypeIdentifierElemUP(
mkTypeIdElem(ctx->static_ref_path()->member_path_elem()->identifier())
));

if (ctx->bit_slice()) {
ref->setSlice(mkExprBitSlice(ctx->bit_slice()));
}

ret = ref;
} else {
// This is a multi-element path
DEBUG("case2 (multi-element path)");
// static_ref_path_prefix type_identifier_elem+ member_path_elem

ast::IExprRefPathStatic *ref = m_factory->mkExprRefPathStatic(
ctx->static_ref_path()->static_ref_path_prefix()->is_global
);
Expand Down
3 changes: 3 additions & 0 deletions src/AstBuilderInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ class AstBuilderInt :
ast::ITypeIdentifierElem *mkTypeIdElem(
PSSParser::Type_identifier_elemContext *ctx);

ast::ITypeIdentifierElem *mkTypeIdElem(
PSSParser::IdentifierContext *ctx);

ast::IExpr *mkExpr(
PSSParser::ExpressionContext *ctx);

Expand Down
49 changes: 43 additions & 6 deletions src/include/zsp/parser/impl/TaskCopyAst.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,20 @@ class TaskCopyAst : public ast::VisitorBase {

virtual void visitConstraintStmt(ast::IConstraintStmt *i) { }

virtual void visitExprRefPathElem(ast::IExprRefPathElem *i) { }
virtual void visitExprRefPathElem(ast::IExprRefPathElem *i) {
DEBUG_ENTER("visitExprRefPathElem");
DEBUG("TODO: visitExprRefPathElem");
DEBUG_LEAVE("visitExprRefPathElem");
}

virtual void visitExprStaticRefPath(ast::IExprStaticRefPath *i) {
DEBUG_ENTER("visitExprStaticRefPath");
ast::IExprStaticRefPath *ic = m_factory->mkExprStaticRefPath(
i->getIs_global(),
copyT<ast::IExprMemberPathElem>(i->getLeaf())
);
m_expr = ic;
DEBUG_LEAVE("visitExprStaticRefPath");
}

virtual void visitExprString(ast::IExprString *i) {
Expand Down Expand Up @@ -352,20 +358,27 @@ class TaskCopyAst : public ast::VisitorBase {

virtual void visitExprOpenRangeValue(ast::IExprOpenRangeValue *i) { }

virtual void visitExprRefPath(ast::IExprRefPath *i) { }
virtual void visitExprRefPath(ast::IExprRefPath *i) {
DEBUG_ENTER("visitExprRefPath");
DEBUG("TODO: visitExprRefPath");
DEBUG_LEAVE("visitExprRefPath");
}

virtual void visitExprRefPathId(ast::IExprRefPathId *i) {
DEBUG_ENTER("visitExprRefPathId");
ast::IExprRefPathId *ic = m_factory->mkExprRefPathId(
copyT<ast::IExprId>(i->getId()));
if (i->getSlice()) {
ic->setSlice(copyT<ast::IExprBitSlice>(i->getSlice()));
}
m_expr = ic;
DEBUG_LEAVE("visitExprRefPathId");
}

virtual void visitConstraintScope(ast::IConstraintScope *i) { }

virtual void visitExprRefPathContext(ast::IExprRefPathContext *i) {
DEBUG_ENTER("visitExprRefPathContext");
ast::IExprRefPathContext *ic = m_factory->mkExprRefPathContext(
copyT<ast::IExprHierarchicalId>(i->getHier_id())
);
Expand All @@ -374,6 +387,7 @@ class TaskCopyAst : public ast::VisitorBase {
ic->setSlice(copyT<ast::IExprBitSlice>(i->getSlice()));
}
m_expr = ic;
DEBUG_LEAVE("visitExprRefPathContext");
}

virtual void visitConstraintStmtExpr(ast::IConstraintStmtExpr *i) {
Expand All @@ -382,8 +396,21 @@ class TaskCopyAst : public ast::VisitorBase {
);
}

virtual void visitExprRefPathStatic(ast::IExprRefPathStatic *i) {

virtual void visitExprRefPathStatic(ast::IExprRefPathStatic *i) {
DEBUG_ENTER("visitExprRefPathStatic");
ast::IExprRefPathStatic *ic = m_factory->mkExprRefPathStatic(
i->getIs_global());
for (std::vector<ast::ITypeIdentifierElemUP>::const_iterator
it=i->getBase().begin();
it!=i->getBase().end(); it++) {
ic->getBase().push_back(ast::ITypeIdentifierElemUP(
copyT<ast::ITypeIdentifierElem>(it->get())));
}
if (i->getSlice()) {
ic->setSlice(copyT<ast::IExprBitSlice>(i->getSlice()), true);
}
m_expr = ic;
DEBUG_LEAVE("visitExprRefPathStatic");
}

virtual void visitExprSignedNumber(ast::IExprSignedNumber *i) {
Expand All @@ -405,11 +432,13 @@ class TaskCopyAst : public ast::VisitorBase {
virtual void visitConstraintStmtField(ast::IConstraintStmtField *i) { }

virtual void visitExprRefPathStaticRooted(ast::IExprRefPathStaticRooted *i) {
DEBUG_ENTER("visitExprRefPathStaticRooted");
ast::IExprRefPathStaticRooted *ic = m_factory->mkExprRefPathStaticRooted(
copyT<ast::IExprStaticRefPath>(i->getRoot()),
(i->getLeaf())?copyT<ast::IExprHierarchicalId>(i->getLeaf()):0
);
m_expr = ic;
DEBUG_LEAVE("visitExprRefPathStaticRooted");
}

virtual void visitConstraintStmtIf(ast::IConstraintStmtIf *i) {
Expand Down Expand Up @@ -607,11 +636,19 @@ class TaskCopyAst : public ast::VisitorBase {

virtual void visitConstraintStmtForeach(ast::IConstraintStmtForeach *i) { }

virtual void visitExprRefPathStaticFunc(ast::IExprRefPathStaticFunc *i) { }
virtual void visitExprRefPathStaticFunc(ast::IExprRefPathStaticFunc *i) {
DEBUG_ENTER("visitExprRefPathStaticFunc");
DEBUG("TODO: visitExprRefPathStaticFunc");
DEBUG_LEAVE("visitExprRefPathStaticFunc");
}

virtual void visitConstraintStmtForall(ast::IConstraintStmtForall *i) { }

virtual void visitExprRefPathSuper(ast::IExprRefPathSuper *i) { }
virtual void visitExprRefPathSuper(ast::IExprRefPathSuper *i) {
DEBUG_ENTER("visitExprRefPathSuper");
DEBUG("TODO: visitExprRefPathSuper");
DEBUG_LEAVE("visitExprRefPathSuper");
}

virtual void visitConstraintStmtImplication(ast::IConstraintStmtImplication *i) { }

Expand Down

0 comments on commit c4df554

Please sign in to comment.