7023d2a95a6fe11abe6d04edf9061c81e3210b26
[libfirm] / ir / ir / irprintf.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irprintf.h
4  * Purpose:     A little printf understanding some firm types.
5  * Author:      Sebastian Hack
6  * Created:     29.11.2004
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 1998-2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 /**
13  * @file irprintf.h
14  *
15  * A little printf understanding some firm types.
16  * @author Sebastian Hack
17  * @date 29.11.2004
18  */
19
20 #ifndef _IRPRINTF_H
21 #define _IRPRINTF_H
22
23 #include <stddef.h>
24 #include <stdio.h>
25
26 /**
27  * Something that can append strings and chars to something.
28  */
29 typedef struct _appender_t {
30         void (*append_char)(void *object, size_t n, char ch);
31         void (*append_str)(void *object, size_t n, const char *str);
32 } appender_t;
33
34 /**
35  * A callback function type to add something to an appender.
36  *
37  * @param app    The appender.
38  * @param object The object for the appender.
39  * @param limit  The limit for the appender.
40  * @param arg    The thing to append.
41  */
42 typedef void (ir_printf_cb_t)(const appender_t *app, void *object, size_t limit, const void *arg);
43
44 /**
45  * A string formatting routine for ir objects.
46  * This function rudimentarily implements a kind of printf(3) for ir
47  * nodes. Following conversion specifiers. No length, special or field
48  * width specifiers are accepted.
49  * - @%p A pointer.
50  * - @%s A string.
51  * - @%I An ident.
52  * - @%e An entity name.
53  * - @%E An entity ld_name.
54  * - @%n A full description of a node.
55  * - @%O The opcode name of an ir node.
56  * - @%m The mode name of an ir mode.
57  * - @%N The node number of an ir node.
58  * - @%B The block node number of the nodes block.
59  * - @%b A bitset.
60  * - @%t A tarval.
61  *
62  * Each of these can be prepended by a '+' which means, that the given
63  * pointer is a collection of items specified by the format. In this
64  * case you also have to pass an iterator interface to ir_printf()
65  * suitable for the instance of the collection. So, imagine you have a
66  * @c pset of ir_nodes and want to dump it, you write:
67  * @code
68  *   pset *nodes;
69  *   ...
70  *   ir_printf("Some nodes: %+n\n", it_pset, nodes);
71  * @endcode
72  * The @c it_pset is an iterator interface (of type
73  * @c iterator_t that allows the dumper to traverse the set.
74  *
75  * As special case when working with collections, you can also give a
76  * callback function which will be invoked on each element in the
77  * collection. It gets the appender (the thing where the textual
78  * representation of the element is written to) and its parameters
79  * passed by the dumping function. Suppose you have your own datatype
80  * @c xyz_t and want to dump a pset of it, you have:
81  * @code
82  *   void xyz_dump(const appender_t *app, void *object, size_t limit,
83  *       const void *arg)
84  *   {
85  *     const xyz_t *xyz = arg;
86  *     app->append_str(object, limit, xyz->name);
87  *   }
88  *   ...
89  *   pset *xyzs;
90  *
91  *   ir_printf("A set of xyz\'s: %+C\n", it_pset, xyzs, xyz_dump);
92  * @endcode
93  *
94  * @param fmt The format string.
95  */
96 void ir_printf(const char *fmt, ...);
97
98 /**
99  * @see irn_printf.
100  */
101 void ir_fprintf(FILE *f, const char *fmt, ...);
102
103 /**
104  * @see irn_printf.
105  */
106 void ir_snprintf(char *buf, size_t n, const char *fmt, ...);
107
108
109 #endif