P4C
The P4 Compiler
Loading...
Searching...
No Matches
symbol_table.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations 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
32namespace P4::Util {
33class NamedSymbol;
34class 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.
39class 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
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_ */
Definition node.h:52
Definition symbol_table.cpp:30
Definition symbol_table.cpp:63
Definition symbol_table.h:39
Definition source_file.h:124
Definition cstring.h:85
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 error.h:148
Definition id.h:28