Commit c5c70df2 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

add basic support for enum methods

2007-07-01  Juerg Billeter  <j@bitron.ch>

	* vala/parser.y, vala/valasymbolbuilder.vala,
	  vala/valasymbolresolver.vala, vala/valaenum.vala: add basic support
	  for enum methods

svn path=/trunk/; revision=326
parent 302bc8a0
2007-07-01 Jürg Billeter <j@bitron.ch>
* vala/parser.y, vala/valasymbolbuilder.vala,
vala/valasymbolresolver.vala, vala/valaenum.vala: add basic support
for enum methods
2007-06-30 Jürg Billeter <j@bitron.ch>
* vala/valasymbolbuilder.vala: fix current_symbol handling in
......
......@@ -41,6 +41,7 @@ static gboolean current_namespace_implicit;
static ValaClass *current_class;
static ValaStruct *current_struct;
static ValaInterface *current_interface;
static ValaEnum *current_enum;
typedef enum {
VALA_MODIFIER_NONE,
......@@ -329,10 +330,6 @@ static void yyerror (YYLTYPE *locp, ValaParser *parser, const char *msg);
%type <struct_> struct_header
%type <interface> interface_declaration
%type <enum_> enum_declaration
%type <list> enum_body
%type <list> opt_enum_member_declarations
%type <list> enum_member_declarations
%type <enum_value> enum_member_declaration
%type <flags> flags_declaration
%type <list> flags_body
%type <list> opt_flags_member_declarations
......@@ -3108,7 +3105,7 @@ interface_member_declaration
;
enum_declaration
: comment opt_attributes opt_access_modifier ENUM identifier opt_name_specifier enum_body
: comment opt_attributes opt_access_modifier ENUM identifier opt_name_specifier
{
GList *l;
ValaSourceReference *src;
......@@ -3129,59 +3126,73 @@ enum_declaration
}
src = src_com(@5, $1);
$$ = vala_enum_new (name, src);
current_enum = vala_enum_new (name, src);
g_free (name);
g_object_unref (src);
VALA_CODE_NODE($$)->attributes = $2;
VALA_CODE_NODE(current_enum)->attributes = $2;
if ($3 != 0) {
VALA_DATA_TYPE($$)->access = $3;
}
for (l = $7; l != NULL; l = l->next) {
vala_enum_add_value ($$, l->data);
g_object_unref (l->data);
VALA_DATA_TYPE(current_enum)->access = $3;
}
}
enum_body
{
$$ = current_enum;
current_enum = NULL;
}
;
enum_body
: OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
{
$$ = $2;
}
: OPEN_BRACE opt_enum_member_declarations opt_enum_method_declarations CLOSE_BRACE
;
opt_enum_member_declarations
: /* empty */
{
$$ = NULL;
}
| enum_member_declarations opt_comma
;
enum_member_declarations
: enum_member_declaration
{
$$ = g_list_append (NULL, $1);
}
| enum_member_declarations COMMA enum_member_declaration
{
$$ = g_list_append ($1, $3);
}
;
enum_member_declaration
: opt_attributes identifier
{
$$ = vala_enum_value_new ($2);
ValaEnumValue *ev = vala_enum_value_new ($2);
g_free ($2);
vala_enum_add_value (current_enum, ev);
g_object_unref (ev);
}
| opt_attributes identifier ASSIGN expression
{
$$ = vala_enum_value_new_with_value ($2, $4);
ValaEnumValue *ev = vala_enum_value_new_with_value ($2, $4);
g_free ($2);
g_object_unref ($4);
vala_enum_add_value (current_enum, ev);
g_object_unref (ev);
}
;
opt_enum_method_declarations
: /* empty */
| enum_method_declarations
;
enum_method_declarations
: SEMICOLON enum_method_declaration
| enum_method_declarations enum_method_declaration
;
enum_method_declaration
: method_declaration
{
/* skip declarations with errors */
if ($1 != NULL) {
vala_enum_add_method (current_enum, $1);
g_object_unref ($1);
}
}
;
......
......@@ -27,19 +27,20 @@ using GLib;
*/
public class Vala.Enum : DataType {
private List<EnumValue> values;
private List<Method> methods;
private string cname;
private string cprefix;
private string lower_case_cprefix;
private string lower_case_csuffix;
/**
* Creates a new enum.
*
* @param name type name
* @param source reference to source code
* @return newly created enum
* @param name type name
* @param source_reference reference to source code
* @return newly created enum
*/
public Enum (string! _name, SourceReference source = null) {
name = _name;
source_reference = source;
public Enum (construct string! name, construct SourceReference source_reference = null) {
}
/**
......@@ -51,6 +52,24 @@ public class Vala.Enum : DataType {
values.append (value);
}
/**
* Adds the specified method as a member to this enum.
*
* @param m a method
*/
public void add_method (Method! m) {
methods.append (m);
}
/**
* Returns a copy of the list of methods.
*
* @return list of methods
*/
public List<weak Method> get_methods () {
return methods.copy ();
}
public override void accept (CodeVisitor! visitor) {
visitor.visit_enum (this);
}
......@@ -59,6 +78,10 @@ public class Vala.Enum : DataType {
foreach (EnumValue value in values) {
value.accept (visitor);
}
foreach (Method m in methods) {
m.accept (visitor);
}
}
public override string get_cname (bool const_type = false) {
......@@ -67,7 +90,28 @@ public class Vala.Enum : DataType {
}
return cname;
}
public override ref string get_lower_case_cprefix () {
if (lower_case_cprefix == null) {
lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
}
return lower_case_cprefix;
}
private string get_lower_case_csuffix () {
if (lower_case_csuffix == null) {
lower_case_csuffix = Namespace.camel_case_to_lower_case (name);
}
return lower_case_csuffix;
}
public override ref string get_lower_case_cname (string infix) {
if (infix == null) {
infix = "";
}
return "%s%s%s".printf (@namespace.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
}
public override ref string get_upper_case_cname (string infix) {
return "%s%s".printf (@namespace.get_lower_case_cprefix (), Namespace.camel_case_to_lower_case (name)).up ();
}
......
......@@ -108,6 +108,14 @@ public class Vala.SymbolResolver : CodeVisitor {
current_scope = current_scope.parent_symbol;
}
public override void visit_enum (Enum! en) {
current_scope = en.symbol;
en.accept_children (this);
current_scope = current_scope.parent_symbol;
}
public override void visit_callback (Callback! cb) {
current_scope = cb.symbol;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment