All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wei Liu <wei.liu2@citrix.com>
To: Ian Campbell <Ian.Campbell@citrix.com>
Cc: ian.jackson@eu.citrix.com, Wei Liu <wei.liu2@citrix.com>,
	xen-devel@lists.xen.org
Subject: Re: [PATCH v6 11/18] libxl/gentypes.py: don't generate default values
Date: Tue, 10 Jun 2014 16:49:28 +0100	[thread overview]
Message-ID: <20140610154928.GP11959@zion.uk.xensource.com> (raw)
In-Reply-To: <1402412170.20641.19.camel@kazak.uk.xensource.com>

On Tue, Jun 10, 2014 at 03:56:10PM +0100, Ian Campbell wrote:
[...]
> > > > -        if ty.init_val is not None:
> > > > +        if ty.init_val is not None and ty.typename != "libxl_defbool":
> > > 
> > > Why is libxl_defbool special here?
> > > 
> > 
> > Because it's an opaque type and I didn't bother to modify the code
> > generator to have it generate some other functions to return value from
> > opaque type, as libxl_defbool is the only one that needs extra care so
> > far.
> 
> Ah, ok. BTW I think "ty != libxl_debool" is valid. Or maybe
> isinstance(), whichever one works is better than a string compare I
> think.
> 

No problem.

> > 
> > > >              s += "%s = %s;\n" % (ty.pass_arg(v, parent is None), ty.init_val)
> > > >          elif ty.init_fn is not None:
> > > >              s += "%s(%s);\n" % (ty.init_fn, ty.pass_arg(v, parent is None))
> > > > @@ -206,6 +206,13 @@ def libxl_C_type_gen_map_key(f, parent, indent = ""):
> > > >          s = indent + s
> > > >      return s.replace("\n", "\n%s" % indent).rstrip(indent)
> > > >  
> > > > +def get_init_val(f):
> > > > +    if f.init_val is not None:
> > > > +        return f.init_val
> > > > +    elif f.type.init_val is not None:
> > > > +        return f.type.init_val
> > > > +    return None
> > > > +
> > > >  def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
> > > >      s = ""
> > > >      if parent is None:
> > > > @@ -255,8 +262,22 @@ def libxl_C_type_gen_json(ty, v, indent = "    ", parent = None):
> > > >          s += "    goto out;\n"
> > > >          for f in [f for f in ty.fields if not f.const and not f.type.private]:
> > > >              (nparent,fexpr) = ty.member(v, f, parent is None)
> > > > -            s += libxl_C_type_gen_map_key(f, nparent)
> > > > -            s += libxl_C_type_gen_json(f.type, fexpr, "", nparent)
> > > > +            init_val = get_init_val(f)
> > > > +            if init_val:
> > > > +                if f.type.typename != "libxl_defbool":
> > > > +                    s += "if (%s != %s) {\n" % (fexpr, init_val)
> > > > +                else:
> > > > +                    s += "if (%s.val != %s) {\n" % (fexpr, init_val)
> > > > +                indent1 = "    "
> > > > +            else:
> > > > +                indent1 = ""
> > > 
> > > I don't think this is right. If a type doesn't has an init_val then its
> > > init_val is 0 and you should compare as a boolean. e.g.
> > >      if init_val:
> > >         s += "if (%s != %s) {\n" % (fexpr, init_val)
> > >      else: 
> > > 	s += "if (%s) {\n" % (fexpr)
> > > 
> > 
> > OK, I'm fine with this.
> > 
> > > This gets rid of the special casing of libxl_defbool (where 0 ==
> > > default) as well as removing a bunch of unnecessary p->foo = 0 from the
> > > generated init fns too.
> > > 
> > 
> > Not true for libxl_defbool, as stated above, generating code like
> >    if (some_defbool_field)
> > won't work.
> 
> Oh yes. Ick.
> 
> Can you perhaps abstract this into:
> 
> def get_field_val(ftype, fexpr):
>     ftype == libxl_defbool:
>        return "%s.val" % fexpr
>     else:
>        return fexpr
>     
> ?

