2017-05-22 03:28:31 +00:00
|
|
|
// Copyright 2017 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package ast parses and formats sys files.
|
|
|
|
package ast
|
|
|
|
|
|
|
|
// Pos represents source info for AST nodes.
|
|
|
|
type Pos struct {
|
|
|
|
File string
|
|
|
|
Off int // byte offset, starting at 0
|
|
|
|
Line int // line number, starting at 1
|
|
|
|
Col int // column number, starting at 1 (byte count)
|
|
|
|
}
|
|
|
|
|
2017-08-26 19:36:08 +00:00
|
|
|
// Description contains top-level nodes of a parsed sys description.
|
|
|
|
type Description struct {
|
|
|
|
Nodes []Node
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node is AST node interface.
|
2017-08-27 17:55:14 +00:00
|
|
|
type Node interface {
|
|
|
|
Info() (pos Pos, typ string, name string)
|
sys: support type aliases (aka typedefs)
Complex types that are often repeated can be given short type aliases using the
following syntax:
```
type identifier underlying_type
```
For example:
```
type signalno int32[0:65]
type net_port proc[20000, 4, int16be]
```
Then, type alias can be used instead of the underlying type in any contexts.
Underlying type needs to be described as if it's a struct field, that is,
with the base type if it's required. However, type alias can be used as syscall
arguments as well. Underlying types are currently restricted to integer types,
`ptr`, `ptr64`, `const`, `flags` and `proc` types.
2018-01-06 13:46:52 +00:00
|
|
|
// Clone makes a deep copy of the node.
|
|
|
|
// If newPos is not zero, sets Pos of all nodes to newPos.
|
|
|
|
// If newPos is zero, Pos of nodes is left intact.
|
|
|
|
Clone(newPos Pos) Node
|
2017-08-27 17:55:14 +00:00
|
|
|
}
|
2017-08-26 19:36:08 +00:00
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
// Top-level AST nodes:
|
|
|
|
|
|
|
|
type NewLine struct {
|
|
|
|
Pos Pos
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *NewLine) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokNewLine], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Comment struct {
|
|
|
|
Pos Pos
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Comment) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokComment], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Include struct {
|
|
|
|
Pos Pos
|
|
|
|
File *String
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Include) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokInclude], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Incdir struct {
|
|
|
|
Pos Pos
|
|
|
|
Dir *String
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Incdir) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokInclude], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Define struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Value *Int
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Define) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokDefine], n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Resource struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
2017-08-27 17:55:14 +00:00
|
|
|
Base *Type
|
2017-05-22 03:28:31 +00:00
|
|
|
Values []*Int
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Resource) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokResource], n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Call struct {
|
2017-08-18 14:38:41 +00:00
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
CallName string
|
2017-08-26 19:36:08 +00:00
|
|
|
NR uint64
|
2017-08-18 14:38:41 +00:00
|
|
|
Args []*Field
|
|
|
|
Ret *Type
|
2017-05-22 03:28:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Call) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "syscall", n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Struct struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Fields []*Field
|
|
|
|
Attrs []*Ident
|
|
|
|
Comments []*Comment
|
|
|
|
IsUnion bool
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Struct) Info() (Pos, string, string) {
|
|
|
|
typ := "struct"
|
|
|
|
if n.IsUnion {
|
|
|
|
typ = "union"
|
|
|
|
}
|
|
|
|
return n.Pos, typ, n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type IntFlags struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Values []*Int
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *IntFlags) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "flags", n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type StrFlags struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Values []*String
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *StrFlags) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "string flags", n.Name.Name
|
|
|
|
}
|
|
|
|
|
sys: support type aliases (aka typedefs)
Complex types that are often repeated can be given short type aliases using the
following syntax:
```
type identifier underlying_type
```
For example:
```
type signalno int32[0:65]
type net_port proc[20000, 4, int16be]
```
Then, type alias can be used instead of the underlying type in any contexts.
Underlying type needs to be described as if it's a struct field, that is,
with the base type if it's required. However, type alias can be used as syscall
arguments as well. Underlying types are currently restricted to integer types,
`ptr`, `ptr64`, `const`, `flags` and `proc` types.
2018-01-06 13:46:52 +00:00
|
|
|
type TypeDef struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Type *Type
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *TypeDef) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "type", n.Name.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
// Not top-level AST nodes:
|
|
|
|
|
|
|
|
type Ident struct {
|
|
|
|
Pos Pos
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Ident) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokIdent], n.Name
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type String struct {
|
|
|
|
Pos Pos
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *String) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokString], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Int struct {
|
|
|
|
Pos Pos
|
|
|
|
// Only one of Value, Ident, CExpr is filled.
|
|
|
|
Value uint64
|
|
|
|
ValueHex bool // says if value was in hex (for formatting)
|
|
|
|
Ident string
|
|
|
|
CExpr string
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Int) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, tok2str[tokInt], ""
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Type struct {
|
|
|
|
Pos Pos
|
|
|
|
// Only one of Value, Ident, String is filled.
|
|
|
|
Value uint64
|
|
|
|
ValueHex bool
|
|
|
|
Ident string
|
|
|
|
String string
|
|
|
|
// Part after COLON (for ranges and bitfields).
|
2017-08-26 19:36:08 +00:00
|
|
|
HasColon bool
|
|
|
|
Pos2 Pos
|
2017-05-22 03:28:31 +00:00
|
|
|
Value2 uint64
|
|
|
|
Value2Hex bool
|
|
|
|
Ident2 string
|
|
|
|
Args []*Type
|
|
|
|
}
|
|
|
|
|
2017-08-27 17:55:14 +00:00
|
|
|
func (n *Type) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "type", n.Ident
|
|
|
|
}
|
|
|
|
|
2017-05-22 03:28:31 +00:00
|
|
|
type Field struct {
|
|
|
|
Pos Pos
|
|
|
|
Name *Ident
|
|
|
|
Type *Type
|
|
|
|
NewBlock bool // separated from previous fields by a new line
|
|
|
|
Comments []*Comment
|
|
|
|
}
|
2017-08-27 17:55:14 +00:00
|
|
|
|
|
|
|
func (n *Field) Info() (Pos, string, string) {
|
|
|
|
return n.Pos, "arg/field", n.Name.Name
|
|
|
|
}
|