Please support coo.h

2010-06-10 Thread yuanbin
Coo - C, Object Oriented
http://sourceforge.net/projects/coo/

-coo.h--
#ifndef __COO_H__
#define __COO_H__

typedef struct VTable /*root of virtual table class*/
{
long offset; /*servers for FREE*/
} VTable;

#define EXTENDS(s) \
union \
{ \
s s; \
s; \
};
#define VT(v) const v* vt;
#define EXTENDS2(s,v) \
union \
{ \
VT(v) \
s s; \
s; \
};
#ifndef offsetof
#define offsetof(s,m) ((long)&((s*)0)->m)
#endif
#define SUPER(s,m,p) ((s*)((char*)(p)-offsetof(s,m)))
#define FREE(p,vt) free((char*)(p)-(vt)->offset)

#endif

--
initialization of the enum:
enum {
  int i;
  float f;
} t={1.5}; //t.f
because of EXTENDS2 in coo.h, compiler needs
to initialze last member of enum.


Re: Please support coo.h

2010-06-10 Thread yuanbin
--
initialization of the union:
union {
  int i;
  float f;
} t={1.5}; //t.f
because of EXTENDS2 in coo.h, compiler needs
to initialze last member of union.

#include 
typedef struct VBase {} VBase;
typedef struct CBase { VT(VBase) int i; } CBase;
typedef struct VThis {} VThis;
typedef struct CThis { EXTENDS2(CBase,VThis) int j; } CThis;
CThis t={{.CBase={0, 1}}, 1}; //complex
int main() { return 0; }

gcc -fms-extensions
but i want default format:
CThis t={0, 1, 1}; //simple


Re: Please support coo.h

2010-06-10 Thread yuanbin
initialization of global variable?


2010/6/10 Andreas Schwab :
> yuanbin  writes:
>
>> but i want default format:
>> CThis t={0, 1, 1}; //simple
>
> Define a suitable constructor.
>
> Andreas.
>
> --
> Andreas Schwab, sch...@redhat.com
> GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84  5EC7 45C6 250E 6F00 984E
> "And now for something completely different."
>


Re: Please support coo.h

2010-06-10 Thread yuanbin
This compiler's extension is valuable


2010/6/10 Wojciech Meyer :
> On Thu, Jun 10, 2010 at 2:10 PM, yuanbin  wrote:
>> initialization of global variable?
>
> No, just define a macro.
>
>>
>>
>> 2010/6/10 Andreas Schwab :
>>> yuanbin  writes:
>>>
>>>> but i want default format:
>>>> CThis t={0, 1, 1}; //simple
>>>
>>> Define a suitable constructor.
>>
>
> Wojciech
>


Re: Please support coo.h

2010-06-10 Thread yuanbin
typedef struct CBase { int i; } CBase;
typedef struct CT1 { EXTENDS(CBase) ... } CT1;
typedef struct CT2 { EXTENDS(CT1) ... } CT2;
...
typedef struct CTN { EXTENDS(CTN_1) ... } CTN;
CTN t;
t.i=1; //need not t.CTN_1CT2.CT1.CBase.i ---complex
CBase* p=&t.CBase; //need not t.CTN_1CT2.CT1.CBase, need not
(CBase*)&t ---not safe


2010/6/11 Dave Korn :
> On 10/06/2010 18:07, yuanbin wrote:
>> This compiler's extension is valuable
>
>  No, it isn't very valuable, sorry to be blunt.  I think you are following a
> really wrong path here.  You are trying to implement a C++-alike
> object-oriented system in C.  That makes sense as far as it goes, but if you
> find yourself having to propose modifying the C compiler in a direction that
> basically makes it speak C++, you might as well just use C++ in the first
> place.  You want the compiler to automatically choose one of several different
> ways to initialise a union according to the data type of the argument you use
> to initialise it with; basically, that means you want overloaded constructors.
>  So you should just use C++, which already is C with overloaded constructors.
>  And it also already has all the other features that you'll discover you need
> in the compiler as you carry along this path.
>
>  By the time you get to the end of your journey, "coo.h" will be an empty
> file and all the functionality will have been added to the C compiler until it
> turns into a C++ compiler.  I think you need to choose a different plan.
>
>    cheers,
>      DaveK
>
>


Re: Please support coo.h

2010-06-11 Thread yuanbin
I am familiar with C++, Do not recommend C++ to me.
gcc initial the first member of union now, This is not the C standard also.
I just want gcc initial the last member of union with some switch.


