summaryrefslogtreecommitdiff
path: root/graphics/asymptote/symbolmaps.cc
blob: aabc8fc2b9d56c5b40089791a7f0fb385e2d4552 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#include "common.h"

#ifdef HAVE_LSP

#include "symbolmaps.h"
#include "locate.h"
#include <unordered_map>

namespace AsymptoteLsp
{
  [[nodiscard]]
  bool isVirtualFile(std::string const& filename)
  {
    bool isSettings=filename=="settings";
#ifdef HAVE_LIBGSL
    bool isGSL=filename=="gsl" and settings::getSetting<bool>("gsl");
#else
    bool isGSL=false;
#endif
    return isSettings || isGSL;
  }

  std::string getPlainFile()
  {
    return std::string((std::string) settings::locateFile("plain", true).c_str());
  }

  positions::positions(filePos const& positionInFile)
  {
    add(positionInFile);
  }

  void positions::add(const filePos &positionInFile)
  {
    auto fileLoc=pos.find(positionInFile.first);
    if (fileLoc == pos.end())
    {
      pos.emplace(positionInFile.first, std::vector<posInFile>{positionInFile.second});
    } else
    {
      fileLoc->second.push_back(positionInFile.second);
    }
  }

  ostream& operator <<(std::ostream& os, const SymbolMaps& sym)
  {
    os << "var decs:" << endl;
    for (auto const& s : sym.varDec)
    {
      auto const& key=std::get<0>(s);
      auto const& value=std::get<1>(s);
      os << key << " " << value.pos.first << ":" << value.pos.second << endl;
    }
    return os;
  }

  optional<fullSymPosRangeInFile> SymbolMaps::searchSymbol(posInFile const& inputPos)
  {
    // FIXME: can be optimized by binary search.
    for (auto const& s : usageByLines)
    {
      auto const& pos=std::get<0>(s);
      auto const& syLit=std::get<1>(s);
      size_t endCharacter = pos.second + syLit.name.length() - 1;
      bool posMatches =
              pos.first == inputPos.first and
              pos.second <= inputPos.second and
              inputPos.second <= endCharacter;
      bool isOperator = syLit.name.find("operator ") == 0;
      if (posMatches and !isOperator)
      {
        posInFile endPos(pos.first, endCharacter + 1);
        return make_optional(std::make_tuple(syLit, pos, endPos));
      }
    }
    return nullopt;
  }

  FunctionInfo& SymbolMaps::addFunDef(
          std::string const& funcName, posInFile const& position, std::string const& returnType)
  {
    auto fit=std::get<0>(funDec.emplace(std::piecewise_construct,
                                        std::forward_as_tuple(funcName), std::forward_as_tuple()));

    fit->second.emplace_back(funcName, position, returnType);
    return fit->second.back();
  }

  std::pair<optional<fullSymPosRangeInFile>, SymbolContext*> SymbolContext::searchSymbol(posInFile const& inputPos)
  {
    auto currCtxSym = symMap.searchSymbol(inputPos);
    if (currCtxSym.has_value())
    {
      return make_pair(currCtxSym, this);
    }
    // else, not found in currCtx;
    for (auto& subContext : subContexts)
    {
      if (!posLt(inputPos, subContext->contextLoc))
      {
        auto v=subContext->searchSymbol(inputPos);
        auto subCtxSym=std::get<0>(v);;
        auto ctx=std::get<1>(v);
        if (subCtxSym.has_value())
        {
          return make_pair(subCtxSym, ctx);
        }
      }
    }
    return make_pair(nullopt, nullptr);
  }

  SymbolInfo const* SymbolContext::searchVarRaw(std::string const& symbol) const
  {
    // local variable declarations
    auto pt = symMap.varDec.find(symbol);
    if (pt != symMap.varDec.end())
    {
      return &pt->second;
    }

    // otherwise, search parent.
    return parent != nullptr ? parent->searchVarRaw(symbol) : nullptr;
  }

