From 87f4bcef2e8f097767884070a002fcb2afcec204 Mon Sep 17 00:00:00 2001 From: Art Yerkes Date: Thu, 4 Dec 2003 06:57:57 +0000 Subject: [PATCH] This example both shows the new enum literal facility, and is a regression test against the enum scoping problem I had. Ocaml.cxx: Accidentally reintroduced the enum scoping problem, so I added an example that will fail if this is ever broken again. I encountered the actual problem in avifile.h git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@5478 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Examples/ocaml/scoped_enum/Makefile | 33 +++++++++++++++++++ SWIG/Examples/ocaml/scoped_enum/README | 1 + SWIG/Examples/ocaml/scoped_enum/example.i | 7 ++++ .../ocaml/scoped_enum/example_prog.ml | 4 +++ SWIG/Examples/ocaml/scoped_enum/foo.h | 5 +++ SWIG/Source/Modules/ocaml.cxx | 23 +++++++++---- 6 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 SWIG/Examples/ocaml/scoped_enum/Makefile create mode 100644 SWIG/Examples/ocaml/scoped_enum/README create mode 100644 SWIG/Examples/ocaml/scoped_enum/example.i create mode 100644 SWIG/Examples/ocaml/scoped_enum/example_prog.ml create mode 100644 SWIG/Examples/ocaml/scoped_enum/foo.h diff --git a/SWIG/Examples/ocaml/scoped_enum/Makefile b/SWIG/Examples/ocaml/scoped_enum/Makefile new file mode 100644 index 000000000..ae3a80ab9 --- /dev/null +++ b/SWIG/Examples/ocaml/scoped_enum/Makefile @@ -0,0 +1,33 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = +TARGET = example +INTERFACE = example.i +MLFILE = example.ml +PROGFILE = example_prog.ml +OBJS = + +all:: static + +dynamic:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \ + PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \ + ocaml_dynamic_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \ + PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \ + ocaml_static_cpp + +toplevel:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \ + PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \ + ocaml_static_cpp_toplevel + +clean:: + $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean + +check: all diff --git a/SWIG/Examples/ocaml/scoped_enum/README b/SWIG/Examples/ocaml/scoped_enum/README new file mode 100644 index 000000000..88b6693fd --- /dev/null +++ b/SWIG/Examples/ocaml/scoped_enum/README @@ -0,0 +1 @@ +This tests our handling of scoped enums. Run with argument Tag1 or Tag2. \ No newline at end of file diff --git a/SWIG/Examples/ocaml/scoped_enum/example.i b/SWIG/Examples/ocaml/scoped_enum/example.i new file mode 100644 index 000000000..de553f851 --- /dev/null +++ b/SWIG/Examples/ocaml/scoped_enum/example.i @@ -0,0 +1,7 @@ +%module example + +%{ +#include "foo.h" +%} + +%include "foo.h" \ No newline at end of file diff --git a/SWIG/Examples/ocaml/scoped_enum/example_prog.ml b/SWIG/Examples/ocaml/scoped_enum/example_prog.ml new file mode 100644 index 000000000..7df03cbd5 --- /dev/null +++ b/SWIG/Examples/ocaml/scoped_enum/example_prog.ml @@ -0,0 +1,4 @@ +open Swig +open Example + +let _ = _f (match Sys.argv.(1) with "Tag1" -> ``Tag1 | "Tag2" -> ``Tag2) diff --git a/SWIG/Examples/ocaml/scoped_enum/foo.h b/SWIG/Examples/ocaml/scoped_enum/foo.h new file mode 100644 index 000000000..8238cb66d --- /dev/null +++ b/SWIG/Examples/ocaml/scoped_enum/foo.h @@ -0,0 +1,5 @@ +namespace foo { + enum Bar { Tag1, Tag2 }; + static void f( Bar b ) { printf( "b = %d\n", (int)b ); } +} + diff --git a/SWIG/Source/Modules/ocaml.cxx b/SWIG/Source/Modules/ocaml.cxx index d8684db81..c224fcae5 100755 --- a/SWIG/Source/Modules/ocaml.cxx +++ b/SWIG/Source/Modules/ocaml.cxx @@ -1280,8 +1280,10 @@ public: String *fully_qualify_enum_name( Node *n, String *name ) { Node *parent = 0; + String *qualification = NewString(""); String *fully_qualified_name = NewString(""); String *parent_type = 0; + String *normalized_name; parent = parentNode(n); while( parent ) { @@ -1291,16 +1293,21 @@ public: NewStringf("%s::",Getattr(parent,"name")); if( !Cmp(parent_type,"class") || !Cmp(parent_type,"namespace") ) - Insert(fully_qualified_name,0,parent_copy); + Insert(qualification,0,parent_copy); Delete(parent_copy); } if( !Cmp( parent_type, "class" ) ) break; parent = parentNode(parent); } - Printf( fully_qualified_name, "%s", name ); + Printf( fully_qualified_name, "%s%s", qualification, name ); - return normalizeTemplatedClassName(fully_qualified_name); + normalized_name = normalizeTemplatedClassName(fully_qualified_name); + if( !strncmp(Char(normalized_name),"enum ",5) ) { + Insert(normalized_name,5,qualification); + } + + return normalized_name; } /* Benedikt Grundmann inspired --> Enum wrap styles */ @@ -1370,10 +1377,14 @@ public: * This would make one of the cases below unnecessary. * * * */ Printf( f_mlbody, - "let _ = Callback.register \"%s_marker\" (`%s)\n" - "let _ = Callback.register \"enum %s_marker\" (`%s)\n", - fully_qualified_name, name, + "let _ = Callback.register \"%s_marker\" (`%s)\n", fully_qualified_name, name ); + if( !strncmp(Char(fully_qualified_name),"enum ",5) ) { + String *fq_noenum = NewString(Char(fully_qualified_name) + 5); + Printf( f_mlbody, + "let _ = Callback.register \"%s_marker\" (`%s)\n", + fq_noenum, name ); + } Printf( f_enumtypes_type,"| `%s\n", name ); Insert(fully_qualified_name,0,"enum ");