//===- ConstantInitBuilder.h - Builder for LLVM IR constants ----*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This class provides a convenient interface for building complex // global initializers of the sort that are frequently required for // language ABIs. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_CODEGEN_CONSTANTINITBUILDER_H #define LLVM_CLANG_CODEGEN_CONSTANTINITBUILDER_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/IR/Constants.h" #include "llvm/IR/GlobalValue.h" #include "clang/AST/CharUnits.h" #include "clang/CodeGen/ConstantInitFuture.h" #include <vector> namespace clang { class GlobalDecl; class PointerAuthSchema; class QualType; namespace CodeGen { class CodeGenModule; /// A convenience builder class for complex constant initializers, /// especially for anonymous global structures used by various language /// runtimes. /// /// The basic usage pattern is expected to be something like: /// ConstantInitBuilder builder(CGM); /// auto toplevel = builder.beginStruct(); /// toplevel.addInt(CGM.SizeTy, widgets.size()); /// auto widgetArray = builder.beginArray(); /// for (auto &widget : widgets) { /// auto widgetDesc = widgetArray.beginStruct(); /// widgetDesc.addInt(CGM.SizeTy, widget.getPower()); /// widgetDesc.add(CGM.GetAddrOfConstantStringFromLiteral(widget.getName())); /// widgetDesc.add(CGM.GetAddrOfGlobal(widget.getInitializerDecl())); /// widgetDesc.finishAndAddTo(widgetArray); /// } /// widgetArray.finishAndAddTo(toplevel); /// auto global = toplevel.finishAndCreateGlobal("WIDGET_LIST", Align, /// /*constant*/ true); class ConstantInitBuilderBase { … }; /// A concrete base class for struct and array aggregate /// initializer builders. class ConstantAggregateBuilderBase { … }; template <class Impl, class Traits> class ConstantAggregateBuilderTemplateBase : public Traits::AggregateBuilderBase { … }; template <class Traits> class ConstantArrayBuilderTemplateBase : public ConstantAggregateBuilderTemplateBase<typename Traits::ArrayBuilder, Traits> { … }; /// A template class designed to allow other frontends to /// easily customize the builder classes used by ConstantInitBuilder, /// and thus to extend the API to work with the abstractions they /// prefer. This would probably not be necessary if C++ just /// supported extension methods. template <class Traits> class ConstantStructBuilderTemplateBase : public ConstantAggregateBuilderTemplateBase<typename Traits::StructBuilder, Traits> { … }; /// A template class designed to allow other frontends to /// easily customize the builder classes used by ConstantInitBuilder, /// and thus to extend the API to work with the abstractions they /// prefer. This would probably not be necessary if C++ just /// supported extension methods. template <class Traits> class ConstantInitBuilderTemplateBase : public ConstantInitBuilderBase { … }; class ConstantInitBuilder; class ConstantStructBuilder; class ConstantArrayBuilder; struct ConstantInitBuilderTraits { … }; /// The standard implementation of ConstantInitBuilder used in Clang. class ConstantInitBuilder : public ConstantInitBuilderTemplateBase<ConstantInitBuilderTraits> { … }; /// A helper class of ConstantInitBuilder, used for building constant /// array initializers. class ConstantArrayBuilder : public ConstantArrayBuilderTemplateBase<ConstantInitBuilderTraits> { … }; /// A helper class of ConstantInitBuilder, used for building constant /// struct initializers. class ConstantStructBuilder : public ConstantStructBuilderTemplateBase<ConstantInitBuilderTraits> { … }; } // end namespace CodeGen } // end namespace clang #endif