1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LOCATION_H_
18 #define LOCATION_H_
19 
20 #include <stdint.h>
21 #include <ostream>
22 #include <string>
23 
24 // Mimics for yy::location and yy::position
25 namespace android {
26 
27 #define HIDL_LOCATION_HERE                                 \
28     Location {                                             \
29         {__FILE__, __LINE__, 0}, { __FILE__, __LINE__, 0 } \
30     }
31 
32 struct Position {
33     Position() = default;
34     Position(std::string filename, size_t line, size_t column);
35 
36     const std::string& filename() const;
37 
38     size_t line() const;
39     size_t column() const;
40 
41     static bool inSameFile(const Position& lhs, const Position& rhs);
42 
43     bool operator<(const Position& pos) const;
44 
45    private:
46     // File name to which this position refers.
47     std::string mFilename;
48     // Current line number.
49     size_t mLine;
50     // Current column number.
51     size_t mColumn;
52 };
53 
54 std::ostream& operator<<(std::ostream& ostr, const Position& pos);
55 
56 struct Location {
57     Location() = default;
58     Location(const Position& begin, const Position& end);
59 
60     void setLocation(const Position& begin, const Position& end);
61 
62     bool isValid() const;
63     const Position& begin() const;
64     const Position& end() const;
65 
66     static Location startOf(const std::string& path);
67 
68     static bool inSameFile(const Location& lhs, const Location& rhs);
69     static bool intersect(const Location& lhs, const Location& rhs);
70 
71     bool operator<(const Location& loc) const;
72 
73    private:
74     bool mIsValid = false;
75 
76     // Beginning of the located region.
77     Position mBegin;
78     // End of the located region.
79     Position mEnd;
80 };
81 
82 std::ostream& operator<<(std::ostream& ostr, const Location& loc);
83 
84 } // namespace android
85 
86 #endif  // LOCATION_H_
87