  optional<posRangeInFile> SymbolContext::searchVarDecl(
          std::string const& symbol, optional<posInFile> const& position)
  {
    auto pt = symMap.varDec.find(symbol);
    if (pt != symMap.varDec.end())
    {
      auto line = std::get<0>(pt->second.pos);
      auto ch = std::get<1>(pt->second.pos);
      if ((not position.has_value()) or (!posLt(position.value(), pt->second.pos)))
      {
        return std::make_tuple(getFileName().value_or(""),
                               pt->second.pos, std::make_pair(line, ch + symbol.length()));
      }
    }

    auto ptFn = symMap.funDec.find(symbol);
    if (ptFn != symMap.funDec.end() and !ptFn->second.empty())
    {
      // FIXME: Right now, we have no way of knowing the exact position of the
      //        start of where the function name is. As an example, we do not know
      //        where the exact position of
      //        real testFunction(...) { ...
      //             ^
      auto ptValue = ptFn->second[0];
      if ((not position.has_value()) or (!posLt(position.value(), ptValue.pos)))
      {
        return std::make_tuple(getFileName().value_or(""), ptValue.pos, ptValue.pos);
      }
    }

    // otherwise, search parent.
    return parent != nullptr ? parent->searchVarDecl(symbol, position) : nullopt;
  }

  void SymbolContext::addPlainFile()
  {
    std::string plainFile = getPlainFile();
    addEmptyExtRef(plainFile);
  }

  SymbolContext::SymbolContext(posInFile loc):
    fileLoc(nullopt), contextLoc(std::move(loc)), parent(nullptr)
  {
    addPlainFile();
  }

  SymbolContext::SymbolContext(posInFile loc, std::string filename):
          fileLoc(std::move(filename)), contextLoc(std::move(loc)), parent(nullptr)
  {
    addPlainFile();
  }

  optional<posRangeInFile>
  SymbolContext::searchVarDeclFull(std::string const& symbol, optional<posInFile> const& position)
  {
    std::unordered_set<SymbolContext*> searched;
    std::unordered_set<std::string> symbolSearch { symbol };

    auto returnVal = _searchVarFull<posRangeInFile>(
            searched, symbolSearch,
            [&position](SymbolContext* ctx, std::string const& sym)
            {
              return ctx->searchVarDecl(sym, position);
            },
            [](SymbolContext* ctx, std::string const& sym)
            {
              return ctx->searchVarDecl(sym);
            },
            fnFromDeclCreateTrav);
    if (returnVal.has_value())
    {
      return returnVal;
    }

    return _searchVarFull<posRangeInFile>(
            symbol,
            [](SymbolContext* ctx, std::string const& sym) -> optional<posRangeInFile>
            {
              for (auto const& unravelVal : ctx->extRefs.unraveledVals)
              {
                if (auto* pctx=ctx->searchStructCtxFull(unravelVal))
                {
                  auto retVal = pctx->searchVarDecl(sym);
                  if (retVal.has_value())
                  {
                    return retVal;
                  }
                }
              }
              return nullopt;
            });
  }

  std::list<ExternalRefs::extRefMap::iterator> SymbolContext::getEmptyRefs()
  {
    std::list<ExternalRefs::extRefMap::iterator> finalList;

    for (auto it = extRefs.extFileRefs.begin(); it != extRefs.extFileRefs.end(); it++)
    {
      if (it->second == nullptr)
      {
        // cerr << it->first << endl;
        finalList.emplace_back(it);
      }
    }

    for (auto& ctx : subContexts)
    {
      finalList.splice(finalList.end(), ctx->getEmptyRefs());
    }

    return finalList;
  }

  optional<std::string> SymbolContext::getFileName() const
  {
    if (fileLoc.has_value())
    {
      return fileLoc;
    }
    else
    {
      return parent == nullptr ? fileLoc : parent->getFileName();
    }
  }

  optional<std::string> SymbolContext::searchVarSignatureFull(std::string const& symbol)
  {
    std::unordered_set<SymbolContext*> searched;
    auto retVal = _searchVarFull<std::string>(
            symbol,
            [](SymbolContext const* ctx, std::string const& sym) -> optional<std::string>
            {
              if (auto const* info = ctx->searchVarRaw(sym))
              {
                return info->signature();
              }
              return nullopt;
            },
            fnFromDeclCreateTrav);

    if (retVal.has_value())
    {
      return retVal;
    }

    // else,
    // search every unravel values possible (not only here, but also in every includes + unravels as well).
    // If an unravel value match a struct, then see if symbol matches the struct. If yes, return otherwise skip.

    return _searchVarFull<std::string>(
            symbol,
            [](SymbolContext* ctx, std::string const& sym) -> optional<std::string>
            {
              if (SymbolInfo* info=ctx->searchVarUnravelStructRaw(sym))
              {
                return info->signature();
              }
              return nullopt;
            });
  }

