• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020, 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 #pragma once
18 
19 #include <iostream>
20 #include <optional>
21 #include <string>
22 
23 class AidlLocation {
24  public:
25   struct Point {
26     int line;
27     int column;
28   };
29 
30   enum class Source {
31     // From internal aidl source code
32     INTERNAL,
33     // From a parsed file
34     EXTERNAL,
35     // Derived from a parsed file. These are used for types generated by
36     // the compiler that we still want to track mostly like EXTERNAL types.
37     // An example is the Tag enum that is generated for each EXTERNAL union.
38     DERIVED_INTERNAL,
39   };
40 
41   AidlLocation(const std::string& file, Point begin, Point end, Source source);
AidlLocation(const std::string & file,Source source)42   AidlLocation(const std::string& file, Source source)
43       : AidlLocation(file, {0, 0}, {0, 0}, source) {}
44 
IsInternal()45   bool IsInternal() const { return source_ == Source::INTERNAL; }
IsDerived()46   bool IsDerived() const { return source_ == Source::DERIVED_INTERNAL; }
47 
48   // The first line of a file is line 1.
LocationKnown()49   bool LocationKnown() const { return begin_.line != 0; }
50 
GetFile()51   std::string GetFile() const { return file_; }
52 
53   // Get an AidlLocation derived from this external location.
54   // nullopt if this location is not EXTERNAL
55   std::optional<AidlLocation> ToDerivedLocation() const;
56 
57   friend std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
58   friend class AidlNode;
59 
60  private:
61   // INTENTIONALLY HIDDEN: only operator<< should access details here.
62   // Otherwise, locations should only ever be copied around to construct new
63   // objects.
64   const std::string file_;
65   Point begin_;
66   Point end_;
67   Source source_;
68 };
69 
70 #define AIDL_LOCATION_HERE \
71   (AidlLocation{__FILE__, {__LINE__, 0}, {__LINE__, 0}, AidlLocation::Source::INTERNAL})
72 
73 std::ostream& operator<<(std::ostream& os, const AidlLocation& l);
74