entity.c 22.3 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Michael Beck's avatar
Michael Beck committed
6
7
8
9
/**
 * @file
 * @brief   Representation of all program known entities.
 * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Michael Beck
10
 */
11
12
#include <stdlib.h>
#include <stddef.h>
13

Michael Beck's avatar
Michael Beck committed
14
15
16
#include "xmalloc.h"
#include "entity_t.h"
#include "array.h"
17
#include "util.h"
Michael Beck's avatar
Michael Beck committed
18
19
#include "irhooks.h"
#include "irprog_t.h"
20
#include "ircons_t.h"
Michael Beck's avatar
Michael Beck committed
21
#include "tv_t.h"
22
23
24
#include "irdump.h"
#include "irgraph_t.h"
#include "callgraph.h"
Matthias Braun's avatar
Matthias Braun committed
25
#include "panic.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
26

27
/** The name of the unknown entity. */
28
#define UNKNOWN_ENTITY_NAME "unknown_entity"
29

30
31
32
33
34
ir_entity *get_unknown_entity(void)
{
	return irp->unknown_entity;
}

Matthias Braun's avatar
Matthias Braun committed
35
static ir_entity *intern_new_entity(ir_type *owner, ir_entity_kind kind,
36
                                    ident *name, ir_type *type)
Christian Schäfer's avatar
Christian Schäfer committed
37
{
Matthias Braun's avatar
Matthias Braun committed
38
	assert(owner != NULL);
39

40
	ir_entity *res = XMALLOCZ(ir_entity);
Matthias Braun's avatar
Matthias Braun committed
41
42
43
44
45
46
47
48
49
	res->kind        = k_entity;
	res->name        = name;
	res->type        = type;
	res->owner       = owner;
	res->entity_kind = kind;
	res->volatility  = volatility_non_volatile;
	res->aligned     = align_is_aligned;
	res->usage       = ir_usage_unknown;
	res->visibility  = ir_visibility_external;
50
#ifdef DEBUG_libfirm
Matthias Braun's avatar
Matthias Braun committed
51
	res->nr          = get_irp_new_node_nr();
52
53
54
#endif

	/* Remember entity in its owner. */
55
	add_compound_member(owner, res);
56
57
58
59
60

	res->visit = 0;
	return res;
}

61
ir_entity *new_entity(ir_type *owner, ident *name, ir_type *type)
62
{
Matthias Braun's avatar
Matthias Braun committed
63
	ir_entity *res;
64
	if (is_Method_type(type)) {
65
		ir_graph *irg = get_const_code_irg();
66
		res                = intern_new_entity(owner, IR_ENTITY_METHOD, name, type);
67
		ir_node *const val = new_r_Address(irg, res);
68
		set_atomic_ent_value(res, val);
Matthias Braun's avatar
Matthias Braun committed
69
70
		res->linkage                   = IR_LINKAGE_CONSTANT;
		res->attr.mtd_attr.properties  = get_method_additional_properties(type);
Matthias Braun's avatar
Matthias Braun committed
71
72
73
74
		res->attr.mtd_attr.vtable_number = IR_VTABLE_NUM_NOT_SET;
		res->attr.mtd_attr.param_access  = NULL;
		res->attr.mtd_attr.param_weight  = NULL;
		res->attr.mtd_attr.irg           = NULL;
75
	} else if (is_compound_type(owner) && !(owner->flags & tf_segment)) {
76
		res = intern_new_entity(owner, IR_ENTITY_COMPOUND_MEMBER, name, type);
77
		res->attr.compound_member.offset = -1;
Matthias Braun's avatar
Matthias Braun committed
78
	} else {
79
		res = intern_new_entity(owner, IR_ENTITY_NORMAL, name, type);
80
81
	}

82
83
84
	hook_new_entity(res);
	return res;
}
85

86
87
88
89
90
91
static ident *make_parameter_entity_name(size_t pos)
{
	char buf[64];
	snprintf(buf, sizeof(buf), "parameter.%lu", (unsigned long) pos);
	return new_id_from_str(buf);
}
Christian Schäfer's avatar
Christian Schäfer committed
92