  SymbolInfo* SymbolContext::searchVarUnravelStructRaw(std::string const& symbol)
  {
    return searchVarUnravelStructRaw(symbol, nullopt);
  }

  SymbolInfo* SymbolContext::searchVarUnravelStructRaw(std::string const& symbol, optional<posInFile> const& position)
  {
    for (auto const& unravelVal : extRefs.unraveledVals)
    {
      if (auto* ctx=searchStructCtxFull(unravelVal))
      {
        // ctx points to a struct.
        auto it=ctx->symMap.varDec.find(symbol);
        if (it != ctx->symMap.varDec.end())
        {
          if (((not position.has_value()) or (!posLt(position.value(), it->second.pos))))
          {
            return &it->second;
          }
        }
      }
    }
    return parent != nullptr ? parent->searchVarUnravelStructRaw(symbol) : nullptr;
  }

  std::list<std::string> SymbolContext::searchFuncUnravelStruct(std::string const& symbol)
  {
    std::list<std::string> finalList;

    for (auto const& unravelVal : extRefs.unraveledVals)
    {
      if (auto* ctx=searchStructCtxFull(unravelVal))
      {
        // ctx points to a struct.
        auto it=ctx->symMap.funDec.find(symbol);
        if (it != ctx->symMap.funDec.end() and not it->second.empty())
        {
          std::transform(
                  it->second.begin(), it->second.end(), std::back_inserter(finalList),
                  [](FunctionInfo const& fnInfo)
                  {
                    return fnInfo.signature();
                  });
        }
      }
    }
    if (parent != nullptr)
    {
      finalList.splice(finalList.end(), parent->searchFuncUnravelStruct(symbol));
    }
    return finalList;
  }


  std::list<std::string> SymbolContext::searchFuncSignature(std::string const& symbol)
  {
    std::list<std::string> funcSigs;
    auto pt = symMap.funDec.find(symbol);
    if (pt != symMap.funDec.end())
    {
      std::transform(pt->second.begin(), pt->second.end(),
                     std::back_inserter(funcSigs),
                     [](FunctionInfo const& fnInfo) {
        return fnInfo.signature();
      });
    }

    if (parent != nullptr)
    {
      funcSigs.splice(funcSigs.end(), parent->searchFuncSignature(symbol));
    }
    return funcSigs;
  }

  std::list<std::string> SymbolContext::searchFuncSignatureFull(std::string const& symbol)
  {
    auto base_list = _searchAllVarFull<std::string>(
            symbol, std::mem_fn(&SymbolContext::searchFuncSignature), fnFromDeclCreateTrav);

    base_list.splice(base_list.end(), _searchAllVarFull<std::string>(
            symbol, std::mem_fn(&SymbolContext::searchFuncUnravelStruct)));
    return base_list;
  }

  optional<SymbolContext*> SymbolContext::searchStructContext(std::string const& tyVal) const
  {
    auto stCtx = symMap.typeDecs.find(tyVal);
    if (stCtx != symMap.typeDecs.end())
    {
      if (auto* stDec = dynamic_cast<StructDecs*>(stCtx->second.get()))
      {
        return make_optional(stDec->ctx);
      }
    }

    return parent != nullptr ? parent->searchStructContext(tyVal) : nullopt;
  };

  optional<std::string> SymbolContext::searchLitSignature(SymbolLit const& symbol)
  {
    if (symbol.scopes.empty())
    {
      return searchVarSignatureFull(symbol.name);
    }
    else
    {
      auto ctx=std::get<0>(searchLitContext(symbol));
      auto isStruct=std::get<1>(searchLitContext(symbol));
      if (ctx)
      {
        if (isStruct)
        {
          // ctx is a struct declaration. should not search beyond this struct.
          auto varDec=ctx->symMap.varDec.find(symbol.name);
          return varDec != ctx->symMap.varDec.end() ?
                 optional<std::string>(varDec->second.signature()) : nullopt;
        }
        else
        {
          return ctx->searchVarSignatureFull(symbol.name);
        }
      }
    }

    return nullopt;
  }

