Skip to content

Commit 868a6b3

Browse files
author
Daniel Kroening
committed
Merge pull request #91 from tautschnig/jsil-type-checking
Started type checking jsil programs
2 parents 16f8b29 + 5435ed5 commit 868a6b3

14 files changed

+2051
-28
lines changed

src/goto-analyzer/taint_analysis.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ void taint_analysist::instrument(
134134
{
135135
bool match=false;
136136
for(const auto & i : identifiers)
137-
if(has_prefix(id2string(i), "java::"+id2string(rule.function_identifier)+":"))
137+
if(has_prefix(id2string(i), "java::"+id2string(rule.function_identifier)+":") ||
138+
id2string(i)==id2string(rule.function_identifier))
138139
{
139140
match=true;
140141
break;

src/goto-programs/goto_convert_functions.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ void goto_convert_functionst::goto_convert()
8080
it->second.type.id()==ID_code &&
8181
(it->second.mode==ID_C ||
8282
it->second.mode==ID_cpp ||
83-
it->second.mode==ID_java))
83+
it->second.mode==ID_java ||
84+
it->second.mode=="jsil"))
8485
symbol_list.push_back(it->first);
8586
}
8687

src/jsil/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
SRC = jsil_y.tab.cpp jsil_lex.yy.cpp \
2-
jsil_convert.cpp jsil_language.cpp jsil_parse_tree.cpp \
2+
jsil_convert.cpp jsil_language.cpp jsil_parse_tree.cpp jsil_types.cpp \
33
jsil_parser.cpp jsil_typecheck.cpp expr2jsil.cpp \
44
jsil_entry_point.cpp jsil_internal_additions.cpp
55

src/jsil/jsil_convert.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ bool jsil_convertt::operator()(const jsil_parse_treet &parse_tree)
5656
if(convert_code(new_symbol, to_code(new_symbol.value)))
5757
return true;
5858