93
ir_entity *new_parameter_entity(ir_type *owner, size_t pos, ir_type *type)
94
{
Matthias Braun's avatar
Matthias Braun committed
95
	ident     *name = make_parameter_entity_name(pos);
Matthias Braun's avatar
Matthias Braun committed
96
	ir_entity *res  = intern_new_entity(owner, IR_ENTITY_PARAMETER, name, type);
97
	res->attr.compound_member.offset = -1;
98
	res->attr.parameter.number = pos;
99
100
	hook_new_entity(res);
	return res;
101
}
102

103
ir_entity *new_label_entity(ir_label_t label)
Matthias Braun's avatar
Matthias Braun committed
104
105
106
{
	ident *name = id_unique("label_%u");
	ir_type *global_type = get_glob_type();
Matthias Braun's avatar
Matthias Braun committed
107
108
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_LABEL, name,
	                                   get_code_type());
Matthias Braun's avatar
Matthias Braun committed
109
110
111
112
113
	res->attr.code_attr.label = label;
	hook_new_entity(res);
	return res;
}

Matthias Braun's avatar
Matthias Braun committed
114
115
116
117
118
119
120
121
122
123
124
125
ir_entity *new_got_entry_entity(ir_entity *referenced)
{
	ir_type *reftype = get_entity_type(referenced);
	ir_type *pointer = new_type_pointer(reftype);
	ir_type *global_type = get_glob_type();
	ir_entity *res = intern_new_entity(global_type, IR_ENTITY_GOTENTRY, NULL,
	                                   pointer);
	res->attr.got.referenced = referenced;
	hook_new_entity(res);
	return res;
}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
ir_entity *new_alias_entity(ir_type *owner, ident *name, ir_entity *aliased,
                            ir_type *type)
{
	ir_entity *res = intern_new_entity(owner, IR_ENTITY_ALIAS, name, type);
	res->attr.alias.aliased = aliased;
	hook_new_entity(res);
	return res;
}

void set_entity_alias(ir_entity *entity, ir_entity *aliased)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	entity->attr.alias.aliased = aliased;
}

ir_entity *get_entity_alias(const ir_entity *entity)
{
	assert(get_entity_kind(entity) == IR_ENTITY_ALIAS);
	return entity->attr.alias.aliased;
}

147
148
149
/**
 * Free entity attributes.
 */
Matthias Braun's avatar
Matthias Braun committed
150
151
static void free_entity_attrs(ir_entity *ent)
{
152
153
154
	if (ent->overwrites != NULL) {
		DEL_ARR_F(ent->overwrites);
		ent->overwrites = NULL;
155
	}
156
157
158
159
160
	if (ent->overwrittenby != NULL) {
		DEL_ARR_F(ent->overwrittenby);
		ent->overwrittenby = NULL;
	}

Matthias Braun's avatar
Matthias Braun committed
161
162
163
	if (ent->initializer != NULL) {
		/* TODO: free initializers */
	}
164
	if (ent->entity_kind == IR_ENTITY_METHOD) {
165
166
167
168
169
170
171
172
173
		if (ent->attr.mtd_attr.param_access) {
			DEL_ARR_F(ent->attr.mtd_attr.param_access);
			ent->attr.mtd_attr.param_access = NULL;
		}
		if (ent->attr.mtd_attr.param_weight) {
			DEL_ARR_F(ent->attr.mtd_attr.param_weight);
			ent->attr.mtd_attr.param_weight = NULL;
		}
	}
174
}
Christian Schäfer's avatar
Christian Schäfer committed
175

176
177
178
179
180
181
182
/**
 * Creates a deep copy of an entity.
 */
static ir_entity *deep_entity_copy(ir_entity *old)
{
	ir_entity *newe = XMALLOC(ir_entity);

183
	*newe = *old;
Matthias Braun's avatar
Matthias Braun committed
184
185
	if (old->initializer != NULL) {
		/* FIXME: the initializers are NOT copied */
186
187
188
189
190
	} else if (is_method_entity(old)) {
		/* do NOT copy them, reanalyze. This might be the best solution */
		newe->attr.mtd_attr.param_access = NULL;
		newe->attr.mtd_attr.param_weight = NULL;
	}
191
192
	newe->overwrites    = NULL;
	newe->overwrittenby = NULL;
193
194
195
196

#ifdef DEBUG_libfirm
	newe->nr = get_irp_new_node_nr();
#endif
197
	hook_new_entity(newe);
198
199
	return newe;
}
200

