srcpos.h revision 238742
1204431Sraj/*
2204431Sraj * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3204431Sraj *
4204431Sraj * This program is free software; you can redistribute it and/or
5204431Sraj * modify it under the terms of the GNU General Public License as
6204431Sraj * published by the Free Software Foundation; either version 2 of the
7204431Sraj * License, or (at your option) any later version.
8204431Sraj *
9204431Sraj *  This program is distributed in the hope that it will be useful,
10204431Sraj *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11204431Sraj *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12204431Sraj *  General Public License for more details.
13204431Sraj *
14204431Sraj *  You should have received a copy of the GNU General Public License
15204431Sraj *  along with this program; if not, write to the Free Software
16204431Sraj *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17204431Sraj *                                                                   USA
18204431Sraj */
19204431Sraj
20204433Sraj#ifndef _SRCPOS_H_
21204433Sraj#define _SRCPOS_H_
22204433Sraj
23204431Sraj#include <stdio.h>
24204431Sraj
25238742Simpstruct srcfile_state {
26238742Simp	FILE *f;
27238742Simp	char *name;
28204431Sraj	char *dir;
29238742Simp	int lineno, colno;
30238742Simp	struct srcfile_state *prev;
31204431Sraj};
32204431Sraj
33238742Simpextern FILE *depfile; /* = NULL */
34238742Simpextern struct srcfile_state *current_srcfile; /* = NULL */
35238742Simp
36238742Simp/**
37238742Simp * Open a source file.
38238742Simp *
39238742Simp * If the source file is a relative pathname, then it is searched for in the
40238742Simp * current directory (the directory of the last source file read) and after
41238742Simp * that in the search path.
42238742Simp *
43238742Simp * We work through the search path in order from the first path specified to
44238742Simp * the last.
45238742Simp *
46238742Simp * If the file is not found, then this function does not return, but calls
47238742Simp * die().
48238742Simp *
49238742Simp * @param fname		Filename to search
50238742Simp * @param fullnamep	If non-NULL, it is set to the allocated filename of the
51238742Simp *			file that was opened. The caller is then responsible
52238742Simp *			for freeing the pointer.
53238742Simp * @return pointer to opened FILE
54238742Simp */
55238742SimpFILE *srcfile_relative_open(const char *fname, char **fullnamep);
56238742Simp
57238742Simpvoid srcfile_push(const char *fname);
58238742Simpint srcfile_pop(void);
59238742Simp
60238742Simp/**
61238742Simp * Add a new directory to the search path for input files
62238742Simp *
63238742Simp * The new path is added at the end of the list.
64238742Simp *
65238742Simp * @param dirname	Directory to add
66238742Simp */
67238742Simpvoid srcfile_add_search_path(const char *dirname);
68238742Simp
69238742Simpstruct srcpos {
70204431Sraj    int first_line;
71204431Sraj    int first_column;
72204431Sraj    int last_line;
73204431Sraj    int last_column;
74238742Simp    struct srcfile_state *file;
75238742Simp};
76204431Sraj
77238742Simp#define YYLTYPE struct srcpos
78204431Sraj
79238742Simp#define YYLLOC_DEFAULT(Current, Rhs, N)						\
80238742Simp	do {									\
81238742Simp		if (N) {							\
82238742Simp			(Current).first_line = YYRHSLOC(Rhs, 1).first_line;	\
83238742Simp			(Current).first_column = YYRHSLOC(Rhs, 1).first_column;	\
84238742Simp			(Current).last_line = YYRHSLOC(Rhs, N).last_line;	\
85238742Simp			(Current).last_column  = YYRHSLOC (Rhs, N).last_column;	\
86238742Simp			(Current).file = YYRHSLOC(Rhs, N).file;			\
87238742Simp		} else {							\
88238742Simp			(Current).first_line = (Current).last_line =		\
89238742Simp				YYRHSLOC(Rhs, 0).last_line;			\
90238742Simp			(Current).first_column = (Current).last_column =	\
91238742Simp				YYRHSLOC(Rhs, 0).last_column;			\
92238742Simp			(Current).file = YYRHSLOC (Rhs, 0).file;		\
93238742Simp		}								\
94238742Simp	} while (0)
95204431Sraj
96204431Sraj
97204433Sraj/*
98204433Sraj * Fictional source position used for IR nodes that are
99204433Sraj * created without otherwise knowing a true source position.
100204433Sraj * For example,constant definitions from the command line.
101204433Sraj */
102238742Simpextern struct srcpos srcpos_empty;
103204431Sraj
104238742Simpextern void srcpos_update(struct srcpos *pos, const char *text, int len);
105238742Simpextern struct srcpos *srcpos_copy(struct srcpos *pos);
106238742Simpextern char *srcpos_string(struct srcpos *pos);
107238742Simpextern void srcpos_dump(struct srcpos *pos);
108204431Sraj
109238742Simpextern void srcpos_verror(struct srcpos *pos, char const *, va_list va)
110238742Simp     __attribute__((format(printf, 2, 0)));
111238742Simpextern void srcpos_error(struct srcpos *pos, char const *, ...)
112204433Sraj     __attribute__((format(printf, 2, 3)));
113238742Simpextern void srcpos_warn(struct srcpos *pos, char const *, ...)
114204433Sraj     __attribute__((format(printf, 2, 3)));
115204433Sraj
116204433Sraj#endif /* _SRCPOS_H_ */
117