Files
swift-mirror/tools/swift-plugin-server/Sources/CSwiftPluginServer/include/PluginServer.h
Rintaro Ishizaki c4b3edd6df [Macros] Add swift-plugin-server executable
This executable is intended to be installed in the toolchain and act as
an executable compiler plugin just like other 'macro' plugins.

This plugin server has an optional method 'loadPluginLibrary' that
dynamically loads dylib plugins.
The compiler has a newly added option '-external-plugin-path'. This
option receives a pair of the plugin library search path (just like
'-plugin-path') and the corresponding "plugin server" path, separated
by '#'. i.e.

  -external-plugin-path
    <plugin library search path>#<plugin server executable path>

For exmaple, when there's a macro decl:

  @freestanding(expression)
  macro stringify<T>(T) -> (T, String) =
      #externalMacro(module: "BasicMacro", type: "StringifyMacro")

The compiler look for 'libBasicMacro.dylib' in '-plugin-path' paths,
if not found, it falls back to '-external-plugin-path' and tries to find
'libBasicMacro.dylib' in them. If it's found, the "plugin server" path
is launched just like an executable plugin, then 'loadPluginLibrary'
method is invoked via IPC, which 'dlopen' the library path in the plugin
server. At the actual macro expansion, the mangled name for
'BasicMacro.StringifyMacro' is used to resolve the macro  just like
dylib plugins in the compiler.

This is useful for
 * Isolating the plugin process, so the plugin crashes doesn't result
   the compiler crash
 * Being able to use library plugins linked with other `swift-syntax`
   versions

rdar://105104850
2023-03-16 14:00:45 -07:00

58 lines
2.0 KiB
C

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_PLUGINSERVER_PLUGINSERVER_H
#define SWIFT_PLUGINSERVER_PLUGINSERVER_H
#include <sys/types.h>
#ifdef __cplusplus
extern "C" {
#endif
//===----------------------------------------------------------------------===//
// Inter-process communication.
//===----------------------------------------------------------------------===//
/// Create an IPC communication handle.
const void *PluginServer_createConnection(const char **errorMessage);
/// Destroy an IPC communication handle created by
/// 'PluginServer_createConnection'.
void PluginServer_destroyConnection(const void *connHandle);
/// Read bytes from the IPC communication handle.
ssize_t PluginServer_read(const void *connHandle, void *data, size_t nbyte);
/// Write bytes to the IPC communication handle.
ssize_t PluginServer_write(const void *connHandle, const void *data,
size_t nbyte);
//===----------------------------------------------------------------------===//
// Dynamic link
//===----------------------------------------------------------------------===//
/// Load a dynamic link library, and return the handle.
void *PluginServer_dlopen(const char *filename, const char **errorMessage);
/// Resolve a type metadata by a pair of the module name and the type name.
/// 'libraryHint' is a
const void *PluginServer_lookupMacroTypeMetadataByExternalName(
const char *moduleName, const char *typeName, void *libraryHint,
const char **errorMessage);
#ifdef __cplusplus
}
#endif
#endif /* SWIFT_PLUGINSERVER_PLUGINSERVER_H */