19fffb55fSDavid Gibson /* 29fffb55fSDavid Gibson * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc. 39fffb55fSDavid Gibson * 49fffb55fSDavid Gibson * This program is free software; you can redistribute it and/or 59fffb55fSDavid Gibson * modify it under the terms of the GNU General Public License as 69fffb55fSDavid Gibson * published by the Free Software Foundation; either version 2 of the 79fffb55fSDavid Gibson * License, or (at your option) any later version. 89fffb55fSDavid Gibson * 99fffb55fSDavid Gibson * This program is distributed in the hope that it will be useful, 109fffb55fSDavid Gibson * but WITHOUT ANY WARRANTY; without even the implied warranty of 119fffb55fSDavid Gibson * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 129fffb55fSDavid Gibson * General Public License for more details. 139fffb55fSDavid Gibson * 149fffb55fSDavid Gibson * You should have received a copy of the GNU General Public License 159fffb55fSDavid Gibson * along with this program; if not, write to the Free Software 169fffb55fSDavid Gibson * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 179fffb55fSDavid Gibson * USA 189fffb55fSDavid Gibson */ 199fffb55fSDavid Gibson 20*9130ba88SRob Herring #ifndef SRCPOS_H 21*9130ba88SRob Herring #define SRCPOS_H 229fffb55fSDavid Gibson 239fffb55fSDavid Gibson #include <stdio.h> 2447605971SRob Herring #include <stdbool.h> 2589d12310SRob Herring #include "util.h" 269fffb55fSDavid Gibson 27658f29a5SJohn Bonesio struct srcfile_state { 28658f29a5SJohn Bonesio FILE *f; 29658f29a5SJohn Bonesio char *name; 309fffb55fSDavid Gibson char *dir; 31658f29a5SJohn Bonesio int lineno, colno; 32658f29a5SJohn Bonesio struct srcfile_state *prev; 339fffb55fSDavid Gibson }; 349fffb55fSDavid Gibson 35136ec204SStephen Warren extern FILE *depfile; /* = NULL */ 36658f29a5SJohn Bonesio extern struct srcfile_state *current_srcfile; /* = NULL */ 37658f29a5SJohn Bonesio 38cd296721SStephen Warren /** 39cd296721SStephen Warren * Open a source file. 40cd296721SStephen Warren * 41cd296721SStephen Warren * If the source file is a relative pathname, then it is searched for in the 42cd296721SStephen Warren * current directory (the directory of the last source file read) and after 43cd296721SStephen Warren * that in the search path. 44cd296721SStephen Warren * 45cd296721SStephen Warren * We work through the search path in order from the first path specified to 46cd296721SStephen Warren * the last. 47cd296721SStephen Warren * 48cd296721SStephen Warren * If the file is not found, then this function does not return, but calls 49cd296721SStephen Warren * die(). 50cd296721SStephen Warren * 51cd296721SStephen Warren * @param fname Filename to search 52cd296721SStephen Warren * @param fullnamep If non-NULL, it is set to the allocated filename of the 53cd296721SStephen Warren * file that was opened. The caller is then responsible 54cd296721SStephen Warren * for freeing the pointer. 55cd296721SStephen Warren * @return pointer to opened FILE 56cd296721SStephen Warren */ 57658f29a5SJohn Bonesio FILE *srcfile_relative_open(const char *fname, char **fullnamep); 58cd296721SStephen Warren 59658f29a5SJohn Bonesio void srcfile_push(const char *fname); 6047605971SRob Herring bool srcfile_pop(void); 61658f29a5SJohn Bonesio 62cd296721SStephen Warren /** 63cd296721SStephen Warren * Add a new directory to the search path for input files 64cd296721SStephen Warren * 65cd296721SStephen Warren * The new path is added at the end of the list. 66cd296721SStephen Warren * 67cd296721SStephen Warren * @param dirname Directory to add 68cd296721SStephen Warren */ 69cd296721SStephen Warren void srcfile_add_search_path(const char *dirname); 70cd296721SStephen Warren 71658f29a5SJohn Bonesio struct srcpos { 729fffb55fSDavid Gibson int first_line; 739fffb55fSDavid Gibson int first_column; 749fffb55fSDavid Gibson int last_line; 759fffb55fSDavid Gibson int last_column; 76658f29a5SJohn Bonesio struct srcfile_state *file; 77658f29a5SJohn Bonesio }; 789fffb55fSDavid Gibson 79658f29a5SJohn Bonesio #define YYLTYPE struct srcpos 809fffb55fSDavid Gibson 819fffb55fSDavid Gibson #define YYLLOC_DEFAULT(Current, Rhs, N) \ 82658f29a5SJohn Bonesio do { \ 83658f29a5SJohn Bonesio if (N) { \ 849fffb55fSDavid Gibson (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \ 859fffb55fSDavid Gibson (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \ 869fffb55fSDavid Gibson (Current).last_line = YYRHSLOC(Rhs, N).last_line; \ 879fffb55fSDavid Gibson (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ 889fffb55fSDavid Gibson (Current).file = YYRHSLOC(Rhs, N).file; \ 89658f29a5SJohn Bonesio } else { \ 909fffb55fSDavid Gibson (Current).first_line = (Current).last_line = \ 919fffb55fSDavid Gibson YYRHSLOC(Rhs, 0).last_line; \ 929fffb55fSDavid Gibson (Current).first_column = (Current).last_column = \ 939fffb55fSDavid Gibson YYRHSLOC(Rhs, 0).last_column; \ 949fffb55fSDavid Gibson (Current).file = YYRHSLOC (Rhs, 0).file; \ 959fffb55fSDavid Gibson } \ 96658f29a5SJohn Bonesio } while (0) 979fffb55fSDavid Gibson 989fffb55fSDavid Gibson 99658f29a5SJohn Bonesio /* 100658f29a5SJohn Bonesio * Fictional source position used for IR nodes that are 101658f29a5SJohn Bonesio * created without otherwise knowing a true source position. 102658f29a5SJohn Bonesio * For example,constant definitions from the command line. 103658f29a5SJohn Bonesio */ 104658f29a5SJohn Bonesio extern struct srcpos srcpos_empty; 1059fffb55fSDavid Gibson 106658f29a5SJohn Bonesio extern void srcpos_update(struct srcpos *pos, const char *text, int len); 107658f29a5SJohn Bonesio extern struct srcpos *srcpos_copy(struct srcpos *pos); 108658f29a5SJohn Bonesio extern char *srcpos_string(struct srcpos *pos); 1099fffb55fSDavid Gibson 11089d12310SRob Herring extern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix, 11189d12310SRob Herring const char *fmt, va_list va); 11289d12310SRob Herring extern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix, 11389d12310SRob Herring const char *fmt, ...); 1149fffb55fSDavid Gibson 115cd296721SStephen Warren extern void srcpos_set_line(char *f, int l); 116cd296721SStephen Warren 117*9130ba88SRob Herring #endif /* SRCPOS_H */ 118