Good idea.

> > 
> > > You could also possibly implement this by having get_init_val return an
> > > explicit "0" as a fallback but I think that will resent in less clear
> > > generated code.
> > > 
> > > > +            s += libxl_C_type_gen_map_key(f, nparent, indent1)
> > > > +            s += libxl_C_type_gen_json(f.type, fexpr, indent1, nparent)
> > > > +
> > > > +            if init_val:
> > > > +                s += "}\n"
> > > > +
> > > >          s += "s = yajl_gen_map_close(hand);\n"
> > > >          s += "if (s != yajl_gen_status_ok)\n"
> > > >          s += "    goto out;\n"
> > > > diff --git a/tools/libxl/idl.py b/tools/libxl/idl.py
> > > > index 8b118dd..14ca165 100644
> > > > --- a/tools/libxl/idl.py
> > > > +++ b/tools/libxl/idl.py
> > > > @@ -142,6 +142,7 @@ class EnumerationValue(object):
> > > >  class Enumeration(Type):
> > > >      def __init__(self, typename, values, **kwargs):
> > > >          kwargs.setdefault('dispose_fn', None)
> > > > +        kwargs.setdefault('init_val', '0')
> > > 
> > > I'm not sure about this, but I think you don't need it for the reasons
> > > above.
> > > 
> > 
> > IIRC with this line Enumeration type doesn't have a default value.
>            ^out (per your other mail)
> 
> It should be None, which I think is set by the following Type.__init__.
> 

Yes, it's None if it's not explicitly set.

If I make get_init_val function return 0 when it encounters None then I
don't need this hunk anymore.

> > > >  
> > > > -libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE)
> > > > +libxl_defbool = Builtin("defbool", passby=PASS_BY_REFERENCE,
> > > > +                        init_val="LIBXL__DEFBOOL_DEFAULT")
> > > 
> > > Or this.
> > > 
> > 
> > I would rather be explicit than implicit. LIBXL__DEFBOOL_DEFAULT happens
> > to be 0 and that's all. If we ever change our internal implementation
> > (however unlikely), generator will generate wrong thing. And I bet it
> > won't be very pleasant to debug this...
> 
> The assumption that the default bit pattern for any type is all zeroes
> is pretty firmly baked into lots of places. It is incredibly unlikely
> that this is going to change.
> 

Ack.

> > > BTW, I happened to notice that some enums in the IDL define their
> > > (non-zero) default using the integer form, which isn't wrong but did
> > > leap out at me from the generated code. If you fancy fixing that while
> > > you are in the area that would be great. I noticed this on
> > > libxl_action_on_shutdown and libxl_vga_interface_type.
> > > 
> > 
> > I did that in my previous round but you said you wouldn't worry about
> > readability of generated code (albeit I also modified those "0"s).
> 
> So I did, I started off mentioning the unlogged
> s/1/LIBXL_VGA_INTERFACE_TYPE_CIRRUS/ but then didn't notice that we got
> on to talking about 0s too. Sorry.
> 
> > So you think it's only necessary to have non-zero default using LIBXL_*
> > defines? I'm fine with this.
> 
> If they have an init_val at all I think it should use the names not the
> numbers. If the default is 0 then it needn't be specified explicitly
> (and in that case it'll be if (x) not if (x==THING) in the generated
> code)
> 
> But no need to fix this unless you want to, it's just a minor wrinkle in
> the existing thing.
> 

This is rather trivial after all. I can fix this in my next version.

