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