201
ir_entity *copy_entity_own(ir_entity *old, ir_type *new_owner)
202
{
203
	assert(old->kind == k_entity);
204
	assert(is_compound_type(new_owner));
205
	assert(get_type_state(new_owner) != layout_fixed);
206
207
208
209
	if (old->owner == new_owner)
		return old;

	/* create a deep copy so we are safe of aliasing and double-freeing. */
Matthias Braun's avatar
Matthias Braun committed
210
	ir_entity *newe = deep_entity_copy(old);
211
	newe->owner = new_owner;
212
	add_compound_member(new_owner, newe);
213

214
	return newe;
215
}
216

217
ir_entity *copy_entity_name(ir_entity *old, ident *new_name)
218
{
Matthias Braun's avatar
Matthias Braun committed
219
	assert(old->kind == k_entity);
220
221
222
	if (old->name == new_name)
		return old;

Matthias Braun's avatar
Matthias Braun committed
223
224
	ir_entity *newe = deep_entity_copy(old);
	newe->name    = new_name;
225
	newe->ld_name = NULL;
226
	add_compound_member(old->owner, newe);
227

228
	return newe;
229
}
230

231
void free_entity(ir_entity *ent)
232
{
233
	remove_compound_member(ent->owner, ent);
234

Matthias Braun's avatar
Matthias Braun committed
235
	assert(ent->kind == k_entity);
236
	free_entity_attrs(ent);
237
#ifdef DEBUG_libfirm
238
	ent->kind = k_BAD;
239
#endif
240
	free(ent);
241
}
242

243
long get_entity_nr(const ir_entity *ent)
244
{
Matthias Braun's avatar
Matthias Braun committed
245
	assert(ent->kind == k_entity);
246
#ifdef DEBUG_libfirm
247
	return ent->nr;
248
#else
249
	return (long)PTR_TO_INT(ent);
250
#endif
251
}
252

253
254
const char *(get_entity_name)(const ir_entity *ent)
{
255
	return _get_entity_name(ent);
Matthias Braun's avatar
Matthias Braun committed
256
}
Christian Schäfer's avatar
Christian Schäfer committed
257

258
259
ident *(get_entity_ident)(const ir_entity *ent)
{
260
	return _get_entity_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
261
}
Michael Beck's avatar
Michael Beck committed
262

