blob: 36ab0e7ca5c104018fef5fc1ed1dfeb1b20b207b [file] [log] [blame]
Carl Wortha1e32bc2010-05-10 13:17:25 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#ifndef GLCPP_H
25#define GLCPP_H
26
Carl Worth35419092010-05-24 11:27:23 -070027#include <stdint.h>
28
Carl Wortha807fb72010-05-18 22:10:04 -070029#include <talloc.h>
30
Carl Worth0b27b5f2010-05-10 16:16:06 -070031#include "hash_table.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
33#define yyscan_t void*
34
Carl Wortha807fb72010-05-18 22:10:04 -070035/* Some data types used for parser values. */
Carl Worth0b27b5f2010-05-10 16:16:06 -070036
Carl Worth610053b2010-05-14 10:05:11 -070037typedef struct string_node {
Carl Worth33cc4002010-05-12 12:17:10 -070038 const char *str;
Carl Worth610053b2010-05-14 10:05:11 -070039 struct string_node *next;
40} string_node_t;
Carl Worth33cc4002010-05-12 12:17:10 -070041
Carl Worth610053b2010-05-14 10:05:11 -070042typedef struct string_list {
43 string_node_t *head;
44 string_node_t *tail;
45} string_list_t;
Carl Worth33cc4002010-05-12 12:17:10 -070046
Carl Worth808401f2010-05-25 14:52:43 -070047typedef struct token token_t;
48typedef struct token_list token_list_t;
49
50typedef union YYSTYPE
51{
Carl Worthf6914fd2010-05-26 09:32:57 -070052 intmax_t ival;
Carl Worth808401f2010-05-25 14:52:43 -070053 char *str;
Carl Worthb1854fd2010-05-25 16:28:26 -070054 string_list_t *string_list;
Carl Worth808401f2010-05-25 14:52:43 -070055 token_t *token;
56 token_list_t *token_list;
57} YYSTYPE;
58
59# define YYSTYPE_IS_TRIVIAL 1
60# define YYSTYPE_IS_DECLARED 1
61
62struct token {
Carl Worthb5693832010-05-20 08:01:44 -070063 int type;
Carl Worth808401f2010-05-25 14:52:43 -070064 YYSTYPE value;
65};
Carl Worthb5693832010-05-20 08:01:44 -070066
Carl Worth47252442010-05-19 13:54:37 -070067typedef struct token_node {
Carl Worth808401f2010-05-25 14:52:43 -070068 token_t *token;
Carl Worth47252442010-05-19 13:54:37 -070069 struct token_node *next;
70} token_node_t;
71
Carl Worth808401f2010-05-25 14:52:43 -070072struct token_list {
Carl Worth47252442010-05-19 13:54:37 -070073 token_node_t *head;
74 token_node_t *tail;
Carl Worth10ae4382010-05-25 20:35:01 -070075 token_node_t *non_space_tail;
Carl Worth808401f2010-05-25 14:52:43 -070076};
Carl Worth47252442010-05-19 13:54:37 -070077
Carl Worth8f6a8282010-05-14 10:44:19 -070078typedef struct argument_node {
Carl Worth47252442010-05-19 13:54:37 -070079 token_list_t *argument;
Carl Worth8f6a8282010-05-14 10:44:19 -070080 struct argument_node *next;
81} argument_node_t;
82
83typedef struct argument_list {
84 argument_node_t *head;
85 argument_node_t *tail;
86} argument_list_t;
87
Carl Worth33cc4002010-05-12 12:17:10 -070088typedef struct glcpp_parser glcpp_parser_t;
89
Carl Wortha807fb72010-05-18 22:10:04 -070090typedef enum {
Carl Wortha807fb72010-05-18 22:10:04 -070091 TOKEN_CLASS_IDENTIFIER,
Carl Worthb5693832010-05-20 08:01:44 -070092 TOKEN_CLASS_IDENTIFIER_FINALIZED,
Carl Wortha807fb72010-05-18 22:10:04 -070093 TOKEN_CLASS_FUNC_MACRO,
94 TOKEN_CLASS_OBJ_MACRO
95} token_class_t;
96
97token_class_t
98glcpp_parser_classify_token (glcpp_parser_t *parser,
99 const char *identifier,
100 int *parameter_index);
101
102typedef struct {
103 int is_function;
104 string_list_t *parameters;
105 const char *identifier;
Carl Worth47252442010-05-19 13:54:37 -0700106 token_list_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700107} macro_t;
108
109typedef struct expansion_node {
110 macro_t *macro;
Carl Worth47252442010-05-19 13:54:37 -0700111 token_node_t *replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700112 struct expansion_node *next;
113} expansion_node_t;
114
Carl Worthb20d33c2010-05-20 22:27:07 -0700115typedef enum skip_type {
116 SKIP_NO_SKIP,
117 SKIP_TO_ELSE,
118 SKIP_TO_ENDIF
119} skip_type_t;
120
121typedef struct skip_node {
122 skip_type_t type;
123 struct skip_node *next;
124} skip_node_t;
125
Carl Wortha807fb72010-05-18 22:10:04 -0700126struct glcpp_parser {
127 yyscan_t scanner;
128 struct hash_table *defines;
Carl Worthae6517f2010-05-25 15:24:59 -0700129 string_list_t *active;
Carl Worthf34a0002010-05-25 16:59:02 -0700130 int space_tokens;
Carl Worthb20d33c2010-05-20 22:27:07 -0700131 skip_node_t *skip_stack;
Carl Wortha807fb72010-05-18 22:10:04 -0700132};
133
Carl Worth33cc4002010-05-12 12:17:10 -0700134glcpp_parser_t *
135glcpp_parser_create (void);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700136
137int
138glcpp_parser_parse (glcpp_parser_t *parser);
139
140void
Carl Worth33cc4002010-05-12 12:17:10 -0700141glcpp_parser_destroy (glcpp_parser_t *parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700142
143/* Generated by glcpp-lex.l to glcpp-lex.c */
144
Carl Wortha1e32bc2010-05-10 13:17:25 -0700145int
Carl Worth8f38aff2010-05-19 10:01:29 -0700146glcpp_lex_init_extra (glcpp_parser_t *parser, yyscan_t* scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700147
148int
Carl Worth8f38aff2010-05-19 10:01:29 -0700149glcpp_lex (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700150
151int
Carl Worth8f38aff2010-05-19 10:01:29 -0700152glcpp_lex_destroy (yyscan_t scanner);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700153
154/* Generated by glcpp-parse.y to glcpp-parse.c */
155
156int
Carl Worth0b27b5f2010-05-10 16:16:06 -0700157yyparse (glcpp_parser_t *parser);
Carl Wortha1e32bc2010-05-10 13:17:25 -0700158
Carl Worth5070a202010-05-12 12:45:33 -0700159/* xtalloc - wrappers around talloc to check for out-of-memory */
160
161#define xtalloc(ctx, type) (type *)xtalloc_named_const(ctx, sizeof(type), #type)
162
Carl Wortha807fb72010-05-18 22:10:04 -0700163#define xtalloc_size(ctx, size) xtalloc_named_const(ctx, size, __location__)
164
Carl Worth5070a202010-05-12 12:45:33 -0700165void *
166xtalloc_named_const (const void *context, size_t size, const char *name);
167
168char *
169xtalloc_strdup (const void *t, const char *p);
170
Carl Wortha807fb72010-05-18 22:10:04 -0700171char *
172xtalloc_strndup (const void *t, const char *p, size_t n);
173
Carl Worthb8945832010-05-20 15:02:03 -0700174char *
175xtalloc_asprintf (const void *t, const char *fmt, ...);
176
Carl Worth9bb796f2010-05-25 14:40:47 -0700177void *
178_xtalloc_reference_loc (const void *context,
179 const void *ptr, const char *location);
180
181#define xtalloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_xtalloc_reference_loc((ctx),(ptr), __location__)
182
Carl Wortha1e32bc2010-05-10 13:17:25 -0700183#endif