mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
A small step to virtual functions - adding vtable pointers to the
layout, but not initializing or otherwise using them at this stage.
A few open design questions I'd love feedback on:
* Is this the right/good enough SemIR representation for now? This patch
adds a `is_dynamic` attribute to `SemIR::Class` and populates/flags it
based on the flag of the base class, or if any virtual function is
declared in the class (or, at least that's my intent). Some other
options include:
* Each `Class` could store a `ClassId` (or `TypeId`?) of the (possibly
indirect, possibly self) base class that is the first one that is
dynamic/has a vtable pointer
* Could make the property narrower, like `has vtable pointer` and have
it `true` only on the type that introduces the vtable - then derived
classes would have to walk their base classes to check if they're the
one that needs to define the vtable pointer or not
* Should the vtable be the first element in the type? If there's a
non-dynamic base type, we could have a layout that's `{<non-dynamic base
type>, vtable ptr, <derived members>}`? Derived types would still be
able to uniquely identify where their vtable pointer is just fine... -
and the vtable pointer is, in a sense, a member of that intermediate
type, so it does seem a bit strange to force it to the front - but I
guess it's probably more efficient in some ways?
Open to any other suggestions/advice/thoughts on the direction, etc.
---------
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
98 lines
3.2 KiB
C++
98 lines
3.2 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_COMMON_ARRAY_STACK_H_
|
|
#define CARBON_COMMON_ARRAY_STACK_H_
|
|
|
|
#include "common/check.h"
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
namespace Carbon {
|
|
|
|
// Provides a stack of arrays. Only the array at the top of the stack can have
|
|
// elements added.
|
|
//
|
|
// Example usage:
|
|
// // Push to start.
|
|
// PushArray();
|
|
// // Add values.
|
|
// AppendToTop(3);
|
|
// // Look at values.
|
|
// PeekArray();
|
|
// // Pop when done.
|
|
// PopArray();
|
|
//
|
|
// By using a single vector for elements, the intent is that as arrays are
|
|
// pushed and popped, the same storage will be reused. This should yield
|
|
// efficiencies for heap allocations. For example, in the toolchain we
|
|
// frequently have an array per scope, and only add to the current scope's
|
|
// array; this allows better reuse when entering and leaving scopes.
|
|
template <typename ValueT>
|
|
class ArrayStack {
|
|
public:
|
|
// Pushes a new array onto the stack.
|
|
auto PushArray() -> void { array_offsets_.push_back(values_.size()); }
|
|
|
|
// Pops the top array from the stack.
|
|
auto PopArray() -> void {
|
|
auto region = array_offsets_.pop_back_val();
|
|
values_.truncate(region);
|
|
}
|
|
|
|
// Returns the top array from the stack.
|
|
auto PeekArray() const -> llvm::ArrayRef<ValueT> {
|
|
CARBON_CHECK(!array_offsets_.empty());
|
|
return llvm::ArrayRef(values_).slice(array_offsets_.back());
|
|
}
|
|
|
|
// Returns the array at a specific index.
|
|
auto PeekArrayAt(int index) const -> llvm::ArrayRef<ValueT> {
|
|
auto ref = llvm::ArrayRef(values_).slice(array_offsets_[index]);
|
|
if (index + 1 < static_cast<int>(array_offsets_.size())) {
|
|
ref = ref.take_front(array_offsets_[index + 1] - array_offsets_[index]);
|
|
}
|
|
return ref;
|
|
}
|
|
|
|
// Returns the full set of values on the stack, regardless of whether any
|
|
// arrays are pushed.
|
|
auto PeekAllValues() const -> llvm::ArrayRef<ValueT> { return values_; }
|
|
|
|
// Appends a value to the top array on the stack.
|
|
auto AppendToTop(ValueT value) -> void {
|
|
CARBON_CHECK(!array_offsets_.empty(),
|
|
"Must call PushArray before AppendToTop.");
|
|
values_.push_back(value);
|
|
}
|
|
|
|
// Prepends a value to the top array on the stack.
|
|
auto PrependToTop(ValueT value) -> void {
|
|
CARBON_CHECK(!array_offsets_.empty(),
|
|
"Must call PushArray before PrependToTop.");
|
|
values_.insert(values_.begin() + array_offsets_.back(), value);
|
|
}
|
|
|
|
// Adds multiple values to the top array on the stack.
|
|
auto AppendToTop(llvm::ArrayRef<ValueT> values) -> void {
|
|
CARBON_CHECK(!array_offsets_.empty(),
|
|
"Must call PushArray before PushValues.");
|
|
values_.append(values.begin(), values.end());
|
|
}
|
|
|
|
// Returns the current number of values in all arrays.
|
|
auto all_values_size() const -> size_t { return values_.size(); }
|
|
|
|
private:
|
|
// For each pushed array, the start index in elements_.
|
|
llvm::SmallVector<int32_t> array_offsets_;
|
|
|
|
// The full set of elements in all arrays.
|
|
llvm::SmallVector<ValueT> values_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_COMMON_ARRAY_STACK_H_
|