  std::list<std::string> SymbolContext::searchLitFuncSignature(SymbolLit const& symbol)
  {
    std::list<std::string> signatures;
    if (symbol.scopes.empty())
    {
      signatures.splice(signatures.end(), searchFuncSignatureFull(symbol.name));
    }
    else
    {
      auto ctx=std::get<0>(searchLitContext(symbol));
      auto isStruct=std::get<1>(searchLitContext(symbol));
      if (ctx)
      {
        if (isStruct)
        {
          // ctx is a struct declaration. should not search beyond this struct.
          auto fnDecs=ctx->symMap.funDec.find(symbol.name);
          if (fnDecs != ctx->symMap.funDec.end())
          {
            std::transform(
                    fnDecs->second.begin(), fnDecs->second.end(),
                    std::back_inserter(signatures),
                    [&scopes=symbol.scopes](FunctionInfo const& fnInf)
                    {
                      return fnInf.signature(scopes);
                    });
          }
        }
        else
        {
          signatures.splice(signatures.end(), searchFuncSignatureFull(symbol.name));
        }
      }
    }

    return signatures;
  }

  optional<posRangeInFile> SymbolContext::searchLitPosition(
          SymbolLit const& symbol,
          optional<posInFile> const& position)
  {
    if (symbol.scopes.empty())
    {
      return searchVarDeclFull(symbol.name, position);
    }
    else
    {
      auto ctx=std::get<0>(searchLitContext(symbol));
      auto isStruct=std::get<1>(searchLitContext(symbol));
      if (ctx) // ctx is a struct declaration. should not search beyond this struct.
      {
        if (isStruct)
        {
          auto varDec=ctx->symMap.varDec.find(symbol.name);
          if (varDec != ctx->symMap.varDec.end())
          {
            auto line=std::get<0>(varDec->second.pos);
            auto ch=std::get<1>(varDec->second.pos);
            return std::make_tuple(ctx->getFileName().value_or(""),
                                   varDec->second.pos, std::make_pair(line, ch + symbol.name.length()));
          }
        }
        else
        {
          return searchVarDeclFull(symbol.name, position);
        }
      }
      return nullopt;
    }
  }

  std::list<posRangeInFile>
  SymbolContext::searchLitFuncPositions(SymbolLit const& symbol, optional<posInFile> const& position)
  {
    if (symbol.scopes.empty())
    {
      return searchFuncDeclsFull(symbol.name, position);
    }
    else
    {
      std::list<posRangeInFile> fnDecls;
      auto v=searchLitContext(symbol);
      auto ctx=std::get<0>(v);
      auto isStruct=std::get<1>(v);
      if (ctx) // ctx is a struct declaration. should not search beyond this struct.
      {
        if (isStruct)
        {
          auto fnDec=ctx->symMap.funDec.find(symbol.name);
          if (fnDec != ctx->symMap.funDec.end())
          {
            for (auto const& ptValue : fnDec->second)
            {
              fnDecls.emplace_back(getFileName().value_or(""), ptValue.pos, ptValue.pos);
            }
          }
        }
        else
        {
          return searchFuncDeclsFull(symbol.name, position);
        }
      }
      return fnDecls;
    }
  }

  SymbolContext* SymbolContext::searchStructCtxFull(std::string const& symbol)
  {
    std::unordered_set<SymbolContext*> searched;
    optional<std::string> tyInfo=_searchVarFull<std::string, std::string>(
            symbol,
            [](SymbolContext const* ctx, std::string const& sym) -> optional<std::string>
            {
              if (auto const* info = ctx->searchVarRaw(sym))
              {
                return info->type;
              }
              return nullopt;
            },
            fnFromDeclCreateTrav);


    if (not tyInfo.has_value())
    {
      return nullptr;
    }

    return _searchVarFull<SymbolContext*>(
            tyInfo.value(), std::mem_fn(&SymbolContext::searchStructContext)).value_or(nullptr);
  }

