github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/third_party/mlir/lib/IR/Location.cpp (about)

     1  //===- Location.cpp - MLIR Location Classes -------------------------------===//
     2  //
     3  // Copyright 2019 The MLIR Authors.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //   http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  // =============================================================================
    17  
    18  #include "mlir/IR/Location.h"
    19  #include "LocationDetail.h"
    20  #include "llvm/ADT/SetVector.h"
    21  
    22  using namespace mlir;
    23  using namespace mlir::detail;
    24  
    25  //===----------------------------------------------------------------------===//
    26  // CallSiteLoc
    27  //===----------------------------------------------------------------------===//
    28  
    29  Location CallSiteLoc::get(Location callee, Location caller) {
    30    return Base::get(callee->getContext(), StandardAttributes::CallSiteLocation,
    31                     callee, caller);
    32  }
    33  
    34  Location CallSiteLoc::get(Location name, ArrayRef<Location> frames) {
    35    assert(!frames.empty() && "required at least 1 call frame");
    36    Location caller = frames.back();
    37    for (auto frame : llvm::reverse(frames.drop_back()))
    38      caller = CallSiteLoc::get(frame, caller);
    39    return CallSiteLoc::get(name, caller);
    40  }
    41  
    42  Location CallSiteLoc::getCallee() const { return getImpl()->callee; }
    43  
    44  Location CallSiteLoc::getCaller() const { return getImpl()->caller; }
    45  
    46  //===----------------------------------------------------------------------===//
    47  // FileLineColLoc
    48  //===----------------------------------------------------------------------===//
    49  
    50  Location FileLineColLoc::get(Identifier filename, unsigned line,
    51                               unsigned column, MLIRContext *context) {
    52    return Base::get(context, StandardAttributes::FileLineColLocation, filename,
    53                     line, column);
    54  }
    55  
    56  Location FileLineColLoc::get(StringRef filename, unsigned line, unsigned column,
    57                               MLIRContext *context) {
    58    return get(Identifier::get(filename.empty() ? "-" : filename, context), line,
    59               column, context);
    60  }
    61  
    62  StringRef FileLineColLoc::getFilename() const { return getImpl()->filename; }
    63  unsigned FileLineColLoc::getLine() const { return getImpl()->line; }
    64  unsigned FileLineColLoc::getColumn() const { return getImpl()->column; }
    65  
    66  //===----------------------------------------------------------------------===//
    67  // FusedLoc
    68  //===----------------------------------------------------------------------===//
    69  
    70  Location FusedLoc::get(ArrayRef<Location> locs, Attribute metadata,
    71                         MLIRContext *context) {
    72    // Unique the set of locations to be fused.
    73    llvm::SmallSetVector<Location, 4> decomposedLocs;
    74    for (auto loc : locs) {
    75      // If the location is a fused location we decompose it if it has no
    76      // metadata or the metadata is the same as the top level metadata.
    77      if (auto fusedLoc = loc.dyn_cast<FusedLoc>()) {
    78        if (fusedLoc.getMetadata() == metadata) {
    79          // UnknownLoc's have already been removed from FusedLocs so we can
    80          // simply add all of the internal locations.
    81          decomposedLocs.insert(fusedLoc.getLocations().begin(),
    82                                fusedLoc.getLocations().end());
    83          continue;
    84        }
    85      }
    86      // Otherwise, only add known locations to the set.
    87      if (!loc.isa<UnknownLoc>())
    88        decomposedLocs.insert(loc);
    89    }
    90    locs = decomposedLocs.getArrayRef();
    91  
    92    // Handle the simple cases of less than two locations.
    93    if (locs.empty())
    94      return UnknownLoc::get(context);
    95    if (locs.size() == 1)
    96      return locs.front();
    97    return Base::get(context, StandardAttributes::FusedLocation, locs, metadata);
    98  }
    99  
   100  ArrayRef<Location> FusedLoc::getLocations() const {
   101    return getImpl()->getLocations();
   102  }
   103  
   104  Attribute FusedLoc::getMetadata() const { return getImpl()->metadata; }
   105  
   106  //===----------------------------------------------------------------------===//
   107  // NameLoc
   108  //===----------------------------------------------------------------------===//
   109  
   110  Location NameLoc::get(Identifier name, Location child) {
   111    assert(!child.isa<NameLoc>() &&
   112           "a NameLoc cannot be used as a child of another NameLoc");
   113    return Base::get(child->getContext(), StandardAttributes::NameLocation, name,
   114                     child);
   115  }
   116  
   117  Location NameLoc::get(Identifier name, MLIRContext *context) {
   118    return get(name, UnknownLoc::get(context));
   119  }
   120  
   121  /// Return the name identifier.
   122  Identifier NameLoc::getName() const { return getImpl()->name; }
   123  
   124  /// Return the child location.
   125  Location NameLoc::getChildLoc() const { return getImpl()->child; }