263
264
void (set_entity_ident)(ir_entity *ent, ident *id)
{
265
	_set_entity_ident(ent, id);
Matthias Braun's avatar
Matthias Braun committed
266
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
267

268
269
ir_type *(get_entity_owner)(const ir_entity *ent)
{
270
	return _get_entity_owner(ent);
Matthias Braun's avatar
Matthias Braun committed
271
}
Christian Schäfer's avatar
Christian Schäfer committed
272

273
void set_entity_owner(ir_entity *ent, ir_type *owner)
274
{
275
	assert(ent->kind == k_entity);
276
	assert(is_compound_type(owner));
277
278
279

	remove_compound_member(ent->owner, ent);
	add_compound_member(owner, ent);
280
	ent->owner = owner;
Matthias Braun's avatar
Matthias Braun committed
281
}
Christian Schäfer's avatar
Christian Schäfer committed
282

Matthias Braun's avatar
Matthias Braun committed
283
284
ident *(get_entity_ld_ident)(const ir_entity *ent)
{
285
	return _get_entity_ld_ident(ent);
Matthias Braun's avatar
Matthias Braun committed
286
}
287

288
289
void (set_entity_ld_ident)(ir_entity *ent, ident *ld_ident)
{
290
	_set_entity_ld_ident(ent, ld_ident);
Matthias Braun's avatar
Matthias Braun committed
291
}
Christian Schäfer's avatar
Christian Schäfer committed
292

Matthias Braun's avatar
Matthias Braun committed
293
294
const char *(get_entity_ld_name)(const ir_entity *ent)
{
295
	return _get_entity_ld_name(ent);
Matthias Braun's avatar
Matthias Braun committed
296
}
297

298
299
300
301
302
int entity_has_ld_ident(const ir_entity *entity)
{
	return entity->ld_name != NULL;
}

303
304
ir_type *(get_entity_type)(const ir_entity *ent)
{
305
	return _get_entity_type(ent);
Matthias Braun's avatar
Matthias Braun committed
306
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
307

Matthias Braun's avatar
Matthias Braun committed
308
309
310
311
312
313
314
315
316
317
void set_entity_type(ir_entity *ent, ir_type *type)
{
	switch (ent->entity_kind) {
	case IR_ENTITY_METHOD:
		assert(is_Method_type(type));
		break;
	case IR_ENTITY_NORMAL:
		assert(!is_Method_type(type));
		break;
	case IR_ENTITY_LABEL:
318
		assert(type == get_code_type());
Matthias Braun's avatar
Matthias Braun committed
319
320
321
322
323
		break;
	case IR_ENTITY_COMPOUND_MEMBER:
		break;
	}
	ent->type = type;
Matthias Braun's avatar
Matthias Braun committed
324
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
325

326
327
ir_volatility (get_entity_volatility)(const ir_entity *ent)
{
328
	return _get_entity_volatility(ent);
Matthias Braun's avatar
Matthias Braun committed
329
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
330

331
332
void (set_entity_volatility)(ir_entity *ent, ir_volatility vol)
{
333
	_set_entity_volatility(ent, vol);
Matthias Braun's avatar
Matthias Braun committed
334
}
Götz Lindenmaier's avatar
a    
Götz Lindenmaier committed
335

336
const char *get_volatility_name(ir_volatility var)
337
{
338
#define X(a)    case a: return #a
339
340
341
342
	switch (var) {
	X(volatility_non_volatile);
	X(volatility_is_volatile);
	}
343
#undef X
Matthias Braun's avatar
Matthias Braun committed
344
	return "BAD VALUE";
345
}
346

347
348
ir_align (get_entity_aligned)(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
349
350
	return _get_entity_aligned(ent);
}
351

352
353
void (set_entity_aligned)(ir_entity *ent, ir_align a)
{
Matthias Braun's avatar
Matthias Braun committed
354
355
	_set_entity_aligned(ent, a);
}
356

357
358
unsigned (get_entity_alignment)(const ir_entity *ent)
{
359
360
361
	return _get_entity_alignment(ent);
}

362
363
void (set_entity_alignment)(ir_entity *ent, unsigned alignment)
{
364
365
366
	_set_entity_alignment(ent, alignment);
}

Michael Beck's avatar
Michael Beck committed
367
368
369
370
371
372
373
374
const char *get_align_name(ir_align a)
{
#define X(a)    case a: return #a
	switch (a) {
	X(align_non_aligned);
	X(align_is_aligned);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
375
	return "BAD VALUE";
376
}
Michael Beck's avatar
Michael Beck committed
377

378
void set_entity_label(ir_entity *ent, ir_label_t label)
379
{
Matthias Braun's avatar
Matthias Braun committed
380
	assert(ent->entity_kind == IR_ENTITY_LABEL);
381
382
383
384
385
	ent->attr.code_attr.label = label;
}

ir_label_t get_entity_label(const ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
386
	assert(ent->entity_kind == IR_ENTITY_LABEL);
387
388
389
	return ent->attr.code_attr.label;
}

390
391
392
393
394
395
396
void set_entity_visibility(ir_entity *entity, ir_visibility visibility)
{
	entity->visibility = visibility;
}

ir_visibility get_entity_visibility(const ir_entity *entity)
{
397
	return (ir_visibility)entity->visibility;
398
399
}

Matthias Braun's avatar
Matthias Braun committed
400
401
402
403
404
405
406
407
408
void set_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage = linkage;
}

ir_linkage (get_entity_linkage)(const ir_entity *entity)
{
	return get_entity_linkage(entity);
}
409

Matthias Braun's avatar
Matthias Braun committed
410
411
412
413
void add_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage |= linkage;
}
414

Matthias Braun's avatar
Matthias Braun committed
415
416
417
418
void remove_entity_linkage(ir_entity *entity, ir_linkage linkage)
{
	entity->linkage &= ~linkage;
}
419

420
421
int (is_entity_compiler_generated)(const ir_entity *ent)
{
Michael Beck's avatar
Michael Beck committed
422
	return _is_entity_compiler_generated(ent);
423
}
424

425
426
void (set_entity_compiler_generated)(ir_entity *ent, int flag)
{
Michael Beck's avatar
Michael Beck committed
427
	_set_entity_compiler_generated(ent, flag);
428
}
429

430
431
ir_entity_usage (get_entity_usage)(const ir_entity *ent)
{
432
433
	return _get_entity_usage(ent);
}
Michael Beck's avatar
Michael Beck committed
434

435
436
void (set_entity_usage)(ir_entity *ent, ir_entity_usage flags)
{
437
438
	_set_entity_usage(ent, flags);
}
439

440
ir_node *get_atomic_ent_value(const ir_entity *entity)
Matthias Braun's avatar
Matthias Braun committed
441
{
Matthias Braun's avatar
Matthias Braun committed
442
	assert(is_atomic_entity(entity));
Matthias Braun's avatar
Matthias Braun committed
443
444

	ir_initializer_t *initializer = get_entity_initializer(entity);
Matthias Braun's avatar
Matthias Braun committed
445
446
447
448
	if (initializer == NULL) {
		ir_type *type = get_entity_type(entity);
		return new_r_Unknown(get_const_code_irg(), get_type_mode(type));
	}
449

Matthias Braun's avatar
Matthias Braun committed
450
451
452
453
	switch (get_initializer_kind(initializer)) {
	case IR_INITIALIZER_NULL: {
		ir_type *type = get_entity_type(entity);
		ir_mode *mode = get_type_mode(type);
454
		return new_r_Const_null(get_const_code_irg(), mode);
Matthias Braun's avatar
Matthias Braun committed
455
456
	}
	case IR_INITIALIZER_TARVAL: {
Matthias Braun's avatar
Matthias Braun committed
457
		ir_tarval *tv = get_initializer_tarval_value(initializer);
Matthias Braun's avatar
Matthias Braun committed
458
459
460
461
462
463
464
465
		return new_r_Const(get_const_code_irg(), tv);
	}
	case IR_INITIALIZER_CONST:
		return get_initializer_const_value(initializer);
	case IR_INITIALIZER_COMPOUND:
		panic("compound initializer in atomic entity not allowed (%+F)", entity);
	}

466
	panic("invalid initializer kind (%+F)", entity);
Matthias Braun's avatar
Matthias Braun committed
467
468
469
}

void set_atomic_ent_value(ir_entity *entity, ir_node *val)
470
{
Matthias Braun's avatar
Matthias Braun committed
471
472
	assert(is_atomic_entity(entity));
	assert(is_Dummy(val) || get_irn_mode(val) == get_type_mode(entity->type));
Matthias Braun's avatar
Matthias Braun committed
473
	ir_initializer_t *initializer = create_initializer_const(val);
Matthias Braun's avatar
Matthias Braun committed
474
475
	entity->initializer = initializer;
}
476

477
478
479
480
481
482
483
484
485
486
const char *get_initializer_kind_name(ir_initializer_kind_t ini)
{
#define X(a)    case a: return #a
	switch (ini) {
	X(IR_INITIALIZER_CONST);
	X(IR_INITIALIZER_TARVAL);
	X(IR_INITIALIZER_NULL);
	X(IR_INITIALIZER_COMPOUND);
	}
#undef X
Matthias Braun's avatar
Matthias Braun committed
487
	return "BAD VALUE";
488
489
}

490
491
492
493
494
495
496
497
498
499
500
501
static ir_initializer_t null_initializer = { IR_INITIALIZER_NULL };

ir_initializer_t *get_initializer_null(void)
{
	return &null_initializer;
}

ir_initializer_t *create_initializer_const(ir_node *value)
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
502
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_const_t);
503
504
505
506
507
508
	initializer->kind         = IR_INITIALIZER_CONST;
	initializer->consti.value = value;

	return initializer;
}

Matthias Braun's avatar
Matthias Braun committed
509
ir_initializer_t *create_initializer_tarval(ir_tarval *tv)
510
511
512
513
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

	ir_initializer_t *initializer
514
		= (ir_initializer_t*)OALLOC(obst, ir_initializer_tarval_t);
515
516
517
518
519
520
	initializer->kind         = IR_INITIALIZER_TARVAL;
	initializer->tarval.value = tv;

	return initializer;
}

