Skip to content

Commit 4fe44e8

Browse files
Daiva Naudziunienetautschnig
Daiva Naudziuniene
authored andcommitted
Improvements to Jsil type checking
Catch decl in the try/catch is not used. Making string constants to be ID_constant expressions with string_typet Typecheking function call. Added "nan" symbol. Assigning type to lhs after unknown function call. White spaces, positioning { in new line. Cleaning up the code. Correct includes. Renaming jsil types. Explicit constructors. Inlining expressions if used only once. Asserting that expression does not have type set just yet. More efficient implementation for is_subtype. Introduced jsil_incompatible_types to hide low level implementation. Fixed bug in is_subtype. Typechecking throw and return labels. Typechecking bitwise operators. Avoiding duplicate type-checking by marking symbols as type-checked. Add empty decl symbol. Special case for index expression. Clean up. Converting all procedure declarations to symbols before typechecking.
1 parent 45b619e commit 4fe44e8

11 files changed

+860
-371
lines changed

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: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ void jsil_internal_additions(symbol_tablet &dest)
4141
symbol.is_lvalue=true;
4242
symbol.is_state_var=true;
4343
symbol.is_thread_local=true;
44+
// mark as already typechecked
45+
symbol.is_extern=true;
4446
dest.add(symbol);
4547
}
4648

@@ -55,6 +57,8 @@ void jsil_internal_additions(symbol_tablet &dest)
5557
symbol.is_lvalue=true;
5658
symbol.is_state_var=true;
5759
symbol.is_thread_local=true;
60+
// mark as already typechecked
61+
symbol.is_extern=true;
5862
dest.add(symbol);
5963
}
6064

@@ -73,28 +77,57 @@ void jsil_internal_additions(symbol_tablet &dest)
7377
dest.add(symbol);
7478
}
7579

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+
76106
// add builtin objects
77107
const std::vector<std::string> builtin_objects=
78-
{ "#lg", "#lg_isNan", "#lg_isFinite", "#lop", "#lop_toString",
79-
"#lop_valueOf", "#lop_isPrototypeOf", "#lfunction", "#lfp",
80-
"#leval", "#lerror", "#lep", "#lrerror", "#lrep", "#lterror",
81-
"#ltep", "#lserror", "#lsep", "#levalerror", "#levalerrorp",
82-
"#lrangeerror", "#lrangeerrorp", "#lurierror", "#lurierrorp",
83-
"#lobject", "#lobject_get_prototype_of", "#lboolean", "#lbp",
84-
"#lbp_toString", "#lbp_valueOf", "#lnumber", "#lnp",
85-
"#lnp_toString", "#lnp_valueOf", "#lmath", "#lstring", "#lsp",
86-
"#lsp_toString", "#lsp_valueOf", "#larray", "#lap", "#ljson"
87-
};
88-
89-
for(const auto & identifier : 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)
90121
{
91122
symbolt new_symbol;
92123
new_symbol.name=identifier;
93-
new_symbol.type=jsil_builtin_objectt();
124+
new_symbol.type=jsil_builtin_object_type();
94125
new_symbol.base_name=identifier;
95126
new_symbol.mode="jsil";
96127
new_symbol.is_type=false;
97128
new_symbol.is_lvalue=false;
129+
// mark as already typechecked
130+
new_symbol.is_extern=true;
98131
dest.add(new_symbol);
99132
}
100133
}

src/jsil/jsil_language.cpp

Lines changed: 21 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:
@@ -128,12 +149,6 @@ 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-
jsil_internal_additions(symbol_table);
135-
136-
// now typecheck
137152
if(jsil_typecheck(symbol_table, get_message_handler()))
138153
return true;
139154

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void jsil_declarationt::to_symbol(symbolt &symbol) const
7373
irep_idt proc_type=s.get("proc_type");
7474

7575
if(proc_type=="builtin")
76-
symbol_type=jsil_builtin_code_typet(symbol_type);
76+
symbol_type=jsil_builtin_code_typet(symbol_type);
7777
else if(proc_type=="spec")
7878
symbol_type=jsil_spec_code_typet(symbol_type);
7979

src/jsil/jsil_parse_tree.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ class jsil_declarationt:public exprt
4747
add(ID_return).set(ID_label, label);
4848
}
4949

50-
const irep_idt &returns_value() const {
51-
return find(ID_return).get(ID_value);
50+
const irep_idt &returns_value() const
51+
{
52+
return find(ID_return).get(ID_value);
5253
}
5354

54-
const irep_idt &returns_label() const {
55-
return find(ID_return).get(ID_label);
55+
const irep_idt &returns_label() const
56+
{
57+
return find(ID_return).get(ID_label);
5658
}
5759

5860
void add_throws(
@@ -63,25 +65,29 @@ class jsil_declarationt:public exprt
6365
add(ID_throw).set(ID_label, label);
6466
}
6567

66-
const irep_idt &throws_value() const {
67-
return find(ID_throw).get(ID_value);
68+
const irep_idt &throws_value() const
69+
{
70+
return find(ID_throw).get(ID_value);
6871
}
6972

70-
const irep_idt &throws_label() const {
71-
return find(ID_throw).get(ID_label);
73+
const irep_idt &throws_label() const
74+
{
75+
return find(ID_throw).get(ID_label);
7276
}
7377

7478
void add_value(const code_blockt &code)
7579
{
7680
add(ID_value, code);
7781
}
7882

79-
const code_blockt &value() const {
80-
return static_cast<const code_blockt &> (find(ID_value));
83+
const code_blockt &value() const
84+
{
85+
return static_cast<const code_blockt &>(find(ID_value));
8186
}
8287

83-
code_blockt &value() {
84-
return static_cast<code_blockt &> (add(ID_value));
88+
code_blockt &value()
89+
{
90+
return static_cast<code_blockt &>(add(ID_value));
8591
}
8692

8793
void to_symbol(symbolt &symbol) const;

0 commit comments

Comments
 (0)