Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion analyze/aps-dnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,10 +455,20 @@ static void *count_if_rules(void *pint, void *node) {
++*count;
break;
case KEYcase_stmt:
{
for (m = first_Match(case_stmt_matchers(decl)); m; m=MATCH_NEXT(m)) {
Match_info(m)->if_index = *count;
++*count;
}
}
break;
case KEYfor_stmt:
{
for (m = first_Match(for_stmt_matchers(decl)); m; m=MATCH_NEXT(m)) {
Match_info(m)->if_index = *count;
++*count;
}
}
break;
default: break;
}
Expand All @@ -477,7 +487,12 @@ static void *get_if_rules(void *varray, void *node) {
array[Declaration_info(decl)->if_index] = decl;
break;
case KEYcase_stmt:
for (m = first_Match(case_stmt_matchers(decl)); m; m=MATCH_NEXT(m)) {
for (m = first_Match(case_stmt_matchers(decl)); m; m=MATCH_NEXT(m)) {
array[Match_info(m)->if_index] = m;
}
break;
case KEYfor_stmt:
for (m = first_Match(for_stmt_matchers(decl)); m; m=MATCH_NEXT(m)) {
array[Match_info(m)->if_index] = m;
}
break;
Expand Down
65 changes: 50 additions & 15 deletions aps2scala/dump-scala.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ void dump_debug_end(ostream& os)
// Output Scala pattern for APS pattern

int formal_count = 0;
static void dump_pattern_call(Pattern p, Pattern result, const char* resultS, ostream& os)
static void dump_Pattern_impl(Pattern p, ostream& os, std::vector<Expression> *conditions);

static void dump_pattern_call(Pattern p, Pattern result, const char* resultS, ostream& os, std::vector<Expression> *conditions = 0)
{
Pattern pf = pattern_call_func(p);
PatternActuals pactuals = pattern_call_actuals(p);
Expand All @@ -286,20 +288,44 @@ static void dump_pattern_call(Pattern p, Pattern result, const char* resultS, os
dump_Use(pfuse,"p_",os);
os << "(";
if (result) {
dump_Pattern(result,os);
dump_Pattern_impl(result,os,conditions);
} else {
os << resultS;
}
formal_count = 0;
for (Pattern pa = first_PatternActual(pactuals); pa ; pa = PAT_NEXT(pa)) {
os << ",";
dump_Pattern(pa,os);
dump_Pattern_impl(pa,os,conditions);
formal_count++;
}
os << ")";
}

void dump_Pattern(Pattern p, ostream& os)
void dump_sequence_element_pattern(Pattern p, ostream& os)
{
std::vector<Expression> conditions;
dump_Pattern_impl(p,os,&conditions);
if (!conditions.empty()) {
os << " if ";
bool started = false;
for (Expression condition : conditions) {
if (started) os << " && ";
else started = true;
dump_Expression(condition,os);
}
}
}

void dump_sequence_elements(Pattern p, Expression value, ostream& os)
{
Pattern pf = pattern_call_func(p);
dump_Use(pattern_use_use(pf),"p_",os);
os << ".unapplySeq(";
dump_Expression(value,os);
os << ").get._2";
}

static void dump_Pattern_impl(Pattern p, ostream& os, std::vector<Expression> *conditions)
{
switch (Pattern_KEY(p)) {
default:
Expand All @@ -313,14 +339,14 @@ void dump_Pattern(Pattern p, ostream& os)
break;
case KEYpattern_var: break;
}
dump_Pattern(match_pattern_pat(p),os);
dump_Pattern_impl(match_pattern_pat(p),os,conditions);
os << ":";
dump_Type(match_pattern_type(p),os);
break;

case KEYpattern_call:
{
dump_pattern_call(p,((Pattern)0),"_",os);
dump_pattern_call(p,((Pattern)0),"_",os,conditions);
}
break;

Expand All @@ -342,18 +368,22 @@ void dump_Pattern(Pattern p, ostream& os)
switch (Pattern_KEY(and_pattern_p2(p))) {
default: break;
case KEYcondition:
dump_Pattern(and_pattern_p1(p),os);
os << " if ";
dump_Expression(condition_e(and_pattern_p2(p)),os);
dump_Pattern_impl(and_pattern_p1(p),os,conditions);
if (conditions) {
conditions->push_back(condition_e(and_pattern_p2(p)));
} else {
os << " if ";
dump_Expression(condition_e(and_pattern_p2(p)),os);
}
return;
case KEYpattern_call:
dump_pattern_call(and_pattern_p2(p),and_pattern_p1(p),"",os);
dump_pattern_call(and_pattern_p2(p),and_pattern_p1(p),"",os,conditions);
return;
}
os << "P_AND(";
dump_Pattern(and_pattern_p1(p),os);
dump_Pattern_impl(and_pattern_p1(p),os,conditions);
os << ",";
dump_Pattern(and_pattern_p2(p),os);
dump_Pattern_impl(and_pattern_p2(p),os,conditions);
os << ")";
break;

Expand All @@ -379,6 +409,11 @@ void dump_Pattern(Pattern p, ostream& os)
}
}

void dump_Pattern(Pattern p, ostream& os)
{
dump_Pattern_impl(p,os,0);
}


bool type_is_syntax(Type t)
{
Expand Down Expand Up @@ -1989,11 +2024,11 @@ void dump_scala_Declaration(Declaration decl,ostream& oss)
case KEYattribute_decl:
break; // handled by module

case KEYfunction_decl:
case KEYsome_function_decl:
{
Type fty = function_decl_type(decl);
Type fty = some_function_decl_type(decl);
Declaration rdecl = first_Declaration(function_type_return_values(fty));
Block b = function_decl_body(decl);
Block b = some_function_decl_body(decl);
Declaration mdecl = get_enclosing_some_class_decl(decl);
bool override_needed = false;
if (mdecl != NULL) {
Expand Down
2 changes: 2 additions & 0 deletions aps2scala/dump-scala.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void dump_debug_end(ostream& os);
// these two must always be called in pairs: the first
// leaves information around for the second:
void dump_Pattern(Pattern p, ostream&);
void dump_sequence_element_pattern(Pattern p, ostream&);
void dump_sequence_elements(Pattern p, Expression value, ostream&);

// override <<
ostream& operator<<(ostream&o,Symbol s);
Expand Down
101 changes: 87 additions & 14 deletions aps2scala/dyn-impl.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <stdint.h>
#include <sstream>
#include <stack>
#include <vector>
Expand Down Expand Up @@ -296,8 +297,7 @@ void dump_local_decl(void *, Declaration local, ostream& o)
dump_Expression(simple_value(init),o);
break;
default:
aps_error(local,"Can only handle initialized locals");
o << "0";
o << "null.asInstanceOf[" << value_decl_type(local) << "]";
}
o << ";\n";
}
Expand All @@ -313,6 +313,60 @@ void dump_Matches(Matches ms, bool exclusive, ASSIGNFUNC f, void*arg, ostream&os
);
}

static void dump_sequence_case(Declaration d, Match match, Pattern middle,
ASSIGNFUNC f, void *arg, ostream& os)
{
unsigned sequence_number = (unsigned)(uintptr_t)match;
activate_attr_context(os);
os << indent() << "{\n";
++nesting_level;
os << indent() << "val sequenceMatch" << sequence_number << " = ";
dump_sequence_elements(matcher_pat(match),case_stmt_expr(d),os);
os << ".collectFirst {\n";
++nesting_level;
os << indent() << "case ";
dump_sequence_element_pattern(middle,os);
os << " => {\n";
++nesting_level;
dump_Block(matcher_body(match),f,arg,os);
os << indent() << "()\n";
--nesting_level;
os << indent() << "}\n";
--nesting_level;
os << indent() << "}\n";
os << indent() << "if (sequenceMatch" << sequence_number << ".isEmpty) {\n";
++nesting_level;
dump_Block(case_stmt_default(d),f,arg,os);
--nesting_level;
os << indent() << "}\n";
--nesting_level;
os << indent() << "}\n";
}

static void dump_sequence_for(Declaration d, Match match, Pattern middle,
ASSIGNFUNC f, void *arg, ostream& os)
{
activate_attr_context(os);
os << indent();
dump_sequence_elements(matcher_pat(match),for_stmt_expr(d),os);
os << ".foreach { v_sequence_element =>\n";
++nesting_level;
os << indent() << "v_sequence_element match {\n";
++nesting_level;
os << indent() << "case ";
dump_sequence_element_pattern(middle,os);
os << " => {\n";
++nesting_level;
dump_Block(matcher_body(match),f,arg,os);
--nesting_level;
os << indent() << "}\n";
os << indent() << "case _ => {}\n";
--nesting_level;
os << indent() << "}\n";
--nesting_level;
os << indent() << "}\n";
}

void dump_Block(Block b,ASSIGNFUNC f,void*arg,ostream&os)
{
FOR_SEQUENCE
Expand All @@ -335,18 +389,37 @@ void dump_Block(Block b,ASSIGNFUNC f,void*arg,ostream&os)
pop_attr_context(os);
break;
case KEYcase_stmt:
push_attr_context(d);
//!! we implement case and for!!
dump_Matches(case_stmt_matchers(d),true,f,arg,os);
push_attr_context(case_stmt_default(d));
dump_Block(case_stmt_default(d),f,arg,os);
pop_attr_context(os);
pop_attr_context(os);
{
Match match;
Pattern middle;
if (sequence_search_matcher(d,&match,&middle) &&
(block_assigns_to(matcher_body(match),arg) ||
block_assigns_to(case_stmt_default(d),arg))) {
dump_sequence_case(d,match,middle,f,arg,os);
} else {
push_attr_context(d);
//!! we implement case and for!!
dump_Matches(case_stmt_matchers(d),true,f,arg,os);
push_attr_context(case_stmt_default(d));
dump_Block(case_stmt_default(d),f,arg,os);
pop_attr_context(os);
pop_attr_context(os);
}
}
break;
case KEYfor_stmt:
push_attr_context(d);
dump_Matches(for_stmt_matchers(d),false,f,arg,os);
pop_attr_context(os);
{
Match match;
Pattern middle;
if (sequence_search_matcher(d,&match,&middle) &&
block_assigns_to(matcher_body(match),arg)) {
dump_sequence_for(d,match,middle,f,arg,os);
} else {
push_attr_context(d);
dump_Matches(for_stmt_matchers(d),false,f,arg,os);
pop_attr_context(os);
}
}
break;
case KEYvalue_decl:
if (!(Declaration_info(d)->decl_flags & LOCAL_ATTRIBUTE_FLAG) &&
Expand Down Expand Up @@ -720,9 +793,9 @@ class Dynamic : public Implementation
}

void implement_function_body(Declaration f, ostream& os) {
Type fty = function_decl_type(f);
Type fty = some_function_decl_type(f);
Declaration rdecl = first_Declaration(function_type_return_values(fty));
Block b = function_decl_body(f);
Block b = some_function_decl_body(f);
bool is_col = direction_is_collection(value_decl_direction(rdecl));
const char *name = decl_name(f);

Expand Down
Loading
Loading