srcpos.h revision 266130
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> 24266130Sian#include <stdbool.h> 25204431Sraj 26238742Simpstruct srcfile_state { 27238742Simp FILE *f; 28238742Simp char *name; 29204431Sraj char *dir; 30238742Simp int lineno, colno; 31238742Simp struct srcfile_state *prev; 32204431Sraj}; 33204431Sraj 34238742Simpextern FILE *depfile; /* = NULL */ 35238742Simpextern struct srcfile_state *current_srcfile; /* = NULL */ 36238742Simp 37238742Simp/** 38238742Simp * Open a source file. 39238742Simp * 40238742Simp * If the source file is a relative pathname, then it is searched for in the 41238742Simp * current directory (the directory of the last source file read) and after 42238742Simp * that in the search path. 43238742Simp * 44238742Simp * We work through the search path in order from the first path specified to 45238742Simp * the last. 46238742Simp * 47238742Simp * If the file is not found, then this function does not return, but calls 48238742Simp * die(). 49238742Simp * 50238742Simp * @param fname Filename to search 51238742Simp * @param fullnamep If non-NULL, it is set to the allocated filename of the 52238742Simp * file that was opened. The caller is then responsible 53238742Simp * for freeing the pointer. 54238742Simp * @return pointer to opened FILE 55238742Simp */ 56238742SimpFILE *srcfile_relative_open(const char *fname, char **fullnamep); 57238742Simp 58238742Simpvoid srcfile_push(const char *fname); 59266130Sianbool srcfile_pop(void); 60238742Simp 61238742Simp/** 62238742Simp * Add a new directory to the search path for input files 63238742Simp * 64238742Simp * The new path is added at the end of the list. 65238742Simp * 66238742Simp * @param dirname Directory to add 67238742Simp */ 68238742Simpvoid srcfile_add_search_path(const char *dirname); 69238742Simp 70238742Simpstruct srcpos { 71204431Sraj int first_line; 72204431Sraj int first_column; 73204431Sraj int last_line; 74204431Sraj int last_column; 75238742Simp struct srcfile_state *file; 76238742Simp}; 77204431Sraj 78238742Simp#define YYLTYPE struct srcpos 79204431Sraj 80238742Simp#define YYLLOC_DEFAULT(Current, Rhs, N) \ 81238742Simp do { \ 82238742Simp if (N) { \ 83238742Simp (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \ 84238742Simp (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \ 85238742Simp (Current).last_line = YYRHSLOC(Rhs, N).last_line; \ 86238742Simp (Current).last_column = YYRHSLOC (Rhs, N).last_column; \ 87238742Simp (Current).file = YYRHSLOC(Rhs, N).file; \ 88238742Simp } else { \ 89238742Simp (Current).first_line = (Current).last_line = \ 90238742Simp YYRHSLOC(Rhs, 0).last_line; \ 91238742Simp (Current).first_column = (Current).last_column = \ 92238742Simp YYRHSLOC(Rhs, 0).last_column; \ 93238742Simp (Current).file = YYRHSLOC (Rhs, 0).file; \ 94238742Simp } \ 95238742Simp } while (0) 96204431Sraj 97204431Sraj 98204433Sraj/* 99204433Sraj * Fictional source position used for IR nodes that are 100204433Sraj * created without otherwise knowing a true source position. 101204433Sraj * For example,constant definitions from the command line. 102204433Sraj */ 103238742Simpextern struct srcpos srcpos_empty; 104204431Sraj 105238742Simpextern void srcpos_update(struct srcpos *pos, const char *text, int len); 106238742Simpextern struct srcpos *srcpos_copy(struct srcpos *pos); 107238742Simpextern char *srcpos_string(struct srcpos *pos); 108238742Simpextern void srcpos_dump(struct srcpos *pos); 109204431Sraj 110266130Sianextern void srcpos_verror(struct srcpos *pos, const char *prefix, 111266130Sian const char *fmt, va_list va) 112266130Sian __attribute__((format(printf, 3, 0))); 113266130Sianextern void srcpos_error(struct srcpos *pos, const char *prefix, 114266130Sian const char *fmt, ...) 115266130Sian __attribute__((format(printf, 3, 4))); 116204433Sraj 117266130Sianextern void srcpos_set_line(char *f, int l); 118266130Sian 119204433Sraj#endif /* _SRCPOS_H_ */ 120