Wei.

  reply	other threads:[~2014-06-10 15:49 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-09 12:43 [PATCH v6 00/18] libxl: JSON infrastructure, fixes and prerequisite patches for new API Wei Liu
2014-06-09 12:43 ` [PATCH v6 01/18] libxl: make cpupool_qualifier_to_cpupoolid a library function Wei Liu
2014-06-09 12:43 ` [PATCH v6 02/18] xl / libxl: push parsing of SSID and CPU pool ID down to libxl Wei Liu
2014-06-10 12:57   ` Ian Campbell
2014-06-10 14:16     ` Wei Liu
2014-06-09 12:43 ` [PATCH v6 03/18] xl / libxl: push VCPU affinity pinning " Wei Liu
2014-06-09 12:53   ` Wei Liu
2014-06-10  6:59   ` Dario Faggioli
2014-06-10  8:09     ` Wei Liu
2014-06-10 13:01       ` Ian Campbell
2014-06-10 13:09         ` Dario Faggioli
2014-06-10 13:46           ` Wei Liu
2014-06-10 14:01   ` Ian Campbell
2014-06-10 14:06     ` Wei Liu
2014-06-10 15:59       ` Dario Faggioli
2014-06-09 12:43 ` [PATCH v6 04/18] libxl: libxl_uuid_copy now takes a ctx argument Wei Liu
2014-06-10 13:05   ` Ian Campbell
2014-06-10 13:55     ` Wei Liu
2014-06-09 12:43 ` [PATCH v6 05/18] xl: remove parsing of "vncviewer" option in xl domain config file Wei Liu
2014-06-09 12:43 ` [PATCH v6 06/18] libxl: fix JSON generator for uint64_t Wei Liu
2014-06-09 12:43 ` [PATCH v6 07/18] libxl IDL: rename json_fn to json_gen_fn Wei Liu
2014-06-09 12:43 ` [PATCH v6 08/18] libxl_json: introduce libxl__object_from_json Wei Liu
2014-06-09 12:43 ` [PATCH v6 09/18] libxl_json: introduce parser functions for builtin types Wei Liu
2014-06-09 12:43 ` [PATCH v6 10/18] libxl/gentypes.py: special-case KeyedUnion map handle generation Wei Liu
2014-06-09 12:43 ` [PATCH v6 11/18] libxl/gentypes.py: don't generate default values Wei Liu
2014-06-10 13:25   ` Ian Campbell
2014-06-10 13:43     ` Wei Liu
2014-06-10 14:31       ` Wei Liu
2014-06-10 14:56       ` Ian Campbell
2014-06-10 15:49         ` Wei Liu [this message]
2014-06-10 15:54           ` Ian Campbell
2014-06-10 15:59             ` Wei Liu
2014-06-09 12:43 ` [PATCH v6 12/18] libxl IDL: generate code to parse libxl__json_object to libxl_FOO struct Wei Liu
2014-06-10 13:32   ` Ian Campbell
2014-06-09 12:43 ` [PATCH v6 13/18] libxl/gentest.py: test JSON parser Wei Liu
2014-06-09 12:43 ` [PATCH v6 14/18] libxl: introduce libxl_key_value_list_length Wei Liu
2014-06-09 12:43 ` [PATCH v6 15/18] libxl: introduce libxl_cpuid_policy_list_length Wei Liu
2014-06-09 12:43 ` [PATCH v6 16/18] libxl: copy function for builtin types Wei Liu
2014-06-09 12:43 ` [PATCH v6 17/18] libxl IDL: generate deep copy functions Wei Liu
2014-06-09 12:43 ` [PATCH v6 18/18] libxl/gentest.py: test " Wei Liu
2014-06-10 14:14 ` [PATCH v6 00/18] libxl: JSON infrastructure, fixes and prerequisite patches for new API Ian Campbell
2014-06-10 20:10   ` Boris Ostrovsky
2014-06-10 20:51     ` Boris Ostrovsky
2014-06-10 21:21       ` Wei Liu
2014-06-10 21:38         ` Boris Ostrovsky
2014-06-10 21:42           ` Wei Liu
2014-06-11  8:47         ` Ian Campbell
2014-06-11  8:53           ` Wei Liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140610154928.GP11959@zion.uk.xensource.com \
    --to=wei.liu2@citrix.com \
    --cc=Ian.Campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.