521
ir_initializer_t *create_initializer_compound(size_t n_entries)
522
523
524
{
	struct obstack *obst = get_irg_obstack(get_const_code_irg());

Matthias Braun's avatar
Matthias Braun committed
525
526
527
	size_t size = sizeof(ir_initializer_compound_t)
	            + n_entries * sizeof(ir_initializer_t*)
	            - sizeof(ir_initializer_t*);
528

529
530
	ir_initializer_t *initializer
		= (ir_initializer_t*)obstack_alloc(obst, size);
531
532
533
	initializer->kind                    = IR_INITIALIZER_COMPOUND;
	initializer->compound.n_initializers = n_entries;

Matthias Braun's avatar
Matthias Braun committed
534
	for (size_t i = 0; i < n_entries; ++i) {
535
536
537
538
539
540
541
542
543
		initializer->compound.initializers[i] = get_initializer_null();
	}

	return initializer;
}

ir_node *get_initializer_const_value(const ir_initializer_t *initializer)
{
	assert(initializer->kind == IR_INITIALIZER_CONST);
544
	return skip_Id(initializer->consti.value);
545
546
}

Matthias Braun's avatar
Matthias Braun committed
547
ir_tarval *get_initializer_tarval_value(const ir_initializer_t *initializer)
548
549
550
551
552
{
	assert(initializer->kind == IR_INITIALIZER_TARVAL);
	return initializer->tarval.value;
}