  optional<SymbolContext*> SymbolContext::searchAccessDecls(std::string const& accessVal)
  {
    // fileIdPair includes accessVals

    auto src=extRefs.fileIdPair.find(accessVal);
    if (src != extRefs.fileIdPair.end())
    {
      std::string& fileSrc=src->second;
      auto ctxMap=extRefs.extFileRefs.find(fileSrc);
      if (ctxMap != extRefs.extFileRefs.end() && ctxMap->second != nullptr)
      {
        return ctxMap->second;
      }
    }

    return parent != nullptr ? parent->searchAccessDecls(accessVal) : nullopt;
  }

  /**
   * Fully search context for source structs or source file in access
   * @param symbol Symbol to search
   * @return pair of <code>[context, isStruct]</code>, where <code>isStruct</code> is true
   *    if the <code>context</code> is a struct, and false if the context is a file from access declaration.
   *    If context is nullptr, <code>isStruct</code>'s return does not have defined behavior.
   */
  std::pair<SymbolContext*, bool> SymbolContext::searchLitContext(SymbolLit const& symbol)
  {
    if (symbol.scopes.empty())
    {
      // if the symbol is empty, then we can access every variable, not just those in the struct context
      // and hence should be treated as "not a struct", even if the variable is accessed in the struct.
      // for example,
      // int z; struct St { int x; int y=x+z; }
      // here, x and z can be accessed in the struct context.
      return std::make_pair(this, false);
    }
    else
    {
      std::vector<std::string> scopes(symbol.scopes);
      bool isStruct=false;

      // search in struct
      auto* ctx=searchStructCtxFull(scopes.back());
      if (ctx)
      {
        isStruct = true;
        scopes.pop_back();
      }
      else
      {
        // search in access declarations
        ctx =_searchVarFull<SymbolContext*>(
                scopes.back(), std::mem_fn(&SymbolContext::searchAccessDecls)).value_or(nullptr);

        if (ctx)
        {
          scopes.pop_back();
        }
      }

      for (auto it = scopes.rbegin(); it != scopes.rend() and ctx != nullptr; it++)
      {
        // FIXME: Impelemnt scope searching
        //        example:
        //        varx.vary.varz => go into varx's type context (struct or external file), repeat.
        //        struct and extfile handled very differently

        // get next variable declaration loc.
        // assumes struct, hence we do not search entire workspace
        SymbolContext* newCtx=nullptr;

        auto locVarDec = ctx->symMap.varDec.find(*it);
        if (locVarDec != ctx->symMap.varDec.end() and locVarDec->second.type.has_value())
        {
          newCtx = ctx->_searchVarFull<SymbolContext*>(
                  locVarDec->second.type.value(), std::mem_fn(&SymbolContext::searchStructContext)
                  ).value_or(nullptr);
        }

        // here, we searched for a struct context and did not find anything.
        // If we are in a struct context, we hit a dead end.
        // otherwise, we search for access declaration contexts.
        if (newCtx == nullptr)
        {
          if (isStruct)
          {
            return make_pair(nullptr, false);
          }
          else
          {
            ctx =_searchVarFull<SymbolContext*>(
                    *it, std::mem_fn(&SymbolContext::searchAccessDecls)).value_or(nullptr);
          }
        }
        else
        {
          isStruct = true;
        }

        ctx = newCtx;
      }
      return make_pair(ctx, isStruct);
    }
  }

  std::unordered_set<std::string> SymbolContext::createTraverseSet()
  {
    std::unordered_set<std::string> traverseSet(extRefs.includeVals);
    traverseSet.emplace(getPlainFile());
    for (auto const& unravelVal : extRefs.unraveledVals)
    {
      auto it = extRefs.fileIdPair.find(unravelVal);
      if (it != extRefs.fileIdPair.end())
      {
        traverseSet.emplace(it->second);
      }
    }
    return traverseSet;
  }

  SymbolContext* SymbolContext::getExternalRef(std::string const& symbol)
  {
    auto symRef = extRefs.extFileRefs.find(symbol);
    if (symRef != extRefs.extFileRefs.end())
    {
      return symRef->second;
    }

    return parent != nullptr ? parent->getExternalRef(symbol) : nullptr;
  }

  std::list<posRangeInFile> SymbolContext::searchFuncDecls(std::string const& symbol)
  {
    return searchFuncDecls(symbol, nullopt);
  }