2010/6/11 Magnus Fromreide :
> On Fri, 2010-06-11 at 08:44 +0800, yuanbin wrote:
>> typedef struct CBase { int i; } CBase;
>> typedef struct CT1 { EXTENDS(CBase) ... } CT1;
>> typedef struct CT2 { EXTENDS(CT1) ... } CT2;
>> ...
>> typedef struct CTN { EXTENDS(CTN_1) ... } CTN;
>> CTN t;
>> t.i=1; //need not t.CTN_1CT2.CT1.CBase.i ---complex
>> CBase* p=&t.CBase; //need not t.CTN_1CT2.CT1.CBase, need not
>> (CBase*)&t ---not safe
>
> struct CBase { int i; };
> struct CT1 : CBase { ... };
> struct CT2 : CT1 { ... };
> struct CTN : CTN_1 { ... };
> CTN t;
>
> t.i = 1; // assumes this is in function scope
> CBase* p = &t; // Even simpler than your proposal and still safe.
>
> Yep, this is valid C++. I think Dave is right, you really want C++.
>
> /MF
>
>>
>> 2010/6/11 Dave Korn :
>> > On 10/06/2010 18:07, yuanbin wrote:
>> >> This compiler's extension is valuable
>> >
>> >  No, it isn't very valuable, sorry to be blunt.  I think you are following 
>> > a
>> > really wrong path here.  You are trying to implement a C++-alike
>> > object-oriented system in C.  That makes sense as far as it goes, but if 
>> > you
>> > find yourself having to propose modifying the C compiler in a direction 
>> > that
>> > basically makes it speak C++, you might as well just use C++ in the first
>> > place.  You want the compiler to automatically choose one of several 
>> > different
>> > ways to initialise a union according to the data type of the argument you 
>> > use
>> > to initialise it with; basically, that means you want overloaded 
>> > constructors.
>> >  So you should just use C++, which already is C with overloaded 
>> > constructors.
>> >  And it also already has all the other features that you'll discover you 
>> > need
>> > in the compiler as you carry along this path.
>> >
>> >  By the time you get to the end of your journey, "coo.h" will be an empty
>> > file and all the functionality will have been added to the C compiler 
>> > until it
>> > turns into a C++ compiler.  I think you need to choose a different plan.
>> >
>> >    cheers,
>> >      DaveK
>> >
>> >
>
>
>


Re: [Tinycc-devel] Please support coo.h

2010-06-11 Thread yuanbin
i have pushed the patch on "mob"
Coo: initial last member of union
Please change my name of patch: U-YUAN\Administrator -> yuanbin


2010/6/11 grischka :
> Too all:
>
> Please push patches on our "mob" branch:
>  http://repo.or.cz/w/tinycc.git
>
> People who don't like/know git: at least base patches on latest
> mob or master branch.
>
> Also please give a short description of what the patch really
> does.  Otherwise it will be too hard for us other people.
>
> Thanks,
>
> --- grischka
>
> yuanbin wrote:
>>
>> Coo - C, Object Oriented
>> http://sourceforge.net/projects/coo/
>>
>> -coo.h--
>> #ifndef __COO_H__
>> #define __COO_H__
>>
>> typedef struct VTable /*root of virtual table class*/
>> {
>>    long offset; /*servers for FREE*/
>> } VTable;
>>
>> #define EXTENDS(s) \
>>    union \
>>    { \
>>        s s; \
>>        s; \
>>    };
>> #define VT(v) const v* vt;
>> #define EXTENDS2(s,v) \
>>    union \
>>    { \
>>        VT(v) \
>>        s s; \
>>        s; \
>>    };
>> #ifndef offsetof
>> #define offsetof(s,m) ((long)&((s*)0)->m)
>> #endif
>> #define SUPER(s,m,p) ((s*)((char*)(p)-offsetof(s,m)))
>> #define FREE(p,vt) free((char*)(p)-(vt)->offset)
>>
>> #endif
>>
>> --
>> initialization of the enum:
>> enum {
>>  int i;
>>  float f;
>> } t={1.5}; //t.f
>> because of EXTENDS2 in coo.h, compiler needs
>> to initialze last member of enum.
>>
>> --tccgen.c--
>> static void decl_initializer(CType *type, Section *sec, unsigned long c,
>>                             int first, int size_only)
>> {
>>    int index, array_length, n, no_oblock, nb, parlevel, i;
>>    int size1, align1, expr_type;
>>    Sym *s, *f;
>>    CType *t1;
>>
>>    if (type->t & VT_ARRAY) {
>>        s = type->ref;
>>        n = s->c;
>>        array_length = 0;
>>        t1 = pointed_type(type);
>>        size1 = type_size(t1, &align1);
>>
>>        no_oblock = 1;
>>        if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
>>            tok == '{') {
>>            skip('{');
>>            no_oblock = 0;
>>        }
>>
>>        /* only parse strings here if correct type (otherwise: handle
>>           them as ((w)char *) expressions */
>>        if ((tok == TOK_LSTR &&
>> #ifdef TCC_TARGET_PE
>>             (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)
>> #else
>>             (t1->t & VT_BTYPE) == VT_INT
>> #endif
>>            ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
>>            while (tok == TOK_STR || tok == TOK_LSTR) {
>>                int cstr_len, ch;
>>                CString *cstr;
>>
>>                cstr = tokc.cstr;
>>                /* compute maximum number of chars wanted */
>>                if (tok == TOK_STR)
>>                    cstr_len = cstr->size;
>>                else
>>                    cstr_len = cstr->size / sizeof(nwchar_t);
>>                cstr_len--;
>>                nb = cstr_len;
>>                if (n >= 0 && nb > (n - array_length))
>>                    nb = n - array_length;
>>                if (!size_only) {
>>                    if (cstr_len > nb)
>>                        warning("initializer-string for array is too
>> long");
>>                    /* in order to go faster for common case (char
>>                       string in global variable, we handle it
>>                       specifically */
>>                    if (sec && tok == TOK_STR && size1 == 1) {
>>                        memcpy(sec->data + c + array_length, cstr->data,
>> nb);
>>                    } else {
>>                        for(i=0;i>                            if (tok == TOK_STR)
>>                                ch = ((unsigned char *)cstr->data)[i];
>>                            else
>>                                ch = ((nwchar_t *)cstr->data)[i];
>>                            init_putv(t1, sec, c + (array_length + i) *
>> size1,
>>                                      ch, EXPR_VAL);
>>                        }
>>                    }
>>                }
>>                array_lengt