553
size_t get_initializer_compound_n_entries(const ir_initializer_t *initializer)
554
555
556
557
558
559
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	return initializer->compound.n_initializers;
}

void set_initializer_compound_value(ir_initializer_t *initializer,
560
                                    size_t index, ir_initializer_t *value)
561
562
563
564
565
566
567
568
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	initializer->compound.initializers[index] = value;
}

ir_initializer_t *get_initializer_compound_value(
569
		const ir_initializer_t *initializer, size_t index)
570
571
572
573
574
575
576
577
578
579
580
581
582
583
{
	assert(initializer->kind == IR_INITIALIZER_COMPOUND);
	assert(index < initializer->compound.n_initializers);

	return initializer->compound.initializers[index];
}

ir_initializer_kind_t get_initializer_kind(const ir_initializer_t *initializer)
{
	return initializer->kind;
}

static void check_entity_initializer(ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
584
585
#ifndef NDEBUG
	ir_initializer_t *initializer = entity->initializer;
586
587
	if (initializer == NULL)
		return;
588
	ir_type          *entity_tp   = get_entity_type(entity);
Matthias Braun's avatar
Matthias Braun committed
589
590
	switch (initializer->kind) {
	case IR_INITIALIZER_COMPOUND:
591
		assert(is_aggregate_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
592
593
		break;
	case IR_INITIALIZER_CONST:
594
		/* methods are initialized by an Address */
595
596
		assert(is_atomic_type(entity_tp) || is_Method_type(entity_tp));
		break;
Matthias Braun's avatar
Matthias Braun committed
597
	case IR_INITIALIZER_TARVAL:
598
		assert(is_atomic_type(entity_tp));
Matthias Braun's avatar
Matthias Braun committed
599
600
601
602
		break;
	case IR_INITIALIZER_NULL:
		break;
	}
yb9976's avatar
yb9976 committed
603
604
#else
	(void)entity;
Matthias Braun's avatar
Matthias Braun committed
605
#endif
606
607
608
609
}

void set_entity_initializer(ir_entity *entity, ir_initializer_t *initializer)
{
Matthias Braun's avatar
Matthias Braun committed
610
	entity->initializer = initializer;
611
612
613
614
615
	check_entity_initializer(entity);
}

ir_initializer_t *get_entity_initializer(const ir_entity *entity)
{
Matthias Braun's avatar
Matthias Braun committed
616
	return entity->initializer;
617
618
}

619
620
int (get_entity_offset)(const ir_entity *ent)
{
621
	return _get_entity_offset(ent);
622
}
Michael Beck's avatar
Michael Beck committed
623

624
625
void (set_entity_offset)(ir_entity *ent, int offset)
{
626
	_set_entity_offset(ent, offset);
627
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
628

629
unsigned (get_entity_bitfield_offset)(const ir_entity *ent)
630
{
631
	return _get_entity_bitfield_offset(ent);
632
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
633

634
void (set_entity_bitfield_offset)(ir_entity *ent, unsigned offset)
635
{
636
637
638
639
640
641
642
643
644
645
646
	_set_entity_bitfield_offset(ent, offset);
}

unsigned (get_entity_bitfield_size)(const ir_entity *ent)
{
	return _get_entity_bitfield_size(ent);
}

void (set_entity_bitfield_size)(ir_entity *ent, unsigned size)
{
	_set_entity_bitfield_size(ent, size);
647
}
Michael Beck's avatar
Michael Beck committed
648

649
650
void add_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
651
652
653
	if (ent->overwrites == NULL) {
		ent->overwrites = NEW_ARR_F(ir_entity*, 0);
	}
654
	ARR_APP1(ir_entity *, ent->overwrites, overwritten);
655
656
657
	if (overwritten->overwrittenby == NULL) {
		overwritten->overwrittenby = NEW_ARR_F(ir_entity*, 0);
	}
658
	ARR_APP1(ir_entity *, overwritten->overwrittenby, ent);
659
}
660

661
size_t get_entity_n_overwrites(const ir_entity *ent)
662
{
663
664
665
	if (ent->overwrites == NULL)
		return 0;
	return ARR_LEN(ent->overwrites);
666
}
667

668
size_t get_entity_overwrites_index(const ir_entity *ent, ir_entity *overwritten)
669
{
Matthias Braun's avatar
Matthias Braun committed
670
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
671
672
		if (get_entity_overwrites(ent, i) == overwritten)
			return i;
673
	}
674
	return (size_t)-1;
675
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
676

677
ir_entity *get_entity_overwrites(const ir_entity *ent, size_t pos)
678
{
679
680
	assert(pos < get_entity_n_overwrites(ent));
	return ent->overwrites[pos];
681
}
682

683
void set_entity_overwrites(ir_entity *ent, size_t pos, ir_entity *overwritten)
684
{
685
686
	assert(pos < get_entity_n_overwrites(ent));
	ent->overwrites[pos] = overwritten;
687
}
688

689
690
void remove_entity_overwrites(ir_entity *ent, ir_entity *overwritten)
{
Matthias Braun's avatar
Matthias Braun committed
691
	for (size_t i = 0, n = get_entity_n_overwrites(ent); i < n; ++i) {
692
		if (ent->overwrites[i] == overwritten) {
693
			for (; i < n - 1; i++)
694
				ent->overwrites[i] = ent->overwrites[i+1];
695
			ARR_SETLEN(ir_entity*, ent->overwrites, n - 1);
696
697
			break;
		}
698
	}
699
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
700

701

702
size_t get_entity_n_overwrittenby(const ir_entity *ent)
703
{
704
705
	if (ent->overwrittenby == NULL)
		return 0;
706
	return ARR_LEN(ent->overwrittenby);
707
}
708

709
710
size_t get_entity_overwrittenby_index(const ir_entity *ent,
                                      ir_entity *overwrites)
711
{
Matthias Braun's avatar
Matthias Braun committed
712
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
713
714
		if (get_entity_overwrittenby(ent, i) == overwrites)
			return i;
715
	}
716
	return (size_t)-1;
717
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
718

719
ir_entity *get_entity_overwrittenby(const ir_entity *ent, size_t pos)
720
{
721
722
	assert(pos < get_entity_n_overwrittenby(ent));
	return ent->overwrittenby[pos];
723
}
724

725
void set_entity_overwrittenby(ir_entity *ent, size_t pos, ir_entity *overwrites)
726
{
727
728
	assert(pos < get_entity_n_overwrittenby(ent));
	ent->overwrittenby[pos] = overwrites;
729
}
730

731
732
void remove_entity_overwrittenby(ir_entity *ent, ir_entity *overwrites)
{
Matthias Braun's avatar
Matthias Braun committed
733
	for (size_t i = 0, n = get_entity_n_overwrittenby(ent); i < n; ++i) {
734
		if (ent->overwrittenby[i] == overwrites) {
735
			for (; i < n - 1; ++i)
736
				ent->overwrittenby[i] = ent->overwrittenby[i+1];
737
			ARR_SETLEN(ir_entity*, ent->overwrittenby, n - 1);
738
739
			break;
		}
740
	}
741
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
742

743
744
void *(get_entity_link)(const ir_entity *ent)
{
745
	return _get_entity_link(ent);
746
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
747

748
749
void (set_entity_link)(ir_entity *ent, void *l)
{
750
	_set_entity_link(ent, l);
751
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
752

753
754
ir_graph *(get_entity_irg)(const ir_entity *ent)
{
755
	return _get_entity_irg(ent);
756
}
Christian Schäfer's avatar
Christian Schäfer committed
757

758
759
760
761
762
ir_graph *(get_entity_linktime_irg)(const ir_entity *ent)
{
	return _get_entity_linktime_irg(ent);
}

763
764
void set_entity_irg(ir_entity *ent, ir_graph *irg)
{
765
766
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.irg = irg;
767
}
768

769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
int (is_parameter_entity)(const ir_entity *entity)
{
	return _is_parameter_entity(entity);
}

size_t (get_entity_parameter_number)(const ir_entity *entity)
{
	return _get_entity_parameter_number(entity);
}

void set_entity_parameter_number(ir_entity *entity, size_t n)
{
	assert(is_parameter_entity(entity));
	entity->attr.parameter.number = n;
}

785
786
unsigned get_entity_vtable_number(const ir_entity *ent)
{
yb9976's avatar
yb9976 committed
787
	assert(is_method_entity(ent));
788
	return ent->attr.mtd_attr.vtable_number;
789
}
790

791
792
void set_entity_vtable_number(ir_entity *ent, unsigned vtable_number)
{
793
794
	assert(is_method_entity(ent));
	ent->attr.mtd_attr.vtable_number = vtable_number;
795
}
796

797
798
799
800
801
int is_unknown_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_UNKNOWN;
}

Matthias Braun's avatar
Matthias Braun committed
802
int is_atomic_entity(const ir_entity *ent)
803
{
Matthias Braun's avatar
Matthias Braun committed
804
	ir_type     *t  = get_entity_type(ent);
805
	const tp_op *op = get_type_tpop(t);
806
807
	return op == type_primitive || op == type_pointer
	    || op == type_method;
808
}
809

Matthias Braun's avatar
Matthias Braun committed
810
int is_compound_entity(const ir_entity *ent)
811
{
812
813
814
	ir_type     *t  = get_entity_type(ent);
	const tp_op *op = get_type_tpop(t);
	return (op == type_class || op == type_struct ||
815
	        op == type_array || op == type_union);
816
}
817

Matthias Braun's avatar
Matthias Braun committed
818
int is_method_entity(const ir_entity *ent)
819
{
820
821
822
823
824
825
	return ent->entity_kind == IR_ENTITY_METHOD;
}

int is_alias_entity(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_ALIAS;
826
}
827

Matthias Braun's avatar
Matthias Braun committed
828
ir_visited_t (get_entity_visited)(const ir_entity *ent)
829
{
830
	return _get_entity_visited(ent);
831
}
Michael Beck's avatar
Michael Beck committed
832

833
834
void (set_entity_visited)(ir_entity *ent, ir_visited_t num)
{
835
	_set_entity_visited(ent, num);
836
}
Michael Beck's avatar
Michael Beck committed
837

838
839
void (mark_entity_visited)(ir_entity *ent)
{
840
	_mark_entity_visited(ent);
841
}
Götz Lindenmaier's avatar
Götz Lindenmaier committed
842

Matthias Braun's avatar
Matthias Braun committed
843
int (entity_visited)(const ir_entity *ent)
844
{
845
	return _entity_visited(ent);
846
}
847

Matthias Braun's avatar
Matthias Braun committed
848
int (entity_not_visited)(const ir_entity *ent)
849
{
850
	return _entity_not_visited(ent);
851
}
852

853
854
855
856
857
858
int entity_has_additional_properties(const ir_entity *entity)
{
	return entity->entity_kind == IR_ENTITY_METHOD
	    || entity->entity_kind == IR_ENTITY_ALIAS;
}

859
mtp_additional_properties get_entity_additional_properties(const ir_entity *ent)
Matthias Braun's avatar