xref: /rk3399_rockchip-uboot/scripts/dtc/srcpos.h (revision d18719a48ffdf6be4a0724f88d8968904df3a0d9)
1*d18719a4STom Rini /*
2*d18719a4STom Rini  * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3*d18719a4STom Rini  *
4*d18719a4STom Rini  * This program is free software; you can redistribute it and/or
5*d18719a4STom Rini  * modify it under the terms of the GNU General Public License as
6*d18719a4STom Rini  * published by the Free Software Foundation; either version 2 of the
7*d18719a4STom Rini  * License, or (at your option) any later version.
8*d18719a4STom Rini  *
9*d18719a4STom Rini  *  This program is distributed in the hope that it will be useful,
10*d18719a4STom Rini  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d18719a4STom Rini  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12*d18719a4STom Rini  *  General Public License for more details.
13*d18719a4STom Rini  *
14*d18719a4STom Rini  *  You should have received a copy of the GNU General Public License
15*d18719a4STom Rini  *  along with this program; if not, write to the Free Software
16*d18719a4STom Rini  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17*d18719a4STom Rini  *                                                                   USA
18*d18719a4STom Rini  */
19*d18719a4STom Rini 
20*d18719a4STom Rini #ifndef _SRCPOS_H_
21*d18719a4STom Rini #define _SRCPOS_H_
22*d18719a4STom Rini 
23*d18719a4STom Rini #include <stdio.h>
24*d18719a4STom Rini #include <stdbool.h>
25*d18719a4STom Rini 
26*d18719a4STom Rini struct srcfile_state {
27*d18719a4STom Rini 	FILE *f;
28*d18719a4STom Rini 	char *name;
29*d18719a4STom Rini 	char *dir;
30*d18719a4STom Rini 	int lineno, colno;
31*d18719a4STom Rini 	struct srcfile_state *prev;
32*d18719a4STom Rini };
33*d18719a4STom Rini 
34*d18719a4STom Rini extern FILE *depfile; /* = NULL */
35*d18719a4STom Rini extern struct srcfile_state *current_srcfile; /* = NULL */
36*d18719a4STom Rini 
37*d18719a4STom Rini /**
38*d18719a4STom Rini  * Open a source file.
39*d18719a4STom Rini  *
40*d18719a4STom Rini  * If the source file is a relative pathname, then it is searched for in the
41*d18719a4STom Rini  * current directory (the directory of the last source file read) and after
42*d18719a4STom Rini  * that in the search path.
43*d18719a4STom Rini  *
44*d18719a4STom Rini  * We work through the search path in order from the first path specified to
45*d18719a4STom Rini  * the last.
46*d18719a4STom Rini  *
47*d18719a4STom Rini  * If the file is not found, then this function does not return, but calls
48*d18719a4STom Rini  * die().
49*d18719a4STom Rini  *
50*d18719a4STom Rini  * @param fname		Filename to search
51*d18719a4STom Rini  * @param fullnamep	If non-NULL, it is set to the allocated filename of the
52*d18719a4STom Rini  *			file that was opened. The caller is then responsible
53*d18719a4STom Rini  *			for freeing the pointer.
54*d18719a4STom Rini  * @return pointer to opened FILE
55*d18719a4STom Rini  */
56*d18719a4STom Rini FILE *srcfile_relative_open(const char *fname, char **fullnamep);
57*d18719a4STom Rini 
58*d18719a4STom Rini void srcfile_push(const char *fname);
59*d18719a4STom Rini bool srcfile_pop(void);
60*d18719a4STom Rini 
61*d18719a4STom Rini /**
62*d18719a4STom Rini  * Add a new directory to the search path for input files
63*d18719a4STom Rini  *
64*d18719a4STom Rini  * The new path is added at the end of the list.
65*d18719a4STom Rini  *
66*d18719a4STom Rini  * @param dirname	Directory to add
67*d18719a4STom Rini  */
68*d18719a4STom Rini void srcfile_add_search_path(const char *dirname);
69*d18719a4STom Rini 
70*d18719a4STom Rini struct srcpos {
71*d18719a4STom Rini     int first_line;
72*d18719a4STom Rini     int first_column;
73*d18719a4STom Rini     int last_line;
74*d18719a4STom Rini     int last_column;
75*d18719a4STom Rini     struct srcfile_state *file;
76*d18719a4STom Rini };
77*d18719a4STom Rini 
78*d18719a4STom Rini #define YYLTYPE struct srcpos
79*d18719a4STom Rini 
80*d18719a4STom Rini #define YYLLOC_DEFAULT(Current, Rhs, N)						\
81*d18719a4STom Rini 	do {									\
82*d18719a4STom Rini 		if (N) {							\
83*d18719a4STom Rini 			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
84*d18719a4STom Rini 			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
85*d18719a4STom Rini 			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
86*d18719a4STom Rini 			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
87*d18719a4STom Rini 			(Current).file = YYRHSLOC(Rhs, N).file;			\
88*d18719a4STom Rini 		} else {							\
89*d18719a4STom Rini 			(Current).first_line = (Current).last_line =		\
90*d18719a4STom Rini 				YYRHSLOC(Rhs, 0).last_line;			\
91*d18719a4STom Rini 			(Current).first_column = (Current).last_column =	\
92*d18719a4STom Rini 				YYRHSLOC(Rhs, 0).last_column;			\
93*d18719a4STom Rini 			(Current).file = YYRHSLOC (Rhs, 0).file;		\
94*d18719a4STom Rini 		}								\
95*d18719a4STom Rini 	} while (0)
96*d18719a4STom Rini 
97*d18719a4STom Rini 
98*d18719a4STom Rini /*
99*d18719a4STom Rini  * Fictional source position used for IR nodes that are
100*d18719a4STom Rini  * created without otherwise knowing a true source position.
101*d18719a4STom Rini  * For example,constant definitions from the command line.
102*d18719a4STom Rini  */
103*d18719a4STom Rini extern struct srcpos srcpos_empty;
104*d18719a4STom Rini 
105*d18719a4STom Rini extern void srcpos_update(struct srcpos *pos, const char *text, int len);
106*d18719a4STom Rini extern struct srcpos *srcpos_copy(struct srcpos *pos);
107*d18719a4STom Rini extern char *srcpos_string(struct srcpos *pos);
108*d18719a4STom Rini 
109*d18719a4STom Rini extern void srcpos_verror(struct srcpos *pos, const char *prefix,
110*d18719a4STom Rini 			  const char *fmt, va_list va)
111*d18719a4STom Rini 	__attribute__((format(printf, 3, 0)));
112*d18719a4STom Rini extern void srcpos_error(struct srcpos *pos, const char *prefix,
113*d18719a4STom Rini 			 const char *fmt, ...)
114*d18719a4STom Rini 	__attribute__((format(printf, 3, 4)));
115*d18719a4STom Rini 
116*d18719a4STom Rini extern void srcpos_set_line(char *f, int l);
117*d18719a4STom Rini 
118*d18719a4STom Rini #endif /* _SRCPOS_H_ */
119