P4C
The P4 Compiler
Loading...
Searching...
No Matches
symbol_table.h
1
/*
2
Copyright 2013-present Barefoot Networks, Inc.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
/* -*-C++-*- */
18
19
#ifndef P4_SYMBOL_TABLE_H_
20
#define P4_SYMBOL_TABLE_H_
21
22
/* A very simple symbol table that recognizes types; necessary because
23
the v1.2 grammar is ambiguous without type information */
24
25
#include <unordered_map>
26
#include <vector>
27
28
#include "ir/ir.h"
29
#include "lib/cstring.h"
30
#include "lib/source_file.h"
31
32
namespace
P4::Util {
33
class
NamedSymbol;
34
class
Namespace;
35
36
// Maintains a high-level representation of the program structure
37
// built on the fly during parsing. Used by the lexer to
38
// disambiguate identifiers.
39
class
ProgramStructure
final {
40
private
:
41
bool
debug;
42
FILE *debugStream;
43
Namespace
*rootNamespace;
44
Namespace
*currentNamespace;
45
46
struct
PathContext {
47
const
NamedSymbol
*previousSymbol;
48
const
Namespace
*lookupContext;
49
PathContext() : previousSymbol(
nullptr
), lookupContext(
nullptr
) {}
50
} identifierContext;
51
52
void
push(
Namespace
*ns);
53
NamedSymbol
*lookup(
const
cstring
identifier);
54
void
declare(
NamedSymbol
*symbol);
55
56
public
:
57
enum class
SymbolKind {
58
Identifier,
59
TemplateIdentifier,
60
Type,
61
TemplateType,
62
};
63
64
ProgramStructure
();
65
66
void
setDebug(
bool
debug) { this->debug = debug; }
67
void
pushNamespace(
SourceInfo
info
,
bool
allowDuplicates);
68
void
pushContainerType(
IR::ID
id
,
bool
allowDuplicates);
69
void
declareType(
IR::ID
id
);
70
void
declareObject(
IR::ID
id
,
cstring
type);
71
void
markAsTemplate(
IR::ID
id
);
// the symbol expects template args
72
73
// the last namespace has been exited
74
void
pop();
75
// Declares these types in the current scope
76
void
declareTypes(
const
IR::IndexedVector<IR::Type_Var>
*typeVars);
77
void
declareParameters(
const
IR::IndexedVector<IR::Parameter>
*params);
78
SymbolKind lookupIdentifier(
cstring
identifier);
79
80
void
startAbsolutePath();
81
void
relativePathFromLastSymbol();
82
void
clearPath();
83
84
void
endParse();
85
86
cstring
toString()
const
;
87
void
clear();
88
};
89
90
}
// namespace P4::Util
91
#endif
/* P4_SYMBOL_TABLE_H_ */
P4::IR::IndexedVector
Definition
node.h:52
P4::Util::NamedSymbol
Definition
symbol_table.cpp:30
P4::Util::Namespace
Definition
symbol_table.cpp:63
P4::Util::ProgramStructure
Definition
symbol_table.h:39
P4::Util::SourceInfo
Definition
source_file.h:131
P4::cstring
Definition
cstring.h:85
P4::info
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition
lib/error.h:148
P4::IR::ID
Definition
id.h:28
frontends
p4
symbol_table.h
Generated by
1.12.0