[flang] Fix bug in adding symbols to parse tree.
authorTim Keith <tkeith@nvidia.com>
Tue, 19 Jun 2018 21:59:41 +0000 (14:59 -0700)
committerTim Keith <tkeith@nvidia.com>
Tue, 19 Jun 2018 21:59:41 +0000 (14:59 -0700)
We were collecting symbols in a map of SourceName to Symbol*.
This is wrong because sometimes different occurrences of a name
map to different symbols (e.g. in different scopes).
SourceName::begin() is unique for each occurrence so use that
as the map key instead.

The problem can be reproduced by running:
`f18 -fdebug-resolve-names -fparse-only -fdebug-dump-parse-tree`
on the following source. The two symbols 'i' should have different
types and they were both coming out as INTEGER because they both
pointed to the first symbol for 'i'.

```
module m
  integer :: i
contains
  subroutine s
    real :: i
  end
end
```

Original-commit: flang-compiler/f18@a165c717ff20d3ccca334b91fa538f54b54071cf
Reviewed-on: https://github.com/flang-compiler/f18/pull/107
Tree-same-pre-rewrite: false

flang/lib/semantics/rewrite-parse-tree.cc

index d3cc1be..816e168 100644 (file)
@@ -23,7 +23,7 @@
 namespace Fortran::semantics {
 
 // Symbols collected during name resolution that are added to parse tree.
-using symbolMap = std::map<const SourceName, Symbol *>;
+using symbolMap = std::map<const char *, Symbol *>;
 
 /// Walk the parse tree and add symbols from the symbolMap in Name nodes.
 /// Convert mis-identified statement functions to array element assignments.
@@ -37,7 +37,7 @@ public:
 
   // Fill in name.symbol if there is a corresponding symbol
   void Post(parser::Name &name) {
-    const auto it = symbols_.find(name.source);
+    const auto it = symbols_.find(name.source.begin());
     if (it != symbols_.end()) {
       name.symbol = it->second;
     }
@@ -104,9 +104,9 @@ private:
 
 static void CollectSymbols(Scope &scope, symbolMap &symbols) {
   for (auto &pair : scope) {
-    Symbol &symbol{pair.second};
-    for (const auto &name : symbol.occurrences()) {
-      symbols.emplace(name, &symbol);
+    Symbol *symbol{pair.second};
+    for (const auto &name : symbol->occurrences()) {
+      symbols.emplace(name.begin(), symbol);
     }
   }
   for (auto &child : scope.children()) {