Skip to content

Commit aa18b86

Browse files
committed
Document CodegenFunction and CodegenMethod
1 parent a8c6094 commit aa18b86

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

docs/_data/nav_docs.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
- id: hack-builder-keys-and-values
1212
- id: hack-builder-shapes
1313
- id: hack-builder-custom-renderers
14-
- title: Other Classes
14+
- title: Classes
1515
items:
1616
- id: classes-codegen-file
17+
- id: classes-codegen-function
18+
- id: classes-other
1719

1820
# n title:, 1 items: per title:, n id: per items:
1921
# - title: A
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
docid: classes-codegen-function
3+
title: CodegenFunction
4+
layout: docs
5+
permalink: /docs/classes/CodegenFunction/
6+
---
7+
8+
`CodegenFunctionBase` provides shared functionality for methods and functions;
9+
`CodegenFunction` does not add to it. The recommended way to get an instance of
10+
`CodegenFunction` is to call `->codegenFunction('function_name()')` on an instance of
11+
`IHackCodegenFactory`.
12+
13+
Basics
14+
------
15+
16+
- `->setReturnType(string $type)`: set the return type of the function if
17+
generating Hack code
18+
- `->setIsAsync(bool)`: if the function should use the `async` keyword or not. This
19+
doesn't affect the return type - include `Awaitable<>` in `setReturnType()` if
20+
appropriate
21+
- `->addParameter(string $param)`: add a parameter to the signature. `$param` should
22+
include both the type and variable name - you can use
23+
`->addParameterf($format, ...)` for complicated cases
24+
- `->setBody(string $code)`: set the body (implementation) of the function. You
25+
should generally use `HackBuilder` to create the string
26+
- `->setManualBody(bool $manual = true)`: if true, make the entire function body a
27+
manual section. If called, `->setBody()` is still used for the default value when
28+
generating a new file.
29+
30+
Attributes
31+
----------
32+
33+
To make a function memoized (adding the `<<__Memoize>>` attribute), call
34+
`->setIsMemoized(bool $value = true)`; for other attributes, call
35+
`->setUserAttribute(string $name, ?string $value)`.
36+
37+
CodegenMethod
38+
-------------
39+
40+
Methods are created with `$factory->codegenMethod('methodName')`, and have some
41+
additional features over functions:
42+
43+
- `->setIsOverride(bool $value = true)`: add the `<<__Override>>` attribute, which
44+
requires that the method is declared in a parent class
45+
- `->setIsFinal(bool $value = true)`: mark the method as final
46+
- `->setIsAbstract(bool $value = true)`: mark the method as abstract
47+
- `->setIsStatic(bool $value = true)`: mark the method as static
48+
49+
FIXMEs
50+
------
51+
52+
In some cases, you need to add an `HH_FIXME` or similar to the method declaration -
53+
for example, variadics are not support in Hack before HHVM 3.15, so a fixme is
54+
needed for all variadic functions.
55+
56+
Call `->addHHFixMe(int $code, string $why)` to add `/* HH_FIXME[$code] $why */` to
57+
the function declaration.
58+
59+
Metadata
60+
--------
61+
62+
`CodegenFunctionBase` supports `->setDocBlock(string)` and
63+
`->setGeneratedFrom(CodegenGeneratedFrom)` in the same way as `CodegenFile`.

docs/_docs/classes/other.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
docid: classes-other
3+
title: Other Classes
4+
layout: docs
5+
permalink: /docs/classes/other/
6+
---
7+
8+
CodegenMethod
9+
-------------
10+
11+
See [CodegenFunction](/hack-codegen/docs/classes/CodegenFunction/#codegenmethod)

0 commit comments

Comments
 (0)