Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / sourcelocation.h
blob775727a8dc058335f5f7856d4f1a252462894f24
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008, 2009 Henrik Tidefelt
19 #pragma once
21 #include <stddef.h>
22 #include <iostream>
23 #include <vector>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include "Shapes_Ast_decls.h"
29 namespace Shapes
31 namespace Ast
33 class FileInfo
35 public:
36 std::string name_;
37 std::vector< Ast::Node * > topLevelNodes_;
39 FileInfo( const std::string & name );
42 class FileID
44 static const char * INPUTLIST_PREFIX;
45 static const size_t INPUTLIST_PREFIX_LENGTH;
47 public:
48 typedef enum { NORMAL_FILE, IN_MEMORY, SPECIAL, INTERNAL } Kind;
49 private:
50 Kind kind_;
51 dev_t st_dev_;
52 ino_t st_ino_; /* Also used to index an IN_MEMORY */
53 const char * strData_; /* A complete filename for a NORMAL_FILE, the identifier of a SPECIAL file, and unused for IN_MEMORY data. */
54 FileID( Kind kind, dev_t st_dev, ino_t st_ino, const char * strData );
55 public:
56 static const FileID * build_stat( const struct stat & stat, const std::string & filename );
57 static const FileID * build_inMemory( ino_t index, const std::string & sourceName );
58 static const FileID * build_fresh_inMemory( );
59 static const FileID * build_special( const char * specialName );
60 static const FileID * build_internal( const char * locationName ); /* Does not deep-copy locationName. Returns a new object each time, no matter is locationName has been used before! */
61 static bool is_inMemory( const std::string & filename, const FileID ** fileDst );
63 std::vector< Ast::Node * > & nodes( ) const;
64 const char * name( ) const;
65 bool hasPosition( ) const;
66 std::istream * open( std::ifstream * ifsMem, std::istringstream * issMem ) const;
67 void setMem( const char * data ); /* Applicable only to IN_MEMORY objects. */
68 int inMemoryIndex( ) const;
69 bool operator < ( const FileID & other ) const;
70 size_t byteColumnToUTF8Column( size_t line, size_t byteCol ) const;
71 size_t UTF8ColumnTobyteColumn( size_t line, size_t utf8Col ) const;
73 static size_t inputlistPrefixLength( );
74 static size_t UTF8ColumnTobyteColumn( const std::string & line, size_t utf8Col );
75 static size_t byteColumnToUTF8Column( const std::string & line, size_t byteCol );
77 private:
78 void initInfo( const std::string & name ) const;
80 class FileIDPtrLess
82 public:
83 bool operator () ( const FileID * fid1, const FileID * fid2 ) const
85 return (*fid1) < (*fid2);
89 class SourceLocation
91 public:
92 static const FileID * UNKNOWN_FILE;
94 const FileID * file_;
95 size_t firstLine;
96 size_t firstColumn; // In bytes, shall be converted to utf-8 position when displaying.
97 size_t lastLine;
98 size_t lastColumn; // In bytes, shall be converted to utf-8 position when displaying.
100 SourceLocation( ); /* A default constructor is required by Bison. Besides that, it should not be used! */
101 SourceLocation( const FileID * file );
102 SourceLocation( const SourceLocation & orig );
103 SourceLocation( const SourceLocation & firstLoc, const Ast::SourceLocation & lastLoc );
104 bool isUnknown( ) const;
105 bool contains( const SourceLocation & loc2 ) const;
106 friend std::ostream & operator << ( std::ostream & os, const SourceLocation & self );
108 void copy( std::ostream * os ) const;
109 Ast::Expression * findExpression( ) const;
112 extern SourceLocation THE_UNKNOWN_LOCATION;
114 std::ostream & operator << ( std::ostream & os, const SourceLocation & self );