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