59+
if(symbol_table.has_symbol(new_symbol.name))
60+
{
61+
symbolt &s=symbol_table.lookup(new_symbol.name);
62+
if(s.value.id()=="no-body-just-yet")
63+
{
64+
symbol_table.remove(s.name);
65+
}
66+
}
5967
if(symbol_table.add(new_symbol))
6068
{
6169
throw "duplicate symbol "+id2string(new_symbol.name);
@@ -104,7 +112,8 @@ bool jsil_convertt::convert_code(const symbolt &symbol, codet &code)
104112

105113
code_try_catcht t_c;
106114
t_c.try_code().swap(t);
107-
code_declt d(symbol.symbol_expr());
115+
// Adding empty symbol to catch decl
116+
code_declt d(symbol_exprt("decl_symbol"));
108117
t_c.add_catch(d, g);
109118
t_c.add_source_location()=code.source_location();
110119

src/jsil/jsil_internal_additions.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Author: Michael Tautschnig, [email protected]
1212

1313
#include <ansi-c/c_types.h>
1414

15+
#include "jsil_types.h"
16+
1517
#include "jsil_internal_additions.h"
1618

1719
/*******************************************************************\
@@ -39,6 +41,8 @@ void jsil_internal_additions(symbol_tablet &dest)
3941
symbol.is_lvalue=true;
4042
symbol.is_state_var=true;
4143
symbol.is_thread_local=true;
44+
// mark as already typechecked
45+
symbol.is_extern=true;
4246
dest.add(symbol);
4347
}
4448

@@ -53,6 +57,8 @@ void jsil_internal_additions(symbol_tablet &dest)
5357
symbol.is_lvalue=true;
5458
symbol.is_state_var=true;
5559
symbol.is_thread_local=true;
60+
// mark as already typechecked
61+
symbol.is_extern=true;
5662
dest.add(symbol);
5763
}
5864

@@ -70,4 +76,58 @@ void jsil_internal_additions(symbol_tablet &dest)
7076
symbol.mode="jsil";
7177
dest.add(symbol);
7278
}
79+
80+
// add nan
81+
82+
{
83+
symbolt symbol;
84+
symbol.base_name="nan";
85+
symbol.name="nan";
86+
symbol.type=floatbv_typet();
87+
symbol.mode="jsil";
88+
// mark as already typechecked
89+
symbol.is_extern=true;
90+
dest.add(symbol);
91+
}
92+
93+
// add empty symbol used for decl statemements
94+
95+
{
96+
symbolt symbol;
97+
symbol.base_name="decl_symbol";
98+
symbol.name="decl_symbol";
99+
symbol.type=empty_typet();
100+
symbol.mode="jsil";
101+
// mark as already typechecked
102+
symbol.is_extern=true;
103+
dest.add(symbol);
104+
}
105+
106+
// add builtin objects
107+
const std::vector<std::string> builtin_objects=
108+
{
109+
"#lg", "#lg_isNan", "#lg_isFinite", "#lop", "#lop_toString",
110+
"#lop_valueOf", "#lop_isPrototypeOf", "#lfunction", "#lfp",
111+
"#leval", "#lerror", "#lep", "#lrerror", "#lrep", "#lterror",
112+
"#ltep", "#lserror", "#lsep", "#levalerror", "#levalerrorp",
113+
"#lrangeerror", "#lrangeerrorp", "#lurierror", "#lurierrorp",
114+
"#lobject", "#lobject_get_prototype_of", "#lboolean", "#lbp",
115+
"#lbp_toString", "#lbp_valueOf", "#lnumber", "#lnp",
116+
"#lnp_toString", "#lnp_valueOf", "#lmath", "#lstring", "#lsp",
117+
"#lsp_toString", "#lsp_valueOf", "#larray", "#lap", "#ljson"
118+
};
119+
120+
for(const auto &identifier : builtin_objects)
121+
{
122+
symbolt new_symbol;
123+
new_symbol.name=identifier;
124+
new_symbol.type=jsil_builtin_object_type();
125+
new_symbol.base_name=identifier;
126+
new_symbol.mode="jsil";
127+
new_symbol.is_type=false;
128+
new_symbol.is_lvalue=false;
129+
// mark as already typechecked
130+
new_symbol.is_extern=true;
131+
dest.add(new_symbol);
132+
}
73133
}

src/jsil/jsil_language.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,27 @@ void jsil_languaget::modules_provided(std::set<std::string> &modules)
5656

5757
/*******************************************************************\
5858
59+
Function: jsil_languaget::interfaces
60+
61+
Inputs:
62+
63+
Outputs:
64+
65+
Purpose: Adding symbols for all procedure declarations
66+
67+
\*******************************************************************/
68+
69+
bool jsil_languaget::interfaces(symbol_tablet &symbol_table)
70+
{
71+
if(jsil_convert(parse_tree, symbol_table, get_message_handler()))
72+
return true;
73+
74+
jsil_internal_additions(symbol_table);
75+
return false;
76+
}
77+
78+
/*******************************************************************\
79+
5980
Function: jsil_languaget::preprocess
6081
6182
Inputs:
@@ -120,18 +141,14 @@ Function: jsil_languaget::typecheck
120141
121142
Outputs:
122143
123-
Purpose:
144+
Purpose: Converting from parse tree and type checking.
124145
125146
\*******************************************************************/
126147

127148
bool jsil_languaget::typecheck(
128149
symbol_tablet &symbol_table,
129150
const std::string &module)
130151
{
131-
if(jsil_convert(parse_tree, symbol_table, get_message_handler()))
132-
return true;
133-
134-
// now typecheck
135152
if(jsil_typecheck(symbol_table, get_message_handler()))
136153
return true;
137154

@@ -152,7 +169,6 @@ Function: jsil_languaget::final
152169

153170
bool jsil_languaget::final(symbol_tablet &symbol_table)
154171
{
155-
jsil_internal_additions(symbol_table);
156172

157173
if(jsil_entry_point(
158174
symbol_table,

src/jsil/jsil_language.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class jsil_languaget:public languaget
6262
virtual std::set<std::string> extensions() const;
6363

6464
virtual void modules_provided(std::set<std::string> &modules);
65+
virtual bool interfaces(symbol_tablet &symbol_table);
6566

6667
protected:
6768
jsil_parse_treet parse_tree;

src/jsil/jsil_parse_tree.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Author: Michael Tautschnig, [email protected]
88

99
#include <util/symbol.h>
1010

11+
#include "jsil_types.h"
12+
1113
#include "jsil_parse_tree.h"
1214

1315
/*******************************************************************\
@@ -66,10 +68,19 @@ void jsil_declarationt::to_symbol(symbolt &symbol) const
6668
symbol_exprt s(to_symbol_expr(
6769
static_cast<const exprt&>(find(ID_declarator))));
6870

71+
code_typet symbol_type=to_code_type(s.type());
72+
73+
irep_idt proc_type=s.get("proc_type");
74+
75+
if(proc_type=="builtin")
76+
symbol_type=jsil_builtin_code_typet(symbol_type);
77+
else if(proc_type=="spec")
78+
symbol_type=jsil_spec_code_typet(symbol_type);
79+
6980
symbol.name=s.get_identifier();
7081
symbol.base_name=s.get_identifier();
7182
symbol.mode="jsil";
72-
symbol.type=s.type();
83+
symbol.type=symbol_type;
7384
symbol.location=s.source_location();
7485

7586
code_blockt code(to_code_block(
@@ -133,4 +144,3 @@ void jsil_parse_treet::output(std::ostream &out) const
133144
out << "\n";
134145
}
135146
}
136-

src/jsil/jsil_parse_tree.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class jsil_declarationt:public exprt
2929
add(ID_declarator, expr);
3030
}
3131

32+
const symbol_exprt &declarator() const
33+
{
34+
return static_cast<const symbol_exprt &>(find(ID_declarator));
35+
}
36+
37+
symbol_exprt &declarator()
38+
{
39+
return static_cast<symbol_exprt &>(add(ID_declarator));
40+
}
41+
3242
void add_returns(
3343
const irep_idt &value,
3444
const irep_idt &label)
@@ -37,6 +47,16 @@ class jsil_declarationt:public exprt
3747
add(ID_return).set(ID_label, label);
3848
}
3949

50+
const irep_idt &returns_value() const
51+
{
52+
return find(ID_return).get(ID_value);
53+
}
54+
55+
const irep_idt &returns_label() const
56+
{
57+
return find(ID_return).get(ID_label);
58+
}
59+
4060
void add_throws(
4161
const irep_idt &value,
4262
const irep_idt &label)
@@ -45,11 +65,31 @@ class jsil_declarationt:public exprt
4565
add(ID_throw).set(ID_label, label);
4666
}
4767

68+
const irep_idt &throws_value() const
69+
{
70+
return find(ID_throw).get(ID_value);
71+
}
72+
73+
const irep_idt &throws_label() const
74+
{
75+
return find(ID_throw).get(ID_label);
76+
}
77+
4878
void add_value(const code_blockt &code)
4979
{
5080
add(ID_value, code);
5181
}
5282

83+
const code_blockt &value() const
84+
{
85+
return static_cast<const code_blockt &>(find(ID_value));
86+
}
87+
88+
code_blockt &value()
89+
{
90+
return static_cast<code_blockt &>(add(ID_value));
91+
}
92+
5393
void to_symbol(symbolt &symbol) const;
5494

5595
void output(std::ostream &) const;

0 commit comments

Comments
 (0)