2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
Class loader
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
(c) 2000-2007 Benoit Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 1, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBX_CLASS_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_buffer.h"
|
|
|
|
#include "gb_alloc.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_limit.h"
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "gb_buffer.h"
|
|
|
|
#include "gb_file.h"
|
|
|
|
#include "gbx_type.h"
|
|
|
|
#include "gbx_exec.h"
|
|
|
|
#include "gbx_debug.h"
|
|
|
|
#include "gb_magic.h"
|
|
|
|
#include "gbx_stream.h"
|
|
|
|
|
|
|
|
#include "gbx_string.h"
|
|
|
|
#include "gbx_object.h"
|
|
|
|
#include "gbx_variant.h"
|
|
|
|
#include "gbx_number.h"
|
|
|
|
#include "gbx_c_array.h"
|
|
|
|
|
|
|
|
#include "gambas.h"
|
|
|
|
|
|
|
|
#include "gbx_class.h"
|
|
|
|
|
|
|
|
//#define DEBUG_COMP 1
|
|
|
|
//#define DEBUG_LOAD 1
|
|
|
|
//#define DEBUG_DESC 1
|
|
|
|
|
|
|
|
/* Global class table */
|
|
|
|
static TABLE _global_table;
|
|
|
|
/* List of all classes */
|
|
|
|
static CLASS *_classes = NULL;
|
|
|
|
/* First created class (must be 'Class' class) */
|
|
|
|
static CLASS *_first = NULL;
|
|
|
|
/* This flag forces CLASS_find() to only register in the global table */
|
|
|
|
static bool _global = FALSE;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
static CLASS *Class;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void CLASS_init(void)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
//if (sizeof(CLASS) > 128)
|
|
|
|
// fprintf(stderr, "Warning: Use the 64 bits version of Gambas at your own risk!\n");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
TABLE_create_static(&_global_table, sizeof(CLASS_SYMBOL), TF_IGNORE_CASE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_init_native();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TABLE *CLASS_get_table(void)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
return &_global_table;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void exit_class(CLASS *class, bool native)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
//CLASS_DESC *desc;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
/*if (!class->ready && !CLASS_is_virtual(class))
|
|
|
|
printf("WARNING: class %s was never loaded.\n", class->name);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Exiting class %s...\n", class->name);
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
/* Est-ce que _exit marche pour les classes gambas ? */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (CLASS_is_native(class) != native)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
EXEC_public(class, NULL, "_exit", 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
/*desc = CLASS_get_symbol_desc_kind(class, "_exit", CD_STATIC_METHOD, 0);
|
|
|
|
if (!desc)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
EXEC.desc = &desc->method;
|
|
|
|
EXEC.native = CLASS_is_native(class);
|
|
|
|
EXEC.class = class;
|
|
|
|
EXEC.object = NULL;
|
|
|
|
EXEC.nparam = 0;
|
|
|
|
EXEC.drop = TRUE;
|
|
|
|
EXEC.use_stack = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
EXEC_native();*/
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void unload_class(CLASS *class)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Unloading class %s...\n", class->name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (class->free_name)
|
|
|
|
FREE(&class->name, "unload_class");
|
|
|
|
|
|
|
|
if (!CLASS_is_native(class))
|
|
|
|
{
|
|
|
|
#ifdef OS_64BITS
|
|
|
|
|
|
|
|
FREE(&class->load->desc, "unload_class");
|
|
|
|
FREE(&class->load->cst, "unload_class");
|
|
|
|
FREE(&class->load->class_ref, "unload_class");
|
|
|
|
FREE(&class->load->unknown, "unload_class");
|
|
|
|
FREE(&class->load->event, "unload_class");
|
|
|
|
FREE(&class->load->ext, "unload_class");
|
|
|
|
FREE(&class->load->local, "unload_class");
|
|
|
|
FREE(&class->load->array, "unload_class");
|
|
|
|
|
|
|
|
if (class->debug)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
FUNCTION *func;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
for (i = 0; i < class->load->n_func; i++)
|
|
|
|
{
|
|
|
|
func = &class->load->func[i];
|
|
|
|
FREE(&func->debug->local, "unload_class");
|
|
|
|
}
|
|
|
|
|
|
|
|
FREE(&class->load->global, "unload_class");
|
|
|
|
FREE(&class->load->debug, "unload_class");
|
2009-01-01 14:42:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FREE(&class->load->func, "unload_class");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
FREE(&class->load, "unload_class");
|
|
|
|
if (!class->mmapped)
|
|
|
|
FREE(&class->data, "unload_class");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (class->signature)
|
|
|
|
FREE(&class->signature, "unload_class");
|
2007-12-30 17:41:49 +01:00
|
|
|
if (class->data)
|
2009-01-01 14:42:17 +01:00
|
|
|
FREE(&class->data, "unload_class");
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->free_event)
|
|
|
|
FREE(&class->event, "unload_class");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->stat)
|
|
|
|
FREE(&class->stat, "unload_class");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->table)
|
|
|
|
FREE(&class->table, "unload_class");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class->state = CS_NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-16 12:12:13 +02:00
|
|
|
void CLASS_clean_up(bool silent)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int n, nc, nb;
|
|
|
|
//CLASS_SYMBOL *csym;
|
|
|
|
CLASS *class;
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "\n------------------- CLASS_exit -------------------\n\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Freeing auto-creatable objects...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Count how many classes should be freed
|
|
|
|
// And free automatic instances
|
|
|
|
|
|
|
|
nc = 0;
|
|
|
|
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
{
|
|
|
|
if (class->instance)
|
|
|
|
OBJECT_UNREF(class->instance, "CLASS_exit");
|
|
|
|
if (!CLASS_is_native(class) && class->state)
|
|
|
|
{
|
|
|
|
/*printf("Must free: %s\n", class->name);*/
|
|
|
|
nc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Calling _exit on loaded classes...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
exit_class(class, FALSE);
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Freeing classes with no instance...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Free classes having no instance
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (n < nc)
|
|
|
|
{
|
|
|
|
nb = n;
|
|
|
|
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
{
|
|
|
|
/*if (!CLASS_is_native(class) && class->ready && !class->exit)
|
|
|
|
printf("%s: %d ready = %d\n", class->name, class->count, class->ready);*/
|
|
|
|
|
|
|
|
if (class->count == 0 && !CLASS_is_native(class) && class->state && !class->exit)
|
|
|
|
{
|
|
|
|
/*printf("Freeing %s\n", class->name);*/
|
|
|
|
OBJECT_release(class, NULL);
|
|
|
|
class->exit = TRUE;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == nb) /* On n'a rien pu faire */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Freeing other classes...\n");
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-07-16 12:12:13 +02:00
|
|
|
// Remaining objects are circular references
|
|
|
|
// Everything is forced to be freed
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (n < nc)
|
|
|
|
{
|
|
|
|
if (!silent)
|
|
|
|
fprintf(stderr, "WARNING: circular references detected\n");
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
{
|
|
|
|
if (!CLASS_is_native(class) && class->state && !class->exit)
|
|
|
|
{
|
|
|
|
if (!silent)
|
|
|
|
fprintf(stderr, "%s (%d)\n", class->name, class->count);
|
|
|
|
OBJECT_release(class, NULL);
|
|
|
|
class->exit = TRUE;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Calling _exit on native classes...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
exit_class(class, TRUE);
|
2008-07-16 12:12:13 +02:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-07-16 12:12:13 +02:00
|
|
|
void CLASS_exit()
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class, *next;
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Unloading classes...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (class = _classes; class; class = class->next)
|
|
|
|
unload_class(class);
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
fprintf(stderr, "Destroying classes...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class = _classes;
|
|
|
|
while (class)
|
|
|
|
{
|
|
|
|
next = class->next;
|
|
|
|
FREE(&class, "CLASS_exit");
|
|
|
|
class = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
TABLE_delete_static(&_global_table);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_look(const char *name, int len)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_SYMBOL *csym;
|
|
|
|
ARCHIVE *arch = NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
int index;
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, "CLASS_look: %s in %s\n", name, _global ? "global" : "local");
|
|
|
|
|
|
|
|
if (_global && (!strcmp(name, "CControl[]") || !strcmp(name, "CControl")))
|
|
|
|
BREAKPOINT();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//if (CP && CP->component && CP->component->archive)
|
|
|
|
if (!_global && !ARCHIVE_get_current(&arch))
|
|
|
|
{
|
|
|
|
if (TABLE_find_symbol(arch->classes, name, len, (SYMBOL **)(void *)&csym, NULL))
|
|
|
|
{
|
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, " -> in %s\n", arch->name ? arch->name : "main");
|
|
|
|
#endif
|
|
|
|
return csym->class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (TABLE_find_symbol(&_global_table, name, len, (SYMBOL **)(void *)&csym, &index))
|
|
|
|
{
|
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, " -> %d in global\n", index);
|
|
|
|
#endif
|
|
|
|
return csym->class;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if DEBUG_COMP
|
|
|
|
fprintf(stderr, " -> ?\n");
|
|
|
|
#endif
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CLASS *CLASS_find(const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_SYMBOL *csym;
|
|
|
|
CLASS *class;
|
|
|
|
int index;
|
|
|
|
int len;
|
|
|
|
ARCHIVE *arch = NULL;
|
|
|
|
bool global;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (name == NULL)
|
|
|
|
name = COMMON_buffer;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "CLASS_find: %s (%d)\n", name, _global);
|
2007-12-30 17:41:49 +01:00
|
|
|
#endif
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class = CLASS_look(name, len);
|
|
|
|
if (class)
|
|
|
|
return class;
|
|
|
|
|
|
|
|
//if (CP && CP->component && CP->component->archive)
|
|
|
|
if (!_global && !ARCHIVE_get_current(&arch))
|
|
|
|
{
|
|
|
|
global = FALSE;
|
|
|
|
TABLE_add_symbol(arch->classes, name, len, (SYMBOL **)(void *)&csym, NULL);
|
|
|
|
#if DEBUG_LOAD || DEBUG_COMP
|
|
|
|
fprintf(stderr, "Not found -> creating new one in %s\n", arch->name ? arch->name : "main");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
global = TRUE;
|
|
|
|
TABLE_add_symbol(&_global_table, name, len, (SYMBOL **)(void *)&csym, &index);
|
|
|
|
#if DEBUG_LOAD || DEBUG_COMP
|
|
|
|
fprintf(stderr, "Not found -> creating new one in global\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-02-02 14:04:04 +01:00
|
|
|
ALLOC_ZERO(&class, sizeof(CLASS), "CLASS_find");
|
2009-01-01 14:42:17 +01:00
|
|
|
csym->class = class;
|
|
|
|
/*csym->class->id = index;*/
|
|
|
|
class->state = CS_NULL;
|
|
|
|
class->count = 0;
|
|
|
|
class->ref = 1;
|
|
|
|
|
|
|
|
class->next = _classes;
|
|
|
|
_classes = class;
|
|
|
|
|
|
|
|
ALLOC(&class->name, len + 1, "CLASS_find");
|
|
|
|
strcpy((char *)class->name, name);
|
|
|
|
|
|
|
|
csym->sym.name = class->name;
|
|
|
|
|
|
|
|
class->free_name = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
// The first class must be the Class class!
|
2009-01-01 14:42:17 +01:00
|
|
|
if (_first == NULL)
|
|
|
|
_first = class;
|
|
|
|
class->class = _first;
|
|
|
|
|
|
|
|
class->global = global;
|
|
|
|
|
|
|
|
#if DEBUG_LOAD
|
|
|
|
if (!strcmp(name, "BugModule"))
|
|
|
|
fprintf(stderr, "New class %s %p !\n", name, csym->class);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CLASS_count(void)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
return TABLE_count(&_global_table);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_get(const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class = CLASS_find(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->state == CS_NULL)
|
|
|
|
CLASS_load(class);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_look_global(const char *name, int len)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
_global = TRUE;
|
|
|
|
class = CLASS_look(name, len);
|
|
|
|
_global = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_find_global(const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
_global = TRUE;
|
|
|
|
class = CLASS_find(name);
|
|
|
|
_global = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boolean CLASS_inherits(CLASS *class, CLASS *parent)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
class = class->parent;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class == NULL)
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class == parent)
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CLASS_find_symbol(CLASS *class, const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
//printf("%s.%s\n", class->name, name);
|
|
|
|
|
|
|
|
//if (!strcmp(class->name, "Collection") && !strcmp(name, "Exist"))
|
|
|
|
// printf("STOP\n");
|
|
|
|
|
|
|
|
SYMBOL_find(class->table, class->n_desc, sizeof(CLASS_DESC_SYMBOL), TF_IGNORE_CASE, name, strlen(name), NULL, &index);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
//if (strcmp(name, "m1") == 0)
|
|
|
|
//printf("find: %s in %s -> %d\n", name, class->name, index);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return index;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CLASS_find_symbol_with_prefix(CLASS *class, const char *name, const char *prefix)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
SYMBOL_find(class->table, class->n_desc, sizeof(CLASS_DESC_SYMBOL), TF_IGNORE_CASE, name, strlen(name), prefix, &index);
|
|
|
|
return index;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC_SYMBOL *CLASS_get_symbol(CLASS *class, const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
index = CLASS_find_symbol(class, name);
|
|
|
|
if (index == NO_SYMBOL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return &class->table[index];
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC *CLASS_get_symbol_desc(CLASS *class, const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_DESC_SYMBOL *cds = CLASS_get_symbol(class, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (cds == NULL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return cds->desc;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
short CLASS_get_symbol_index_kind(CLASS *class, const char *name, int kind, int kind2)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_DESC *desc;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
index = CLASS_find_symbol(class, name);
|
|
|
|
if (index != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
desc = CLASS_get_desc(class, index);
|
|
|
|
if (desc)
|
|
|
|
if ((CLASS_DESC_get_type(desc) == kind) || (CLASS_DESC_get_type(desc) == kind2))
|
|
|
|
return (short)index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (short)NO_SYMBOL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC *CLASS_get_symbol_desc_kind(CLASS *class, const char *name, int kind, int kind2)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
short index;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
index = CLASS_get_symbol_index_kind(class, name, kind, kind2);
|
|
|
|
if (index == NO_SYMBOL)
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return CLASS_get_desc(class, index);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC_EVENT *CLASS_get_event_desc(CLASS *class, const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int index;
|
|
|
|
CLASS_DESC *cd;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
index = CLASS_find_symbol_with_prefix(class, name, ":");
|
|
|
|
if (index == NO_SYMBOL)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
cd = class->table[index].desc;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (CLASS_DESC_get_type(cd) != CD_EVENT)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return &cd->event;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char *CLASS_DESC_get_signature(CLASS_DESC *cd)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
#if 0
|
|
|
|
switch (CLASS_DESC_get_type(cd))
|
|
|
|
{
|
|
|
|
case CD_METHOD:
|
|
|
|
case CD_STATIC_METHOD:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return cd->method.help;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
case CD_EVENT:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return cd->event.help;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
case CD_PROPERTY:
|
|
|
|
case CD_STATIC_PROPERTY:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return cd->property.help;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
default:
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-14 21:42:27 +02:00
|
|
|
void CLASS_free(void *object)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class = OBJECT_class(object);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
((OBJECT *)object)->ref = 1; /* Prevents anybody from freeing the object ! */
|
|
|
|
//fprintf(stderr, "CLASS_free: %s %p\n", class->name, object);
|
|
|
|
EXEC_special_inheritance(SPEC_FREE, class, object, 0, TRUE);
|
|
|
|
((OBJECT *)object)->ref = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
OBJECT_release(class, object);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_REF
|
|
|
|
void CLASS_ref(void *object)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
((OBJECT *)object)->ref++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "%s: ref(%s %p) -> %d\n",
|
|
|
|
DEBUG_get_current_position(),
|
|
|
|
OBJECT_class(object)->name, object, ((OBJECT *)object)->ref);
|
|
|
|
fflush(stdout);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
bool CLASS_unref(void *ob, boolean can_free)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-09-22 01:22:07 +02:00
|
|
|
OBJECT *object = (OBJECT *)ob;
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (object->ref <= 0)
|
|
|
|
fprintf(stderr, "*** %p REF = %d !\n", object, object->ref);
|
|
|
|
|
|
|
|
fprintf(stderr, "%s: unref(%s %p) -> %d\n",
|
|
|
|
DEBUG_get_current_position(),
|
|
|
|
OBJECT_class(object)->name, object, object->ref - 1);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*if (strcmp(OBJECT_class(object)->name, "Class") == 0)
|
|
|
|
fprintf(stderr, "Class ?\n");*/
|
|
|
|
|
|
|
|
if ((--(object->ref) <= 0) && can_free)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "FREE %p !\n", object);
|
|
|
|
CLASS_free(object);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void CLASS_do_nothing()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLASS_return_zero()
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
return 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CLASS_DESC_SYMBOL *SortClassDesc;
|
|
|
|
|
|
|
|
static int sort_desc(ushort *i1, ushort *i2)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
/*return strcmp(SortClassDesc[*i1].load.symbol.name, SortClassDesc[*i2].load.symbol.name);*/
|
|
|
|
return TABLE_compare_ignore_case_len(
|
|
|
|
SortClassDesc[*i1].name,
|
|
|
|
SortClassDesc[*i1].len,
|
|
|
|
SortClassDesc[*i2].name,
|
|
|
|
SortClassDesc[*i2].len
|
|
|
|
);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLASS_sort(CLASS *class)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
ushort *sym;
|
|
|
|
ushort i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->n_desc == 0)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
ALLOC(&sym, sizeof(ushort) * class->n_desc, "CLASS_sort");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
for (i = 0; i < class->n_desc; i++)
|
|
|
|
sym[i] = i;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
SortClassDesc = class->table;
|
|
|
|
qsort(sym, class->n_desc, sizeof(ushort), (int (*)(const void *, const void *))sort_desc);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
for (i = 0; i < class->n_desc; i++)
|
|
|
|
class->table[i].sort = sym[i];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
#if DEBUG_DESC
|
|
|
|
{
|
|
|
|
SYMBOL *sym;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "\nSORT %s\n", class->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
for (i = 0; i < class->n_desc; i++)
|
|
|
|
{
|
|
|
|
sym = (SYMBOL *)&class->table[i];
|
|
|
|
//sym = (SYMBOL *)&class->table[sym->sort];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "[%d] (%d) %.*s\n", i, (int)sym->sort, (int)sym->len, sym->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
FREE(&sym, "CLASS_sort");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GET_IF_NULL(_callback) if (class->_callback == NULL) class->_callback = class->parent->_callback
|
|
|
|
|
|
|
|
void CLASS_inheritance(CLASS *class, CLASS *parent)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->parent != NULL)
|
|
|
|
THROW(E_CLASS, class->name, "Multiple inheritance", "");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class->parent = parent;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_load(class->parent);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
THROW(E_CLASS, class->name, "Cannot load parent class ", parent->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
GET_IF_NULL(check);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
// CREATE STATIC is inherited, but not CREATE PRIVATE
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (parent->auto_create)
|
|
|
|
class->auto_create = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
//fprintf(stderr, "CLASS_inheritance: %s %s\n", class->name, class->auto_create ? "AUTO CREATE" : "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_replace_global(const char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_SYMBOL *csym;
|
|
|
|
char *new_name;
|
|
|
|
CLASS *class;
|
|
|
|
CLASS *new_class;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
class = CLASS_find_global(name);
|
|
|
|
if (class->state)
|
|
|
|
{
|
|
|
|
len = strlen(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
ALLOC(&new_name, len + 2, "CLASS_replace_global");
|
|
|
|
snprintf(new_name, len+2, ">%s", name);
|
|
|
|
new_class = CLASS_replace_global(new_name);
|
|
|
|
FREE(&new_name, "CLASS_replace_global");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
new_name = (char *)new_class->name;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
TABLE_find_symbol(&_global_table, name, len, (SYMBOL **)(void *)&csym, NULL);
|
|
|
|
csym->class = new_class;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
TABLE_find_symbol(&_global_table, new_name, len + 1, (SYMBOL **)(void *)&csym, NULL);
|
|
|
|
csym->class = class;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
new_class->name = class->name;
|
|
|
|
class->name = new_name;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class = new_class;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-07-18 01:45:32 +02:00
|
|
|
const char *CLASS_DESC_get_type_name(CLASS_DESC *desc)
|
|
|
|
{
|
|
|
|
switch (desc->gambas.val4)
|
|
|
|
{
|
|
|
|
case CD_PROPERTY_ID: return "p";
|
|
|
|
case CD_VARIABLE_ID: return "v";
|
|
|
|
case CD_METHOD_ID: return "m";
|
|
|
|
case CD_STATIC_PROPERTY_ID: return "P";
|
|
|
|
case CD_STATIC_VARIABLE_ID: return "V";
|
|
|
|
case CD_STATIC_METHOD_ID: return "M";
|
|
|
|
case CD_CONSTANT_ID: return "C";
|
|
|
|
case CD_EVENT_ID: return ":";
|
|
|
|
case CD_EXTERN_ID: return "X";
|
|
|
|
default: return "?";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
// A symbol can be overridden by a more powerful one:
|
|
|
|
// - A read-only property can be overridden by a read-write property.
|
|
|
|
// - A constant can be overridden by any property.
|
|
|
|
|
|
|
|
static bool check_override(char parent_type, char type)
|
|
|
|
{
|
|
|
|
if (parent_type == type)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (parent_type == 'r' && type == 'p')
|
|
|
|
return FALSE;
|
|
|
|
if (parent_type == 'R' && type == 'P')
|
|
|
|
return FALSE;
|
|
|
|
if (parent_type == 'C' && (type == 'R' || type == 'P'))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void CLASS_make_description(CLASS *class, CLASS_DESC *desc, int n_desc, int *first)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
static const char *nonher[] = { "_new", "_free", "_init", "_exit", NULL };
|
|
|
|
|
|
|
|
int ind;
|
|
|
|
int i, j;
|
|
|
|
const char *name;
|
|
|
|
const char **pnonher;
|
|
|
|
CLASS *parent;
|
|
|
|
char type, parent_type;
|
|
|
|
CLASS_DESC_SYMBOL *cds;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
#if DEBUG_DESC
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "\n---- %s\n", class->name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Number of descriptions */
|
|
|
|
|
|
|
|
class->n_desc = n_desc;
|
|
|
|
if (class->parent)
|
|
|
|
class->n_desc += class->parent->n_desc;
|
|
|
|
|
|
|
|
/* Fabrication de la table */
|
|
|
|
|
|
|
|
if (class->n_desc)
|
|
|
|
ALLOC(&class->table, sizeof(CLASS_DESC_SYMBOL) * class->n_desc, "CLASS_make_description");
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
if (class->parent && class->parent->n_desc)
|
|
|
|
{
|
|
|
|
for (j = 0; j < class->parent->n_desc; j++)
|
|
|
|
{
|
|
|
|
class->table[i] = class->parent->table[j];
|
|
|
|
class->table[i].sort = 0;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < n_desc; j++)
|
|
|
|
{
|
|
|
|
if (CLASS_is_native(class))
|
|
|
|
{
|
|
|
|
name = &(desc[j].gambas.name[1]);
|
|
|
|
type = CLASS_DESC_get_type(&desc[j]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = desc[j].gambas.name;
|
|
|
|
type = *CLASS_DESC_get_type_name(&desc[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//fprintf(stderr, "%s.%s\n", class->name, name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
// A inherited symbol has two or more entries in the table. Only the first one
|
|
|
|
// will be used, and so it must point at the new description, not the inherited one.
|
2008-04-12 00:36:53 +02:00
|
|
|
parent = class;
|
2008-05-10 17:17:07 +02:00
|
|
|
while ((parent = parent->parent))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2008-04-12 00:36:53 +02:00
|
|
|
ind = CLASS_find_symbol(parent, name);
|
|
|
|
if (ind != NO_SYMBOL)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
cds = &parent->table[ind];
|
|
|
|
|
|
|
|
// The parent class public symbols of non-native classes were replaced by the symbol kind returned by CLASS_DESC_get_type_name()
|
|
|
|
|
|
|
|
if (cds->desc)
|
|
|
|
{
|
|
|
|
parent_type = CLASS_DESC_get_type(cds->desc);
|
|
|
|
|
|
|
|
if (check_override(parent_type, type))
|
2009-01-31 16:54:28 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, "type = '%c' parent_type = '%c'\n", type, parent_type);
|
2009-01-01 14:42:17 +01:00
|
|
|
THROW(E_OVERRIDE, parent->name, cds->name, class->name);
|
2009-01-31 16:54:28 +01:00
|
|
|
}
|
2009-01-01 14:42:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cds = &class->table[ind];
|
|
|
|
|
2008-04-12 00:36:53 +02:00
|
|
|
#if DEBUG_DESC
|
2009-01-01 14:42:17 +01:00
|
|
|
fprintf(stderr, "%s: [%d] (%p %d) := (%p %d)\n", name, ind, cds->desc, cds->desc ? cds->desc->gambas.val1 : 0, &desc[j], desc[j].gambas.val1);
|
2008-04-12 00:36:53 +02:00
|
|
|
#endif
|
2009-01-01 14:42:17 +01:00
|
|
|
|
|
|
|
cds->desc = &desc[j];
|
|
|
|
cds->sort = 0;
|
|
|
|
cds->name = ".";
|
|
|
|
cds->len = 1;
|
|
|
|
|
2008-07-18 01:45:32 +02:00
|
|
|
if (!desc[j].gambas.val1 && index(CD_CALL_SOMETHING_LIST, type) != NULL)
|
2008-04-12 00:36:53 +02:00
|
|
|
{
|
2008-07-18 01:45:32 +02:00
|
|
|
#if DEBUG_DESC
|
|
|
|
fprintf(stderr, "'%s' gambas.val1: %d -> %d\n", desc[j].gambas.name, desc[j].gambas.val1, class->parent->table[ind].desc->gambas.val1);
|
|
|
|
#endif
|
2008-04-12 00:36:53 +02:00
|
|
|
desc[j].gambas.val1 = class->parent->table[ind].desc->gambas.val1;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
2009-01-01 14:42:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (pnonher = nonher; *pnonher; pnonher++)
|
|
|
|
{
|
|
|
|
ind = CLASS_find_symbol(class->parent, *pnonher);
|
|
|
|
if (ind != NO_SYMBOL)
|
|
|
|
{
|
|
|
|
cds = &class->table[ind];
|
|
|
|
cds->desc = NULL;
|
|
|
|
cds->sort = 0;
|
|
|
|
cds->name = ".";
|
|
|
|
cds->len = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*first = i;
|
|
|
|
|
|
|
|
for (j = 0; j < n_desc; j++, i++)
|
|
|
|
{
|
|
|
|
class->table[i].desc = &desc[j];
|
|
|
|
|
|
|
|
/* On saute le caractère de type de symbole */
|
|
|
|
if (CLASS_is_native(class))
|
|
|
|
class->table[i].name = &(desc[j].gambas.name[1]);
|
|
|
|
else
|
|
|
|
class->table[i].name = desc[j].gambas.name;
|
|
|
|
|
|
|
|
class->table[i].sort = 0;
|
|
|
|
class->table[i].len = strlen(class->table[i].name);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_DESC
|
|
|
|
{
|
|
|
|
CLASS_DESC_SYMBOL *cds;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
for (i = 0; i < class->n_desc; i++)
|
|
|
|
{
|
|
|
|
cds = &class->table[i];
|
|
|
|
fprintf(stderr, "%d (%d): %.*s %p\n", i, cds->sort, cds->len, cds->name, cds->desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 'all' means that size_dynamic is the entire size of the object, not just the size of the data */
|
2008-03-11 14:29:47 +01:00
|
|
|
/* And check if the class is a stream */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
void CLASS_calc_info(CLASS *class, int n_event, int size_dynamic, boolean all, int size_static)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->parent)
|
|
|
|
{
|
|
|
|
if (all)
|
|
|
|
class->off_event = size_dynamic;
|
|
|
|
else
|
|
|
|
class->off_event = class->parent->off_event + size_dynamic;
|
|
|
|
|
|
|
|
class->n_event = class->parent->n_event + n_event;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (all)
|
|
|
|
class->off_event = size_dynamic;
|
|
|
|
else
|
|
|
|
class->off_event = sizeof(OBJECT) + size_dynamic;
|
|
|
|
|
|
|
|
class->n_event = n_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
class->size = class->off_event + sizeof(OBJECT_EVENT) + class->n_event * sizeof(ushort);
|
|
|
|
|
|
|
|
class->size_stat = size_static;
|
|
|
|
if (size_static)
|
|
|
|
ALLOC_ZERO(&class->stat, class->size_stat, "CLASS_calc_info");
|
|
|
|
else
|
|
|
|
class->stat = NULL;
|
|
|
|
|
|
|
|
class->is_stream = (class == CLASS_Stream) || (class->parent && class->parent->is_stream);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CLASS_make_event(CLASS *class, int *first)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int i, ev;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
*first = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->n_event == 0)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (!CLASS_is_native(class))
|
|
|
|
{
|
|
|
|
if (class->parent == NULL)
|
|
|
|
{
|
|
|
|
class->event = class->load->event;
|
|
|
|
class->free_event = FALSE;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
ALLOC_ZERO(&class->event, sizeof(CLASS_EVENT) * class->n_event, "CLASS_make_event");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (class->parent != NULL)
|
|
|
|
{
|
|
|
|
ev = class->parent->n_event;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
for (i = 0; i < ev; i++)
|
|
|
|
class->event[i] = class->parent->event[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ev = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class->free_event = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
*first = ev;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CLASS_get_inheritance(CLASS *class, CLASS **her)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
int nher = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
while ((nher < MAX_INHERITANCE) && (class != NULL))
|
|
|
|
{
|
|
|
|
*her++ = class;
|
|
|
|
nher++;
|
|
|
|
class = class->parent;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return nher;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void *CLASS_auto_create(CLASS *class, int nparam)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
void *ob = class->instance;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (ob)
|
|
|
|
{
|
|
|
|
if (!class->check || !class->check(ob))
|
|
|
|
{
|
|
|
|
RELEASE_MANY(SP, nparam);
|
|
|
|
return ob;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
OBJECT_UNREF(class->instance, "CLASS_auto_create");
|
|
|
|
class->instance = NULL;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
/*fprintf(stderr, "CLASS_auto_create: create %s\n", class->name);*/
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
OBJECT_create(&class->instance, class, NULL, NULL, nparam);
|
|
|
|
ob = class->instance;
|
|
|
|
OBJECT_REF(ob, "CLASS_auto_create");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return ob;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
CLASS *CLASS_copy(CLASS *class_src, char *prefix)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class;
|
|
|
|
CLASS save;
|
|
|
|
int len = strlen(prefix) + 1 + strlen(class_src->name) + 1;
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
ALLOC(&name, len, "CLASS_copy");
|
|
|
|
sprintf(name, "%s.%s", prefix, class_src->name);
|
|
|
|
|
|
|
|
class = CLASS_find(name);
|
|
|
|
|
|
|
|
if (!class->ready)
|
|
|
|
{
|
|
|
|
save = *class;
|
|
|
|
*class = *class_src;
|
|
|
|
|
|
|
|
class->name = name;
|
|
|
|
class->ready = TRUE;
|
|
|
|
class->copy = TRUE;
|
|
|
|
class->template = class_src;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
FREE(&name, "CLASS_copy");
|
|
|
|
|
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
void CLASS_search_special(CLASS *class)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
class->special[SPEC_NEW] = CLASS_get_symbol_index_kind(class, "_new", CD_METHOD, 0);
|
|
|
|
class->special[SPEC_FREE] = CLASS_get_symbol_index_kind(class, "_free", CD_METHOD, 0);
|
|
|
|
class->special[SPEC_GET] = CLASS_get_symbol_index_kind(class, "_get", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_PUT] = CLASS_get_symbol_index_kind(class, "_put", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_FIRST] = CLASS_get_symbol_index_kind(class, "_first", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_NEXT] = CLASS_get_symbol_index_kind(class, "_next", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_CALL] = CLASS_get_symbol_index_kind(class, "_call", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_UNKNOWN] = CLASS_get_symbol_index_kind(class, "_unknown", CD_METHOD, CD_STATIC_METHOD);
|
|
|
|
class->special[SPEC_COMPARE] = CLASS_get_symbol_index_kind(class, "_compare", CD_METHOD, 0);
|
|
|
|
class->special[SPEC_PRINT] = CLASS_get_symbol_index_kind(class, "_print", CD_METHOD, 0);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (class->special[SPEC_NEXT] != NO_SYMBOL)
|
2009-01-01 14:42:17 +01:00
|
|
|
class->enum_static = CLASS_DESC_get_type(CLASS_get_desc(class, class->special[SPEC_NEXT])) == CD_STATIC_METHOD;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_check_global(char *name)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS *class, *parent;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
class = CLASS_find_global(name);
|
|
|
|
if (class->state)
|
|
|
|
{
|
|
|
|
if (COMPONENT_current && class->component == COMPONENT_current)
|
|
|
|
ERROR_panic("Class '%s' declared twice in the component '%s'.", class->name, class->component->name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
parent = class;
|
|
|
|
class = CLASS_replace_global(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_inheritance(class, parent);
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
return class;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC_METHOD *CLASS_get_special_desc(CLASS *class, int spec)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
short index = class->special[spec];
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
if (index == NO_SYMBOL)
|
|
|
|
return NULL;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
|
|
|
return &CLASS_get_desc(class, index)->method;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CLASS_DESC_SYMBOL *CLASS_get_next_sorted_symbol(CLASS *class, int *index)
|
|
|
|
{
|
2009-01-01 14:42:17 +01:00
|
|
|
CLASS_DESC_SYMBOL *old = NULL;
|
|
|
|
CLASS_DESC_SYMBOL *cur;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (*index >= class->n_desc)
|
|
|
|
return NULL;
|
|
|
|
|
2009-01-01 14:42:17 +01:00
|
|
|
cur = &class->table[class->table[*index].sort];
|
|
|
|
if (*index > 0)
|
|
|
|
old = &class->table[class->table[*index - 1].sort];
|
|
|
|
|
|
|
|
(*index)++;
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!cur->desc)
|
|
|
|
continue;
|
2009-01-01 14:42:17 +01:00
|
|
|
|
|
|
|
if (old && !TABLE_compare_ignore_case(cur->name, cur->len, old->name, old->len))
|
|
|
|
continue;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CLASS_create_array_class(CLASS *class)
|
|
|
|
{
|
2008-08-31 02:32:21 +02:00
|
|
|
void *save = TYPE_joker;
|
|
|
|
char *name_joker;
|
2007-12-30 17:41:49 +01:00
|
|
|
GB_DESC *desc;
|
2008-08-31 02:32:21 +02:00
|
|
|
|
2008-10-23 16:09:48 +02:00
|
|
|
//fprintf(stderr, "CLASS_create_array_class: create %s\n", class->name);
|
2008-09-01 01:45:47 +02:00
|
|
|
|
2008-08-31 02:32:21 +02:00
|
|
|
STRING_new(&name_joker, class->name, strlen(class->name) - 2);
|
|
|
|
|
2008-09-22 01:22:07 +02:00
|
|
|
TYPE_joker = class->array_type = class->global ? CLASS_find_global(name_joker) : CLASS_find(name_joker);
|
2009-01-05 12:38:03 +01:00
|
|
|
class->array_type->array_class = class;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
ALLOC(&desc, sizeof(GB_DESC) * ARRAY_TEMPLATE_NDESC, "CLASS_create_array_class");
|
|
|
|
memcpy(desc, NATIVE_TemplateArray, sizeof(GB_DESC) * ARRAY_TEMPLATE_NDESC);
|
|
|
|
((CLASS_DESC_GAMBAS *)desc)->name = class->name;
|
|
|
|
|
|
|
|
CLASS_register_class(desc, class);
|
|
|
|
|
|
|
|
class->data = (char *)desc;
|
2009-01-05 12:38:03 +01:00
|
|
|
|
2008-08-31 02:32:21 +02:00
|
|
|
STRING_free(&name_joker);
|
2007-12-30 17:41:49 +01:00
|
|
|
TYPE_joker = save;
|
|
|
|
}
|
2009-01-05 12:38:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
CLASS *CLASS_get_array_class(CLASS *class)
|
|
|
|
{
|
|
|
|
if (!class->array_class)
|
|
|
|
{
|
|
|
|
char name[strlen(class->name) + 3];
|
|
|
|
strcpy(name, class->name);
|
|
|
|
strcat(name, "[]");
|
|
|
|
class->array_class = class->global ? CLASS_find_global(name) : CLASS_find(name);
|
|
|
|
CLASS_create_array_class(class->array_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return class->array_class;
|
|
|
|
}
|