From 0229e05728dcabdd7ce1a696c3db2dede65fd37c Mon Sep 17 00:00:00 2001 From: Minero Aoki Date: Sun, 31 Aug 2008 06:18:26 +0000 Subject: [PATCH] * net/loveruby/cflat/ast/Declarations.java: reject duplicated symbols to support duplicated import. * net/loveruby/cflat/compiler/LibraryLoader.java: should return previous loaded symbols for the same import file. * test: test duplicated import. git-svn-id: file:///Users/aamine/c/gitwork/public/cbc/trunk@3978 1b9489fe-b721-0410-924e-b54b9192deb8 --- ChangeLog | 10 ++++ ToDo | 9 +++- net/loveruby/cflat/ast/Declarations.java | 51 +++++++++++-------- .../cflat/compiler/LibraryLoader.java | 14 ++--- test/duplicated-import.cb | 9 ++++ test/test_cbc.sh | 26 ++++++---- 6 files changed, 78 insertions(+), 41 deletions(-) create mode 100644 test/duplicated-import.cb diff --git a/ChangeLog b/ChangeLog index eb442af..c39e088 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Sun Aug 31 15:17:05 2008 Minero Aoki + + * net/loveruby/cflat/ast/Declarations.java: reject duplicated + symbols to support duplicated import. + + * net/loveruby/cflat/compiler/LibraryLoader.java: should return + previous loaded symbols for the same import file. + + * test: test duplicated import. + Sat Aug 30 20:03:36 2008 Minero Aoki * net/loveruby/cflat/parser/Parser.jj: implement NULL. diff --git a/ToDo b/ToDo index 1b662a5..4ae5182 100644 --- a/ToDo +++ b/ToDo @@ -188,9 +188,16 @@ - call GNU as and GNU ld instead of gcc. - rename asm methods - lvar overlapped allocation +- pointer comparison + - ptr==ptr should work + - ptr&&ptr should work + - cond?ptr:ptr should work + - !ptr should work +- implement NULL as reserved word +- duplicated import did not propagate symbols * prohibit multi-dimension array without size * dump UTF-8 string byte by byte. -* simple vararg retrieve +* simple vararg retrieve (get vararg address) * easy peephole optimization * PIC generation * remove all FIXME diff --git a/net/loveruby/cflat/ast/Declarations.java b/net/loveruby/cflat/ast/Declarations.java index 519168a..51300ef 100644 --- a/net/loveruby/cflat/ast/Declarations.java +++ b/net/loveruby/cflat/ast/Declarations.java @@ -2,25 +2,32 @@ package net.loveruby.cflat.ast; import java.util.*; public class Declarations { - protected List defvars, vardecls, defuns, funcdecls; - protected List defstructs, defunions, typedefs; + protected Set defvars, vardecls, defuns, funcdecls; + protected Set defstructs, defunions, typedefs; public Declarations() { - defvars = new ArrayList(); - vardecls = new ArrayList(); - defuns = new ArrayList(); - funcdecls = new ArrayList(); - defstructs = new ArrayList(); - defunions = new ArrayList(); - typedefs = new ArrayList(); + defvars = new LinkedHashSet(); + vardecls = new LinkedHashSet(); + defuns = new LinkedHashSet(); + funcdecls = new LinkedHashSet(); + defstructs = new LinkedHashSet(); + defunions = new LinkedHashSet(); + typedefs = new LinkedHashSet(); } public void add(Declarations decls) { - vardecls.addAll(decls.vardecls()); - funcdecls.addAll(decls.funcdecls()); - defstructs.addAll(decls.defstructs()); - defunions.addAll(decls.defunions()); - typedefs.addAll(decls.typedefs()); + updateSet(vardecls, decls.vardecls()); + updateSet(funcdecls, decls.funcdecls()); + updateSet(defstructs, decls.defstructs()); + updateSet(defunions, decls.defunions()); + updateSet(typedefs, decls.typedefs()); + } + + protected void updateSet(Set s, List items) { + Iterator i = items.iterator(); + while (i.hasNext()) { + s.add(i.next()); + } } public void addDefvar(DefinedVariable var) { @@ -28,11 +35,11 @@ public class Declarations { } public void addDefvars(List vars) { - defvars.addAll(vars); + updateSet(defvars, vars); } public List defvars() { - return defvars; + return new ArrayList(defvars); } public void addVardecl(UndefinedVariable var) { @@ -40,7 +47,7 @@ public class Declarations { } public List vardecls() { - return vardecls; + return new ArrayList(vardecls); } public void addDefun(Function func) { @@ -48,7 +55,7 @@ public class Declarations { } public List defuns() { - return defuns; + return new ArrayList(defuns); } public void addFuncdecl(UndefinedFunction func) { @@ -56,7 +63,7 @@ public class Declarations { } public List funcdecls() { - return funcdecls; + return new ArrayList(funcdecls); } public void addDefstruct(StructNode n) { @@ -64,7 +71,7 @@ public class Declarations { } public List defstructs() { - return defstructs; + return new ArrayList(defstructs); } public void addDefunion(UnionNode n) { @@ -72,7 +79,7 @@ public class Declarations { } public List defunions() { - return defunions; + return new ArrayList(defunions); } public void addTypedef(TypedefNode n) { @@ -80,6 +87,6 @@ public class Declarations { } public List typedefs() { - return typedefs; + return new ArrayList(typedefs); } } diff --git a/net/loveruby/cflat/compiler/LibraryLoader.java b/net/loveruby/cflat/compiler/LibraryLoader.java index 6e8ec7a..6e4fabd 100644 --- a/net/loveruby/cflat/compiler/LibraryLoader.java +++ b/net/loveruby/cflat/compiler/LibraryLoader.java @@ -22,8 +22,8 @@ public class LibraryLoader { public LibraryLoader(List loadPath) { this.loadPath = loadPath; - loadingLibraries = new LinkedList(); - loadedLibraries = new HashMap(); + this.loadingLibraries = new LinkedList(); + this.loadedLibraries = new HashMap(); } public void addLoadPath(String path) { @@ -38,12 +38,12 @@ public class LibraryLoader { + ": " + libid); } loadingLibraries.addLast(libid); // stop recursive import - if (loadedLibraries.containsKey(libid)) { - // Already loaded import file. Do nothing. - return null; + Declarations decls = (Declarations)loadedLibraries.get(libid); + if (decls != null) { + // Already loaded import file. Returns cached declarations. + return decls; } - Declarations decls = - Parser.parseDeclFile(searchLibrary(libid), this, handler); + decls = Parser.parseDeclFile(searchLibrary(libid), this, handler); loadedLibraries.put(libid, decls); loadingLibraries.removeLast(); return decls; diff --git a/test/duplicated-import.cb b/test/duplicated-import.cb new file mode 100644 index 0000000..1b437fb --- /dev/null +++ b/test/duplicated-import.cb @@ -0,0 +1,9 @@ +import stdio; +import string; + +int +main(int argc, char **argv) +{ + puts("OK"); + return 0; +} diff --git a/test/test_cbc.sh b/test/test_cbc.sh index 4df6f25..d9dbc33 100644 --- a/test/test_cbc.sh +++ b/test/test_cbc.sh @@ -2,8 +2,6 @@ # test_cbc.sh # -CBC=../cbc - test_01_exec() { assert_stat 0 ./zero assert_stat 1 ./one @@ -94,7 +92,7 @@ test_09_cmp() { } test_10_assign() { - assert_out "2;2;3;4;5;6;7;8;8;9;10;11;777" ./assign + assert_out "2;2;3;4;5;6;7;8;8;9;10;11;777;S" ./assign assert_out "3;4;3;12;4;1;1;7;5;1;4" ./opassign assert_out "0;1;2;2;3;3;4" ./inc assert_out "4;3;2;2;1;1;0" ./dec @@ -151,7 +149,7 @@ test_18_array() { test_19_struct() { assert_out "11;22" ./struct assert_stat 0 ./struct-semcheck - assert_status 0 $CBC empstruct.cb + assert_compile_success empstruct.cb assert_compile_error struct-semcheck2.cb assert_compile_error struct-semcheck3.cb assert_compile_error struct-semcheck4.cb @@ -238,27 +236,27 @@ test_27_switch() { test_28_syntax() { assert_out "1, 2, 0" ./syntax1 - assert_status 0 $CBC syntax2.cb + assert_compile_success syntax2.cb assert_stat 0 ./syntax3 } +test_29_import() { + assert_compile_success duplicated-import.cb +} + ### ### Local Assertions ### -assert_compile() { - assert_status 0 $CBC "$@" -} - assert_stat() { st=$1; shift - assert_compile "$1.cb" && + assert_compile_success "$1.cb" && assert_status $st "$@" } assert_out() { msg="$1"; shift - assert_compile "$1.cb" && + assert_compile_success "$1.cb" && assert_stdout "$msg" "$@" } @@ -284,6 +282,12 @@ assert_private() { assert_eq "private" `symbol_visibility $1 $2` } +CBC=../cbc + +assert_compile_success() { + assert_status 0 $CBC "$@" +} + assert_compile_error() { shunit_begin_test if eval "$CBC $@" >tc.out 2>&1