  std::list<posRangeInFile>
  SymbolContext::searchFuncDecls(std::string const& symbol, optional<posInFile> const& position)
  {
    std::list<posRangeInFile> funcDecls;
    auto pt = symMap.funDec.find(symbol);
    if (pt != symMap.funDec.end())
    {
      for (auto const& fnInfo : pt->second)
      {
        if ((not position.has_value()) or (!posLt(position.value(), fnInfo.pos)))
        {
          funcDecls.emplace_back(getFileName().value_or(""), fnInfo.pos, fnInfo.pos);
        }
      }
    }

    if (parent != nullptr)
    {
      funcDecls.splice(funcDecls.end(), parent->searchFuncDecls(symbol, position));
    }
    return funcDecls;
  }

  std::list<posRangeInFile>
  SymbolContext::searchFuncDeclsFull(std::string const& symbol, optional<posInFile> const& position)
  {
    std::unordered_set<SymbolContext*> searched;
    auto retVal = _searchAllVarFull<posRangeInFile>(
            searched,
            std::unordered_set<std::string> { symbol },
            [&position](SymbolContext* ctx, std::string const& symbol)
            {
              return ctx->searchFuncDecls(symbol, position);
            },
            [](SymbolContext* ctx, std::string const& symbol)
            {
              return ctx->searchFuncDecls(symbol);
            },
            fnFromDeclCreateTrav);

    auto retValStruct =_searchAllVarFull<posRangeInFile>(
            symbol,
            [](SymbolContext* ctx, std::string const& sym) -> std::list<posRangeInFile>
            {
              std::list<posRangeInFile> finalList;
              for (auto const& unravelVal : ctx->extRefs.unraveledVals)
              {
                if (auto* pctx=ctx->searchStructCtxFull(unravelVal))
                {
                  finalList.splice(finalList.end(), pctx->searchFuncDecls(sym));
                }
              }
              return finalList;
            });
    retVal.splice(retVal.end(), std::move(retValStruct));
    return retVal;
  }

  optional<posRangeInFile> AddDeclContexts::searchVarDecl(
          std::string const& symbol, optional<posInFile> const& position)
  {
    auto pt = additionalDecs.find(symbol);
    if (pt != additionalDecs.end())
    {
      auto line=std::get<0>(pt->second.pos);
      auto ch=std::get<1>(pt->second.pos);
      if ((not position.has_value()) or (!posLt(position.value(), pt->second.pos)))
      {
        return std::make_tuple(getFileName().value_or(""),
                               pt->second.pos,
                               std::make_pair(line, ch + symbol.length()));
      }
    }

    return SymbolContext::searchVarDecl(symbol, position);
  }

  SymbolInfo const* AddDeclContexts::searchVarRaw(std::string const& symbol) const
  {
    auto pt = additionalDecs.find(symbol);
    return pt != additionalDecs.end() ? &pt->second : SymbolContext::searchVarRaw(symbol);
  }

  bool SymbolInfo::operator==(SymbolInfo const& sym) const
  {
    return name==sym.name and type==sym.type and pos == sym.pos;
  }

  std::string SymbolInfo::signature() const
  {
    return type.value_or("<decl-unknown>") + " " + name + ";";
  }

  std::string FunctionInfo::signature() const
  {
    return signature(std::vector<std::string>());
  }

  std::string FunctionInfo::signature(std::vector<std::string> const& scopes) const
  {
    std::stringstream ss;
    ss << returnType << " ";
    for (auto it=scopes.crbegin(); it!=scopes.crend(); it++)
    {
      ss << *it << ".";
    }
    ss << name << "(";
    for (auto it = arguments.begin(); it != arguments.end(); it++)
    {
      auto const& argtype=std::get<0>(*it);
      auto const& argname=std::get<1>(*it);
      ss << argtype;
      if (argname.has_value())
      {
        ss << " " << argname.value();
      }

      if (std::next(it) != arguments.end())
      {
        ss << ",";
      }

      if (std::next(it) != arguments.end() or restArgs.has_value())
      {
        ss << " ";
      }
    }

    if (restArgs.has_value())
    {
      auto const& v=restArgs.value();
      auto const& argtype=std::get<0>(v);
      auto const& argname=std::get<1>(v);
      ss << "... " << argtype;

      if (argname.has_value())
      {
        ss << " " << argname.value();
      }
    }
    ss << ");";
    return ss.str();
  }
}

#endif