deferred_definition_scope.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_CHECK_DEFERRED_DEFINITION_SCOPE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_DEFERRED_DEFINITION_SCOPE_H_
  6. #include "common/array_stack.h"
  7. #include "toolchain/check/scope_stack.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::Check {
  10. // A thunk that has been declared but not yet defined.
  11. //
  12. // This type is large, so moves of this type should be avoided.
  13. struct PendingThunk : public MoveOnly<PendingThunk> {
  14. SemIR::FunctionId signature_id;
  15. SemIR::FunctionId function_id;
  16. SemIR::InstId decl_id;
  17. SemIR::InstId callee_id;
  18. ScopeStack::SuspendedScope scope;
  19. };
  20. // A stack of the current non-nested deferred definition scopes. For example, in
  21. // this code:
  22. //
  23. // class A {
  24. // class B {
  25. // fn F() {
  26. // class C {
  27. // // ...
  28. // }
  29. // }
  30. // }
  31. // }
  32. //
  33. // ... we have two non-nested deferred definition scopes: one for class A, and
  34. // one for class C. The scope for class B is nested, so is not tracked.
  35. //
  36. // At the end of each such scope, pending function definitions for functions
  37. // defined inline are processed. At this location, we also generate bodies for
  38. // thunks generated by checking.
  39. class DeferredDefinitionScopeStack {
  40. public:
  41. // Push a new scope.
  42. auto Push() -> void { pending_thunks_.PushArray(); }
  43. // Pop a scope.
  44. auto Pop() -> void { pending_thunks_.PopArray(); }
  45. // Add a pending thunk definition for the current scope.
  46. auto AddPendingThunk(PendingThunk&& thunk) -> void {
  47. pending_thunks_.AppendToTop(std::move(thunk));
  48. }
  49. // Peek the list of pending thunks in this scope.
  50. auto PeekPendingThunks() -> llvm::MutableArrayRef<PendingThunk> {
  51. return pending_thunks_.PeekArray();
  52. }
  53. private:
  54. ArrayStack<PendingThunk> pending_thunks_;
  55. };
  56. } // namespace Carbon::Check
  57. #endif // CARBON_TOOLCHAIN_CHECK_DEFERRED_DEFINITION_SCOPE_H_