Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Add AST class reference #164

Merged
merged 5 commits into from
Jun 10, 2020
Merged

Conversation

owen-mc
Copy link
Contributor

@owen-mc owen-mc commented Jun 4, 2020

Here is a draft AST class reference for codeql-go. The ordering is probably a bit weird, because I based it on the java one and added go-specific classes at the end of each table.

@owen-mc owen-mc requested a review from a team June 4, 2020 16:35
@owen-mc owen-mc force-pushed the ast-class-reference branch from 8023b53 to 3e40b99 Compare June 8, 2020 11:08
@owen-mc owen-mc marked this pull request as ready for review June 8, 2020 11:09
Copy link
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thank you very much for tackling this! I have a bunch of fairly minor comments, but on the whole this looks great.

+---------------------------+--------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+
| ``^x`` | `ComplementExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ComplementExpr.html>`__ | `BitwiseUnaryExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BitwiseUnaryExpr.html>`__ |
+---------------------------+--------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------------------------------------+
| ``*x`` | `DerefExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$DerefExpr.html>`__ | |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a StarExpr. Class DerefExpr exists and corresponds to something the Go compiler frontend claims to emit, but it actually never does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth adding a cautionary note to that effect to the qldoc of DerefExpr. Could you do that in this PR?

| .. _VariableName: https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$VariableName.html | .. _DefineStmt: https://help.semmle.com/qldoc/go/semmle/go/Stmt.qll/type.Stmt$DefineStmt.html | .. _SimpleAssignStmt: https://help.semmle.com/qldoc/go/semmle/go/Stmt.qll/type.Stmt$SimpleAssignStmt.html | |
| .. _Expr: https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$Expr.html | | .. _Assignment: https://help.semmle.com/qldoc/go/semmle/go/Stmt.qll/type.Stmt$Assignment.html | |
+---------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------+
| Expr_ ``...`` ``+=`` Expr_ ``...`` | AddAssignStmt_ | CompoundAssignStmt_, Assignment_ | |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe lhs and rhs for compound assignments must always be singletons (cf https://golang.org/ref/spec#Assignments).

Comment on lines 1 to 3
==================================
AST class reference — Learn CodeQL
==================================
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
==================================
AST class reference — Learn CodeQL
==================================
Abstract syntax tree classes for Go
===================================
CodeQL has a large selection of classes for working with the abstract syntax tree representation of Go programs.
The abstract syntax tree (AST) represents the syntactic structure of a program. Nodes in the AST represent elements such as statements and expressions.

+-------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
| ``struct {x, y int; z float32}`` | `StructTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StructTypeExpr.html>`__ | |
+-------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+
| ``fun c(a, b int, c float32) (float32, bool)`` | `FuncTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FuncTypeExpr.html>`__ | |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| ``fun c(a, b int, c float32) (float32, bool)`` | `FuncTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FuncTypeExpr.html>`__ | |
| ``func c(a, b int, c float32) (float32, bool)`` | `FuncTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FuncTypeExpr.html>`__ | |

| ``chan float64`` | `SendRecvChanTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$SendRecvChanTypeExpr.html>`__ | `ChanTypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ChanTypeExpr.html>`__ |
+-------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+----------------------------------------------------------------------------------------------------+

Expressions representing syntactic context
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very clear what this means. Could you try to rephrase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't think of a good way of expressing it. "Expression classes which require syntactic context"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then maybe remove this section and instead add remarks for Ident and SelectorExpr pointing people at these more specialised classes. I think those are the most important ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a shame to lose all that info because it seems useful. I've reworked it as a "Name expressions" section and another table in the Miscellaneous section. Let me know what you think.

- `ValueExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ValueExpr.html>`__:
an expression that can be evaluated to a value (as opposed to
expressions that refer to a package, a type, or a statement
label). This generalises
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
label). This generalises
label). This generalizes

(Our public-facing documentation generally uses US spelling.)

