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