+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+
| ``x.( `TypeExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$TypeExpr.html>`__ )`` | `TypeAssertExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$TypeAssertExpr.html>`__ | |
+------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+
| ``*x`` | `StarExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$StarExpr.html>`__ | |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps worth remarking here that a StarExpr can either be a TypeExpr or a ValueExpr, depending on context.

Copy link
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost good to go, just a few minor comments left.

* ```go
* *p
* ```
* A unary pointer-dereference expression. Never actually emitted by the go compiler - StarExpr is used instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* A unary pointer-dereference expression. Never actually emitted by the go compiler - StarExpr is used instead.
* A unary pointer-dereference expression.
*
* This class exists for compatibility reasons only and should not normally be used directly. Use `StarExpr` instead.

or a
`FunctionName <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$FunctionName.html>`__,
depending on what sort of entity the name refers to.
The following classes relate to what sort of entity the name refers to.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer organizing this as a nested list rather than a table.

| ``f(x)`` | `CallExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CallExpr.html>`__ | `CallOrConversionExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$CallOrConversionExpr.html>`__ | |
+----------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

The following classes do not have a standard syntax.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it sound like something is wrong or broken. Perhaps rephrase as "The following classes organize expressions by the kind of entity they refer to" or something like that, and use a list instead of a table.

+------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `ValueExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BadExpr.html>`__ | an expression that can be evaluated to a value (as opposed to expressions that refer to a package, a type, or a statement label). This generalizes `ReferenceExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$ReferenceExpr.html>`__ |
+------------------------------------------------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| `BadExpr <https://help.semmle.com/qldoc/go/semmle/go/Expr.qll/type.Expr$BadExpr.html>`__ | an expression that cannot be parsed |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps put into one of the other tables, similar to BadStmt?

@max-schaefer
Copy link
Contributor

Looking good; thanks for bearing with me!

I just noticed one thing that may be worth addressing: for statements, you provide schematic syntax (as in "for Expr BlockStmt"), while for expressions you provide concrete syntax examples (as in "x + y").

Now I can see that it is not helpful to give schematic syntax for literals, but perhaps we could do it for the other expressions? So instead of "x + y" use "Expr + Expr". That is, for example, how the JavaScript AST class reference does it.

@owen-mc
Copy link
Contributor Author

owen-mc commented Jun 10, 2020

No problem @max-schaefer , I'm very happy to improve it as much as possible now so I don't need to come back to it later. I learned how to avoid unwanted spaces in RST, so I've done that as well as carrying out your suggestion.

Copy link
Contributor

@max-schaefer max-schaefer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff, thanks!

One last organisational thing: could you rebase this onto the rc/1.24 branch and change the PR to target that branch? As discussed with @felicitymay, we'll want to put a copy of this reference onto the rc/1.24 branch of https://github.com/github/codeql to expedite publication, and it would probably make sense to do the same in this repo for consistency.

@owen-mc owen-mc force-pushed the ast-class-reference branch from 20844db to 7fc4d1d Compare June 10, 2020 11:21
@owen-mc owen-mc changed the base branch from master to rc/1.24 June 10, 2020 11:24
@owen-mc owen-mc force-pushed the ast-class-reference branch from 7fc4d1d to ec53bd9 Compare June 10, 2020 13:20
@owen-mc owen-mc force-pushed the ast-class-reference branch from ec53bd9 to 69eb043 Compare June 10, 2020 13:28
@max-schaefer max-schaefer merged commit c30893a into github:rc/1.24 Jun 10, 2020
@max-schaefer
Copy link
Contributor

🎉

Next, could you make a PR against the rc/124 branch of https://github.com/github/codeql to put a copy of the reference into https://github.com/github/codeql/tree/master/docs/language/learn-ql/go?

@owen-mc owen-mc deleted the ast-class-reference branch June 10, 2020 15:39
@owen-mc
Copy link
Contributor Author

owen-mc commented Jun 10, 2020

I have now done that: github/codeql#3675

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants